MasterWorker.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @author 林海涛
  4. * @date ${DATA}
  5. */
  6. namespace app\common\model\master_worker;
  7. use think\facade\Cache;
  8. use app\common\model\BaseModel;
  9. use app\common\model\dict\DictData;
  10. use app\common\service\FileService;
  11. use app\common\model\works\ServiceWork;
  12. use app\common\model\MasterWorkerRegister;
  13. /**
  14. * 工程师表
  15. * Class MasterWorker
  16. * @package app\common\model
  17. */
  18. class MasterWorker extends BaseModel
  19. {
  20. protected $name = 'master_worker';
  21. public static function createUserSn($prefix = '', $length = 8)
  22. {
  23. $rand_str = '';
  24. for ($i = 0; $i < $length; $i++) {
  25. $rand_str .= mt_rand(1, 9);
  26. }
  27. $sn = $prefix . $rand_str;
  28. if (MasterWorker::where(['sn' => $sn])->find()) {
  29. return self::createUserSn($prefix, $length);
  30. }
  31. return $sn;
  32. }
  33. public function workerRegister()
  34. {
  35. return $this->hasOne(MasterWorkerRegister::class, 'worker_id', 'id');
  36. }
  37. public function workerInfo()
  38. {
  39. return $this->hasOne(MasterWorkerInfo::class, 'worker_id', 'id');
  40. }
  41. public function getRetentionMoneyStatusTextAttr($value,$data):string{
  42. $default = Cache::get('RETENTION_MONEY_STATUS');
  43. if (!$default) {
  44. $status = DictData::whereIn('type_value', 'retention_money_status')->column('name','value');
  45. Cache::set('RETENTION_MONEY_STATUS', json_encode($status,true),5);
  46. } else {
  47. $status = json_decode($default,true);
  48. }
  49. return $status[$data['retention_money_status']] ?? '';
  50. }
  51. public function getRetentionPayStatusTextAttr($value,$data):string{
  52. $default = Cache::get('RETENTION_PAY_STATUS');
  53. if (!$default) {
  54. $status = DictData::whereIn('type_value', 'retention_pay_status')->column('name','value');
  55. Cache::set('RETENTION_PAY_STATUS', json_encode($status,true),5);
  56. } else {
  57. $status = json_decode($default,true);
  58. }
  59. return $status[$data['retention_pay_status']] ?? '';
  60. }
  61. /**
  62. * @param $type [inc=新增,dec=减少]
  63. * @param $worker_id
  64. * @return bool
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public static function setWorktotal($type,$worker_id)
  70. {
  71. $worker = MasterWorker::where('id',$worker_id)->findOrEmpty();
  72. if($type == 'inc'){
  73. $worker->inc('work_total',1);
  74. }else{
  75. $work_total = $worker->work_total == 0?0:1;
  76. $worker->dec('work_total',$work_total);
  77. }
  78. $worker->save();
  79. return true;
  80. }
  81. public function getTimePeriodAttr($value)
  82. {
  83. return explode(',',$value);
  84. }
  85. public function getAvatarAttr($value)
  86. {
  87. return trim($value) ? FileService::getFileUrl($value) : '';
  88. }
  89. public function getRealAvatarAttr($value)
  90. {
  91. return trim($value) ? FileService::getFileUrl($value) : '';
  92. }
  93. public function workerAgree()
  94. {
  95. return $this->hasOne(MasterWorkerAgree::class, 'worker_id', 'id');
  96. }
  97. public function bankAccount()
  98. {
  99. return $this->hasOne(BankAccount::class, 'worker_id', 'id');
  100. }
  101. public function getLabelsAttr($value)
  102. {
  103. return $value?array_map(function($item){ return (int)$item; },explode(',',$value)):[];
  104. }
  105. //工单记录
  106. public function serviceWork()
  107. {
  108. 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');
  109. }
  110. //当月停单记录
  111. public function stopList()
  112. {
  113. return $this->hasMany(MasterWorkerStop::class,'worker_id','id')
  114. ->where('start_time', 'between',[strtotime(date('Y-m-01 00:00:00')),strtotime(date('Y-m-t 23:59:59'))])
  115. ->order('start_time','asc');
  116. }
  117. }