GroupServiceWorkLogic.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\works;
  15. use think\db\Query;
  16. use think\Exception;
  17. use think\facade\Db;
  18. use app\common\enum\RefundEnum;
  19. use app\common\logic\BaseLogic;
  20. use app\common\logic\RefundLogic;
  21. use PhpOffice\PhpSpreadsheet\IOFactory;
  22. use app\common\model\refund\RefundRecord;
  23. use app\common\model\works\GroupServiceWork;
  24. use app\common\model\group_activity\GroupOrder;
  25. use app\workerapi\logic\GroupServiceWorkLogLogic;
  26. use app\common\model\group_activity\GroupUserOrder;
  27. use app\common\model\master_worker\MasterWorkerTemporary;
  28. /**
  29. * GroupServiceWork逻辑
  30. * Class GroupServiceWorkLogic
  31. * @package app\adminapi\logic\works
  32. */
  33. class GroupServiceWorkLogic extends BaseLogic
  34. {
  35. /**
  36. * @notes 编辑
  37. * @param array $params
  38. * @return bool
  39. * @author likeadmin
  40. * @date 2024/07/10 18:17
  41. */
  42. public static function edit(array $params): bool
  43. {
  44. Db::startTrans();
  45. try {
  46. $work = GroupServiceWork::where('id', $params['id'])->findOrEmpty()->toArray();
  47. if (empty($work)) {
  48. throw new Exception('工单不存在');
  49. }
  50. if ($work['third_type'] == 3) {
  51. GroupServiceWork::where('id', $params['id'])->update([
  52. 'work_total' => $params['work_total'],
  53. 'work_amount' => $params['work_amount'],
  54. 'settlement_amount' => $params['settlement_amount'],
  55. 'settlement_status' => $params['settlement_status'],
  56. 'real_name' => $params['real_name'],
  57. 'mobile' => $params['mobile'],
  58. 'address' => $params['address'],
  59. 'appointment_time' => strtotime($params['appointment_time']),
  60. 'remark' => $params['remark'],
  61. ]);
  62. } else {
  63. GroupServiceWork::where('id', $params['id'])->update([
  64. 'real_name' => $params['real_name'],
  65. 'mobile' => $params['mobile'],
  66. 'address' => $params['address'],
  67. 'appointment_time' => strtotime($params['appointment_time']),
  68. 'remark' => $params['remark'],
  69. ]);
  70. }
  71. Db::commit();
  72. return true;
  73. } catch (\Exception $e) {
  74. Db::rollback();
  75. self::setError($e->getMessage());
  76. return false;
  77. }
  78. }
  79. /**
  80. * 工程师确认上门
  81. * @param $params
  82. * @return false|void
  83. */
  84. public static function confirmDoor($params)
  85. {
  86. Db::startTrans();
  87. try {
  88. $work = GroupServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  89. if($work->isEmpty()){
  90. throw new Exception('工单不存在');
  91. }
  92. if($work->work_status != 3){
  93. throw new Exception('请勿重复点击');
  94. }
  95. $work->finally_door_time = time();//最后上门时间
  96. $work->work_status = 4;//已上门
  97. $work->service_status = 1;//服务中
  98. $work->work_images = $params['work_images'];
  99. $work->save();
  100. //添加变更日志
  101. $work_log = [
  102. 'type' => 3,
  103. 'work_id'=>$work->id,
  104. 'master_worker_id'=>$work->master_worker_id,
  105. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'已上门',
  106. ];
  107. GroupServiceWorkLogLogic::add($work_log);
  108. Db::commit();
  109. //给客户发送工单验证码
  110. event('Notice', [
  111. 'scene_id' => 128,
  112. 'params' => [
  113. 'user_id' => 0,
  114. 'mobile' => $work->mobile,
  115. 'code' => $work->code,
  116. ]
  117. ]);
  118. }
  119. catch (\Exception $e) {
  120. Db::rollback();
  121. self::setError($e->getMessage());
  122. return false;
  123. }
  124. }
  125. /**
  126. * 工程师确认服务完成
  127. * @param $params
  128. * @return false|void
  129. */
  130. public static function confirmServiceFinish($params)
  131. {
  132. Db::startTrans();
  133. try {
  134. $work = GroupServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  135. if($work->isEmpty()){
  136. throw new Exception('工单不存在');
  137. }
  138. if($params['code'] != $work->code) {
  139. throw new Exception('验收码错误');
  140. }
  141. if($work->work_status !=4){
  142. throw new Exception('请勿重复操作');
  143. }
  144. $work->finished_time = time();//完成时间
  145. $work->finished_images = $params['finished_images'];
  146. $work->user_confirm_status = 5;//用户确认完成
  147. $work->work_status = 7;//服务工单已完结
  148. $work->service_status = 3;//工单已完成
  149. $work->save();
  150. //添加变更日志
  151. $work_log = [
  152. 'type' => 4,
  153. 'work_id'=>$work->id,
  154. 'master_worker_id'=>$work->master_worker_id,
  155. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'提交了确认服务完成',
  156. ];
  157. GroupServiceWorkLogLogic::add($work_log);
  158. Db::commit();
  159. } catch (\Exception $e) {
  160. Db::rollback();
  161. self::setError($e->getMessage());
  162. return false;
  163. }
  164. }
  165. public static function againDoor($params)
  166. {
  167. Db::startTrans();
  168. try {
  169. $work = GroupServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  170. if($work->isEmpty()){
  171. throw new \Exception('工单不存在');
  172. }
  173. if ($work->service_status >= 1) {
  174. throw new \Exception('工单已完成或服务中');
  175. }
  176. //添加变更日志
  177. $work_log = [
  178. 'type' => 6,
  179. 'work_id'=>$work->id,
  180. 'master_worker_id'=>$work->master_worker_id,
  181. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'修改预约上门时间',
  182. ];
  183. GroupServiceWorkLogLogic::add($work_log);
  184. $work->appointment_time = strtotime($params['appointment_time']);
  185. $work->save();
  186. Db::commit();
  187. return true;
  188. } catch (\Exception $e) {
  189. Db::rollback();
  190. self::setError($e->getMessage());
  191. return false;
  192. }
  193. }
  194. //修改备注
  195. public static function remark($params)
  196. {
  197. try {
  198. $work = GroupServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  199. if($work->isEmpty()){
  200. throw new Exception('工单不存在');
  201. }
  202. if (mb_strlen($params['remark']) > 100) {
  203. throw new Exception('备注不能超过100个字符');
  204. }
  205. $work->remark = $params['remark'];
  206. $work->save();
  207. return true;
  208. } catch (\Exception $e) {
  209. self::setError($e->getMessage());
  210. return false;
  211. }
  212. }
  213. public static function allocateWorker($params,$userInfo){
  214. Db::startTrans();
  215. try {
  216. $ids = explode(",",$params['id']);
  217. if (count($ids) > 50) {
  218. throw new \Exception('单次最多分配50个');
  219. }
  220. $worker = MasterWorkerTemporary::where(['id'=>$params['master_worker_id'],'is_disable' =>0])->findOrEmpty();
  221. if($worker->isEmpty()){
  222. throw new \Exception('工程师不存在或被禁用');
  223. }
  224. //批量派单
  225. $work = GroupServiceWork::whereIn('id',$ids)->where('work_status','<',6)->select()->toArray();
  226. foreach($work as $item) {
  227. if($item['master_worker_id'] != $params['master_worker_id']){
  228. $work_log = [
  229. 'type' => 1,
  230. 'work_id'=>$item['id'],
  231. 'master_worker_id'=>$params['master_worker_id'],
  232. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'分配了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  233. ];
  234. GroupServiceWorkLogLogic::add($work_log);
  235. GroupServiceWork::where('id',$item['id'])->update([
  236. 'master_worker_id' => $params['master_worker_id'],
  237. 'work_status' => 3,
  238. 'dispatch_time' => time(),
  239. 'receive_time' => time(),
  240. 'user_confirm_status' => 2,
  241. 'code' => str_pad(rand(0, 9999), 4, '0', STR_PAD_LEFT),//从 0 - 9 中随机取 4 个数字
  242. ]);
  243. }
  244. }
  245. Db::commit();
  246. // 工程师派单通知【给工程师的通知】【公众号通知,不发短信】
  247. // $workDetail = GroupServiceWorkLogic::detail($params);
  248. // $res = event('Notice', [
  249. // 'scene_id' => 113,
  250. // 'params' => [
  251. // 'user_id' => $params['master_worker_id'],
  252. // 'order_id' => $params['id'],
  253. // 'thing9' => $workDetail['title'],
  254. // 'time7' => $workDetail['appointment_time'],
  255. // 'thing8' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
  256. // 'phone_number6' => asteriskString($workDetail['mobile']),
  257. // ]
  258. // ]);
  259. return true;
  260. }catch(\Exception $e){
  261. Db::rollback();
  262. self::setError($e->getMessage());
  263. return false;
  264. }
  265. }
  266. public static function cancelAllocation($params,$userInfo){
  267. Db::startTrans();
  268. try {
  269. $work = GroupServiceWork::findOrEmpty($params['id']);
  270. if($work->isEmpty()){
  271. throw new Exception('工单不存在');
  272. }
  273. if($work->work_status >=6 ){
  274. throw new \Exception('工单状态只能修改待结算之前的');
  275. }
  276. $worker = MasterWorkerTemporary::where(['id'=>$work->master_worker_id])->findOrEmpty();
  277. if($worker->isEmpty()){
  278. throw new \Exception('工程师不存在');
  279. }
  280. $params['master_worker_id'] = (isset($params['master_worker_id']) && !empty($params['master_worker_id']))?$params['master_worker_id']:$work->master_worker_id;
  281. //添加变更日志
  282. $work_log = [
  283. 'type' => 2,
  284. 'work_id'=>$work->id,
  285. 'master_worker_id'=>$work->master_worker_id,
  286. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'取消了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  287. ];
  288. GroupServiceWorkLogLogic::add($work_log);
  289. $work->master_worker_id = 0;
  290. $work->work_status = 0;
  291. $work->dispatch_time = 0;
  292. $work->receive_time = 0;
  293. $work->save();
  294. Db::commit();
  295. return true;
  296. }catch(\Exception $e){
  297. Db::rollback();
  298. self::setError($e->getMessage());
  299. return false;
  300. }
  301. }
  302. /**
  303. * 工单详情
  304. * @param $params
  305. * @return array|false
  306. */
  307. public static function detail($params){
  308. $work_where = !empty($params['work_sn'])?['work_sn'=>$params['work_sn']]:['id'=>$params['id']];
  309. if (!empty($params['master_worker_id'])) {
  310. $work_where['master_worker_id'] = $params['master_worker_id'];
  311. }
  312. $result = GroupServiceWork::with([
  313. 'worker'=> function(Query $query) {
  314. $query->field('id,worker_number,real_name,mobile');
  315. },
  316. 'serviceWorkLog' =>function(Query $query){
  317. $query->field('id,work_id,opera_log,create_time');
  318. }
  319. ])
  320. ->where($work_where)
  321. ->field('id,work_sn,real_name,mobile,address,title,category_type,master_worker_id,work_status,user_confirm_status,service_status,dispatch_time,appointment_time,receive_time,work_images,finished_images,finished_time,area_name,finally_door_time,create_time,remark,third_type,work_total,work_amount,settlement_amount,settlement_status,work_pay_status')
  322. ->append(['id','work_status_text','service_status_text'])
  323. ->findOrEmpty()->toArray();
  324. if (empty($result)) {
  325. self::setError('工单不存在');
  326. return false;
  327. }
  328. //工程师工单按钮状态
  329. $work_service_status = 0;
  330. $work_service_status_text = '待派单';
  331. //工单状态
  332. if($result['work_status'] == 1){
  333. $work_service_status = 1;
  334. $work_service_status_text = '待领单';
  335. }
  336. if($result['work_status'] == 2){
  337. $work_service_status = 2;
  338. $work_service_status_text = '预约上门';
  339. }
  340. if($result['work_status'] == 3){
  341. $work_service_status = 3;
  342. $work_service_status_text = '等待上门';
  343. }
  344. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  345. $work_service_status = 7;
  346. $work_service_status_text = '完成服务';
  347. }
  348. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  349. $work_service_status = 8;
  350. $work_service_status_text = '用户确认完成服务中';
  351. }
  352. if($result['work_status'] ==6){
  353. $work_service_status = 9;
  354. $work_service_status_text = '待结算';
  355. }
  356. if($result['work_status'] ==7){
  357. $work_service_status = 10;
  358. $work_service_status_text = '已完结';
  359. }
  360. if($result['work_status'] ==8){
  361. $work_service_status = 11;
  362. $work_service_status_text = '已评价';
  363. }
  364. if($result['work_status'] ==9){
  365. $work_service_status = 12;
  366. $work_service_status_text = '已退费';
  367. }
  368. $result['work_service_status'] = $work_service_status;
  369. $result['work_service_status_text'] = $work_service_status_text;
  370. return $result;
  371. }
  372. /**
  373. * 导入拼团工单
  374. */
  375. public static function import($file,$third_type) {
  376. try {
  377. // 移动文件到指定目录
  378. $info = $file->move(root_path('public') . 'uploads');
  379. if ($info) {
  380. $filePath = $info->getPathname();
  381. // 读取 Excel 文件
  382. $spreadsheet = IOFactory::load($filePath);
  383. $worksheet = $spreadsheet->getActiveSheet();
  384. $highestRow = $worksheet->getHighestRow();
  385. // 假设第一行为表头,从第二行开始读取数据
  386. for ($row = 2; $row <= $highestRow; $row++) {
  387. $data = [
  388. 'title' => $worksheet->getCell('A' . $row)->getValue(),
  389. 'real_name' => $worksheet->getCell('B' . $row)->getValue(),
  390. 'mobile' => $worksheet->getCell('C' . $row)->getValue(),
  391. 'address' => $worksheet->getCell('D' . $row)->getValue(),
  392. 'area_name' => $worksheet->getCell('E'. $row)->getValue(),
  393. 'appointment_time' => $worksheet->getCell('F'. $row)->getValue(),
  394. 'work_total' => $worksheet->getCell('G'. $row)->getValue(),
  395. 'work_amount' => $worksheet->getCell('H'. $row)->getValue(),
  396. 'settlement_amount' => $worksheet->getCell('I'. $row)->getValue(),
  397. 'remark' => $worksheet->getCell('J'. $row)->getValue(),
  398. ];
  399. if(empty($data['title']) || empty($data['real_name']) || empty($data['mobile']) || empty($data['address'])){
  400. continue;
  401. }
  402. $data['third_type'] = $third_type;
  403. $data['work_pay_status'] = 2;
  404. $data['category_type'] = 2;
  405. $data['appointment_time'] = $data['appointment_time'] ? strtotime($data['appointment_time']) : 0;
  406. $data['work_sn'] = generate_sn(GroupServiceWork::class, 'work_sn');
  407. GroupServiceWork::create($data);
  408. }
  409. return true;
  410. } else {
  411. self::setError($file->getError());
  412. return false;
  413. }
  414. } catch (\Exception $e) {
  415. self::setError($e->getMessage());
  416. return false;
  417. }
  418. }
  419. /**
  420. * 退款
  421. */
  422. public static function refund($params,$userInfo){
  423. Db::startTrans();
  424. try {
  425. $work = GroupServiceWork::findOrEmpty($params['id']);
  426. if($work->isEmpty()){
  427. throw new Exception('工单不存在');
  428. }
  429. if($work->work_status >=7 ){
  430. throw new \Exception('此工单不支持退款');
  431. }
  432. if(!$work->group_user_order_id ){
  433. throw new \Exception('临时工单,不支持退款');
  434. }
  435. $order = GroupUserOrder::where(['id'=>$work->group_user_order_id])->findOrEmpty()->toArray();
  436. if (empty($order)){
  437. throw new \Exception('用户拼团订单不存在');
  438. }
  439. if ($order['refund_status'] != 0) {
  440. throw new \Exception('用户拼团订单已退款');
  441. }
  442. //添加变更日志
  443. $work_log = [
  444. 'type' => 5,
  445. 'work_id'=>$work->id,
  446. 'master_worker_id'=>$work->master_worker_id,
  447. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'执行退款'
  448. ];
  449. GroupServiceWorkLogLogic::add($work_log);
  450. //退款
  451. $work->work_status = 9;
  452. $work->service_status = 5;
  453. $work->save();
  454. // 生成退款记录
  455. $recordSn = generate_sn(RefundRecord::class, 'sn');
  456. $record = RefundRecord::create([
  457. 'sn' => $recordSn,
  458. 'user_id' => $order['user_id'],
  459. 'order_id' => $order['id'],
  460. 'order_sn' => $order['sn'],
  461. 'order_type' => RefundEnum::ORDER_TYPE_GROUP,
  462. 'order_amount' => $order['paid_amount'],
  463. 'refund_amount' => $order['paid_amount'],
  464. 'refund_type' => RefundEnum::TYPE_ADMIN,
  465. 'transaction_id' => $order['transaction_id'] ?? '',
  466. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  467. ]);
  468. //更新用户拼团订单
  469. GroupUserOrder::where('id',$order['id'])->update(['status' => 2,'refund_status' => 1]);
  470. //更新拼团订单
  471. GroupOrder::where('id',$order['id'])->update([
  472. 'num' => Db::raw('num-1')
  473. ]);
  474. // 退款
  475. $result = RefundLogic::refund($order, $record['id'], $order['paid_amount'], $userInfo['admin_id']);
  476. if ($result !== true) {
  477. throw new Exception(RefundLogic::getError());
  478. Db::rollback();
  479. return false;
  480. } else {
  481. Db::commit();
  482. return true;
  483. }
  484. }catch(\Exception $e){
  485. Db::rollback();
  486. self::setError($e->getMessage());
  487. return false;
  488. }
  489. }
  490. }