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


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


    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author whitef
     * @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 whitef
     * @date 2024/07/10 15:06
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            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' => 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 whitef
     * @date 2024/07/10 15:06
     */
    public static function delete(array $params): bool
    {
        return IssueWork::destroy($params['id']);
    }


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