<?php

namespace app\api\logic;


use app\common\logic\BaseLogic;
use app\common\model\orders\OrderReport;
use app\common\model\works\ServiceWork;
use think\facade\Db;


/**
 * OrderReport
 * Class OrderReportLogic
 * @package app\api\logic
 */
class OrderReportLogic extends BaseLogic
{
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            $work = ServiceWork::where(['id'=>$params['work_id'],'user_id'=>$params['user_id']])->findOrEmpty();
            if($work->isEmpty()){
                self::setError('工单不存在');
                return false;
            }

            if(empty($work->master_worker_id)){
                self::setError('工单未分配');
                return false;
            }

            $is_report = OrderReport::where(['user_id'=>$params['user_id'],'work_id'=>$params['work_id']])->findOrEmpty();
            if(!$is_report->isEmpty()){
                self::setError('您已提交过投诉');
                return false;
            }

            OrderReport::create([
                'user_id'=>$params['user_id'],
                'work_id' => $params['work_id'],
                'worker_id'=> $work->master_worker_id,
                'report' => $params['report'],
                'audit_state' => 0,
                'event_status' => 0,
                'remark' => !empty($params['remark'])?$params['remark']:'',
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }
}