MasterWorkerLogic.php 7.6 KB

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