MasterWorkerLogic.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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,identity_source')
  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. // 该工程师所有必须任务是否完成
  146. $worker['task_required'] = MasterWorkerLogic::taskRequired($worker['id'],$worker['identity_source'])?1:0;
  147. return $worker;
  148. }
  149. public static function setInfo(int $userId, array $params)
  150. {
  151. try {
  152. if ($params['field'] == "avatar" || $params['field'] == "real_avatar") {
  153. $params['value'] = FileService::setFileUrl($params['value']);
  154. }
  155. $upData = [
  156. 'id' => $userId,
  157. $params['field'] => $params['value']
  158. ];
  159. if($params['field'] == 'accept_order_status'){
  160. $masterWorker = MasterWorker::where(['id'=>$userId])->findOrEmpty();
  161. if($masterWorker['work_status'] != 0 || $masterWorker['is_disable'] != 0){
  162. throw new Exception('该账号已禁用或已长期停单');
  163. }
  164. $accept_status_time = $masterWorker['accept_status_time'];
  165. if($masterWorker['accept_order_status'] ==1 && $params['value'] == 0 && $accept_status_time > 0){
  166. if(time() < ($accept_status_time+2*3600)){
  167. throw new Exception('开启接单后两小时后才能关闭接单');
  168. }
  169. }
  170. $upData['accept_status_time'] = time();
  171. //验证身份证信息是否审核通过
  172. $idCard = MasterWorkerInfo::where(['worker_id'=>$userId])->findOrEmpty();
  173. if ($idCard->isEmpty()) {
  174. return ['code'=>20,'msg'=>'请先完善身份证信息'];
  175. }
  176. if($idCard->audit_state == 0){
  177. return ['code'=>20,'msg'=>'身份证信息核验中,请等待核验完成'];
  178. }
  179. if($idCard->audit_state == 2){
  180. return ['code'=>20,'msg'=>'身份证信息核验不通过,请重新填写'];
  181. }
  182. //验证银行卡信息是否审核通过
  183. $bank = BankAccount::where(['worker_id'=>$userId])->findOrEmpty();
  184. if ($bank->isEmpty()) {
  185. return ['code'=>21,'msg'=>'请先完善银行卡信息'];
  186. }
  187. if($bank->audit_state == 0){
  188. return ['code'=>21,'msg'=>'银行卡信息核验中,请等待核验完成'];
  189. }
  190. if($bank->audit_state == 2){
  191. return ['code'=>21,'msg'=>'银行卡信息核验不通过,请重新填写'];
  192. }
  193. //验证协议信息是否审核通过
  194. $agree = MasterWorkerAgree::where(['worker_id'=>$userId])->findOrEmpty();
  195. if ($agree->isEmpty()) {
  196. return ['code'=>22,'msg'=>'请先签写协议信息'];
  197. }
  198. if($agree->audit_state == 0){
  199. return ['code'=>22,'msg'=>'协议信息核验中,请等待核验完成'];
  200. }
  201. if($agree->audit_state == 2){
  202. return ['code'=>22,'msg'=>'协议信息核验不通过,请重新签写'];
  203. }
  204. // 该工程师所有必须任务是否完成
  205. if(self::taskRequired($userId,$masterWorker['identity_source'])){
  206. return ['code'=>23,'msg'=>'培训中心必须任务未完成'];
  207. }
  208. }
  209. MasterWorker::update($upData);
  210. return [];
  211. } catch (\Exception $e) {
  212. self::$error = $e->getMessage();
  213. self::$returnCode = $e->getCode();
  214. return false;
  215. }
  216. }
  217. public static function taskRequired($userId,$identity_source): bool
  218. {
  219. /*[{"block_key":1,"type":"shop_goods_id","type_value":1,"res_name":"operate_status","execute_function":"shop_goods","is_must":false},
  220. {"block_key":2,"type":"training_task_id","type_value":1,"res_name":"operate_status","execute_function":"operateStatus","is_must":true},
  221. {"block_key":3,"type":"task_list","type_value":["1","2","4"],"res_name":"operate_status","execute_function":"operateStatus","is_must":false}]*/
  222. $configs = TrainingBlockConfigLogic::getRequiredConfig($identity_source);
  223. foreach ($configs as $item) {
  224. if($item['is_must']){
  225. Log::info('taskRequired-1'.json_encode($item));
  226. if(empty($item['execute_function'])) continue;
  227. $service = (new MasterWokerTaskRequiredService());
  228. if(!method_exists($service,$item['execute_function'])){
  229. continue;
  230. }
  231. $res = $service->$item['execute_function']($userId);
  232. Log::info('taskRequired-2:'.$userId.'-'.$item['execute_function'].'-'.$res);
  233. if($res === false){
  234. return false;
  235. }
  236. }
  237. }
  238. return true;
  239. }
  240. }