<?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\common\model\works;


use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use app\common\model\master_worker\MasterWorker;
use think\facade\Cache;


/**
 * ReturnWork模型
 * Class ReturnWork
 * @package app\common\model\works
 */
class ReturnWork extends BaseModel
{
    
    protected $name = 'return_work';

    public function serviceWork()
    {
        return $this->hasOne(ServiceWork::class,'id','service_work_id');
    }
    public function masterWorker()
    {
        return $this->hasOne(MasterWorker::class,'id','master_worker_id');
    }

    public function worker()
    {
        return $this->belongsTo(MasterWorker::class, 'master_worker_id', 'id');
    }

        public function getReturnWorkStatusTextAttr($value,$data):string
    {
        $default = Cache::get('RETURN_WORK_STATUS');
        if (!$default) {
            $status = DictData::whereIn('type_value', 'return_work_status')->column('name','value');
            Cache::set('RETURN_WORK_STATUS', json_encode($status,true),5);
        } else {
            $status = json_decode($default,true);
        }
        return $status[$data['return_work_status']] ?? '';
    }
    public function getAppointmentTimeAttr($value,$data)
    {
        return !empty($data['appointment_time'])?date('Y/m/d H:i:s',$data['appointment_time']):'';
    }

    public function getDispatchTimeAttr($value,$data)
    {
        return !empty($data['dispatch_time'])?date('Y/m/d H:i:s',$data['dispatch_time']):'';
    }

    public function getFinishedTimeAttr($value,$data)
    {
        return !empty($data['finished_time'])?date('Y/m/d H:i:s',$data['finished_time']):'';
    }
    public function getFinishedImagesAttr($value)
    {
        return empty($value)?'':json_decode($value,true);
    }

    public function setFinishedImagesAttr($value)
    {
        return empty($value)?'':json_encode($value,JSON_UNESCAPED_UNICODE);
    }
}