MasterWorkerLogic.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\workerapi\logic;
  3. use app\common\enum\worker\WorkerAccountLogEnum;
  4. use app\common\enum\YesNoEnum;
  5. use app\common\logic\BaseLogic;
  6. use app\common\model\master_worker\MasterWorker;
  7. use app\common\model\master_worker\MasterWorkerAccountLog;
  8. use app\common\model\works\ServiceWork;
  9. use think\Exception;
  10. use think\facade\Config;
  11. /**
  12. * @author 林海涛
  13. * @date 2024/7/10 下午1:45
  14. */
  15. class MasterWorkerLogic extends BaseLogic
  16. {
  17. public static function changePassword(array $params, int $userId)
  18. {
  19. try {
  20. $user = MasterWorker::findOrEmpty($userId);
  21. if ($user->isEmpty()) {
  22. throw new \Exception('用户不存在');
  23. }
  24. // 密码盐
  25. $passwordSalt = Config::get('project.unique_identification');
  26. if (!empty($user['password'])) {
  27. if (empty($params['old_password'])) {
  28. throw new \Exception('请填写旧密码');
  29. }
  30. $oldPassword = create_password($params['old_password'], $passwordSalt);
  31. if ($oldPassword != $user['password']) {
  32. throw new \Exception('原密码不正确');
  33. }
  34. }
  35. // 保存密码
  36. $password = create_password($params['password'], $passwordSalt);
  37. $user->password = $password;
  38. $user->save();
  39. return true;
  40. } catch (\Exception $e) {
  41. self::setError($e->getMessage());
  42. return false;
  43. }
  44. }
  45. public static function changeMobile(array $params, int $userId)
  46. {
  47. try {
  48. $user = MasterWorker::findOrEmpty($userId);
  49. if ($user->isEmpty()) {
  50. throw new \Exception('用户不存在');
  51. }
  52. if($user->mobile == $params['mobile']){
  53. throw new \Exception('输入的手机号相同');
  54. }
  55. $where = [['mobile', '=', $params['mobile']]];
  56. $existUser = MasterWorker::where($where)->findOrEmpty();
  57. if (!$existUser->isEmpty()) {
  58. throw new \Exception('该手机号已被使用');
  59. }
  60. $user->mobile= $params['mobile'];
  61. $user->save();
  62. return true;
  63. } catch (\Exception $e) {
  64. self::setError($e->getMessage());
  65. return false;
  66. }
  67. }
  68. public static function logOff(int $userId)
  69. {
  70. try {
  71. $user = MasterWorker::findOrEmpty($userId);
  72. if ($user->isEmpty()) {
  73. throw new \Exception('用户不存在');
  74. }
  75. if($user->work_status == 0 ){
  76. throw new Exception('请先申请长期停单');
  77. }
  78. if($user->work_status == 1 ){
  79. throw new Exception('请等待长期停单审核');
  80. }
  81. $user->is_disable = YesNoEnum::YES;
  82. $user->save();
  83. return true;
  84. } catch (\Exception $e) {
  85. self::setError($e->getMessage());
  86. return false;
  87. }
  88. }
  89. public static function stopWork(int $userId)
  90. {
  91. try {
  92. $user = MasterWorker::findOrEmpty($userId);
  93. if ($user->isEmpty()) {
  94. throw new \Exception('用户不存在');
  95. }
  96. if($user->work_status == 1 ){
  97. throw new Exception('请等待长期停单审核');
  98. }
  99. if($user->work_status == 2 ){
  100. throw new Exception('长期停单审核通过');
  101. }
  102. $user->work_status = 1;
  103. $user->save();
  104. return true;
  105. } catch (\Exception $e) {
  106. self::setError($e->getMessage());
  107. return false;
  108. }
  109. }
  110. public static function detail($userId): array
  111. {
  112. $worker = MasterWorker::field('id,sn,avatar,real_avatar,real_name,nickname,account,mobile,sex,estimate_money,user_money,earnest_money,exp,worker_number,work_status')
  113. ->findOrEmpty($userId)
  114. ->toArray();
  115. //今日收益
  116. $worker['account_today'] = MasterWorkerAccountLog::where(['worker_id'=> $worker['id'],'action'=>1,'change_type'=>WorkerAccountLogEnum::UM_INC_ADMIN])->whereTime('create_time', 'today')->sum('change_amount');
  117. //本月成功订单
  118. $worker['success_work'] = ServiceWork::where(['master_worker_id'=>$worker['id'],'service_status'=>3])->whereTime('create_time', 'month')->count();
  119. //本月失败单
  120. $worker['fail_work'] = ServiceWork::where(['master_worker_id'=>$worker['id'],'service_status'=>4])->whereTime('create_time', 'month')->count();
  121. return $worker;
  122. }
  123. }