<?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\auth\Admin;
use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use app\common\model\master_worker\MasterWorker;
use app\common\model\user\User;


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

    public function masterWorker()
    {
        return $this->hasOne(MasterWorker::class,'id','master_worker_id');
    }
    public function user()
    {
        return $this->hasOne(User::class,'id','user_id');
    }
    public function serviceWork()
    {
        return $this->hasOne(ServiceWork::class,'id','service_work_id');
    }
    public function returnWork()
    {
        return $this->hasOne(ReturnWork::class,'issue_work_id','id');
    }
    public function admin()
    {
        return $this->hasOne(Admin::class,'id','approval_admin_id');
    }
    public function getIssueApprovalTextAttr($value,$data)
    {
        $approval = [0=>'等待受理',1=>'受理中',2=>'已核实',3=>'返修中',4=>'已完结'];
        return $approval[$data['issue_approval']];
    }
    public function getFinishedImagesAttr($value)
    {
        return empty($value)?'':json_decode($value,true);
    }
    public function getComplaintImagesAttr($value)
    {
        return empty($value)?'':json_decode($value,true);
    }

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

    public function getComplaintTypeAttr($value)
    {
        return empty($value)?[]:json_decode($value,true);
    }
    public function getComplaintTypeTextAttr($value, $data)
    {
        $complaint_type_data = DictData::where('type_value','complaint_type')->column('name','value');
        if(is_array($data['complaint_type'])){
            $complaint_types = $data['complaint_type'];
        }else{
            $complaint_types = json_decode($data['complaint_type'],true);
        }
        $complaint_type_text = [];
        foreach ($complaint_types as $k=>$v){
            $complaint_type_text[] = $complaint_type_data[$v];
        }
        return implode(',',$complaint_type_text);
    }
    public function setComplaintTypeAttr($value)
    {
        return empty($value)?[]:json_encode($value,JSON_UNESCAPED_UNICODE);
    }

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

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