MasterWorkerLogic.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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->accept_order_status = 0;
  108. $user->save();
  109. return true;
  110. } catch (\Exception $e) {
  111. self::setError($e->getMessage());
  112. return false;
  113. }
  114. }
  115. public static function detail($userId): array
  116. {
  117. $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')
  118. ->findOrEmpty($userId)
  119. ->toArray();
  120. //验证是否上传身份证
  121. $is_id_card = MasterWorkerInfo::where('worker_id',$userId)->whereIn('audit_state','0,1')->findOrEmpty()->toArray();
  122. //判断是否填写银行信息
  123. $is_bank = BankAccount::where('worker_id',$userId)->whereIn('audit_state','0,1')->findOrEmpty()->toArray();
  124. //监测是否签署服务合作协议
  125. $pdf = MasterWorkerAgree::where(['agree_type'=>'master_service_content','worker_id'=>$userId])->whereIn('audit_state','0,1')->value('pdf_url');
  126. $worker['is_id_card'] = !empty($is_id_card)?1:0;
  127. $worker['is_bank'] = !empty($is_bank)?1:0;
  128. $worker['is_service_agree'] = !empty($pdf)?1:0;
  129. //今日退款
  130. $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');
  131. //今日收益
  132. $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'];
  133. //本月成功订单
  134. $worker['success_work'] = ServiceWork::where(['master_worker_id'=>$worker['id'],'service_status'=>3])->whereTime('create_time', 'month')->count();
  135. //本月失败单
  136. $worker['fail_work'] = ServiceWork::where(['master_worker_id'=>$worker['id']])->whereIn('service_status','4,5')->whereTime('create_time', 'month')->count();
  137. return $worker;
  138. }
  139. public static function setInfo(int $userId, array $params)
  140. {
  141. try {
  142. if ($params['field'] == "avatar" || $params['field'] == "real_avatar") {
  143. $params['value'] = FileService::setFileUrl($params['value']);
  144. }
  145. $upData = [
  146. 'id' => $userId,
  147. $params['field'] => $params['value']
  148. ];
  149. if($params['field'] == 'accept_order_status'){
  150. $masterWorker = MasterWorker::where(['id'=>$userId])->findOrEmpty();
  151. if($masterWorker['work_status'] != 0 || $masterWorker['is_disable'] != 0){
  152. throw new Exception('该账号已禁用或已长期停单');
  153. }
  154. $accept_status_time = $masterWorker['accept_status_time'];
  155. if($masterWorker['accept_order_status'] ==1 && $params['value'] == 0 && $accept_status_time > 0){
  156. if(time() < ($accept_status_time+2*3600)){
  157. throw new Exception('开启接单后两小时后才能关闭接单');
  158. }
  159. }
  160. $upData['accept_status_time'] = time();
  161. //验证身份证信息是否审核通过
  162. $idCard = MasterWorkerInfo::where(['worker_id'=>$userId])->findOrEmpty();
  163. if ($idCard->isEmpty()) {
  164. return ['code'=>20,'msg'=>'请先完善身份证信息'];
  165. }
  166. if($idCard->audit_state == 0){
  167. return ['code'=>20,'msg'=>'身份证信息核验中,请等待核验完成'];
  168. }
  169. if($idCard->audit_state == 2){
  170. return ['code'=>20,'msg'=>'身份证信息核验不通过,请重新填写'];
  171. }
  172. //验证银行卡信息是否审核通过
  173. $bank = BankAccount::where(['worker_id'=>$userId])->findOrEmpty();
  174. if ($bank->isEmpty()) {
  175. return ['code'=>21,'msg'=>'请先完善银行卡信息'];
  176. }
  177. if($bank->audit_state == 0){
  178. return ['code'=>21,'msg'=>'银行卡信息核验中,请等待核验完成'];
  179. }
  180. if($bank->audit_state == 2){
  181. return ['code'=>21,'msg'=>'银行卡信息核验不通过,请重新填写'];
  182. }
  183. }
  184. MasterWorker::update($upData);
  185. return [];
  186. } catch (\Exception $e) {
  187. self::$error = $e->getMessage();
  188. self::$returnCode = $e->getCode();
  189. return false;
  190. }
  191. }
  192. }