| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- /**
- * @author 林海涛
- * @date ${DATA}
- */
- namespace app\common\model\master_worker;
- use think\facade\Cache;
- use app\common\model\BaseModel;
- use app\common\model\dict\DictData;
- use app\common\service\FileService;
- use app\common\model\works\ServiceWork;
- use app\common\model\MasterWorkerRegister;
- /**
- * 工程师表
- * 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)):[];
- }
-
- //工单记录
- public function serviceWork()
- {
- return $this->hasMany(ServiceWork::class,'master_worker_id','id')->field('id,master_worker_id,title,address,appointment_time,dispatch_time,estimated_finish_time,finished_time,goods_category_id,mobile,real_name,city,work_sn,work_status','in',[0,1])->order('appointment_time','asc');
- }
- //当月停单记录
- public function stopList()
- {
- return $this->hasMany(MasterWorkerStop::class,'worker_id','id')
- ->where('start_time', 'between',[strtotime(date('Y-m-01 00:00:00')),strtotime(date('Y-m-t 23:59:59'))])
- ->order('start_time','asc');
- }
- }
|