MasterWorkerLogic.php 6.6 KB

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