<?php
// +----------------------------------------------------------------------
// | likeshop100%开源免费商用商城系统
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | 商业版本务必购买商业授权，以免引起法律纠纷
// | 禁止对系统程序代码以任何目的，任何形式的再发布
// | gitee下载：https://gitee.com/likeshop_gitee
// | github下载：https://github.com/likeshop-github
// | 访问官网：https://www.likeshop.cn
// | 访问社区：https://home.likeshop.cn
// | 访问手册：http://doc.likeshop.cn
// | 微信公众号：likeshop技术社区
// | likeshop团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeshopTeam
// +----------------------------------------------------------------------


namespace app\api\logic;

use app\common\model\Ad;
use app\common\server\UrlServer;
use think\Db;

class AdLogic
{
    public static function lists($pid, $client)
    {
        $ad_list = Db::name('ad a')
            ->join('ad_position ap', 'a.pid = ap.id')
            ->where(['pid' => $pid, 'ap.client' => $client, 'a.status' => 1, 'a.del' => 0, 'ap.status' => 1, 'ap.del' => 0])
            ->field('a.*')
            ->select();

        $list = [];
        foreach ($ad_list as $key => $ad) {
            $url = $ad['link'];
            $is_tab = 0;
            $params = [];
            switch ($ad['link_type']) {
                case 1:

                    $page = Ad::getLinkPage($ad['client'], $ad['link']);
                    $url = $page['path'];
                    $is_tab = $page['is_tab'] ?? 0;
                    break;
                case 2:
                    $goods_path = Ad::getGoodsPath($ad['client']);
                    $url = $goods_path;
                    $params = [
                        'id' => $ad['link'],
                    ];
                    break;
            }
            $list[] = [
                'image'     => UrlServer::getFileUrl($ad['image']),
                'link'      => $url,
                'link_type' => $ad['link_type'],
                'params'    => $params,
                'is_tab'    => $is_tab,
            ];
        }
        return $list;
    }
    
    public static function list_comost($get){
        $staff=Db::name('staff')->where('id',$get['staff_id'])->find();
        $admin=Db::name('admin')->where('phone',$staff['mobile'])->find();
        $result = Db::name('user_address')->where('admin_id',$admin['id'])->select();
        foreach ($result as $key => $k) {
          if(is_null($result[$key]['brand_id'])){
              $result[$key]['brand_name']='-';
            }else{
            $brand=Db::name('staffchannel')->where('id',$result[$key]['brand_id'])->find();
            $result[$key]['brand_name']=$brand['name'];
          }
          if(is_null($result[$key]['label_id'])){
              $result[$key]['label_name']='-';
        }else{
            $brand=Db::name('user_label')->where('id',$result[$key]['label_id'])->find();
            if($brand){
                $result[$key]['label_name']=$brand['name'];
            }
           
         }
           
        }
        
        return $result;
    }
    
    //增加跟进的内容
    public static function follow_comost($get){
        $result = Db::name('custom')
        ->where('custom_id',$get['id'])
        ->page($get['page'], $get['limit'])
        ->order('id desc')
        ->select();
         foreach ($result as $key => $k) {
                $result[$key]['time']=date('Y-m-d H:i:s',$result[$key]['create_time']);
                $admin=Db::name('admin')->where('id',$result[$key]['admin_id'])->find();
                if($admin){
                    $result[$key]['admin_name']=$admin['name'];
                }
          }
        return  $result;
        
    }
    
    public static function comost_add($get){
        $staff=Db::name('staff')->where('id',$get['staff_id'])->find();
        $admin=Db::name('admin')->where('phone',$staff['mobile'])->find();
        $data=[
            'admin_id'=>$admin['id'],
            'connect' =>$get['reak'],
            'custom_id'=>$get['orderid'],
            'create_time'=>time()
        ];
        return Db::name('custom')->data($data)->insert();
    }
    
    public static function notice(){
       $lists=Db::name('staff_notice')->order('id asc')->limit(2)->select();
       foreach ($lists as &$item){
           $time1=time();
           $daysDiff = abs(($time1 - $item['time']) / 86400);
           if($daysDiff>10){
               $item['date']=10;
           }else{
               $item['date']=intval($daysDiff);
           }
       }
       return  $lists;
    }
    
    public static function position(){
        return Db::name('recruitype')->select();
    }
    
    public static function vode_type(){
           return Db::name('video_type')->select();
    }
    
    public static function position_list($get){
      
        $where=[];

        if (isset($get['title']) && $get['title']) {
            $where[] = ['name', 'like', '%' . $get['title'] . '%'];
        }

        if (isset($get['value']) && $get['value'] != '') {
            $where[] = ['type_id', '=', $get['value']];
        }
        $data=Db::name('recrui')
        ->where($where)
        ->page($get['page'], $get['limit'])
        ->order('id desc')
        ->select();
      
         foreach ($data as &$item) {
             
             $type=Db::name('recruitype')->where('id',$item['type_id'])->find();  //获取岗位
             $item['type_name']=$type['zpname'];
             $salary=Db::name('recrui_salary')->where('id',$item['tment_id'])->find();
             $item['salary_name']=$salary['name'];
              
             $branch=Db::name('staff_group')->where('id',$item['branch_id'])->find();
             $item['branch_name']=$branch['name'];
             
         }
        return $data;
        
    }
    
