MasterWorkerLogic.php 12 KB

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