MasterWorkerLogic.php 11 KB

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