    public static function video_list($get){
        $where=[];
        if (isset($get['title']) && $get['title']) {
            $where[] = ['name', 'like', '%' . $get['title'] . '%'];
        }
        if (isset($get['value']) && $get['value'] != '') {
            $where[] = ['type_id', '=', $get['value']];
        }
        $data=Db::name('video')
        ->where($where)
        ->page($get['page'], $get['limit'])
        ->order('id desc')
        ->select();
         foreach ($data as &$item) {
             $type=Db::name('video_type')->where('id',$item['type_id'])->find();  //获取岗位
             $item['type_name']=$type['title'];
             $item['images']= UrlServer::getFileUrl($item['image']);
         }
        return $data;
    }
    
    public static function video_info($get){
        return Db::name('video')->where('id',$get['id'])->find();
    }
    
    public static function user_wages($get){
        
        $now = time();
        $start_of_last_month = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month', $now)));
        $end_of_last_month = strtotime("last day of last month",$now); //上一个月结束时间搓
        
        //总订单统计
        $data['order']=Db::name('order_exe')
            ->where('staff_id',$get['user_id'])
            ->where('staff_status',3)
            ->whereTime('autotime','last month')
        ->count();
        //上午的订单
        $data['sw_orders']=Db::name('order_exe')
            ->where('staff_id',$get['user_id'])
            ->where('addtime',2)
            ->where('staff_status',3)
            ->whereTime('autotime','last month')
        ->count();
        //下午的订单
        $data['xw_orders']=Db::name('order_exe')
            ->where('staff_id',$get['user_id'])
            ->where('addtime',2)
            ->where('staff_status',3)
            ->whereTime('autotime','last month')
        ->count();
        //报销的金额
        $data['sement']=Db::name('finance')
            ->where('user_id',$get['user_id'])
            ->where('status',2)
            ->whereTime('create_time','last month')
            ->sum('pay');
  
        //上午加时
        
        $data['swaddtime']=Db::name('order_exe a')
                              ->join('order_timeadd u','u.orderid=a.id')
                              ->where('a.staff_id',$get['user_id'])
                              ->where('a.autotime','>=',$start_of_last_month)
                              ->where('a.autotime','<=',$end_of_last_month)
                              ->where('u.status',2)
                              ->where('a.addtime',1)
                              ->sum('minute');        
        
        //下午加时
        $data['xwaddtime']=Db::name('order_exe a')
                              ->join('order_timeadd u','u.orderid=a.id')
                              ->where('a.staff_id',$get['user_id'])
                              ->where('a.autotime','>=',$start_of_last_month)
                              ->where('a.autotime','<=',$end_of_last_month)
                              ->where('u.status',2)
                              ->where('a.addtime',2)
                              ->sum('minute');
        
        
        //请假代码

            $data['leave']=Db::name('leavesd')
                            ->where('staff_id',$get['user_id'])
                            ->whereTime('time','last month')
         
                            ->where('addtime','<>',2)
                            ->count();
            $number=Db::name('leavesd')
                            ->where('staff_id',$get['user_id'])
                            ->whereTime('time','last month')
                            ->where('addtime',2)
                            ->count();                
            $data['leaves']= $data['leave']/2+$number;  
        
        
    
        
        $data['fine']=Db::name('fine')
            ->where('staff_id',$get['user_id'])
            ->whereTime('create_time','last month')
        ->sum('score');
        $data['abnormal']=Db::name('order_exe')
            ->where('staff_id',$get['user_id'])
            ->where('staff_status',3)
            ->where('abnormal',1)
            ->whereTime('autotime','last month')
        ->count();
        

        
        $data['annual']=Db::name('order_abnormal')
                ->where('staff_id',$get['user_id'])
                ->whereTime('create_time','last month')
                ->count();
  
        // 假设当前日期是2023-03-15
        $current_date = date('Y-m-d');
         
        // 获取上个月的第一天
        $first_day_of_last_month = date('Y-m-01', strtotime($current_date . ' -1 month'));
         
        // 获取上个月的最后一天
        $last_day_of_last_month = date('Y-m-t', strtotime($first_day_of_last_month));
         
        // 计算天数
        $days_in_last_month = date('j', strtotime($last_day_of_last_month)) - date('j', strtotime($first_day_of_last_month)) + 1;
         
        $data['lastdate']=$days_in_last_month-$data['leaves'];
        
        return $data;
    }
    
