<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

namespace app\common\model\user;

use think\facade\Db;
use think\model;

class Task extends Model
{
    const ZERO = 0; //#648A8D
    const ONE = 1; //#4AC8BE
    const TWO = 2; //#409CDE
    const THREE = 3; //#C0DB38
    const FOUR = 4; //#4DCE58
    const FIVE = 5; //#FEC939
    const SIX = 6; //#8838DA
    const SEVEN = 7; //#FD6206
    const EIGHT = 8; //#F03347
    const NINE = 9; //#A38B82

    public static $Priority = [
        self::ZERO => '未设置',
        self::ONE => '一般',
        self::TWO => '中',
        self::THREE => '高',
        self::FOUR => '紧急',
    ];
    public static $FlowStatus = [
        self::ZERO => '未设置',
        self::ONE => '未开始',
        self::TWO => '进行中',
        self::THREE => '等待验收',
        self::FOUR => '已拒绝',
        self::FIVE => '已关闭',
        self::SIX => '通过验收',
    ];

    //列表
    function list($param, $type = 0)
    {
        $where = array();
        $map1 = [];
        $map2 = [];
        $map3 = [];
        $map4 = [];
        if (!empty($param['project_id'])) {
            $where[] = ['project_id', '=', $param['project_id']];
        }
        if (!empty($param['product_id'])) {
            $where[] = ['product_id', '=', $param['product_id']];
        } else {
            $map1[] = ['admin_id', '=', $param['uid']];
            $map2[] = ['director_uid', '=', $param['uid']];
            $map3[] = ['', 'exp', Db::raw("FIND_IN_SET({$param['uid']},assist_admin_ids)")];
        }
        if (!empty($param['type'])) {
            $where[] = ['type', '=', $param['type']];
        }
        if (!empty($param['flow_status'])) {
            $where[] = ['flow_status', '=', $param['flow_status']];
        }
        if (!empty($param['priority'])) {
            $where[] = ['priority', '=', $param['priority']];
        }
        if (!empty($param['cate'])) {
            $where[] = ['cate', '=', $param['cate']];
        }
        if (!empty($param['director_uid'])) {
            $where[] = ['director_uid', 'in', $param['director_uid']];
        }
        if (!empty($param['keywords'])) {
            $where[] = ['title|content', 'like', '%' . $param['keywords'] . '%'];
        }
        if (!empty($param['did'])) {
            $where[] = ['did', '=', $param['did']];
        }
        if (!empty($param['create_time'])) {
            $where[] = ['create_time', 'BETWEEN', [strtotime($param['create_time']), strtotime($param['create_time'] . " +1 month -1 day")]];
        }
        if (!empty($param['types']) && $param['types'] == 9) {
            $where = [];
            $where[] = ['end_time', 'BETWEEN', [strtotime(date('Y-m-d'), time()), strtotime(date('Y-m-d'), time()) + 86399]];
            $where[] = ['flow_status', '=', 2];

        }
        if (!empty($param['start_time'])) {
            $where[] = ['start_time', '>=', strtotime($param['start_time'])];
        }
        if (!empty($param['end_time'])) {
            $where[] = ['end_time', '<=', strtotime($param['end_time']) + 86400];
        }
        $where[] = ['delete_time', '=', 0];
        if ($type == 1) {
            unset($where['project_id']);
            $map1 = [];
            $map2 = [];
            $map3 = [];
            $map4 = [];
        }
        $param['limit'] = !empty($param['day']) ? 999 : $param['limit'];

        $query = Db::name('Task')->where(function ($query) use ($map1, $map2, $map3, $map4) {
            $query->where($map1)->whereor($map2)->whereor($map3);
        })->where($where);
        $count = $query->count();
        $list = $query->withoutField('content,md_content')
        ->order('flow_status asc')
        ->order('id desc')
        ->page($param['page'])
        ->limit($param['limit'])
        ->select()
        ->each(function ($item, $key) {
            $item['director_name'] = Db::name('Admin')->where(['id' => $item['director_uid']])->value('name');
            $item['admin_name'] = Db::name('Admin')->where(['id' => $item['admin_id']])->value('name');
            $assist_admin_names = Db::name('Admin')->where([['id', 'in', $item['assist_admin_ids']]])->column('name');
            $item['cate_name'] = Db::name('WorkCate')->where(['id' => $item['cate']])->value('title');
            $item['type_name'] = Db::name('TaskCate')->where(['id' => $item['type']])->value('title');
            $item['did_name'] = Db::name('department')->where(['id' => $item['did']])->value('title');
            if (empty($assist_admin_names)) {
                $item['assist_admin_names'] = '-';
            } else {
                $item['assist_admin_names'] = implode(',', $assist_admin_names);
            }
            $item['start_time'] = date('Y-m-d', $item['start_time']);
            $item['end_time'] = date('Y-m-d', $item['end_time']);
            $item['delay'] = 0;
            if ($item['over_time'] > 0 && $item['flow_status'] > 3 && $item['initial_end_time'] < time()) {
                $item['delay'] = countDays(date('Y-m-d', $item['initial_end_time']), date('Y-m-d', $item['over_time']));
            }
            if ($item['over_time'] == 0 && $item['flow_status'] < 2 && $item['initial_end_time'] < time()) {
                $item['delay'] = countDays(date('Y-m-d', time()), $item['initial_end_time']);
            }
            $item['priority_name'] = self::$Priority[(int)$item['priority']];
            $item['flow_name'] = self::$FlowStatus[(int)$item['flow_status']];
            return $item;
        });
        $return = [];
        if (!empty($param['day']) && !empty($param['start_time']) && !empty($param['end_time'])) {
            foreach ($list as $item) {
                if (isset($return[$item['start_time']]) && count($return[$item['start_time']]) > 2) {
                    continue;
                }
                $return[$item['start_time']][] = $item;
            }
        } else {
            $return = $list;
        }
        return [$count, $return];
    }

