MasterWorkerLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace app\workerapi\logic;
  3. use app\adminapi\logic\training\TrainingBlockConfigLogic;
  4. use app\common\enum\worker\WorkerAccountLogEnum;
  5. use app\common\enum\YesNoEnum;
  6. use app\common\logic\BaseLogic;
  7. use app\common\model\bank_account\BankAccount;
  8. use app\common\model\master_worker\MasterWorker;
  9. use app\common\model\master_worker\MasterWorkerAccountLog;
  10. use app\common\model\master_worker\MasterWorkerAgree;
  11. use app\common\model\master_worker\MasterWorkerInfo;
  12. use app\common\model\master_worker_register\MasterWorkerRegister;
  13. use app\common\model\works\ServiceWork;
  14. use app\common\service\FileService;
  15. use app\workerapi\service\MasterWokerTaskRequiredService;
  16. use think\Exception;
  17. use think\facade\Config;
  18. use think\facade\Log;
  19. /**
  20. * @author 林海涛
  21. * @date 2024/7/10 下午1:45
  22. */
  23. class MasterWorkerLogic extends BaseLogic
  24. {
  25. public static function changePassword(array $params, int $userId)
  26. {
  27. try {
  28. $user = MasterWorker::findOrEmpty($userId);
  29. if ($user->isEmpty()) {
  30. throw new \Exception('用户不存在');
  31. }
  32. // 密码盐
  33. $passwordSalt = Config::get('project.unique_identification');
  34. /*if (!empty($user['password'])) {
  35. if (empty($params['old_password'])) {
  36. throw new \Exception('请填写旧密码');
  37. }
  38. $oldPassword = create_password($params['old_password'], $passwordSalt);
  39. if ($oldPassword != $user['password']) {
  40. throw new \Exception('原密码不正确');
  41. }
  42. }*/
  43. // 保存密码
  44. $password = create_password($params['password'], $passwordSalt);
  45. $user->password = $password;
  46. $user->save();
  47. return true;
  48. } catch (\Exception $e) {
  49. self::setError($e->getMessage());
  50. return false;
  51. }
  52. }
  53. public static function changeMobile(array $params, int $userId)
  54. {
  55. try {
  56. $user = MasterWorker::findOrEmpty($userId);
  57. if ($user->isEmpty()) {
  58. throw new \Exception('用户不存在');
  59. }
  60. if($user->mobile == $params['mobile']){
  61. throw new \Exception('输入的手机号相同');
  62. }
  63. $where = [['mobile', '=', $params['mobile']]];
  64. $existUser = MasterWorker::where($where)->findOrEmpty();
  65. if (!$existUser->isEmpty()) {
  66. throw new \Exception('该手机号已被使用');
  67. }
  68. $user->mobile= $params['mobile'];
  69. $user->save();
  70. //更新注册表手机号
  71. MasterWorkerRegister::where('worker_id',$userId)->update(['mobile'=>$params['mobile']]);
  72. //更新师傅信息表手机号
  73. MasterWorkerInfo::where('worker_id',$userId)->update(['mobile'=>$params['mobile']]);
  74. return true;
  75. } catch (\Exception $e) {
  76. self::setError($e->getMessage());
  77. return false;
  78. }
  79. }
  80. public static function logOff(int $userId)
  81. {
  82. try {
  83. $user = MasterWorker::findOrEmpty($userId);
  84. if ($user->isEmpty()) {
  85. throw new \Exception('用户不存在');
  86. }
  87. if($user->work_status == 0 ){
  88. throw new Exception('请先申请长期停单');
  89. }
  90. if($user->work_status == 1 ){
  91. throw new Exception('请等待长期停单审核');
  92. }
  93. $user->is_disable = YesNoEnum::YES;
  94. $user->save();
  95. return true;
  96. } catch (\Exception $e) {
  97. self::setError($e->getMessage());
  98. return false;
  99. }
  100. }
  101. public static function stopWork(int $userId)
  102. {
  103. try {
  104. $user = MasterWorker::findOrEmpty($userId);
  105. if ($user->isEmpty()) {
  106. throw new \Exception('用户不存在');
  107. }
  108. if($user->work_status == 1 ){
  109. throw new Exception('请等待长期停单审核');
  110. }
  111. if($user->work_status == 2 ){
  112. throw new Exception('长期停单审核通过');
  113. }
  114. $user->work_status = 1;
  115. $user->accept_order_status = 0;
  116. $user->save();
  117. return true;
  118. } catch (\Exception $e) {
  119. self::setError($e->getMessage());
  120. return false;
  121. }
  122. }
  123. public static function detail($userId): array
  124. {
  125. $worker = MasterWorker::field('id,team_id,team_role,sn,avatar,real_avatar,real_name,nickname,account,mobile,sex,estimate_money,user_money,earnest_money,exp,worker_number,work_status,accept_order_status')
  126. ->findOrEmpty($userId)
  127. ->toArray();
  128. //验证是否上传身份证
  129. $is_id_card = MasterWorkerInfo::where('worker_id',$userId)->whereIn('audit_state','0,1')->findOrEmpty()->toArray();
  130. //判断是否填写银行信息
  131. $is_bank = BankAccount::where('worker_id',$userId)->whereIn('audit_state','0,1')->findOrEmpty()->toArray();
  132. //监测是否签署服务合作协议
  133. $pdf = MasterWorkerAgree::where(['agree_type'=>'master_service_content','worker_id'=>$userId])->whereIn('audit_state','0,1')->value('pdf_url');
  134. $worker['is_id_card'] = !empty($is_id_card)?1:0;
  135. $worker['is_bank'] = !empty($is_bank)?1:0;
  136. $worker['is_service_agree'] = !empty($pdf)?1:0;
  137. //今日退款
  138. $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');
  139. //今日收益
  140. $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'];
  141. //本月成功订单
  142. $worker['success_work'] = ServiceWork::where(['master_worker_id'=>$worker['id'],'service_status'=>3])->whereTime('create_time', 'month')->count();
  143. //本月失败单
  144. $worker['fail_work'] = ServiceWork::where(['master_worker_id'=>$worker['id']])->whereIn('service_status','4,5')->whereTime('create_time', 'month')->count();
  145. return $worker;
  146. }
  147. public static function setInfo(int $userId, array $params)
  148. {
  149. try {
  150. if ($params['field'] == "avatar" || $params['field'] == "real_avatar") {
  151. $params['value'] = FileService::setFileUrl($params['value']);
  152. }
  153. $upData = [
  154. 'id' => $userId,
  155. $params['field'] => $params['value']
  156. ];
  157. if($params['field'] == 'accept_order_status'){
  158. $masterWorker = MasterWorker::where(['id'=>$userId])->findOrEmpty();
  159. if($masterWorker['work_status'] != 0 || $masterWorker['is_disable'] != 0){
  160. throw new Exception('该账号已禁用或已长期停单');
  161. }
  162. $accept_status_time = $masterWorker['accept_status_time'];
  163. if($masterWorker['accept_order_status'] ==1 && $params['value'] == 0 && $accept_status_time > 0){
  164. if(time() < ($accept_status_time+2*3600)){
  165. throw new Exception('开启接单后两小时后才能关闭接单');
  166. }
  167. }
  168. $upData['accept_status_time'] = time();
  169. //验证身份证信息是否审核通过
  170. $idCard = MasterWorkerInfo::where(['worker_id'=>$userId])->findOrEmpty();
  171. if ($idCard->isEmpty()) {
  172. return ['code'=>20,'msg'=>'请先完善身份证信息'];
  173. }
  174. if($idCard->audit_state == 0){
  175. return ['code'=>20,'msg'=>'身份证信息核验中,请等待核验完成'];
  176. }
  177. if($idCard->audit_state == 2){
  178. return ['code'=>20,'msg'=>'身份证信息核验不通过,请重新填写'];
  179. }
  180. //验证银行卡信息是否审核通过
  181. $bank = BankAccount::where(['worker_id'=>$userId])->findOrEmpty();
  182. if ($bank->isEmpty()) {
  183. return ['code'=>21,'msg'=>'请先完善银行卡信息'];
  184. }
  185. if($bank->audit_state == 0){
  186. return ['code'=>21,'msg'=>'银行卡信息核验中,请等待核验完成'];
  187. }
  188. if($bank->audit_state == 2){
  189. return ['code'=>21,'msg'=>'银行卡信息核验不通过,请重新填写'];
  190. }
  191. //验证协议信息是否审核通过
  192. $agree = MasterWorkerAgree::where(['worker_id'=>$userId])->findOrEmpty();
  193. if ($agree->isEmpty()) {
  194. return ['code'=>22,'msg'=>'请先签写协议信息'];
  195. }
  196. if($agree->audit_state == 0){
  197. return ['code'=>22,'msg'=>'协议信息核验中,请等待核验完成'];
  198. }
  199. if($agree->audit_state == 2){
  200. return ['code'=>22,'msg'=>'协议信息核验不通过,请重新签写'];
  201. }
  202. // 该工程师所有必须任务是否完成
  203. if(self::taskRequired($userId,$masterWorker['identity_source'])){
  204. return ['code'=>23,'msg'=>'培训中心必须任务未完成'];
  205. }
  206. }
  207. MasterWorker::update($upData);
  208. return [];
  209. } catch (\Exception $e) {
  210. self::$error = $e->getMessage();
  211. self::$returnCode = $e->getCode();
  212. return false;
  213. }
  214. }
  215. public static function taskRequired($userId,$identity_source): bool
  216. {
  217. /*[{"block_key":1,"type":"shop_goods_id","type_value":1,"res_name":"operate_status","execute_function":"shop_goods","is_must":false},
  218. {"block_key":2,"type":"training_task_id","type_value":1,"res_name":"operate_status","execute_function":"operateStatus","is_must":true},
  219. {"block_key":3,"type":"task_list","type_value":["1","2","4"],"res_name":"operate_status","execute_function":"operateStatus","is_must":false}]*/
  220. $configs = TrainingBlockConfigLogic::getRequiredConfig($identity_source);
  221. foreach ($configs as $item) {
  222. if($item['is_must']){
  223. Log::info('taskRequired-1'.json_encode($item));
  224. $service = (new MasterWokerTaskRequiredService());
  225. if(!method_exists($service,$item['execute_function'])){
  226. continue;
  227. }
  228. $res = $service->$item['execute_function']($userId);
  229. Log::info('taskRequired-2:'.$userId.'-'.$item['execute_function'].'-'.$res);
  230. if($res === false){
  231. return false;
  232. }
  233. }
  234. }
  235. return true;
  236. }
  237. }