<?php

namespace app\api\logic;
use app\common\server\UrlServer;
use think\Db;

class StaffOrderLogic
{
    /**
     * note 根据id获取获取管理员
     * create_time 2020/10/21 19:05
     */
    public static function admin($id){
        
        return Db::name('admin')->where('id',$id)->find();
    }
    /**
     * note 根据id获取的商品
     * create_time 2020/10/21 19:05
     */
    public static function goods($id){
        $goods=Db::name('goods')->where('id',$id)->find();
        $goods['image']=UrlServer::getFileUrl($goods['image']);
        return $goods;
    }
    /**
     * note 根据订单编号获取子订单编号
     * create_time 2020/10/21 19:05
     */
    public static function order_sn($order_sn){
        $count=Db::name('order_exe')
        ->where('order_sn',$order_sn)
        ->where('staff_status',3)
        ->count();
        return $count;
    }
    /**
     * note 查询已经完成的订单
     * create_time 2020/10/21 19:05
     */
    public static function wc_order($page,$limt,$param){
         $where=[];
         if (!empty($param['search'])) {
            if (preg_match('/^1[3-9]\d{9}$/', $param['search'])) {
                $where[] = ['mobile', '=', $param['search']];
            } else {
               $where[] = ['consignee', 'LIKE', '%' . $param['search'] . '%'];
            }
            } else {
                $where = []; 
            }
        $data=Db::name('order')
            ->order('id', 'desc')
            ->where($where)
            ->page($page,$limt)
            ->select();
        $datas=[];
        foreach ($data as &$item){
                $item['create_time'] = date('Y-m-d', $item['create_time']);
                $goods=self::goods($item['goods_id']);                     //订单的商品
                if($goods){
                   $item['goods_name']=$goods['name']; 
                   $item['image']=$goods['image']; 
                   $item['code']=$goods['code'];
                }
                $admin=self::admin($item['admin_id']);
                if($admin){
                    $item['admin_name']=$admin['name'];
                }
                $count=self::order_sn($item['order_sn']);    //统计总的条数
                $item['order_wc']=$count;                               //统计完成次数
                $item['order_dsy']=$item['code']-$count;
                if($item['order_dsy']==0){
                   $datas[]=$item;
                }
        }
        
        return $datas;
        
    }
   /**
     * note 获取客户的渠道
     * create_time 2020/10/21 19:05
     */
    public static function addqudao($id){
        return Db::name('staffchannel')->where('id',$id)->find();
    }
     /**
     * note 获取到用户的信息
     * create_time 2020/10/21 19:05
     */
     public static function user($user_id){
         return Db::name('user')->where('id',$user_id)->find();
     }
     
    /**
     * note 根据商品的id修改商品
     * create_time 2020/10/21 19:05
     */
     public static function edit_order($param){
        if (empty($param['id']) || !is_numeric($param['id'])) {
            throw new \Exception('订单ID无效');
        }
         $data=[
                'consignee'     =>        $param['consignee'],
                'pay_zd'        =>        $param['pay_zd'],
                'gord_id'       =>        $param['gord_id'],
                'channel_id'    =>        $param['channel_id'],
                'order_amount'  =>        $param['order_amount'],
                'order_remarks' =>        $param['remark']
        ];
        return Db::name('order')->where('id',$param['id'])->update($data);
     }
    /**
     * note 根据ID获取到保洁的信息
     * create_time 2020/10/21 19:05
     */ 
     public static function staff($id){
         
         return Db::name('staff')->where('id',$id)->field('id,name,mobile')->find();
         
     }
     
}