1
0

ServiceWorkLogic.php 48 KB

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