<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\master_worker;


use app\adminapi\lists\BaseAdminDataLists;
use app\adminapi\service\DistributeLeafletsService;
use app\common\model\master_worker\MasterWorker;
use app\common\lists\ListsSearchInterface;
use app\common\model\works\ServiceWork;
use think\db\Query;
use think\facade\Log;

/**
 * MasterWorker列表
 * Class MasterWorkerLists
 * @package app\adminapi\listsmaster_worker
 */
class MasterWorkerLists extends BaseAdminDataLists implements ListsSearchInterface
{
    public $count = 0;
    public $lon = 0;
    public $lat = 0;

    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/07/10 18:17
     */
    public function setSearch(): array
    {
        return [
            '=' => ['mw.sn', 'mw.real_name', 'mw.account', 'mw.password', 'mw.mobile', 'mw.sex', 'mw.channel', 'mw.is_disable', 'mw.is_new_user', 'mw.create_time', 'mw.update_time'],

        ];
    }

    public function queryWhere(){
        $where = [];
        // 用户昵称
        if (isset($this->params['nickname']) && !empty($this->params['nickname'])) {
            $where[] = ['mw.nickname|mw.worker_number', 'like','%' .$this->params['nickname'] . '%'];
        }
        if (isset($this->params['worker_number']) && !empty($this->params['worker_number'])) {
            $where[] = ['mw.worker_number', 'like','%' .$this->params['worker_number'] . '%'];
        }
        if (isset($this->params['worker_exp_type']) && !empty($this->params['worker_exp_type'])) {
            $where[] = ['mwr.maintain_exp_type', '=', $this->params['worker_exp_type']];
        }
        if(isset($this->params['min_work_number']) && is_numeric($this->params['min_work_number'])){
            $where[] =[ 'mw.work_total','>=',$this->params['min_work_number']];
        }
        if(isset($this->params['max_work_number']) && is_numeric($this->params['max_work_number'])){
            $where[] =[ 'mw.work_total','<= ',$this->params['max_work_number']];
        }
        $goods_category_id = 0;
        if(isset($this->params['order_id']) && $this->params['order_id']){
            $lon_lat = (ServiceWork::where('id',$this->params['order_id'])->column("lon,lat,goods_category_id"))[0];
            $this->lon = $lon_lat['lon'];
            $this->lat = $lon_lat['lat'];
            $goods_category_id = $lon_lat['goods_category_id'];
        }
        $ids = DistributeLeafletsService::getIntersection($this->lon, $this->lat,$goods_category_id);
        $where[] = [ 'mw.id','IN',$ids];
        Log::debug('MasterWorker-where:'.json_encode($where));
        return $where;
    }
    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/07/10 18:17
     */
    public function lists(): array
    {
        $list = MasterWorker::alias('mw')
            ->join('master_worker_register mwr', 'mwr.worker_id = mw.id')
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->field([
                'mw.*',
                'mwr.maintain_exp_type',
                'mwr.other_exp_type',
            ])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['mw.id' => 'desc'])
            ->select()
            ->toArray();
        $distances = DistributeLeafletsService::locationIds($this->lon, $this->lat,0,array_keys(array_column($list, 'distance','id')));
        foreach ($list as &$item){
            $item['user_distance'] = $distances[$item['id']]??0;
        }
        return $list;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/07/10 18:17
     */
    public function count(): int
    {
        return MasterWorker::alias('mw')
            ->join('master_worker_register mwr', 'mwr.worker_id = mw.id')
            ->field([
                'mw.*'])
            ->where($this->searchWhere)->where($this->queryWhere())
            ->count();
        //return $this->count;
    }
}