ServiceWorkLogic.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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\adminapi\logic\user\UserLogic;
  16. use app\common\enum\ThirdTypeEnum;
  17. use app\common\enum\worker\WorkerAccountLogEnum;
  18. use app\common\logic\ThirdOrderLogic;
  19. use app\common\logic\WorkerAccountLogLogic;
  20. use app\common\model\third\ThirdOrders;
  21. use app\common\model\works\ServiceWorkDeterminedPrice;
  22. use think\Exception;
  23. use think\db\Query;
  24. use think\facade\Db;
  25. use think\facade\Log;
  26. use app\common\logic\BaseLogic;
  27. use app\api\logic\PerformanceLogic;
  28. use app\common\model\dict\DictData;
  29. use app\common\model\coupon\UserCoupon;
  30. use app\common\model\works\ServiceWork;
  31. use app\common\model\recharge\OrderGoods;
  32. use app\common\model\spare_part\SparePart;
  33. use app\common\model\works\ServiceWorkLog;
  34. use app\common\model\coupon\CouponCategory;
  35. use app\common\model\recharge\RechargeOrder;
  36. use app\common\model\works\ServiceWorkSpare;
  37. use app\workerapi\logic\ServiceWorkLogLogic;
  38. use app\common\model\orders\OrderEffectiveLog;
  39. use app\common\model\master_worker\MasterWorker;
  40. use app\common\model\training\TrainingWorkerTask;
  41. use app\common\model\performance\PerformanceRules;
  42. use app\common\model\works\ServiceWorkCustomerLog;
  43. use app\workerapi\logic\ServiceWorkReceiveLogLogic;
  44. use app\common\model\master_worker\MasterWorkerTeam;
  45. use app\common\model\works\ServiceWorkAppointmentLog;
  46. use app\workerapi\logic\ServiceWorkerAllocateWorkerLogic;
  47. use app\common\model\master_worker\MasterWorkerAccountLog;
  48. use app\common\model\master_worker\MasterWorkerRetentionMoneyLog;
  49. use app\common\model\master_commission\MasterWorkerCommissionRatio;
  50. use app\common\model\master_commission\MasterWorkerCommissionConfig;
  51. use app\common\model\service_area\ServiceArea;
  52. /**
  53. * ServiceWork逻辑
  54. * Class ServiceWorkLogic
  55. * @package app\adminapi\logic\works
  56. */
  57. class ServiceWorkLogic extends BaseLogic
  58. {
  59. /**
  60. * @notes 编辑
  61. * @param array $params
  62. * @return bool
  63. * @author likeadmin
  64. * @date 2024/07/10 18:17
  65. */
  66. public static function edit(array $params): bool
  67. {
  68. Db::startTrans();
  69. try {
  70. $userId = UserLogic::getUserIdByMobile($params['mobile']);
  71. $service_area_id = ServiceArea::serviceAreaId(['lon' => $params['lon'], 'lat' => $params['lat']]);
  72. if($params['address'] && (empty($params['lon']) || empty($params['lat']))){
  73. $lon_lat = get_address_lat_lng($params['address']);
  74. $params['lon'] = $lon_lat['lon'];
  75. $params['lat'] = $lon_lat['lat'];
  76. }
  77. ServiceWork::where('id', $params['id'])->update([
  78. 'real_name' => $params['real_name'],
  79. 'mobile' => $params['mobile'],
  80. 'user_id' => $userId,
  81. 'address' => $params['address'],
  82. 'appointment_time' => strtotime($params['appointment_time']),
  83. 'estimated_finish_time' => $params['estimated_finish_time'],
  84. 'lon' => $params['lon'],
  85. 'lat' => $params['lat'],
  86. 'service_area_id' => $service_area_id,
  87. ]);
  88. Db::commit();
  89. return true;
  90. } catch (\Exception $e) {
  91. Db::rollback();
  92. self::setError($e->getMessage());
  93. return false;
  94. }
  95. }
  96. /**
  97. *
  98. * @return false|void
  99. */
  100. public static function pickWork($params)
  101. {
  102. Db::startTrans();
  103. try {
  104. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  105. if($work->isEmpty()){
  106. throw new Exception('工单不存在');
  107. }
  108. $receive_time = time();
  109. $work->work_status = 2;//待联系
  110. $work->service_status = 1;//服务中
  111. $work->receive_time = $receive_time;
  112. $work->save();
  113. //添加变更日志
  114. $work_log = [
  115. 'work_id'=>$work->id,
  116. 'master_worker_id'=>$work->master_worker_id,
  117. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',$receive_time).'领取了工单',
  118. ];
  119. ServiceWorkLogLogic::add($work_log);
  120. //添加领单日志
  121. ServiceWorkReceiveLogLogic::add($work_log);
  122. Db::commit();
  123. }
  124. catch (\Exception $e) {
  125. Db::rollback();
  126. self::setError($e->getMessage());
  127. return false;
  128. }
  129. }
  130. /**
  131. * 预约成功,等待上门
  132. * @return false|void
  133. */
  134. public static function appointWork($params)
  135. {
  136. Db::startTrans();
  137. try {
  138. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  139. if($work->isEmpty()){
  140. throw new Exception('工单不存在');
  141. }
  142. if($work->work_status != 2){
  143. throw new Exception('请勿重复点击');
  144. }
  145. //验证更改的预约时间必须是在领单时间内的半小内修改,否则不允许修改
  146. if(strtotime($work->appointment_time) != strtotime($params['appointment_time']) && (time()-strtotime($work->receive_time))>1800){
  147. throw new Exception('距离领单时间已超过半小时,无法修改预约时间,请联系客服');
  148. }
  149. $work->work_status = 3;//待上门
  150. $work->appointment_time = strtotime($params['appointment_time']);
  151. $work->save();
  152. //添加变更日志
  153. $work_log = [
  154. 'work_id'=>$work->id,
  155. 'master_worker_id'=>$work->master_worker_id,
  156. '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'].'预约上门',
  157. ];
  158. ServiceWorkLogLogic::add($work_log);
  159. Db::commit();
  160. }
  161. catch (\Exception $e) {
  162. self::setError($e->getMessage());
  163. return false;
  164. }
  165. }
  166. /**
  167. * 工程师确认上门
  168. * @param $params
  169. * @return false|void
  170. */
  171. public static function confirmDoor($params)
  172. {
  173. Db::startTrans();
  174. try {
  175. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  176. if($work->isEmpty()){
  177. throw new Exception('工单不存在');
  178. }
  179. if($work->work_status != 3){
  180. throw new Exception('请勿重复点击');
  181. }
  182. $work->finally_door_time = time();//最后上门时间
  183. $work->work_status = 4;//已上门
  184. $work->save();
  185. //如果是美团订单,且上门时间小于预约时间,则修改预约时间
  186. if($work->third_type==1 and $work->finally_door_time<$work->appointment_time){
  187. $third_order = ThirdOrders::where('work_id',$work->id)->order('id desc')->findOrEmpty();
  188. if(!$third_order->isEmpty()){
  189. ThirdOrderLogic::merchantmodify([
  190. 'reserveOrderId'=>$third_order['orderId'],
  191. 'reserveUserName'=>$work['real_name'],
  192. "reservePhone"=>$work['mobile'],
  193. "reserveAddress"=>$work['address'],
  194. "bookStartTime"=>$work->finally_door_time*1000,
  195. "bookEndTime"=>($work->finally_door_time+2*3600)*1000,
  196. 'request_msg'=>$third_order['request_msg']
  197. ]);
  198. }
  199. }
  200. //添加变更日志
  201. $work_log = [
  202. 'work_id'=>$work->id,
  203. 'master_worker_id'=>$work->master_worker_id,
  204. '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()).'已上门',
  205. ];
  206. ServiceWorkLogLogic::add($work_log);
  207. Db::commit();
  208. }
  209. catch (\Exception $e) {
  210. Db::rollback();
  211. self::setError($e->getMessage());
  212. return false;
  213. }
  214. }
  215. /**
  216. * 工程师确认报价单
  217. * @param $params
  218. * @return false|void
  219. */
  220. public static function confirmPrice($params)
  221. {
  222. Db::startTrans();
  223. try {
  224. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  225. if($work->isEmpty()){
  226. throw new Exception('工单不存在');
  227. }
  228. //搜索待支付订单
  229. $paid_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>1])->findOrEmpty()->toArray();
  230. if(empty($paid_order)){
  231. throw new Exception('订单错误');
  232. }
  233. if($work->work_status != 4){
  234. throw new Exception('请勿重复操作');
  235. }
  236. // 关联配件信息.计算配件总价 id company_price original_price offering_price number
  237. $spare_total = 0;
  238. isset($params['spare_parts']) && $params['spare_parts'] && $params['spare_parts'] = json_decode($params['spare_parts'], true);
  239. if(isset($params['spare_parts']) && $params['spare_parts']){
  240. $spare_parts = $params['spare_parts'];
  241. foreach ($spare_parts as $spare){
  242. $spare_total += $spare['company_price']*$spare['number'];
  243. }
  244. $work->spare_total = $spare_total;
  245. $service_work_spare = ServiceWorkSpare::where(['service_work_id'=>$work['id']])->findOrEmpty();
  246. if($service_work_spare->isEmpty()){
  247. //新增
  248. $service_work_spare = ServiceWorkSpare::create([
  249. 'service_work_id'=>$work['id'],
  250. 'spare_parts'=>$params['spare_parts'],
  251. 'remark'=>''
  252. ]);
  253. }else{
  254. //修改
  255. $service_work_spare->spare_parts = $params['spare_parts'];
  256. $service_work_spare->save();
  257. }
  258. $work->service_work_spare_id = $service_work_spare->id;
  259. }
  260. // order_amount 原 = $params['amount'] 修改为 = 配件总价 + 服务尾款
  261. $order_amount = $params['amount'] + $spare_total;
  262. //定金存在尾款结算功能,全款直接提交
  263. if($paid_order['payment_type']==1){
  264. $un_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>0])->findOrEmpty();
  265. if($un_order->isEmpty()){
  266. //新增待支付尾款
  267. $order_data = [
  268. 'order_type'=>$paid_order['order_type'],
  269. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  270. 'work_id'=>$paid_order['work_id'],
  271. 'user_id'=>$paid_order['user_id'],
  272. 'payment_type'=>2,
  273. 'order_total'=>$order_amount,
  274. //'order_amount'=>$params['amount'],
  275. 'order_amount'=>$order_amount,
  276. 'order_terminal'=>$paid_order['order_terminal']
  277. ];
  278. RechargeOrder::create($order_data);
  279. }else{
  280. //修改尾款信息
  281. $un_order->order_total = $order_amount;
  282. //$un_order->order_amount = $params['amount'];
  283. $un_order->order_amount = $order_amount;
  284. $un_order->save();
  285. }
  286. //更新服务费用
  287. $work->service_fee = $paid_order['paid_amount']+$params['amount'];
  288. }
  289. //总工单费用
  290. $work->work_total = $work->service_fee+$spare_total;
  291. $work->work_images = $params['work_images'];
  292. $work->explanation = $params['explanation']??'';
  293. //判断是否是第三个订单,如果是美团订单直接滤过用户确认环节
  294. if($work->third_type==1){
  295. $work->work_status = 5;
  296. $work->user_confirm_status = 2;
  297. }else{
  298. $work->user_confirm_status = 1;//待确认报价
  299. }
  300. if($work->external_platform_id > 0){
  301. $work->work_status = 5;
  302. $work->user_confirm_status = 2;
  303. }
  304. $work->price_approval = 0;
  305. $work->save();
  306. //添加变更日志
  307. $work_log = [
  308. 'work_id'=>$work->id,
  309. 'master_worker_id'=>$work->master_worker_id,
  310. '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()).'提交了报价单',
  311. ];
  312. ServiceWorkLogLogic::add($work_log);
  313. // 添加工单尾款报价记录
  314. if(isset($params['price_content'])){
  315. !is_array($params['price_content']) && $params['price_content'] = json_decode($params['price_content'], true);
  316. ServiceWorkDeterminedPrice::where('work_id',$work->id)->delete();
  317. ServiceWorkDeterminedPrice::create([
  318. 'work_id'=>$work->id,
  319. 'content'=>$params['price_content']??[],
  320. ]);
  321. }
  322. Db::commit();
  323. // 商家报价通知外部平台 即报价
  324. if($work->external_platform_id > 0){
  325. event('ExternalPlatform', [
  326. 'send_code' => 1006,
  327. 'params' => [
  328. 'work_id' => $work->id,
  329. ]
  330. ]);
  331. }
  332. }
  333. catch (\Exception $e) {
  334. Db::rollback();
  335. self::setError($e->getMessage());
  336. return false;
  337. }
  338. }
  339. /**
  340. * 工程师确认服务完成
  341. * @param $params
  342. * @return false|void
  343. */
  344. public static function confirmServiceFinish($params)
  345. {
  346. Db::startTrans();
  347. try {
  348. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  349. if($work->isEmpty()){
  350. throw new Exception('工单不存在');
  351. }
  352. if($work->user_confirm_status !=2){
  353. throw new Exception('请勿重复操作');
  354. }
  355. $work->finished_images = $params['finished_images'];
  356. $work->user_confirm_status = 3;//待确认服务完成
  357. $work->save();
  358. //添加变更日志
  359. $work_log = [
  360. 'work_id'=>$work->id,
  361. 'master_worker_id'=>$work->master_worker_id,
  362. '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()).'提交了待用户确认服务完成',
  363. ];
  364. ServiceWorkLogLogic::add($work_log);
  365. Db::commit();
  366. // 外部平台工单 - 通知外边平台
  367. if($work->external_platform_id > 0){
  368. /*event('ExternalPlatform', [
  369. 'scene' => 'service_finish',
  370. 'params' => [
  371. 'work_id' => $work->id,
  372. ]
  373. ]);*/
  374. }
  375. } catch (\Exception $e) {
  376. Db::rollback();
  377. self::setError($e->getMessage());
  378. return false;
  379. }
  380. }
  381. public static function allocateWorker($params,$userInfo){
  382. Db::startTrans();
  383. try {
  384. $work = ServiceWork::findOrEmpty($params['id']);
  385. if($work->isEmpty()){
  386. throw new Exception('工单不存在');
  387. }
  388. if($work->work_status >=6 ){
  389. throw new \Exception('工单状态只能修改待结算之前的');
  390. }
  391. if($work->master_worker_id == $params['master_worker_id']){
  392. throw new \Exception('分配的工程师相同');
  393. }
  394. $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'is_disable' =>0])->findOrEmpty();
  395. if($worker->isEmpty()){
  396. throw new \Exception('工程师不存在或被禁用');
  397. }
  398. if($worker->master_worker_id){
  399. MasterWorker::setWorktotal('dec',$worker->master_worker_id);
  400. }
  401. $work->master_worker_id = $params['master_worker_id'];
  402. $work->work_status = 1;
  403. $work->dispatch_time = time();
  404. MasterWorker::setWorktotal('inc',$params['master_worker_id']);
  405. $work->save();
  406. $work_log = [
  407. 'work_id'=>$work->id,
  408. 'master_worker_id'=>$work->master_worker_id,
  409. 'type' => 0,
  410. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'分配了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  411. ];
  412. ServiceWorkerAllocateWorkerLogic::add($work_log);
  413. Db::commit();
  414. //美团的订单更新履约状态为已分配
  415. if($work->third_type == 1){
  416. ThirdOrderLogic::updateorderfulfillinfo($work,ThirdTypeEnum::MEITUAN_MEITUAN);
  417. }
  418. // 工程师派单通知【给工程师的通知】【公众号通知,不发短信】
  419. $workDetail = ServiceWorkLogic::detail($params);
  420. $res = event('Notice', [
  421. 'scene_id' => 113,
  422. 'params' => [
  423. 'user_id' => $params['master_worker_id'],
  424. 'order_id' => $params['id'],
  425. 'thing9' => $workDetail['title'],
  426. 'time7' => $workDetail['appointment_time'],
  427. 'thing8' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
  428. 'phone_number6' => asteriskString($workDetail['mobile']),
  429. ]
  430. ]);
  431. return true;
  432. }catch(\Exception $e){
  433. Db::rollback();
  434. self::setError($e->getMessage());
  435. return false;
  436. }
  437. }
  438. /**
  439. * 工单详情
  440. * @param $params
  441. * @return array|false
  442. */
  443. public static function detail($params){
  444. $work_where = !empty($params['work_sn'])?['work_sn'=>$params['work_sn']]:['id'=>$params['id']];
  445. $result = ServiceWork::with([
  446. 'worker'=> function(Query $query) {
  447. $query->field('id,worker_number,real_name,mobile');
  448. },
  449. 'allocateWorkerLog' =>function(Query $query){
  450. $query->field('id,work_id,opera_log,create_time');
  451. },
  452. 'serviceWorkLog' =>function(Query $query){
  453. $query->field('id,work_id,opera_log,create_time');
  454. }
  455. ])->append(['id','work_status_text','service_status_text'])
  456. ->where($work_where)
  457. ->findOrEmpty()->toArray();
  458. $result['is_operate_prohibit'] = 0;
  459. if(isset($params['user_id']) && $params['user_id'] && ($params['user_id'] != $result['master_worker_id']) ){
  460. // 带徒师傅的工单
  461. $lead_master_worker_id = TrainingWorkerTask::where('master_worker_id',$params['user_id'])->where('training_status',2)
  462. ->where('operate_status',0)->value('lead_master_worker_id');
  463. // 团队负责人编辑 工单池(包括已派队员) 工单
  464. if($params['user_info']['team_role'] == 1 && $params['user_info']['tenant_id'] == $result['tenant_id'] ){
  465. $result['is_operate_prohibit'] = 0;
  466. }elseif(!empty($lead_master_worker_id) && !empty($result['master_worker_id']) && $lead_master_worker_id == $result['master_worker_id']){
  467. // 徒查看师父的工单 不能编辑可查看
  468. $result['is_operate_prohibit'] = 1;
  469. }else{
  470. throw new \Exception('您没有权限操作该工单');
  471. }
  472. }
  473. //工程师工单按钮状态
  474. $work_service_status = 0;
  475. $work_service_status_text = '待派单';
  476. //工单状态
  477. if($result['work_status'] == 1){
  478. $work_service_status = 1;
  479. $work_service_status_text = '待领单';
  480. }
  481. if($result['work_status'] == 2){
  482. $work_service_status = 2;
  483. $work_service_status_text = '预约上门';
  484. }
  485. if($result['work_status'] == 3){
  486. $work_service_status = 3;
  487. $work_service_status_text = '等待上门';
  488. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  489. $work_service_status = 4;
  490. $work_service_status_text = '确认上门';
  491. }
  492. }
  493. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  494. $work_service_status = 5;
  495. $work_service_status_text = '确认报价';
  496. }
  497. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  498. $work_service_status = 6;
  499. $work_service_status_text = '用户确认报价中';
  500. }
  501. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  502. $work_service_status = 7;
  503. $work_service_status_text = '完成服务';
  504. }
  505. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  506. $work_service_status = 8;
  507. $work_service_status_text = '用户确认完成服务中';
  508. }
  509. if($result['work_status'] ==6){
  510. $work_service_status = 9;
  511. $work_service_status_text = '待结算';
  512. }
  513. if($result['work_status'] ==7){
  514. $work_service_status = 10;
  515. $work_service_status_text = '已完结';
  516. }
  517. if($result['work_status'] ==8){
  518. $work_service_status = 11;
  519. $work_service_status_text = '已评价';
  520. }
  521. if($result['work_status'] ==9){
  522. $work_service_status = 12;
  523. $work_service_status_text = '已退费';
  524. }
  525. $result['work_service_status'] = $work_service_status;
  526. $result['work_service_status_text'] = $work_service_status_text;
  527. //搜索当前工单下的所有订单记录
  528. $result['pay_orders'] = RechargeOrder::with(['orderGoods'=>function(Query $query){
  529. $query->field('id,sn,goods_id,goods_name,goods_image,goods_number,good_unit,goods_size,goods_payment_type,goods_type,goods_brand,base_service_fee,service_total,service_fee')->order(['id'=>'desc']);
  530. }])->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();
  531. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  532. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  533. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  534. $order_type_data = DictData::where('type_value','order_type')->column('name','value');
  535. $coupon_price = 0;
  536. foreach ($result['pay_orders'] as $k=>&$v){
  537. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  538. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  539. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  540. $v['order_type_name'] = $order_type_data[$v['order_type']];
  541. $v['pay_time'] = $v['pay_time'] && is_numeric($v['pay_time']) ? date('Y-m-d H:i:s',$v['pay_time']):'';
  542. if($v['payment_type']!=1 and !empty($result['spare_total'])){
  543. $v['order_total'] = $v['order_total'] - $result['spare_total'];
  544. $v['order_amount'] = $v['order_amount'] - $result['spare_total'];
  545. }
  546. $coupon_price += $v['coupon_price'];
  547. //服务支付类别
  548. if(!empty($v['orderGoods'][0]['goods_payment_type'])){
  549. $goods_payment_type = $v['orderGoods'][0]['goods_payment_type'];
  550. }
  551. }
  552. $result['goods_payment_type'] = !empty($goods_payment_type)?$goods_payment_type:1;
  553. //汇总优惠卷额度
  554. $result['coupon_price'] = $coupon_price;
  555. //工单总支付金额
  556. $result['worker_account'] = $result['work_amount'];
  557. // 配件信息
  558. $result['spare_parts'] = [];
  559. if($result['service_work_spare_id']){
  560. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$result['service_work_spare_id'])->value('spare_parts'),true);
  561. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  562. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  563. ->select()
  564. ->toArray();
  565. $spare_parts = array_column($spare_parts,null,'id');
  566. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  567. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  568. }
  569. $result['spare_parts'] = array_values($spare_parts)??[];
  570. }
  571. // 保修卡信息
  572. $result['order_effectives'] = OrderEffectiveLog::with(['goods'=>function ($query) {
  573. $query->with(['goodsCategory'=>function ($query1) {
  574. $query1->field(['name','picture']);
  575. }]);
  576. }])->where('work_id',$result['id'])
  577. ->field(['id','goods_id','sn','effective_unit','effective_num','remark','end_effective_time'])
  578. ->append(['effective_unit_text'])
  579. ->order('create_time desc')
  580. ->select()
  581. ->toArray();
  582. //查收工程师提成金额
  583. $change_amount = MasterWorkerAccountLog::where(['work_sn'=>$result['work_sn'],'action'=>1])->value('change_amount');
  584. $result['change_amount'] = !empty($change_amount)?$change_amount:0;
  585. //质保金相关金额
  586. $result['retention'] = MasterWorkerRetentionMoneyLog::where('work_id',$result['id'])->visible(['action','amount'])->select()
  587. ->each(function ($item){
  588. $item['amount'] = $item['action'] == 1 ? '+'.$item['amount'] : '-'.$item['amount'];
  589. })->toArray();
  590. //保修工单时的信息
  591. $result['effective_income_amount'] = 0;
  592. if(!empty($result['order_effective_id'])){
  593. $order_effective = OrderEffectiveLog:: findOrEmpty($result['order_effective_id']);
  594. $serviceWork = ServiceWork::findOrEmpty($order_effective->work_id);
  595. if($serviceWork->master_worker_id != $result['master_worker_id']){
  596. $result['effective_income_amount'] = \app\adminapi\logic\effective\OrderEffectiveLogLogic::commissionAndAssuranceDeposit($serviceWork);
  597. }
  598. }
  599. //获取美团订单信息
  600. $result['meituan_order'] = [];
  601. if($result['third_type']==1){
  602. $result['meituan_order'] = ThirdOrders::where(['work_id'=>$result['id']])->order('create_time desc')->findOrEmpty();
  603. }
  604. return $result;
  605. }
  606. public static function getUserCouponDetails($params)
  607. {
  608. try{
  609. $work = ServiceWork::where('id',$params['id'])->findOrEmpty();
  610. if(!$work){
  611. throw new \Exception('工单不存在');
  612. }
  613. $coupon_all_ids =UserCoupon::where('user_id',$work['user_id'])
  614. ->where('voucher_count','>',0)
  615. ->where('voucher_status',0)
  616. ->where('expire_time','>',time())
  617. ->column('coupon_id');
  618. $coupon_ids = CouponCategory::where('goods_category_id',$work['goods_category_id'])->whereIn('coupon_id',$coupon_all_ids)->column('coupon_id');
  619. $data = UserCoupon::where('user_id',$work['user_id'])
  620. ->where('voucher_count','>',0)
  621. ->where('voucher_status',0)
  622. ->whereIn('coupon_id',$coupon_ids)
  623. ->append(['discount_ratio_text'])
  624. ->where('expire_time','>',time())
  625. ->visible(['id','coupon_id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
  626. ->select()->toArray();
  627. foreach($data as $k => $v){
  628. $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
  629. $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
  630. $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
  631. }
  632. return $data;
  633. } catch(\Exception $e){
  634. self::setError($e->getMessage());
  635. return false;
  636. }
  637. }
  638. public static function getDetailWorkServiceStatus($params)
  639. {
  640. $result = ServiceWork::where('id',$params['id'])->field('work_status,user_confirm_status,appointment_time,price_approval,appoint_approval')->findOrEmpty()->toArray();
  641. //工程师工单按钮状态
  642. $work_service_status = 0;
  643. //工单状态
  644. if($result['work_status'] == 1){
  645. $work_service_status = 1;
  646. }
  647. if($result['work_status'] == 2){
  648. $work_service_status = 2;
  649. }
  650. if($result['work_status'] == 3){
  651. $work_service_status = 3;
  652. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  653. $work_service_status = 4;
  654. }
  655. }
  656. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  657. $work_service_status = 5;
  658. }
  659. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  660. $work_service_status = 6;
  661. }
  662. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  663. $work_service_status = 7;
  664. }
  665. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  666. $work_service_status = 8;
  667. }
  668. if($result['work_status'] ==6){
  669. $work_service_status = 9;
  670. }
  671. if($result['work_status'] ==7){
  672. $work_service_status = 10;
  673. }
  674. if($result['work_status'] ==8){
  675. $work_service_status = 11;
  676. }
  677. if($result['work_status'] ==9){
  678. $work_service_status = 12;
  679. }
  680. return ['work_service_status'=>$work_service_status,'price_approval'=>$result['price_approval'],'appoint_approval'=>$result['appoint_approval']];
  681. }
  682. /**
  683. * @notes 取消操作
  684. * @param array $params
  685. * @return bool
  686. * @author likeadmin
  687. * @date 2024/09/19 10:48
  688. */
  689. public static function cancel(array $params): bool
  690. {
  691. Db::startTrans();
  692. try {
  693. // 04-11-12 不做任何限制强制取消,已支付的费用给工程师余额
  694. $serviceWorkInfo = ServiceWork::find($params['id']);
  695. if((int)$serviceWorkInfo['user_confirm_status'] === 5 || (int)$serviceWorkInfo['service_status'] > 2){
  696. throw new \Exception('用户已完结该工单或已取消,已退款,不可执行取消');
  697. }
  698. //工单如果存在费用情况,不允许取消
  699. $paid_amount = RechargeOrder::where('work_id', $params['id'])->where('pay_status', 1)->value('paid_amount');
  700. if($paid_amount > 0 and ($serviceWorkInfo['third_type'] == 0 && $serviceWorkInfo['external_platform_id'] == 0)){
  701. // 4=已上门,5=服务中,6=待结算, 即工程师已上门服务过
  702. throw new Exception('工单存在费用情况,不允许取消,请走退费流程');
  703. }
  704. ServiceWork::where('id', $params['id'])->update([
  705. 'work_status' => 9,
  706. 'service_status' => 4,
  707. 'remark' => $params['remark']??''
  708. ]);
  709. ServiceWorkLog::create([
  710. 'work_id' => $params['id'],
  711. 'master_worker_id' => $serviceWorkInfo['master_worker_id'],
  712. 'opera_log' => "工单:{$serviceWorkInfo['work_sn']}已取消"
  713. ]);
  714. Db::commit();
  715. Log::info('取消工单'.json_encode([$serviceWorkInfo]));
  716. return true;
  717. } catch (\Exception $e) {
  718. Db::rollback();
  719. self::setError($e->getMessage());
  720. Log::info('取消工单-Error'.json_encode([$e->getMessage()]));
  721. return false;
  722. }
  723. }
  724. /**
  725. * @notes 工程师结算操作
  726. * @param array $params
  727. * @return bool
  728. * @author likeadmin
  729. * @date 2024/09/19 10:48
  730. */
  731. public static function settlement(array $params): bool
  732. {
  733. Db::startTrans();
  734. try {
  735. $serviceWorkInfo = ServiceWork::find($params['id']);
  736. if((int)$serviceWorkInfo['work_pay_status'] !== 3){
  737. throw new \Exception('该工单非待结算状态');
  738. }
  739. $ratio = 0;
  740. $commissionConfig = MasterWorkerCommissionConfig::where('master_worker_id',$serviceWorkInfo->master_worker_id)->where('voucher_status',2)->findOrEmpty();
  741. !$commissionConfig->isEmpty() && $ratio = MasterWorkerCommissionRatio::where('commission_config_id',$commissionConfig['id'])->where('goods_category_id',$serviceWorkInfo->goods_category_id)->value('ratio')?:0;
  742. if($commissionConfig->isEmpty() || empty($ratio)){
  743. //获取工单对应的商品id
  744. $order_sns = \app\common\model\orders\RechargeOrder::where('work_id',$serviceWorkInfo->id)->column('sn');
  745. $goods_id = OrderGoods::whereIn('sn',$order_sns)->value('goods_id');
  746. $rule = PerformanceRules::whereFindInSet('goods_id',$goods_id)->findOrEmpty();
  747. if($rule->isEmpty()){
  748. throw new \Exception('请配置商品业绩规则');
  749. }
  750. PerformanceLogic::calculatePerformance($serviceWorkInfo);
  751. }else{
  752. // 存在服务分类比例进行结算
  753. PerformanceLogic::calculatePerformanceCommission($serviceWorkInfo);
  754. }
  755. Db::commit();
  756. return true;
  757. } catch (\Exception $e) {
  758. Db::rollback();
  759. self::setError($e->getMessage());
  760. return false;
  761. }
  762. }
  763. /**
  764. * 获取所有改约通知
  765. * @param $userId
  766. * @return array|false
  767. */
  768. public static function getAppointmentNotice($userId)
  769. {
  770. return ServiceWork::where(['master_worker_id'=>$userId,'appoint_approval'=>1])
  771. ->where('work_status','>',1)
  772. ->where('work_status','<',7)
  773. ->field(['id', 'work_sn','real_name','mobile', 'address', 'title', 'appointment_time','work_status'])
  774. ->order(['appointment_time' => 'asc'])//上门时间排序
  775. ->select()->each(function (&$item){
  776. $item['last_appointment_time'] = date('Y-m-d H:i:s',ServiceWorkAppointmentLog::where('work_id',$item['id'])->order('id desc')->value('last_appointment_time'));
  777. })
  778. ->toArray();
  779. }
  780. /**
  781. * @param $params
  782. * @return bool
  783. */
  784. public static function submitAppointment($params)
  785. {
  786. Db::startTrans();
  787. try {
  788. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  789. if($serviceWork->isEmpty()){
  790. throw new \Exception('工单不存在');
  791. }
  792. $serviceWork->appoint_approval = 2;
  793. $serviceWork->save();
  794. Db::commit();
  795. return true;
  796. } catch (\Exception $e) {
  797. Db::rollback();
  798. self::setError($e->getMessage());
  799. return false;
  800. }
  801. }
  802. /**
  803. * @param $params
  804. * @return bool
  805. */
  806. public static function submitChangePrice($params)
  807. {
  808. Db::startTrans();
  809. try {
  810. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  811. if($serviceWork->isEmpty()){
  812. throw new \Exception('工单不存在');
  813. }
  814. $serviceWork->work_status = 4;
  815. $serviceWork->user_confirm_status = 0;
  816. $serviceWork->price_approval = 2;
  817. $serviceWork->save();
  818. Db::commit();
  819. return true;
  820. } catch (\Exception $e) {
  821. Db::rollback();
  822. self::setError($e->getMessage());
  823. return false;
  824. }
  825. }
  826. public static function notApproved($params)
  827. {
  828. Db::startTrans();
  829. try {
  830. $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
  831. if($serviceWork->isEmpty()){
  832. throw new \Exception('工单不存在');
  833. }
  834. $serviceWork->refund_approval = 3;
  835. $serviceWork->save();
  836. Db::commit();
  837. return true;
  838. } catch (\Exception $e) {
  839. Db::rollback();
  840. self::setError($e->getMessage());
  841. return false;
  842. }
  843. }
  844. public static function againDoor($params)
  845. {
  846. Db::startTrans();
  847. try {
  848. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  849. if($serviceWork->isEmpty()){
  850. throw new \Exception('工单不存在');
  851. }
  852. //更新预约日志
  853. //更新工单未确认上门的状态
  854. $serviceWork->work_status = 3;
  855. $serviceWork->user_confirm_status = 0;
  856. $appoint = ServiceWorkAppointmentLog::where(['work_id'=>$serviceWork->id,'worker_id'=>$params['user_id']])->count();
  857. if($appoint > 2){
  858. throw new Exception('您的修改预约时间次数已到上限,请联系客服处理');
  859. }
  860. ServiceWorkAppointmentLog::create([
  861. 'work_id'=>$serviceWork->id,
  862. 'worker_id'=>$params['user_id'],
  863. 'last_appointment_time'=>strtotime($serviceWork->appointment_time),
  864. 'this_appointment_time'=>strtotime($params['appointment_time']),
  865. ]);
  866. $serviceWork->appointment_time = strtotime($params['appointment_time']);
  867. $serviceWork->save();
  868. Db::commit();
  869. return true;
  870. } catch (\Exception $e) {
  871. Db::rollback();
  872. self::setError($e->getMessage());
  873. return false;
  874. }
  875. }
  876. public static function cancelAllocation($params,$userInfo){
  877. Db::startTrans();
  878. try {
  879. $work = ServiceWork::findOrEmpty($params['id']);
  880. if($work->isEmpty()){
  881. throw new Exception('工单不存在');
  882. }
  883. if($work->work_status >=6 ){
  884. throw new \Exception('工单状态只能修改待结算之前的');
  885. }
  886. $worker = MasterWorker::where(['id'=>$work->master_worker_id])->findOrEmpty();
  887. if($worker->isEmpty()){
  888. throw new \Exception('工程师不存在');
  889. }
  890. if ($work->tenant_id > 0) {
  891. //团队订单取消时,派单数量减1
  892. $updateData = date("H",strtotime($work->appointment_time)) < 12 ? ['am_order' => Db::raw('am_order - 1')] : ['pm_order' => Db::raw('pm_order - 1')];
  893. MasterWorkerTeam::where('master_worker_id',$work->master_worker_id)->where('tenant_id',$work->tenant_id)->update($updateData);
  894. }
  895. $params['master_worker_id'] = (isset($params['master_worker_id']) && !empty($params['master_worker_id']))?$params['master_worker_id']:$work->master_worker_id;
  896. MasterWorker::setWorktotal('dec',$work->master_worker_id);
  897. $work->master_worker_id = 0;
  898. $work->tenant_id = 0;
  899. $work->work_status = 0;
  900. $work->dispatch_time = 0;
  901. $work->first_contact_time = 0;
  902. $work->estimated_finish_time = 0;
  903. $work->exec_time = 0;
  904. $work->save();
  905. $work_log = [
  906. 'work_id'=>$work->id,
  907. 'master_worker_id'=>$params['master_worker_id'],
  908. 'type' => 1,
  909. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'取消了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  910. ];
  911. ServiceWorkerAllocateWorkerLogic::add($work_log);
  912. Db::commit();
  913. return true;
  914. }catch(\Exception $e){
  915. Db::rollback();
  916. self::setError($e->getMessage());
  917. return false;
  918. }
  919. }
  920. public static function addCustomerLog($params)
  921. {
  922. Db::startTrans();
  923. try {
  924. $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
  925. if($serviceWork->isEmpty()){
  926. throw new \Exception('工单不存在');
  927. }
  928. ServiceWorkCustomerLog::create([
  929. 'work_id'=>$serviceWork->id,
  930. 'content'=>$params['content']??'',
  931. 'admin_id'=>$params['admin_id']??0,
  932. 'create_time'=>time(),
  933. ]);
  934. Db::commit();
  935. return true;
  936. } catch (\Exception $e) {
  937. Db::rollback();
  938. self::setError($e->getMessage());
  939. return false;
  940. }
  941. }
  942. public static function distributeTenants($params,$userInfo)
  943. {
  944. Db::startTrans();
  945. try {
  946. if(empty($params['id']) || empty($params['tenant_id'])){
  947. throw new \Exception('参数错误'.json_encode($params));
  948. }
  949. if (is_array($params['id'])) {
  950. $ids = $params['id'];
  951. } else{
  952. $ids = [$params['id']];
  953. }
  954. // 取消工程师分配
  955. foreach ($ids as $id) {
  956. $res = self::cancelAllocation(['id'=>$id,'master_worker_id'=>''],$userInfo);
  957. if($res === false){
  958. Log::info('分配log'.self::getError());
  959. }
  960. }
  961. ServiceWork::whereIn('id',$ids)->update(['tenant_id'=>$params['tenant_id']]);
  962. Db::commit();
  963. return true;
  964. } catch (\Exception $e) {
  965. Db::rollback();
  966. self::setError($e->getMessage());
  967. return false;
  968. }
  969. }
  970. /**
  971. *
  972. * @return false|void
  973. */
  974. public static function contactCustomer($params)
  975. {
  976. try {
  977. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  978. if($work->isEmpty()){
  979. throw new \Exception('工单不存在');
  980. }
  981. if (empty($work->first_contact_time)) {
  982. $work->first_contact_time = time();
  983. $work->save();
  984. }
  985. return true;
  986. }
  987. catch (\Exception $e) {
  988. Db::rollback();
  989. self::setError($e->getMessage());
  990. return false;
  991. }
  992. }
  993. /**
  994. * 添加第三方平台订单
  995. * @return false|void
  996. */
  997. public static function addThirdPlatformsOrders($params)
  998. {
  999. try {
  1000. if($params['userAddress']){
  1001. $lon_lat = get_address_lat_lng($params['userAddress']);
  1002. $params['lon'] = $lon_lat['lon'];
  1003. $params['lat'] = $lon_lat['lat'];
  1004. }
  1005. return ThirdOrderLogic::submitOrders($params);
  1006. }catch (\Exception $e) {
  1007. self::setError($e->getMessage());
  1008. return false;
  1009. }
  1010. }
  1011. /**
  1012. * 上门码和完成码
  1013. * @param $params
  1014. * @return false|array
  1015. */
  1016. public static function confirmDoorCode($params)
  1017. {
  1018. try {
  1019. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  1020. if($work->isEmpty()){
  1021. throw new Exception('工单不存在');
  1022. }
  1023. $encryptedData = encrypt($params['work_sn'], \think\facade\Config::get('project.work_sn_key'));
  1024. $user_website = \think\facade\Config::get('project.user_website');
  1025. $type = $params['type']??1;
  1026. if($type == 1){
  1027. $user_website .= '?code='.$encryptedData;
  1028. }elseif($type == 2){
  1029. $user_website .= 'pages/quoted_price?code='.$encryptedData;
  1030. }
  1031. return ['url'=>$user_website];
  1032. }catch (\Exception $e) {
  1033. self::setError($e->getMessage());
  1034. return false;
  1035. }
  1036. }
  1037. /**
  1038. * 终止结束服务操作
  1039. */
  1040. public static function terminateService($params): bool
  1041. {
  1042. Db::startTrans();
  1043. try {
  1044. $serviceWork = ServiceWork::where('work_pay_status',1)
  1045. ->where('master_worker_id','>',0)
  1046. ->whereIn('work_status',[0,1,2,3,4,5,6])
  1047. ->where('id',$params['id'])->findOrEmpty();
  1048. if($serviceWork->isEmpty()){
  1049. throw new \Exception('该工单状态不可执行结束服务');
  1050. }
  1051. if($params['isearnest'] == 1){
  1052. $serviceWork->work_status = 7;
  1053. $serviceWork->user_confirm_status = 5;
  1054. $serviceWork->service_status = 3;
  1055. $serviceWork->work_pay_status = 1;
  1056. $serviceWork->finished_time = time();
  1057. $serviceWork->remark = ($serviceWork->remark?:'')." | 终止结束服务:常规流程";
  1058. $serviceWork->save();
  1059. }else{
  1060. $paid_amount = RechargeOrder::where('work_id', $params['id'])->where('payment_type', 1)->value('paid_amount');
  1061. if($paid_amount > 0){
  1062. // 存在上门费给工程师
  1063. Log::info('终止结束服务工单'.$serviceWork->id.',上门费:'.$paid_amount);
  1064. WorkerAccountLogLogic::addAccountLog($serviceWork,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
  1065. }
  1066. ServiceWork::where('id', $params['id'])->update([
  1067. 'work_status' => 7,
  1068. 'user_confirm_status' => 5,
  1069. 'service_status' => 3,
  1070. 'finished_time' => time(),
  1071. 'work_pay_status' => 2, // 已结算则不执行 onAfterUpdate
  1072. 'settlement_amount' => $paid_amount??0,
  1073. 'worker_price' => $paid_amount??0,
  1074. 'remark' => ($serviceWork->remark?:'')." | 终止结束服务:上门费-{$paid_amount}"
  1075. ]);
  1076. }
  1077. ServiceWorkLog::create([
  1078. 'work_id' => $params['id'],
  1079. 'master_worker_id' => $serviceWork->master_worker_id,
  1080. 'opera_log' => "工单:{$serviceWork->work_sn} 终止结束服务"
  1081. ]);
  1082. Db::commit();
  1083. Log::info('终止结束服务'.json_encode([$serviceWork]));
  1084. return true;
  1085. } catch (\Exception $e) {
  1086. Db::rollback();
  1087. self::setError($e->getMessage());
  1088. Log::info('终止结束服务-Error'.json_encode([$e->getMessage()]));
  1089. return false;
  1090. }
  1091. }
  1092. }