1
0

ServiceWorkLogic.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 app\common\model\dict\DictData;
  16. use app\common\model\master_worker\MasterWorker;
  17. use app\common\model\master_worker\MasterWorkerAccountLog;
  18. use app\common\model\recharge\RechargeOrder;
  19. use app\common\model\spare_part\SparePart;
  20. use app\common\model\works\ServiceWork;
  21. use app\common\logic\BaseLogic;
  22. use app\common\model\works\ServiceWorkAllocateWorkerLog;
  23. use app\common\model\works\ServiceWorkLog;
  24. use app\common\model\works\ServiceWorkSpare;
  25. use app\workerapi\logic\ServiceWorkerAllocateWorkerLogic;
  26. use app\workerapi\logic\ServiceWorkLogLogic;
  27. use think\db\Query;
  28. use think\Exception;
  29. use think\facade\Db;
  30. /**
  31. * ServiceWork逻辑
  32. * Class ServiceWorkLogic
  33. * @package app\adminapi\logic\works
  34. */
  35. class ServiceWorkLogic extends BaseLogic
  36. {
  37. /**
  38. *
  39. * @return false|void
  40. */
  41. public static function pickWork($params)
  42. {
  43. Db::startTrans();
  44. try {
  45. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  46. if($work->isEmpty()){
  47. throw new Exception('工单不存在');
  48. }
  49. $receive_time = time();
  50. $work->work_status = 2;//待联系
  51. $work->service_status = 1;//服务中
  52. $work->receive_time = $receive_time;
  53. $work->save();
  54. //添加变更日志
  55. $work_log = [
  56. 'work_id'=>$work->id,
  57. 'master_worker_id'=>$work->master_worker_id,
  58. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',$receive_time).'领取了工单',
  59. ];
  60. ServiceWorkLogLogic::add($work_log);
  61. Db::commit();
  62. }
  63. catch (\Exception $e) {
  64. Db::rollback();
  65. self::setError($e->getMessage());
  66. return false;
  67. }
  68. }
  69. /**
  70. * 预约成功,等待上门
  71. * @return false|void
  72. */
  73. public static function appointWork($params)
  74. {
  75. Db::startTrans();
  76. try {
  77. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  78. if($work->isEmpty()){
  79. throw new Exception('工单不存在');
  80. }
  81. if($work->work_status != 2){
  82. throw new Exception('请勿重复点击');
  83. }
  84. //验证更改的预约时间必须是在领单时间内的半小内修改,否则不允许修改
  85. if(strtotime($work->appointment_time) != strtotime($params['appointment_time']) && (time()-strtotime($work->receive_time))>1800){
  86. throw new Exception('距离领单时间已超过半小时,无法修改预约时间,请联系客服');
  87. }
  88. $work->work_status = 3;//待上门
  89. $work->appointment_time = strtotime($params['appointment_time']);
  90. $work->save();
  91. //添加变更日志
  92. $work_log = [
  93. 'work_id'=>$work->id,
  94. 'master_worker_id'=>$work->master_worker_id,
  95. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'联系了客户,确认了于'.$params['appointment_time'].$params['address'].'预约上门',
  96. ];
  97. ServiceWorkLogLogic::add($work_log);
  98. Db::commit();
  99. }
  100. catch (\Exception $e) {
  101. self::setError($e->getMessage());
  102. return false;
  103. }
  104. }
  105. /**
  106. * 师傅确认上门
  107. * @param $params
  108. * @return false|void
  109. */
  110. public static function confirmDoor($params)
  111. {
  112. Db::startTrans();
  113. try {
  114. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  115. if($work->isEmpty()){
  116. throw new Exception('工单不存在');
  117. }
  118. $order = RechargeOrder::where(['sn'=>$params['order_sn'],'work_id'=>$work['id']])->findOrEmpty();
  119. if($order->isEmpty()){
  120. throw new Exception('订单不存在');
  121. }
  122. if($work->work_status != 3){
  123. throw new Exception('请勿重复点击');
  124. }
  125. $work->work_status = 4;//已上门
  126. $work->save();
  127. //添加变更日志
  128. $work_log = [
  129. 'work_id'=>$work->id,
  130. 'master_worker_id'=>$work->master_worker_id,
  131. '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()).'已上门',
  132. ];
  133. ServiceWorkLogLogic::add($work_log);
  134. Db::commit();
  135. }
  136. catch (\Exception $e) {
  137. Db::rollback();
  138. self::setError($e->getMessage());
  139. return false;
  140. }
  141. }
  142. /**
  143. * 师傅确认报价单
  144. * @param $params
  145. * @return false|void
  146. */
  147. public static function confirmPrice($params)
  148. {
  149. Db::startTrans();
  150. try {
  151. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  152. if($work->isEmpty()){
  153. throw new Exception('工单不存在');
  154. }
  155. //搜索待支付订单
  156. $paid_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>1])->findOrEmpty()->toArray();
  157. if(empty($paid_order)){
  158. throw new Exception('订单错误');
  159. }
  160. if($work->work_status != 4){
  161. throw new Exception('请勿重复操作');
  162. }
  163. // 关联配件信息.计算配件总价 id company_price original_price offering_price number
  164. $spare_total = 0;
  165. isset($params['spare_parts']) && $params['spare_parts'] && $params['spare_parts'] = json_decode($params['spare_parts'], true);
  166. if(isset($params['spare_parts']) && $params['spare_parts']){
  167. $spare_parts = $params['spare_parts'];
  168. foreach ($spare_parts as $spare){
  169. $spare_total += $spare['company_price']*$spare['number'];
  170. }
  171. $work->spare_total = $spare_total;
  172. $service_work_spare = ServiceWorkSpare::where(['service_work_id'=>$work['id']])->findOrEmpty();
  173. if($service_work_spare->isEmpty()){
  174. //新增
  175. $service_work_spare = ServiceWorkSpare::create([
  176. 'service_work_id'=>$work['id'],
  177. 'spare_parts'=>$params['spare_parts'],
  178. 'remark'=>''
  179. ]);
  180. }else{
  181. //修改
  182. $service_work_spare->spare_parts = $params['spare_parts'];
  183. $service_work_spare->save();
  184. }
  185. $work->service_work_spare_id = $service_work_spare->id;
  186. }
  187. // order_amount 原 = $params['amount'] 修改为 = 配件总价 + 服务尾款
  188. $order_amount = $params['amount'] + $spare_total;
  189. //定金存在尾款结算功能,全款直接提交
  190. if($paid_order['payment_type']==1){
  191. $un_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>0])->findOrEmpty();
  192. if($un_order->isEmpty()){
  193. //新增待支付尾款
  194. $order_data = [
  195. 'order_type'=>$paid_order['order_type'],
  196. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  197. 'work_id'=>$paid_order['work_id'],
  198. 'user_id'=>$paid_order['user_id'],
  199. 'payment_type'=>2,
  200. 'order_total'=>$order_amount,
  201. //'order_amount'=>$params['amount'],
  202. 'order_amount'=>$order_amount,
  203. 'order_terminal'=>$paid_order['order_terminal']
  204. ];
  205. RechargeOrder::create($order_data);
  206. }else{
  207. //修改尾款信息
  208. $un_order->order_total = $order_amount;
  209. //$un_order->order_amount = $params['amount'];
  210. $un_order->order_amount = $order_amount;
  211. $un_order->save();
  212. }
  213. //更新服务费用
  214. $work->service_fee = $paid_order['paid_amount']+$params['amount'];
  215. }
  216. //总工单费用
  217. $work->work_total = $order_amount + $paid_order['paid_amount'];
  218. $work->work_images = $params['work_images'];
  219. $work->user_confirm_status = 1;//待确认报价
  220. $work->save();
  221. //添加变更日志
  222. $work_log = [
  223. 'work_id'=>$work->id,
  224. 'master_worker_id'=>$work->master_worker_id,
  225. '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()).'提交了报价单',
  226. ];
  227. ServiceWorkLogLogic::add($work_log);
  228. Db::commit();
  229. }
  230. catch (\Exception $e) {
  231. Db::rollback();
  232. self::setError($e->getMessage());
  233. return false;
  234. }
  235. }
  236. /**
  237. * 师傅确认服务完成
  238. * @param $params
  239. * @return false|void
  240. */
  241. public static function confirmServiceFinish($params)
  242. {
  243. Db::startTrans();
  244. try {
  245. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  246. if($work->isEmpty()){
  247. throw new Exception('工单不存在');
  248. }
  249. if($work->user_confirm_status !=2){
  250. throw new Exception('请勿重复操作');
  251. }
  252. $work->finished_images = $params['finished_images'];
  253. $work->user_confirm_status = 3;//待确认服务完成
  254. $work->save();
  255. //添加变更日志
  256. $work_log = [
  257. 'work_id'=>$work->id,
  258. 'master_worker_id'=>$work->master_worker_id,
  259. '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()).'提交了待用户确认服务完成',
  260. ];
  261. ServiceWorkLogLogic::add($work_log);
  262. Db::commit();
  263. } catch (\Exception $e) {
  264. Db::rollback();
  265. self::setError($e->getMessage());
  266. return false;
  267. }
  268. }
  269. public static function allocateWorker($params,$userInfo){
  270. Db::startTrans();
  271. try {
  272. $work = ServiceWork::findOrEmpty($params['id']);
  273. if($work->isEmpty()){
  274. throw new Exception('工单不存在');
  275. }
  276. if($work->work_status >=6 ){
  277. throw new \Exception('工单状态只能修改待结算之前的');
  278. }
  279. if($work->master_worker_id == $params['master_worker_id']){
  280. throw new \Exception('分配的师傅相同');
  281. }
  282. $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'is_disable' =>0])->findOrEmpty();
  283. if($worker->isEmpty()){
  284. throw new \Exception('师傅不存在或被禁用');
  285. }
  286. if($worker->master_worker_id){
  287. MasterWorker::setWorktotal('dec',$worker->master_worker_id);
  288. }
  289. $work->master_worker_id = $params['master_worker_id'];
  290. $work->work_status = 1;
  291. $work->dispatch_time = time();
  292. MasterWorker::setWorktotal('inc',$params['master_worker_id']);
  293. $work->save();
  294. $work_log = [
  295. 'work_id'=>$work->id,
  296. 'master_worker_id'=>$work->master_worker_id,
  297. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'分配了师傅'.'编号['.$worker->worker_number.']'.$worker->real_name
  298. ];
  299. ServiceWorkerAllocateWorkerLogic::add($work_log);
  300. Db::commit();
  301. return true;
  302. }catch(\Exception $e){
  303. Db::rollback();
  304. self::setError($e->getMessage());
  305. return false;
  306. }
  307. }
  308. /**
  309. * 工单详情
  310. * @param $params
  311. * @return array|false
  312. */
  313. public static function detail($params){
  314. try {
  315. $result = ServiceWork::with([
  316. 'worker'=> function(Query $query) {
  317. $query->field('id,worker_number,real_name');
  318. },
  319. 'allocateWorkerLog' =>function(Query $query){
  320. $query->field('id,work_id,opera_log,create_time');
  321. },
  322. 'serviceWorkLog' =>function(Query $query){
  323. $query->field('id,work_id,opera_log,create_time');
  324. }
  325. ])->append(['id','work_status_text','service_status_text'])
  326. ->findOrEmpty($params['id'])->toArray();
  327. //师傅工单按钮状态
  328. $work_service_status = 0;
  329. $work_service_status_text = '待派单';
  330. //工单状态
  331. if($result['work_status'] == 1){
  332. $work_service_status = 1;
  333. $work_service_status_text = '待领单';
  334. }
  335. if($result['work_status'] == 2){
  336. $work_service_status = 2;
  337. $work_service_status_text = '预约上门';
  338. }
  339. if($result['work_status'] == 3){
  340. $work_service_status = 3;
  341. $work_service_status_text = '等待上门';
  342. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  343. $work_service_status = 4;
  344. $work_service_status_text = '确认上门';
  345. }
  346. }
  347. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  348. $work_service_status = 5;
  349. $work_service_status_text = '确认报价';
  350. }
  351. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  352. $work_service_status = 6;
  353. $work_service_status_text = '用户确认报价中';
  354. }
  355. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  356. $work_service_status = 7;
  357. $work_service_status_text = '完成服务';
  358. }
  359. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  360. $work_service_status = 8;
  361. $work_service_status_text = '用户确认完成服务中';
  362. }
  363. if($result['work_status'] ==6){
  364. $work_service_status = 9;
  365. $work_service_status_text = '待结算';
  366. }
  367. if($result['work_status'] ==7){
  368. $work_service_status = 10;
  369. $work_service_status_text = '已完结';
  370. }
  371. if($result['work_status'] ==8){
  372. $work_service_status = 11;
  373. $work_service_status_text = '已评价';
  374. }
  375. $result['work_service_status'] = $work_service_status;
  376. $result['work_service_status_text'] = $work_service_status_text;
  377. //搜索当前工单下的所有订单记录
  378. $result['pay_orders'] = RechargeOrder::with(['orderGoods'=>function(Query $query){
  379. $query->field('id,sn,goods_id,goods_name,goods_image,goods_number,good_unit,goods_size,goods_type,goods_brand,base_service_fee,service_total,service_fee')->order(['id'=>'desc']);
  380. }])->where(['work_id'=>$result['id']])->field('id as order_id,sn,order_type,pay_status,payment_type,pay_way,pay_time,order_amount,order_total,coupon_price,create_time')->order('id asc')->select()->toArray();
  381. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  382. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  383. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  384. $order_type_data = DictData::where('type_value','order_type')->column('name','value');
  385. $coupon_price = 0;
  386. foreach ($result['pay_orders'] as $k=>&$v){
  387. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  388. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  389. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  390. $v['order_type_name'] = $order_type_data[$v['order_type']];
  391. $v['pay_time'] = $v['pay_time'] && is_numeric($v['pay_time']) ? date('Y-m-d H:i:s',$v['pay_time']):'';
  392. if($v['payment_type']!=1 and !empty($result['spare_total'])){
  393. $v['order_total'] = $v['order_total'] - $result['spare_total'];
  394. $v['order_amount'] = $v['order_amount'] - $result['spare_total'];
  395. }
  396. $coupon_price += $v['coupon_price'];
  397. }
  398. //汇总优惠卷额度
  399. $result['coupon_price'] = $coupon_price;
  400. //工单总支付金额
  401. $result['worker_account'] = $result['work_amount'];
  402. // 配件信息
  403. $result['spare_parts'] = [];
  404. if($result['service_work_spare_id']){
  405. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$result['service_work_spare_id'])->value('spare_parts'),true);
  406. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  407. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  408. ->select()
  409. ->toArray();
  410. $spare_parts = array_column($spare_parts,null,'id');
  411. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  412. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  413. }
  414. $result['spare_parts'] = array_values($spare_parts)??[];
  415. }
  416. return $result;
  417. }catch(\Exception $e){
  418. Db::rollback();
  419. self::setError($e->getMessage());
  420. return false;
  421. }
  422. }
  423. /**
  424. * @notes 取消操作
  425. * @param array $params
  426. * @return bool
  427. * @author likeadmin
  428. * @date 2024/09/19 10:48
  429. */
  430. public static function cancel(array $params): bool
  431. {
  432. Db::startTrans();
  433. try {
  434. $serviceWorkInfo = ServiceWork::find($params['id']);
  435. if((int)$serviceWorkInfo['work_status'] !== 0){
  436. throw new \Exception('该工单不可取消');
  437. }
  438. ServiceWork::where('id', $params['id'])->update([
  439. 'work_status' => 9,
  440. 'remark' => $params['remark']??''
  441. ]);
  442. // 对应订单状态修改
  443. RechargeOrder::where('work_id', $params['id'])->update([
  444. 'pay_status' => 2
  445. ]);
  446. ServiceWorkLog::create([
  447. 'work_id' => $params['id'],
  448. 'master_worker_id' => $serviceWorkInfo['master_worker_id'],
  449. 'opera_log' => "工单:{$serviceWorkInfo['work_sn']}已取消"
  450. ]);
  451. // 退款
  452. Db::commit();
  453. return true;
  454. } catch (\Exception $e) {
  455. Db::rollback();
  456. self::setError($e->getMessage());
  457. return false;
  458. }
  459. }
  460. }