MasterWorkerLogic.php 13 KB

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