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


/**
 * ReturnWork逻辑
 * Class ReturnWorkLogic
 * @package app\workerapi\logic\works
 */
class ReturnWorkLogic 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 {
            ReturnWork::create([
                'work_sn' => $params['work_sn'],
                'service_work_id' => $params['service_work_id'],
                'issue_work_id' => $params['issue_work_id'],
                'return_work_status' => $params['return_work_status'],
                'dispatch_time' => $params['dispatch_time'],
                'appointment_time' => $params['appointment_time'],
                'finished_images' => $params['finished_images'],
                'finished_time' => $params['finished_time'],
                'master_worker_id' => $params['master_worker_id'],
            ]);

            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 {
            ReturnWork::where('id', $params['id'])->update([
                'work_sn' => $params['work_sn'],
                'service_work_id' => $params['service_work_id'],
                'issue_work_id' => $params['issue_work_id'],
                'return_work_status' => $params['return_work_status'],
                'dispatch_time' => $params['dispatch_time'],
                'appointment_time' => $params['appointment_time'],
                'finished_images' => $params['finished_images'],
                'finished_time' => $params['finished_time'],
                'master_worker_id' => $params['master_worker_id'],
            ]);

            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 ReturnWork::destroy($params['id']);
    }


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