<?php
namespace app\api\logic;


use app\common\model\dict\DictData;
use app\common\model\recharge\RechargeOrder;
use app\common\model\works\IssueWork;
use app\common\logic\BaseLogic;
use app\common\model\works\ServiceWork;
use think\Exception;
use think\facade\Db;


/**
 * IssueWork逻辑
 * Class IssueWorkLogic
 * @package app\api\logic\works
 */
class IssueWorkLogic extends BaseLogic
{

    /**
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function lists($params)
    {
        return IssueWork::where('user_id',$params['user_id'])
            ->field(['id', 'sn','title', 'issue_approval','create_time'])
            ->append(['issue_approval_text'])
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
    }


    /**
     * 添加
     * @param array $params
     * @return false|int|string
     */
    public static function add(array $params)
    {
        Db::startTrans();
        try {
            $order = RechargeOrder::where(['sn'=>$params['sn'],'user_id'=>$params['user_id']])->findOrEmpty()->toArray();
            if(empty($order)){
                throw new Exception('订单错误');
            }
            $work = ServiceWork::findOrEmpty($order['work_id']);
            if($work['work_status']!=8){
                throw new Exception('订单暂未结束，不允许投诉，请直接联系客服');
            }
            $IssueWork = new IssueWork();
            $is_issue_work = $IssueWork->where(['user_id'=>$params['user_id'],'service_work_id'=>$work['id']])->findOrEmpty();
            if(!$is_issue_work->isEmpty()){
                throw new Exception('已提交投诉，不允许重复提交');
            }
            $id = $IssueWork->insertGetId([
                'sn' => $params['sn'],
                'work_sn' => $work['work_sn'],
                'service_work_id' => $work['id'],
                'title'=>$work['title'],
                'master_worker_id' => $work['master_worker_id'],
                'complaint_type' => !empty($params['complaint_type'])?json_encode($params['complaint_type']):'',
                'complaint_details' => $params['complaint_details'],
                'complaint_images' => !empty($params['complaint_images']) ? json_encode($params['complaint_images']):'',
                'complaint_mobile'=>$params['complaint_mobile'],
                'user_id'=>$params['user_id'],
                'create_time' => time(),
                'update_time' => time()
            ]);
            Db::commit();
            return $id;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public static function detail($params): array
    {
        $issue_work = IssueWork::where(['id'=>$params['id'],'user_id'=>$params['user_id']])
            ->field('sn,sn,title,issue_approval,complaint_type,complaint_details,complaint_images,complaint_mobile,approval_time,finished_time,create_time')
            ->append(['issue_approval_text'])
            ->findOrEmpty()->toArray();
        if(!empty($issue_work)){
            $issue_work['complaint_type'] = DictData::where(['type_value'=>'complaint_type','status'=>1])->whereIn('value',$issue_work['complaint_type'])->column('name');
        }
        return $issue_work;
    }
}