MasterWorker.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 getRetentionMoneyStatusTextAttr($value,$data):string{
  37. $default = Cache::get('RETENTION_MONEY_STATUS');
  38. if (!$default) {
  39. $status = DictData::whereIn('type_value', 'retention_money_status')->column('name','value');
  40. Cache::set('RETENTION_MONEY_STATUS', json_encode($status,true),5);
  41. } else {
  42. $status = json_decode($default,true);
  43. }
  44. return $status[$data['retention_money_status']] ?? '';
  45. }
  46. public function getRetentionPayStatusTextAttr($value,$data):string{
  47. $default = Cache::get('RETENTION_PAY_STATUS');
  48. if (!$default) {
  49. $status = DictData::whereIn('type_value', 'retention_pay_status')->column('name','value');
  50. Cache::set('RETENTION_PAY_STATUS', json_encode($status,true),5);
  51. } else {
  52. $status = json_decode($default,true);
  53. }
  54. return $status[$data['retention_pay_status']] ?? '';
  55. }
  56. /**
  57. * @param $type [inc=新增,dec=减少]
  58. * @param $worker_id
  59. * @return bool
  60. * @throws \think\db\exception\DataNotFoundException
  61. * @throws \think\db\exception\DbException
  62. * @throws \think\db\exception\ModelNotFoundException
  63. */
  64. public static function setWorktotal($type,$worker_id)
  65. {
  66. $worker = MasterWorker::where('id',$worker_id)->findOrEmpty();
  67. if($type == 'inc'){
  68. $worker->inc('work_total',1);
  69. }else{
  70. $work_total = $worker->work_total == 0?0:1;
  71. $worker->dec('work_total',$work_total);
  72. }
  73. $worker->save();
  74. return true;
  75. }
  76. public function getAvatarAttr($value)
  77. {
  78. return trim($value) ? FileService::getFileUrl($value) : '';
  79. }
  80. public function getRealAvatarAttr($value)
  81. {
  82. return trim($value) ? FileService::getFileUrl($value) : '';
  83. }
  84. }