<?php
namespace app\workerapi\logic;
use app\common\enum\YesNoEnum;
use app\common\logic\BaseLogic;
use app\common\model\dict\DictData;
use app\common\model\dict\DictType;
use app\common\model\master_worker_message\MasterWorkerMessage;

/**
 * @author 林海涛
 * @date 2024/7/11 下午5:39
 */
class MasterWorkerMessageLogic extends  BaseLogic
{
    /**
     * 统计各种类型的未读消息总数
     * @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)
    {
       //查询字典值
        $dictData = DictData::where('type_value','worker_notify_message_type')
            ->field('id,value,name')
            ->where('status',YesNoEnum::YES)
            ->order(['sort'=>'desc'])
            ->select()
            ->toArray();
        $msgTypeArr =  array_column($dictData,'value');
        $msgStatisTicsObj = MasterWorkerMessage::field('msg_type,count(*) as count')
            ->where('show_type',YesNoEnum::NO)
            ->whereIn('msg_type',$msgTypeArr)
            ->where('worker_id',$userId)
            ->group('msg_type')
            ->select();
       foreach($dictData as $k => $v){
           $val = $msgStatisTicsObj->where('msg_type',$v['value'])->first();
           $v['total'] = $val->count?? 0;
           $dictData[$k] = $v;
       }
       return $dictData;
    }

    public static function showMsg($params,$userId)
    {
        return MasterWorkerMessage::whereIn('id',$params['ids'])
            ->where('worker_id',$userId)
            ->update(['show_type'=>YesNoEnum::YES]);
    }
}