    //详情
    public function detail($id)
    {
        $detail = Db::name('Task')->where(['id' => $id])->find();
        if (!empty($detail)) {
            $detail['product_name'] = '';
            $detail['project_name'] = '';
            if ($detail['project_id'] > 0) {
                $project = Db::name('Project')->where(['id' => $detail['project_id']])->field('product_id,name')->find();
                $detail['product_name'] = Db::name('Product')->where(['id' => $project['product_id']])->value('name');
                $detail['project_name'] = $project['name'];
            }
            $detail['admin_name'] = Db::name('Admin')->where(['id' => $detail['admin_id']])->value('name');
            $detail['work_hours'] = Db::name('Schedule')->where(['delete_time' => 0, 'tid' => $detail['id']])->sum('labor_time');
            $detail['cate_name'] = Db::name('WorkCate')->where(['id' => $detail['cate']])->value('title');
            $detail['type_name'] = Db::name('TaskCate')->where(['id' => $detail['type']])->value('title');
            $find = Db::name('Admin')->where(['id' => $detail['director_uid']])->field('name,total_working_hours')->find();
            $detail['director_name'] = $find['name'] ?? '';
            $detail['assist_admin_names'] = '';
            if (!empty($detail['assist_admin_ids'])) {
                $assist_admin_names = Db::name('Admin')->where('id', 'in', $detail['assist_admin_ids'])->column('name');
                $detail['assist_admin_names'] = implode(',', $assist_admin_names);
            }
            $detail['priority_name'] = self::$Priority[(int)$detail['priority']];
            $detail['flow_name'] = self::$FlowStatus[(int)$detail['flow_status']];
            $detail['times'] = time_trans($detail['create_time']);
            $detail['end_time'] = date('Y-m-d H:i', $detail['end_time']);
            $detail['delay'] = 0;
            if ($detail['over_time'] > 0 && $detail['flow_status'] > 3 && $detail['initial_end_time'] < time()) {
                $detail['delay'] = countDays(date('Y-m-d', $detail['initial_end_time']), date('Y-m-d', $detail['over_time']));
            }
            if ($detail['over_time'] == 0 && $detail['flow_status'] < 2 && $detail['initial_end_time'] < time()) {
                $detail['delay'] = countDays(date('Y-m-d', time()), $detail['initial_end_time']);
            }
        }
        return $detail;
    }
}