<?php

namespace app\model;

use think\Model;

class Technical extends Model
{


    protected $connection = 'mysql';

    protected $pk = 'id';

    protected $name = 'technical';

    public static function getTitle($uuid)
    {
        $data = self::where('uuid', $uuid)->whereNotNull('uuid')->find();
        return $data->title;
    }
    public static function getTitlebyuid($uid)
    {
        $data = self::where('uid', $uid)->find();
        return $data->title;
    }
    public static function getcityName($uuid)
    {
        $data = self::where('uuid', $uuid)->whereNotNull('uuid')->find();
        return $data->city_name;
    }
    public static function getTel($uuid)
    {
        $data = self::where('uuid', $uuid)->whereNotNull('uuid')->find();
        return $data->tel;
    }

    public static function getInfobyuid($uid)
    {
        $uuid = UuidRelation::getuuid($uid, 'technical');
        if (!empty($uuid)) {
            $data = Technical::where(['uuid' => $uuid])->find();
            if (!empty($data)) {
                $data = $data->toArray();
            } else {
                $data = Technical::where(['uid' => $uid])->find();
                if (!empty($data)) {
                    $data = $data->toArray();
                    if (!empty($uuid)) {
                        Technical::where('id', $data['id'])->update(['uuid' => $uuid]);
                    }
                }
            }
        }
        return $data;
    }

    public static function getInfo($uuid)
    {
        $data = self::where('uuid', $uuid)->whereNotNull('uuid')->find();
        if (!empty($data)) {
            $data = $data->toArray();
        }
        return $data;
    }

    public static function conversion($vo)
    {
        $vo['cate_ids'] = Category::getmultiple($vo['cate_ids']) ?? '无';
        $vo['level'] = TechnicalLevel::getTitle($vo['level']) ?? '初级';
        $vo['sid'] = Store::getTitle($vo['sid']) ?? '平台';

        if (!empty($vo['uuid'])) {
            $vo['username'] = Users::getusername($vo['uuid']);
        }

        $vo['touxiang'] = toimg($vo['touxiang']);
        if (!empty($vo['photoalbum'])) {
            $vo['photoalbum'] = toimg(explode(',', $vo['photoalbum'])[0]);
        }

        $vo['region_name'] = $vo['province_name'] . $vo['city_name'] . $vo['district_name'];
        $vo['end_time'] = time_format($vo['end_time']) ?? '长期';
        return $vo;
    }

    public static function getpcarray($sid)
    {
        $data = self::field('uuid,title')->where(['weid' => weid(), 'sid' => (int)$sid, 'status' => 1])->select()->toArray();
        $datalist = [];
        foreach ($data as $key => $vo) {
            if (!empty($vo['uuid'])) {
                $datalist[$key]['val'] = $vo['uuid'];
                $datalist[$key]['key'] = $vo['title'];
            }
        }
        return $datalist;
    }
    public static function getstaff($sid, $cate_ids, $cityName)
    {
        $query = new self;
        if (!empty($cate_ids)) {
            foreach ((array) (explode(',', $cate_ids)) as $catevo) {
                if ($catevo) {
                    $query->whereOr('cate_ids', 'find in set', $catevo);
                }
            }
        }

        $orderidsarray = $query->field('id')->select()->toArray();
        foreach ($orderidsarray as $ovo) {
            if (empty($orderids)) {
                $orderids = $ovo['id'];
            } else {
                $orderids = $orderids . "," . $ovo['id'];
            }
        }

        $resquery = self::field('id,city_name,level,uuid,cate_ids,title')->where('is_business', 1)->where('id', 'in', $orderids)->where(['weid' => weid(), 'city_name' => $cityName, 'status' => 1]);

        if (!empty($sid)) {
            $resquery->where(['sid' => (int)$sid]);
        }

        $data = $resquery->select()->toArray();
        return $data;
    }
    public static function getpcarraydetailed($sid, $cate_ids, $cityName)
    {

        $data = self::getstaff($sid, $cate_ids, $cityName);

        $datalist = [];
        foreach ($data as $key => $vo) {
            if (!empty($vo['uuid'])) {
                $strlevel = TechnicalLevel::getTitle($vo['level']) ?? '初级';
                $datalist[$key]['val'] = $vo['uuid'];
                $datalist[$key]['key'] = $vo['title'] . '--[' . $strlevel . ']' . '[已接单量：' . OrderStaff::ordercount($vo['uuid']) . '][可接服务：' . Category::getmultiple($vo['cate_ids']) . ']';
            }
        }
        return $datalist;
    }
    public static function gelist_bycitycate($orderInfo)
    {
        $where['weid'] = weid();
        $where['status'] = 1;
        $Configdata = Config::getconfig();

        if (empty($Configdata['locationscope'])) {
            $Configdata['locationscope'] = 3;
        }
        if ($Configdata['locationscope'] == 3) {
            $where['district_name'] = $orderInfo['shipping_district_name'];
        } else if ($Configdata['locationscope'] == 2) {
            $where['city_name'] = $orderInfo['shipping_city_name'];
        } else if ($Configdata['locationscope'] == 1) {
            $where['province_name'] = $orderInfo['shipping_province_name'];
        }

        $query = Technical::where($where);

        $query->where('cate_ids', 'find in set', $orderInfo['cate_ids']);

        $Sort = 'id desc';

        $data = $query->order($Sort)->select()->toArray();

        return $data;
    }
    public static function getdiy_bycat($cat = 0, $count = 4, $goodsSort = "all")
    {
        $where['weid'] = weid();
        $where['status'] = 1;

        if ($cat > 0) {
            if (!empty($cat)) {
                $where['category_id'] = Category::getsonid($cat);
            }
        }

        $query = Technical::where($where);

        $Sort = 'id desc';

        $data = $query->limit($count)
            ->order($Sort)->select()->toArray();

        return $data;
    }

