<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\works;


use app\common\model\works\IssueWork;
use app\common\logic\BaseLogic;
use app\common\model\works\ReturnWork;
use think\facade\Db;


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


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            IssueWork::create([
                'work_sn' => $params['work_sn'],
                'service_work_id' => $params['service_work_id'],
                'master_worker_id' => $params['master_worker_id'],
                'issue_approval' => $params['issue_approval'],
                'complaint_status' => $params['complaint_status'],
                'responsible' => $params['responsible'],
                'complaint_details' => $params['complaint_details'],
                'complaint_images' => $params['complaint_images'],
                'finished_time' => strtotime($params['finished_time']),
                'approval_admin_id' => $params['approval_admin_id'],
                'approval_time' => strtotime($params['approval_time']),
            ]);

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


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            if($params['issue_approval'] == 4) {
                //查询是否完成返修
                $returnModel = ReturnWork::where('issue_work_id', $params['id'])->findOrEmpty();
                if(!$returnModel->isEmpty() && $returnModel->return_work_status != 2){
                    throw new \Exception("返修工单编号：".$returnModel->work_sn.'未返修完成');
                }
            }
            IssueWork::where('id', $params['id'])->update([
                'work_sn' => $params['work_sn'],
                'service_work_id' => $params['service_work_id'],
                'master_worker_id' => $params['master_worker_id'],
                'issue_approval' => $params['issue_approval'],
                'complaint_status' => $params['complaint_status'],
                'responsible' => $params['responsible'],
                'complaint_details' => $params['complaint_details'],
                'complaint_images' => $params['complaint_images'],
                'finished_time' => strtotime($params['finished_time']),
                'approval_admin_id' =>$params['approval_admin_id'],
                'approval_time' => time(),
            ]);

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


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public static function delete(array $params): bool
    {
        return IssueWork::destroy($params['id']);
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/07/10 15:06
     */
    public static function detail($params): array
    {
        return IssueWork::findOrEmpty($params['id'])->toArray();
    }
}