<?php
namespace app\workerapi\logic;
use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\model\article\Article;
use app\common\model\dict\DictData;
use app\common\model\dict\DictType;
use app\common\model\master_worker\MasterWorkerAccountLog;
use app\common\model\master_worker_message\MasterWorkerMessage;
use app\common\model\works\ServiceWork;
use app\common\model\works\ServiceWorkLog;

/**
 * @author 林海涛
 * @date 2024/7/11 下午5:39
 */
class MasterWorkerMessageLogic extends  BaseLogic
{
    public static  $msgConfigArr = [
        //工单
        ['img_url' => 'https://weixiu.zhongdunzhizhao.com/static/wxapp/master_image/1.png', 'value' => 1],
        //资金
        ['img_url' => 'https://weixiu.zhongdunzhizhao.com/static/wxapp/master_image/3.png', 'value' => 3],
        //重要公告
        ['img_url' => 'https://weixiu.zhongdunzhizhao.com/static/wxapp/master_image/4.png', 'value' => 4],
    ];
    /**
     * 统计各种类型的未读消息总数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author 林海涛
     * @date 2024/7/11 下午5:43
     */
    public static function msgStatistics($userId)
    {

        $msgConfigArr = array_column(self::$msgConfigArr,null,'value');
       //查询字典值
        $dictData = DictData::where('type_value','worker_notify_message_type')
            ->field('id,value,name')
            ->where('status',YesNoEnum::YES)
            ->order(['sort'=>'desc'])
            ->select()
            ->toArray();
        foreach($dictData as &$val){
            $val['date'] = date('Y-m-d');
            $val['title'] = '';
            $val['total'] = 0;
            if(isset($msgConfigArr[$val['value']])){
                $val['img_url'] = $msgConfigArr[$val['value']]['img_url'];
                switch ($val['value']){
                    case 1:
                        $val['total'] = ServiceWorkLog::where(['master_worker_id'=>$userId,'show_type'=>YesNoEnum::NO])->count();
                        if($val['total']){
                            $log = ServiceWorkLog::where(['master_worker_id'=>$userId,'show_type'=>YesNoEnum::NO])->order(['id'=>'desc'])->find()->toArray();
                            $log = self::formatMsg($log,1);
                            $val['title'] = $log['title'];
                            $val['date'] = $log['date'];
                        }
                        break;
                    case 3:
                        $val['total'] = MasterWorkerAccountLog::where(['worker_id'=>$userId,'show_type'=>YesNoEnum::NO ] )->count();
                        if($val['total']){
                            $log = MasterWorkerAccountLog::where(['worker_id'=>$userId,'show_type'=>YesNoEnum::NO])->append(['action_text'])->order(['id'=>'desc'])->find()->toArray();
                            $log = self::formatMsg($log,3);
                            $val['title'] = $log['title'];
                            $val['date'] = $log['date'];
                        }
                        break;
                    case 4:
                        $val['total'] = Article::where(['is_show'=>YesNoEnum::YES])->count();
                        if($val['total']){
                            $log = Article::where('is_show',YesNoEnum::YES)->order(['id'=>'desc'])->find()->toArray();
                            $log = self::formatMsg($log,4);
                            $val['title'] = $log['title'];
                            $val['date'] = $log['date'];
                        }
                        break;
                }
            }
        }
       return $dictData;
    }

    /**
     * 格式化描述信息
     * @param $item
     * @param $type
     * @return mixed
     */
    public static function formatMsg($item,$type){
        switch ($type){
            case 1:
                $item['title'] = $item['opera_log'];
                $item['describe'] = $item['opera_log'];
                $item['date'] =date("Y-m-d", is_string($item['create_time']) ? strtotime($item['create_time']): $item['create_time']);
                break;
            case 3:
                $item['describe'] =  $item['title'] . ' ' . $item['action_text'] . ' '. $item['change_amount'];
                $item['title'] = $item['describe'];
                $item['date'] =date("Y-m-d", is_string($item['create_time']) ? strtotime($item['create_time']): $item['create_time']);
                break;
            case 4:
                $item['describe'] = $item['desc'];
                $item['date'] =date("Y-m-d", is_string($item['create_time']) ? strtotime($item['create_time']): $item['create_time']);
                break;
        }
        return $item;
    }
    public static function showMsg($params,$userId)
    {
        switch($params['msg_type']){
            case 1:
                ServiceWorkLog::where(['master_worker_id'=>$userId,'show_type'=>YesNoEnum::NO])->whereIn('id',$params['ids'])->update(['show_type'=>YesNoEnum::YES]);
            break;
            case 3:
                MasterWorkerAccountLog::where(['worker_id'=>$userId,'show_type'=>YesNoEnum::NO ] )->whereIn('id',$params['ids'])->update(['show_type'=>YesNoEnum::YES]);
            break;
        }
        return true;
    }

    /**
     * @param $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function orderPrompt($userId)
    {
        $where = [
            'master_worker_id'=>$userId,
            'approval'=>1,//派单的时候默认审核了
            'work_status'=>1,
        ];

        return ServiceWork::where($where)
            ->where('work_pay_status','>',0)
            ->field(['id', 'work_sn','real_name','mobile', 'address', 'title', 'work_status', 'service_status','work_pay_status', 'appointment_time','receive_time','base_service_fee','service_fee'])
            ->append(['work_status_text','service_status_text'])
            ->order(['appointment_time' => 'asc'])//上门时间排序
            ->select()
            ->toArray();
    }
}