    public static function setIncome($order_info)
    {
        $uuid =  OrderStaff::getuuid($order_info['id']);
        if (!empty($uuid)) {
            $Technical = Technical::where(['uuid' => $uuid])->find();
            if (!empty($Technical)) {
                $Technical = $Technical->toArray();
            }

            if (!empty($Technical['status'])) {
                $percent = TechnicalLevel::getPercent($Technical['level']);
                if (empty($percent)) {
                    $percent = Config::getconfig('technical')['return_percent'];
                }
                //如果是商户的师傅，分佣比较需要减去商户的佣金
                if (!empty($Technical['sid'])) {
                    $Store = Store::find($Technical['sid']);
                    if (!empty($Store)) {
                        $Store = $Store->toArray();
                        $storepercent = StoreCate::getPercent($Store['stid']);
                        if (empty($storepercent)) {
                            $storepercent = Config::getconfig('store')['return_percent'];
                        }
                        if (!empty($storepercent)) {
                            $percent = $percent - $storepercent;
                        }
                    }
                }

                if ($percent > 0) {

                    if (empty($order_info['cat_id'])) {
                        $income = OrderGoods::getCommission($order_info, '', $percent);
                    } else {
                        $income = $order_info['total'];
                        $income = ($income * percent_to_num($percent));
                    }


                    $return_percent = $percent;
                    if ($income > 0) {

                        if ($income > 0 && $income < 0.01) {
                            $income = 0.01;
                        }

                        $TechnicalIncomelog = TechnicalIncomelog::where([
                            'uuid' => $uuid,
                            'weid' => $order_info['weid'],
                            'order_id' =>  $order_info['id'],
                        ])->find();

                        if (empty($TechnicalIncomelog)) {

                            Technical::where('uuid', $uuid)->inc('income', $income)->update();
                            Technical::where('uuid', $uuid)->inc('total_income', $income)->update();

                            $incomedata['uuid'] = $uuid;
                            $incomedata['ptype'] = 1;
                            $incomedata['weid'] = $order_info['weid'];
                            $incomedata['order_id'] = $order_info['id'];
                            $incomedata['order_num_alias'] = $order_info['order_num_alias'];
                            $incomedata['buyer_id'] = $order_info['uid'];
                            $incomedata['income'] = $income;
                            $incomedata['return_percent'] = floatval($return_percent);
                            $incomedata['percentremark'] = $order_info['total'] . '% x' . $percent . '%';
                            $incomedata['order_total'] = $order_info['total'];
                            $incomedata['pay_time'] = $order_info['pay_time'];
                            $incomedata['month_time'] = date('m', time());
                            $incomedata['year_time'] = date('Y', time());
                            $incomedata['order_status_id'] = 2; //已付款

                            TechnicalIncomelog::create($incomedata);
                        }
                    }
                }
            }
        }
    }
}
