MasterWorkerLogic.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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\master_worker\MasterWorkerQuestion;
  14. use app\common\model\master_worker\MasterWorkerInterview;
  15. use app\common\model\training\TrainingWorkerTask;
  16. use app\common\model\works\ServiceWork;
  17. use app\common\service\FileService;
  18. use app\workerapi\service\MasterWokerTaskRequiredService;
  19. use think\Exception;
  20. use think\facade\Config;
  21. use think\facade\Log;
  22. use think\facade\Db;
  23. /**
  24. * @author 林海涛
  25. * @date 2024/7/10 下午1:45
  26. */
  27. class MasterWorkerLogic extends BaseLogic
  28. {
  29. public static function changePassword(array $params, int $userId)
  30. {
  31. try {
  32. $user = MasterWorker::findOrEmpty($userId);
  33. if ($user->isEmpty()) {
  34. throw new \Exception('用户不存在');
  35. }
  36. // 密码盐
  37. $passwordSalt = Config::get('project.unique_identification');
  38. /*if (!empty($user['password'])) {
  39. if (empty($params['old_password'])) {
  40. throw new \Exception('请填写旧密码');
  41. }
  42. $oldPassword = create_password($params['old_password'], $passwordSalt);
  43. if ($oldPassword != $user['password']) {
  44. throw new \Exception('原密码不正确');
  45. }
  46. }*/
  47. // 保存密码
  48. $password = create_password($params['password'], $passwordSalt);
  49. $user->password = $password;
  50. $user->save();
  51. return true;
  52. } catch (\Exception $e) {
  53. self::setError($e->getMessage());
  54. return false;
  55. }
  56. }
  57. public static function changeMobile(array $params, int $userId)
  58. {
  59. try {
  60. $user = MasterWorker::findOrEmpty($userId);
  61. if ($user->isEmpty()) {
  62. throw new \Exception('用户不存在');
  63. }
  64. if($user->mobile == $params['mobile']){
  65. throw new \Exception('输入的手机号相同');
  66. }
  67. $where = [['mobile', '=', $params['mobile']]];
  68. $existUser = MasterWorker::where($where)->findOrEmpty();
  69. if (!$existUser->isEmpty()) {
  70. throw new \Exception('该手机号已被使用');
  71. }
  72. $user->mobile= $params['mobile'];
  73. $user->save();
  74. //更新注册表手机号
  75. MasterWorkerRegister::where('worker_id',$userId)->update(['mobile'=>$params['mobile']]);
  76. //更新师傅信息表手机号
  77. MasterWorkerInfo::where('worker_id',$userId)->update(['mobile'=>$params['mobile']]);
  78. return true;
  79. } catch (\Exception $e) {
  80. self::setError($e->getMessage());
  81. return false;
  82. }
  83. }
  84. public static function logOff(int $userId)
  85. {
  86. try {
  87. $user = MasterWorker::findOrEmpty($userId);
  88. if ($user->isEmpty()) {
  89. throw new \Exception('用户不存在');
  90. }
  91. if($user->work_status == 0 ){
  92. throw new Exception('请先申请长期停单');
  93. }
  94. if($user->work_status == 1 ){
  95. throw new Exception('请等待长期停单审核');
  96. }
  97. $user->is_disable = YesNoEnum::YES;
  98. $user->save();
  99. return true;
  100. } catch (\Exception $e) {
  101. self::setError($e->getMessage());
  102. return false;
  103. }
  104. }
  105. public static function stopWork(int $userId)
  106. {
  107. try {
  108. $user = MasterWorker::findOrEmpty($userId);
  109. if ($user->isEmpty()) {
  110. throw new \Exception('用户不存在');
  111. }
  112. if($user->work_status == 1 ){
  113. throw new Exception('请等待长期停单审核');
  114. }
  115. if($user->work_status == 2 ){
  116. throw new Exception('长期停单审核通过');
  117. }
  118. $user->work_status = 1;
  119. $user->accept_order_status = 0;
  120. $user->save();
  121. return true;
  122. } catch (\Exception $e) {
  123. self::setError($e->getMessage());
  124. return false;
  125. }
  126. }
  127. public static function detail($userId): array
  128. {
  129. $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')
  130. ->findOrEmpty($userId)
  131. ->toArray();
  132. //验证是否上传身份证
  133. $is_id_card = MasterWorkerInfo::where('worker_id',$userId)->whereIn('audit_state','0,1')->findOrEmpty()->toArray();
  134. //判断是否填写银行信息
  135. $is_bank = BankAccount::where('worker_id',$userId)->whereIn('audit_state','0,1')->findOrEmpty()->toArray();
  136. //监测是否签署服务合作协议
  137. $pdf = MasterWorkerAgree::where(['agree_type'=>'master_service_content','worker_id'=>$userId])->whereIn('audit_state','0,1')->value('pdf_url');
  138. $worker['is_id_card'] = !empty($is_id_card)?1:0;
  139. $worker['is_bank'] = !empty($is_bank)?1:0;
  140. $worker['is_service_agree'] = !empty($pdf)?1:0;
  141. //今日退款
  142. $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');
  143. //今日收益
  144. $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'];
  145. //本月成功订单
  146. $worker['success_work'] = ServiceWork::where(['master_worker_id'=>$worker['id'],'service_status'=>3])->whereTime('create_time', 'month')->count();
  147. //本月失败单
  148. $worker['fail_work'] = ServiceWork::where(['master_worker_id'=>$worker['id']])->whereIn('service_status','4,5')->whereTime('create_time', 'month')->count();
  149. // 该工程师所有必须任务是否完成
  150. $worker['task_required'] = MasterWorkerLogic::taskRequired($worker['id'],$worker['identity_source'])?1:0;
  151. return $worker;
  152. }
  153. public static function setInfo(int $userId, array $params)
  154. {
  155. try {
  156. if ($params['field'] == "avatar" || $params['field'] == "real_avatar") {
  157. $params['value'] = FileService::setFileUrl($params['value']);
  158. }
  159. $upData = [
  160. 'id' => $userId,
  161. $params['field'] => $params['value']
  162. ];
  163. if($params['field'] == 'accept_order_status'){
  164. $masterWorker = MasterWorker::where(['id'=>$userId])->findOrEmpty();
  165. if($masterWorker['work_status'] != 0 || $masterWorker['is_disable'] != 0){
  166. throw new Exception('该账号已禁用或已长期停单');
  167. }
  168. $accept_status_time = $masterWorker['accept_status_time'];
  169. if($masterWorker['accept_order_status'] ==1 && $params['value'] == 0 && $accept_status_time > 0){
  170. if(time() < ($accept_status_time+2*3600)){
  171. throw new Exception('开启接单后两小时后才能关闭接单');
  172. }
  173. }
  174. $upData['accept_status_time'] = time();
  175. //验证身份证信息是否审核通过
  176. $idCard = MasterWorkerInfo::where(['worker_id'=>$userId])->findOrEmpty();
  177. if ($idCard->isEmpty()) {
  178. return ['code'=>20,'msg'=>'请先完善身份证信息'];
  179. }
  180. if($idCard->audit_state == 0){
  181. return ['code'=>20,'msg'=>'身份证信息核验中,请等待核验完成'];
  182. }
  183. if($idCard->audit_state == 2){
  184. return ['code'=>20,'msg'=>'身份证信息核验不通过,请重新填写'];
  185. }
  186. //验证银行卡信息是否审核通过
  187. $bank = BankAccount::where(['worker_id'=>$userId])->findOrEmpty();
  188. if ($bank->isEmpty()) {
  189. return ['code'=>21,'msg'=>'请先完善银行卡信息'];
  190. }
  191. if($bank->audit_state == 0){
  192. return ['code'=>21,'msg'=>'银行卡信息核验中,请等待核验完成'];
  193. }
  194. if($bank->audit_state == 2){
  195. return ['code'=>21,'msg'=>'银行卡信息核验不通过,请重新填写'];
  196. }
  197. //验证协议信息是否审核通过
  198. $agree = MasterWorkerAgree::where(['worker_id'=>$userId])->findOrEmpty();
  199. if ($agree->isEmpty()) {
  200. return ['code'=>22,'msg'=>'请先签写协议信息'];
  201. }
  202. if($agree->audit_state == 0){
  203. return ['code'=>22,'msg'=>'协议信息核验中,请等待核验完成'];
  204. }
  205. if($agree->audit_state == 2){
  206. return ['code'=>22,'msg'=>'协议信息核验不通过,请重新签写'];
  207. }
  208. // 该工程师所有必须任务是否完成
  209. if(self::taskRequired($userId,$masterWorker['identity_source'])){
  210. return ['code'=>23,'msg'=>'培训中心必须任务未完成'];
  211. }
  212. }
  213. MasterWorker::update($upData);
  214. return [];
  215. } catch (\Exception $e) {
  216. self::$error = $e->getMessage();
  217. self::$returnCode = $e->getCode();
  218. return false;
  219. }
  220. }
  221. public static function taskRequired($userId,$identity_source): bool
  222. {
  223. /*[{"block_key":1,"type":"shop_goods_id","type_value":1,"res_name":"operate_status","execute_function":"shop_goods","is_must":false},
  224. {"block_key":2,"type":"training_task_id","type_value":1,"res_name":"operate_status","execute_function":"operateStatus","is_must":true},
  225. {"block_key":3,"type":"task_list","type_value":["1","2","4"],"res_name":"operate_status","execute_function":"operateStatus","is_must":false}]*/
  226. $trainingWorkerTask = TrainingWorkerTask::where(['master_worker_id'=>$userId])->findOrEmpty();
  227. if($trainingWorkerTask->isEmpty()){
  228. return true;
  229. }
  230. $configs = TrainingBlockConfigLogic::getRequiredConfig($identity_source);
  231. foreach ($configs as $item) {
  232. if($item['is_must']){
  233. Log::info('taskRequired-1'.json_encode($item));
  234. if(empty($item['execute_function'])) continue;
  235. if(!class_exists(\app\workerapi\service\MasterWokerTaskRequiredService::class)){
  236. continue;
  237. }
  238. if(!method_exists(\app\workerapi\service\MasterWokerTaskRequiredService::class,$item['execute_function'])){
  239. continue;
  240. }
  241. $execute_function = $item['execute_function'];
  242. $res = (new MasterWokerTaskRequiredService())::$execute_function($userId);
  243. Log::info('taskRequired-2:'.$userId.'-'.$item['execute_function'].'-'.$res);
  244. if($res === false){
  245. return false;
  246. }
  247. }
  248. }
  249. return true;
  250. }
  251. /**
  252. * 查询机器人面试结果
  253. */
  254. public static function getInterview(array $params)
  255. {
  256. if (empty($params['worker_id'])) {
  257. return false;
  258. }
  259. $info = MasterWorkerInterview::where('worker_id',$params['worker_id'])
  260. ->field('id,worker_id,type,status,nickname,create_time')
  261. ->select()
  262. ->toArray();
  263. return $info;
  264. }
  265. /**
  266. * 机器人面试结果
  267. */
  268. public static function setInterview(array $params)
  269. {
  270. try {
  271. if (empty($params['worker_id']) || empty($params['answer']) || empty($params['answer1']) || empty($params['answer2']) ) {
  272. return false;
  273. }
  274. if (!empty($params['answer7'])) {
  275. //维修
  276. $type = 1;
  277. } else if (!empty($params['answer6'])) {
  278. //清洗
  279. $type = 2;
  280. } else if (!empty($params['answer5'])) {
  281. //安装
  282. $type = 3;
  283. $params['status'] = 1;
  284. } else {
  285. $type = 0;
  286. $params['status'] = 0;
  287. }
  288. //校验一下工程师注册ID是否有效
  289. $worker_register_id = $params['worker_id'];
  290. $worker_register_id = MasterWorkerRegister::where('id',$worker_register_id)->value('id');
  291. if (!$worker_register_id) {
  292. return false;
  293. }
  294. $info = MasterWorkerInterview::where('worker_id',$params['worker_id'])->value("id");
  295. if ($info) {
  296. return false;
  297. }
  298. $params['nickname'] = isset($params['nickname']) ?? '';
  299. $insertData = [
  300. 'sys_uuid' => $params['sys_uuid'],
  301. 'worker_id' => $params['worker_id'],
  302. 'nickname' => $params['nickname'],
  303. 'type' => $type,
  304. 'status' => $params['status'],
  305. ];
  306. unset($params['sys_uuid']);
  307. unset($params['worker_id']);
  308. unset($params['nickname']);
  309. unset($params['status']);
  310. $insertData['content'] = json_encode($params);
  311. Db::startTrans();
  312. MasterWorkerInterview::create($insertData);
  313. MasterWorkerRegisterLogic::createMasterWorker(['worker_register_id' => $worker_register_id]);
  314. Db::commit();
  315. return [];
  316. } catch (\Exception $e) {
  317. Db::rollback();
  318. self::$error = $e->getMessage();
  319. self::$returnCode = $e->getCode();
  320. return false;
  321. }
  322. }
  323. /**
  324. * 查询工程师面试题库
  325. */
  326. public static function getQuestion(array $params)
  327. {
  328. if ($params['type'] == 1 || $params['type'] == 2) {
  329. $category = trim($params['category']);
  330. $category = str_replace(',', ',', $category);
  331. $category = explode(',', $category);
  332. $category = array_slice($category, 0, 5);
  333. $list = MasterWorkerQuestion::where('type',$params['type'])
  334. ->whereIn('category', $category)
  335. ->where('type',$params['type'])
  336. ->limit(100)
  337. ->select()
  338. ->toArray();
  339. // 按 category 分组
  340. $grouped = [];
  341. foreach ($list as $item) {
  342. $category = $item['category'];
  343. if (!isset($grouped[$category])) {
  344. $grouped[$category] = [];
  345. }
  346. $grouped[$category][] = $item;
  347. }
  348. // 从每组中随机取 5 个元素
  349. $result = [];
  350. foreach ($grouped as $category => $items) {
  351. $randomItems = [];
  352. $count = count($items);
  353. if ($count <= 5) {
  354. $randomItems = $items;
  355. } else {
  356. $keys = array_rand($items, 5);
  357. foreach ($keys as $key) {
  358. $randomItems[] = $items[$key];
  359. }
  360. }
  361. $result = array_merge($result,$randomItems);
  362. }
  363. $text = '';
  364. $i = 0;
  365. foreach($result as $item) {
  366. $i++;
  367. $text .= $i.'.'.$item['title']."\r\n"." ". str_replace(",","\r\n",$item['options'])."\r\n\r\n";
  368. }
  369. return [
  370. 'list' => array_values($result),
  371. 'text' => $text
  372. ];
  373. }
  374. return false;
  375. }
  376. public static function getRegInfo(array $params)
  377. {
  378. if (empty($params['user'])){
  379. return 0;
  380. }
  381. $user = trim($params['user']);
  382. $pattern = '/(\d{11})/';
  383. if (preg_match($pattern, $user, $matches)) {
  384. // 提取手机号
  385. $mobile = $matches[1];
  386. }
  387. $specialChars = array('.', ',', '。', ',', $mobile);
  388. $name = str_replace($specialChars,"",$user);
  389. return (int)MasterWorkerRegister::where('name',$name)->where('mobile',$mobile)->value('id');
  390. }
  391. }