MasterWorker.php 3.3 KB

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