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


/**
 * ServiceWork逻辑
 * Class ServiceWorkLogic
 * @package app\workerapi\logic\works
 */
class ServiceWorkLogic 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 {
            ServiceWork::create([
                'work_sn' => $params['work_sn'],
                'real_name' => $params['real_name'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
                'title' => $params['title'],
                'category_type' => $params['category_type'],
                'goods_category_ids' => $params['goods_category_ids'],
                'goods_category_id' => $params['goods_category_id'],
                'base_service_fee' => $params['base_service_fee'],
                'service_fee' => $params['service_fee'],
                'work_status' => $params['work_status'],
                'service_status' => $params['service_status'],
                'dispatch_time' => strtotime($params['dispatch_time']),
                'receive_time' => strtotime($params['receive_time']),
                'appointment_time' => strtotime($params['appointment_time']),
                'finished_images' => $params['finished_images'],
                'finished_time' => strtotime($params['finished_time']),
                'master_worker_id' => $params['master_worker_id'],
                'work_amount' => $params['work_amount'],
            ]);

            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 {
            ServiceWork::where('id', $params['id'])->update([
                'work_sn' => $params['work_sn'],
                'real_name' => $params['real_name'],
                'mobile' => $params['mobile'],
                'address' => $params['address'],
                'title' => $params['title'],
                'category_type' => $params['category_type'],
                'goods_category_ids' => $params['goods_category_ids'],
                'goods_category_id' => $params['goods_category_id'],
                'base_service_fee' => $params['base_service_fee'],
                'service_fee' => $params['service_fee'],
                'work_status' => $params['work_status'],
                'service_status' => $params['service_status'],
                'dispatch_time' => strtotime($params['dispatch_time']),
                'receive_time' => strtotime($params['receive_time']),
                'appointment_time' => strtotime($params['appointment_time']),
                'finished_images' => $params['finished_images'],
                'finished_time' => strtotime($params['finished_time']),
                'master_worker_id' => $params['master_worker_id'],
                'work_amount' => $params['work_amount'],
            ]);

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


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