<?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\sale;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\master_worker_register\MasterWorkerRegister;
use app\common\lists\ListsSearchInterface;
use app\common\model\sale\Sale;
use think\facade\Db;


/**
 * Sale列表
 * Class SaleLists
 * @package app\adminapi\lists
 */
class SaleMasterWorkerLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/12/15 10:53
     */
    public function setSearch(): array
    {
        return [
            '=' => ['sale_id'],

        ];
    }

    public function queryWhere()
    {
        $where = [];
        if(isset($this->params['time_range']) && $this->params['time_range']){
            $startDateTime = strtotime($this->params['time_range'][0]);
            $endDateTime = strtotime($this->params['time_range'][1])+86400-1;
            $where[] = ['create_time','BETWEEN',[$startDateTime,$endDateTime]];
        }
        return $where;
    }
    /**
     * 获取数据权限
     * $this->adminInfo['data_rules']
     * province city admin_id sale_group_id sale_id  property_head_id
     */
    public function queryDataWhere(){
        $where = [];
        $data_rules = $this->adminInfo['data_rules'];
        if (isset($data_rules['province']) && !empty($data_rules['province'])) {
            $where[] = ['province','in' ,$data_rules['province']];
        }
        if (isset($data_rules['city']) && !empty($data_rules['city'])) {
            $where[] = ['city','in' ,$data_rules['city']];
        }
        if (isset($data_rules['sale_group_id']) && !empty($data_rules['sale_group_id'])) {
            $sale_ids = Sale::where('sale_group_id','in',$data_rules['sale_group_id'])->column('id')??[];
            $where[] = ['sale_id','in' ,$sale_ids];
        }
        if (isset($data_rules['sale_id']) && !empty($data_rules['sale_id'])) {
            $where[] = ['sale_id','in' ,$data_rules['sale_id']];
        }
        return $where;
    }
    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/12/15 10:53
     */
    public function lists(): array
    {
        return MasterWorkerRegister::with(['sale'])
            ->where('sale_id','>',0)
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->where($this->queryDataWhere())
            ->field([
                'sale_id',
                Db::raw("COUNT(id) AS register_count,SUM(CASE WHEN worker_id > 0 THEN 1 ELSE 0 END) AS settled_count"),
                Db::raw("'".($this->params['time_range'][0]??'')."' AS start_date"),
                Db::raw("'".($this->params['time_range'][1]??'')."' AS end_date"),
            ])
            ->group('sale_id')
            ->limit($this->limitOffset, $this->limitLength)
            ->select()->toArray();
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/12/15 10:53
     */
    public function count(): int
    {
        return MasterWorkerRegister::with(['sale'])
            ->where('sale_id','>',0)
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->where($this->queryDataWhere())
            ->field(['sale_id'])
            ->group('sale_id')
            ->count();
    }

    public function setExcelComplexFields(): array
    {
        $zh_cn_fields = [
            '销售人员','所属组','注册数','入驻数','开始日期', '结束日期'
        ];
        $data_fields = [
            function($row){ return $row['sale']['sale_name']??''; },
            function($row){ return $row['sale']['saleGroupInfo']['sale_name']??''; },
            'register_count','settled_count','start_date','end_date'
        ];
        return [
            'zh_cn_fields' => $zh_cn_fields,
            'data_fields' => $data_fields
        ];
    }




}