ServiceWorkLogic.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  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->save();
  908. $work_log = [
  909. 'work_id'=>$work->id,
  910. 'master_worker_id'=>$params['master_worker_id'],
  911. 'type' => 1,
  912. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'取消了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  913. ];
  914. ServiceWorkerAllocateWorkerLogic::add($work_log);
  915. Db::commit();
  916. return true;
  917. }catch(\Exception $e){
  918. Db::rollback();
  919. self::setError($e->getMessage());
  920. return false;
  921. }
  922. }
  923. public static function addCustomerLog($params)
  924. {
  925. Db::startTrans();
  926. try {
  927. $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
  928. if($serviceWork->isEmpty()){
  929. throw new \Exception('工单不存在');
  930. }
  931. ServiceWorkCustomerLog::create([
  932. 'work_id'=>$serviceWork->id,
  933. 'content'=>$params['content']??'',
  934. 'admin_id'=>$params['admin_id']??0,
  935. 'create_time'=>time(),
  936. ]);
  937. Db::commit();
  938. return true;
  939. } catch (\Exception $e) {
  940. Db::rollback();
  941. self::setError($e->getMessage());
  942. return false;
  943. }
  944. }
  945. public static function distributeTenants($params,$userInfo)
  946. {
  947. Db::startTrans();
  948. try {
  949. if(empty($params['id']) || empty($params['tenant_id'])){
  950. throw new \Exception('参数错误'.json_encode($params));
  951. }
  952. if (is_array($params['id'])) {
  953. $ids = $params['id'];
  954. } else{
  955. $ids = [$params['id']];
  956. }
  957. // 取消工程师分配
  958. foreach ($ids as $id) {
  959. $res = self::cancelAllocation(['id'=>$id,'master_worker_id'=>''],$userInfo);
  960. if($res === false){
  961. Log::info('分配log'.self::getError());
  962. }
  963. }
  964. ServiceWork::whereIn('id',$ids)->update(['tenant_id'=>$params['tenant_id']]);
  965. Db::commit();
  966. return true;
  967. } catch (\Exception $e) {
  968. Db::rollback();
  969. self::setError($e->getMessage());
  970. return false;
  971. }
  972. }
  973. /**
  974. *
  975. * @return false|void
  976. */
  977. public static function contactCustomer($params)
  978. {
  979. try {
  980. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  981. if($work->isEmpty()){
  982. throw new \Exception('工单不存在');
  983. }
  984. if (empty($work->first_contact_time)) {
  985. $work->first_contact_time = time();
  986. $work->save();
  987. }
  988. return true;
  989. }
  990. catch (\Exception $e) {
  991. Db::rollback();
  992. self::setError($e->getMessage());
  993. return false;
  994. }
  995. }
  996. /**
  997. * 添加第三方平台订单
  998. * @return false|void
  999. */
  1000. public static function addThirdPlatformsOrders($params)
  1001. {
  1002. try {
  1003. if($params['userAddress']){
  1004. $lon_lat = get_address_lat_lng($params['userAddress']);
  1005. $params['lon'] = $lon_lat['lon'];
  1006. $params['lat'] = $lon_lat['lat'];
  1007. }
  1008. return ThirdOrderLogic::submitOrders($params);
  1009. }catch (\Exception $e) {
  1010. self::setError($e->getMessage());
  1011. return false;
  1012. }
  1013. }
  1014. /**
  1015. * 上门码和完成码
  1016. * @param $params
  1017. * @return false|array
  1018. */
  1019. public static function confirmDoorCode($params)
  1020. {
  1021. try {
  1022. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  1023. if($work->isEmpty()){
  1024. throw new Exception('工单不存在');
  1025. }
  1026. $encryptedData = encrypt($params['work_sn'], \think\facade\Config::get('project.work_sn_key'));
  1027. $user_website = \think\facade\Config::get('project.user_website');
  1028. $type = $params['type']??1;
  1029. if($type == 1){
  1030. $user_website .= '?code='.$encryptedData;
  1031. }elseif($type == 2){
  1032. $user_website .= 'pages/quoted_price?code='.$encryptedData;
  1033. }
  1034. return ['url'=>$user_website];
  1035. }catch (\Exception $e) {
  1036. self::setError($e->getMessage());
  1037. return false;
  1038. }
  1039. }
  1040. /**
  1041. * 终止结束服务操作
  1042. */
  1043. public static function terminateService($params): bool
  1044. {
  1045. Db::startTrans();
  1046. try {
  1047. $serviceWork = ServiceWork::where('work_pay_status',1)
  1048. ->where('master_worker_id','>',0)
  1049. ->whereIn('work_status',[0,1,2,3,4,5,6])
  1050. ->where('id',$params['id'])->findOrEmpty();
  1051. if($serviceWork->isEmpty()){
  1052. throw new \Exception('该工单状态不可执行结束服务');
  1053. }
  1054. if($params['isearnest'] == 1){
  1055. $serviceWork->work_status = 7;
  1056. $serviceWork->user_confirm_status = 5;
  1057. $serviceWork->service_status = 3;
  1058. $serviceWork->work_pay_status = 1;
  1059. $serviceWork->finished_time = time();
  1060. $serviceWork->remark = ($serviceWork->remark?:'')." | 终止结束服务:常规流程";
  1061. $serviceWork->save();
  1062. }else{
  1063. $paid_amount = RechargeOrder::where('work_id', $params['id'])->where('payment_type', 1)->value('paid_amount');
  1064. if($paid_amount > 0){
  1065. // 存在上门费给工程师
  1066. Log::info('终止结束服务工单'.$serviceWork->id.',上门费:'.$paid_amount);
  1067. WorkerAccountLogLogic::addAccountLog($serviceWork,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
  1068. }
  1069. ServiceWork::where('id', $params['id'])->update([
  1070. 'work_status' => 7,
  1071. 'user_confirm_status' => 5,
  1072. 'service_status' => 3,
  1073. 'finished_time' => time(),
  1074. 'work_pay_status' => 2, // 已结算则不执行 onAfterUpdate
  1075. 'settlement_amount' => $paid_amount??0,
  1076. 'worker_price' => $paid_amount??0,
  1077. 'remark' => ($serviceWork->remark?:'')." | 终止结束服务:上门费-{$paid_amount}"
  1078. ]);
  1079. }
  1080. ServiceWorkLog::create([
  1081. 'work_id' => $params['id'],
  1082. 'master_worker_id' => $serviceWork->master_worker_id,
  1083. 'opera_log' => "工单:{$serviceWork->work_sn} 终止结束服务"
  1084. ]);
  1085. Db::commit();
  1086. Log::info('终止结束服务'.json_encode([$serviceWork]));
  1087. return true;
  1088. } catch (\Exception $e) {
  1089. Db::rollback();
  1090. self::setError($e->getMessage());
  1091. Log::info('终止结束服务-Error'.json_encode([$e->getMessage()]));
  1092. return false;
  1093. }
  1094. }
  1095. }