MasterWorkerLogic.php 5.2 KB

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