<?php
namespace app\workerapi\logic;
use app\common\model\dict\DictData;
use app\common\model\works\IssueWork;
use app\common\logic\BaseLogic;
use think\Exception;
use think\facade\Db;


/**
 * IssueWork逻辑
 * Class IssueWorkLogic
 * @package app\workerapi\logic\works
 */
class IssueWorkLogic extends BaseLogic
{
    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author whitef
     * @date 2024/07/10 15:06
     */
    public static function detail($params): array
    {
        $works = IssueWork::where(['id'=>$params['id'],'master_worker_id'=>$params['user_id']])->findOrEmpty()->toArray();
        if(!empty($works)){
            $works['complaint_type'] = DictData::where(['type_value'=>'complaint_type','status'=>1])->whereIn('value',implode(',',$works['complaint_type']))->column('name');
        }
        return $works;
    }

    /**
     * 提交申述申请
     * @param $params
     * @return false|void
     */
    public static function submitApproval($params)
    {
        try {
            $works = IssueWork::where(['id'=>$params['id'],'master_worker_id'=>$params['user_id']])->findOrEmpty();
            if(empty($works)){
                throw new Exception('工单不存在');          }
            if($works->issue_approval == 2){
                throw new Exception('您已提交申述申请，请等待定责');
            }
            $works->issue_approval = 2;
            $works->appeal_info = $params['appeal_info'];
            $works->save();
        }catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }
}