    public static function user_wages_add($get){
        $now = time();
        // 获取上个月的时间戳
        $lastMonthTimestamp = strtotime('-1 month', $now);
        $staff=Db::name('staff')->where('id',$get['user_id'])->find();
        
        $staff_uages=Db::name('staff_wages')
        ->where('staff_id',$get['user_id'])
        ->whereTime('last_addtime','last month')
        ->find();
        if($staff_uages){
            return -1;
        }
        
        $last_month_month = date('m') - 1;
        if($last_month_month==0){
            $last_month_month =12;
        }

        
        $data=[
          'staff_name'        =>  $staff['name'],
          'staff_phone'       =>  $staff['mobile'],
          'total_orders'      => $get['total_orders'],
          'sw_orders'         =>$get['sw_orders'],
          'xw_orders'         => $get['xw_orders'],
          'finance'           => $get['finance'],
          'sw_addtime'         =>$get['sw_addtime']/60,
          'xw_addtime'         => $get['xw_addtime']/60, 
          'leave'             =>$get['leave']/2,
          'attendance'        =>$get['attendance'],
          'staff_id'          => $get['user_id'],
          'status'            =>$get['status'],
          'break'             =>$get['reake'],
          'month'             => $last_month_month,
          'last_addtime'      =>$lastMonthTimestamp,
          'annual'            =>$get['annual'],
          'abnormal'            =>$get['abnormal'],
          'fine'            =>$get['fine'],
          
          'create_time'       =>$now
        ];
        return Db::name('staff_wages')->data($data)->insert();
    }
    
    public static function user_leave($get){
        
        $lists=Db::name('leavesd')
        ->where('staff_id',$get['staff_id'])
        ->page($get['page'], $get['pageSize'])
        ->order('id desc')
        ->select();
         foreach ($lists as &$item) {
            if($item['addtime']==0){
                $item['addtime']="上午";
                $item['uration']=4;
                }else if($item['addtime']==2){
                    $item['addtime']="全天"; 
                    $item['uration']=8;
                }
                
                else{
                $item['addtime']="下午";
                $item['uration']=4;
              }
             $item['create_time']=date('Y-m-d',$item['create_time']);
             $item['time']=date('Y-m-d',$item['time']);
         }
        return $lists;
        
    }
    public static function last_leave($get){
        $lists=Db::name('leave')
        ->where('user_id',$get['staff_id'])
        ->whereTime('time', 'last month')
        ->page($get['page'], $get['pageSize'])
        ->order('id desc')
        ->select();
         foreach ($lists as &$item) {
             $type=Db::name('leave_type')->where('id',$item['type'])->find();  //获取岗位
             if($type){
             $item['type_name']=$type['name'];
             $item['create_time']=date('Y-m-d',$item['create_time']);
             $item['time']=date('Y-m-d',$item['time']);
             if($item['addtime']==1){
                 $item['addtime']="上午";
             }else{
                  $item['addtime']="下午";
             }
             }


         }
        return $lists;
    }
    
    public static function fine($get){
        $lists=Db::name('fine')
        ->where('staff_id',$get['staff_id'])
        ->page($get['page'], $get['pageSize'])
        ->order('id desc')
        ->select();
         foreach ($lists as &$item) {
             $type=Db::name('staff_fine')->where('id',$item['type_id'])->find();  //获取岗位
             $item['type_name']=$type['name'];
             $item['create_time']=date('Y-m-d',$item['create_time']);
  

         }
        return $lists;
        
    }
    public static function last_fine($get){
        $lists=Db::name('fine')
        ->where('staff_id',$get['staff_id'])
        ->whereTime('create_time', 'last month')
        ->page($get['page'], $get['pageSize'])
        ->order('id desc')
        ->select();
         foreach ($lists as &$item) {
             $type=Db::name('staff_fine')->where('id',$item['type_id'])->find();  //获取岗位
             $item['type_name']=$type['name'];
             $item['create_time']=date('Y-m-d',$item['create_time']);
  

         }
        return $lists;  
    }
    
    public static function addrecruit($get){
        $get['crem_time']=time();
        return Db::name('user_recruit')->data($get)->insert();
    }
    
    public static function notice_list($get){
        $list=Db::name('staff_notice')
        ->page($get['page'], $get['pageSize'])
        ->order('id desc')
        ->select();
         foreach ($list as &$item) {
              $item['time']=date('Y-m-d',$item['time']);
         }
        return $list;
    }
    
    public static function addleave($get){
        
        $staff=Db::name('staff')->where('id',$get['staff_id'])->find();
        $data=[
            'staff_id'  =>$get['staff_id'],
            'time'      =>strtotime($get['time']),
            'type'      =>$get['type'],
            'addtime'   =>$get['after'],
            'reak'      =>$get['reak'],
            'name'      =>$staff['name'],
            'phone'     =>$staff['mobile'],
            'create_time'=>time(),
        ];
        return Db::name('leavesd')->data($data)->insert();
    }
    //获取员工的权限
    public static function auth($param){
        $role=Db::name('staff')->alias('s')
             ->join('staff_group g', 's.group_id = g.id')
             ->where('s.id',$param['staff_id'])
             ->field('g.rloe')
             ->find();
        $roleArray = explode(',', $role['rloe']);
        $data=Db::name('dev_staffauth')
        ->where('id','in', $roleArray)
        ->select();   
         return linear_to_tree($data);     
       
    }
    
}