<?php
/**
 * @author 林海涛
 * @date ${DATA}
 */

namespace app\common\model\master_worker;

use app\common\model\BaseModel;
use app\common\model\dict\DictData;
use app\common\model\MasterWorkerRegister;
use app\common\service\FileService;
use think\facade\Cache;

/**
 * 工程师表
 * Class MasterWorker
 * @package app\common\model
 */
class MasterWorker extends BaseModel
{
    protected $name = 'master_worker';
    public static function createUserSn($prefix = '', $length = 8)
    {
        $rand_str = '';
        for ($i = 0; $i < $length; $i++) {
            $rand_str .= mt_rand(1, 9);
        }
        $sn = $prefix . $rand_str;
        if (MasterWorker::where(['sn' => $sn])->find()) {
            return self::createUserSn($prefix, $length);
        }
        return $sn;
    }

    public function workerRegister()
    {
        return $this->hasOne(MasterWorkerRegister::class, 'worker_id', 'id');
    }

    public function workerInfo()
    {
        return $this->hasOne(MasterWorkerInfo::class, 'worker_id', 'id');
    }
    public function getRetentionMoneyStatusTextAttr($value,$data):string{
        $default = Cache::get('RETENTION_MONEY_STATUS');
        if (!$default) {
            $status = DictData::whereIn('type_value', 'retention_money_status')->column('name','value');
            Cache::set('RETENTION_MONEY_STATUS', json_encode($status,true),5);
        } else {
            $status = json_decode($default,true);
        }
        return $status[$data['retention_money_status']] ?? '';
    }

    public function getRetentionPayStatusTextAttr($value,$data):string{
        $default = Cache::get('RETENTION_PAY_STATUS');
        if (!$default) {
            $status = DictData::whereIn('type_value', 'retention_pay_status')->column('name','value');
            Cache::set('RETENTION_PAY_STATUS', json_encode($status,true),5);
        } else {
            $status = json_decode($default,true);
        }
       return $status[$data['retention_pay_status']] ?? '';

    }

    /**
     * @param $type [inc=新增，dec=减少]
     * @param $worker_id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function setWorktotal($type,$worker_id)
    {
        $worker = MasterWorker::where('id',$worker_id)->findOrEmpty();
        if($type == 'inc'){
            $worker->inc('work_total',1);
        }else{
            $work_total = $worker->work_total == 0?0:1;
            $worker->dec('work_total',$work_total);
        }
        $worker->save();
        return true;
    }
    public function getTimePeriodAttr($value)
    {
        return explode(',',$value);
    }
    public function getAvatarAttr($value)
    {
        return trim($value) ? FileService::getFileUrl($value) : '';
    }

    public function getRealAvatarAttr($value)
    {
        return trim($value) ? FileService::getFileUrl($value) : '';
    }

    public function workerAgree()
    {
        return $this->hasOne(MasterWorkerAgree::class, 'worker_id', 'id');
    }
    public function bankAccount()
    {
        return $this->hasOne(BankAccount::class, 'worker_id', 'id');
    }
    public function getLabelsAttr($value)
    {
        return $value?array_map(function($item){ return (int)$item; },explode(',',$value)):[];
    }
}