<?php

namespace app\common\model;

use think\Model;
use think\model\concern\SoftDelete;

class Comjobs extends Model
{
    use SoftDelete;
    protected $deleteTime = 'deletetime';
    protected $defaultSoftDelete = 0;

    // 设置字段信息
    protected $schema = [
        'id'               => 'int',
        'workerid'         => 'int',
        'title'            => 'string',
        'video'            => 'string',
        'cateid'           => 'int',
        'province'         => 'string',
        'city'             => 'string',
        'district'         => 'string',
        'agegroup'         => 'string',
        'tags'             => 'string',
        'enddate'          => 'int',
        'requirement'      => 'string',
        'comdetails'       => 'string',
        'picall'           => 'string',
        'companydetails'   => 'string',
        'retmoney'         => 'int',
        'wtype'            => 'tinyint',
        'bwagall'          => 'string',
        'zwagall'          => 'string',
        'fwagall'          => 'string',
        'telephone'        => 'string',
        'remark'           => 'string',
        'status'           => 'tinyint',
        'priority'         => 'int',
        'updatetime'       => 'int',
        'createtime'       => 'int',
        'volume'           => 'int',
        'recruitment_cate' => 'int',
        'latitude'         => 'float',
        'longitude'        => 'float',
        'address'          => 'string',
        'community'        => 'string',
        'recruit_num'      => 'int',
        'is_worry'         => 'tinyint',
        'education'        => 'string',
        'emp_time'         => 'string',
        'company_name'     => 'string',
        'contact'          => 'string',
    ];

    // 设置字段自动转换类型
    protected $type = [
        'tags'       => 'json',
        'picall'     => 'json',
        'emp_time'   => 'json',
        'enddate'    => 'timestamp:Y-m-d',
        'updatetime' => 'timestamp:Y-m-d H:i:s',
        'createtime' => 'timestamp:Y-m-d H:i:s',
    ];
    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    public function getWtypeTextAttr($value, $data)
    {
        $wtype = [1 => '按月', 2 => '按时', 3 => '按件', 4 => '按项目', 5 => '面议'];
        return $wtype[$data['wtype']];
    }

    public function getRecruitmentCateTextAttr($value, $data)
    {
        $recruitment_cate = [1 => '普通招聘', 2 => '无忧聘'];
        return $recruitment_cate[$data['recruitment_cate']];
    }

    public function getStatusTextAttr($value, $data)
    {
        $status = [1 => '待修改', 2 => '待审核', 3 => '已上架', 4 => '已停招', 5 => '已下架'];
        return $status[$data['status']];
    }

    // 关联ComjobsCate
    public function comjobsCate()
    {
        return $this->hasOne(ComjobsCate::class, "id", "cateid");
    }

    // 关联Worker
    public function worker()
    {
        return $this->hasOne(Worker::class, "id", "workerid")->removeOption('soft_delete');
    }

    // 关联ComjobsLog
    public function comjobsLog()
    {
        return $this->hasMany(ComjobsLog::class, "comjobsid", "id");
    }

    public function ageCode()
    {
        return $this->hasOne(RensheCode::class, 'name', 'agegroup');
    }

    public function educationCode()
    {
        return $this->hasOne(RensheCode::class, 'name', 'agegroup');
    }

    public function getEmpCodeAttr($value, $data)
    {
        $emp_time = json_decode($data['emp_time'], true);
        if (empty($emp_time)) {
            return '';
        }

        $emp_code = [];
        $emp_list = RensheCode::where('type', 'emp_time')->column('code', 'name');
        foreach ($emp_time as $v) {
            if (!empty($emp_list[$v])) {
                $emp_code[] = $emp_list[$v];
            }
        }

        return implode(',', $emp_code);
    }

    public function getTagsCodeAttr($value, $data)
    {
        $tags = json_decode($data['tags'], true);
        if (empty($tags)) {
            return '';
        }

        $tags_code = [];
        $tags_list = RensheCode::where('type', 'welfare')->column('code', 'name');
        foreach ($tags as $v) {
            if (!empty($tags_list[$v])) {
                $tags_code[] = $tags_list[$v];
            }
        }

        return implode(',', $tags_code);
    }
}