ServiceWorkLogic.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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\api\logic\PerformanceLogic;
  16. use app\common\enum\worker\WorkerAccountLogEnum;
  17. use app\common\logic\WorkerAccountLogLogic;
  18. use app\common\model\coupon\CouponCategory;
  19. use app\common\model\coupon\UserCoupon;
  20. use app\common\model\dict\DictData;
  21. use app\common\model\master_worker\MasterWorker;
  22. use app\common\model\master_worker\MasterWorkerAccountLog;
  23. use app\common\model\master_worker\MasterWorkerRetentionMoneyLog;
  24. use app\common\model\orders\OrderEffectiveLog;
  25. use app\common\model\performance\PerformanceRules;
  26. use app\common\model\recharge\OrderGoods;
  27. use app\common\model\recharge\RechargeOrder;
  28. use app\common\model\spare_part\SparePart;
  29. use app\common\model\works\ServiceWork;
  30. use app\common\logic\BaseLogic;
  31. use app\common\model\works\ServiceWorkAllocateWorkerLog;
  32. use app\common\model\works\ServiceWorkAppointmentLog;
  33. use app\common\model\works\ServiceWorkCustomerLog;
  34. use app\common\model\works\ServiceWorkLog;
  35. use app\common\model\works\ServiceWorkSpare;
  36. use app\workerapi\logic\ServiceWorkerAllocateWorkerLogic;
  37. use app\workerapi\logic\ServiceWorkLogLogic;
  38. use think\db\Query;
  39. use think\Exception;
  40. use think\facade\Db;
  41. use think\facade\Log;
  42. /**
  43. * ServiceWork逻辑
  44. * Class ServiceWorkLogic
  45. * @package app\adminapi\logic\works
  46. */
  47. class ServiceWorkLogic extends BaseLogic
  48. {
  49. /**
  50. * @notes 编辑
  51. * @param array $params
  52. * @return bool
  53. * @author likeadmin
  54. * @date 2024/07/10 18:17
  55. */
  56. public static function edit(array $params): bool
  57. {
  58. Db::startTrans();
  59. try {
  60. ServiceWork::where('id', $params['id'])->update([
  61. 'address' => $params['address'],
  62. 'appointment_time' => strtotime($params['appointment_time']),
  63. 'lon' => $params['lon'],
  64. 'lat' => $params['lat'],
  65. ]);
  66. Db::commit();
  67. return true;
  68. } catch (\Exception $e) {
  69. Db::rollback();
  70. self::setError($e->getMessage());
  71. return false;
  72. }
  73. }
  74. /**
  75. *
  76. * @return false|void
  77. */
  78. public static function pickWork($params)
  79. {
  80. Db::startTrans();
  81. try {
  82. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  83. if($work->isEmpty()){
  84. throw new Exception('工单不存在');
  85. }
  86. $receive_time = time();
  87. $work->work_status = 2;//待联系
  88. $work->service_status = 1;//服务中
  89. $work->receive_time = $receive_time;
  90. $work->save();
  91. //添加变更日志
  92. $work_log = [
  93. 'work_id'=>$work->id,
  94. 'master_worker_id'=>$work->master_worker_id,
  95. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',$receive_time).'领取了工单',
  96. ];
  97. ServiceWorkLogLogic::add($work_log);
  98. Db::commit();
  99. }
  100. catch (\Exception $e) {
  101. Db::rollback();
  102. self::setError($e->getMessage());
  103. return false;
  104. }
  105. }
  106. /**
  107. * 预约成功,等待上门
  108. * @return false|void
  109. */
  110. public static function appointWork($params)
  111. {
  112. Db::startTrans();
  113. try {
  114. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  115. if($work->isEmpty()){
  116. throw new Exception('工单不存在');
  117. }
  118. if($work->work_status != 2){
  119. throw new Exception('请勿重复点击');
  120. }
  121. //验证更改的预约时间必须是在领单时间内的半小内修改,否则不允许修改
  122. if(strtotime($work->appointment_time) != strtotime($params['appointment_time']) && (time()-strtotime($work->receive_time))>1800){
  123. throw new Exception('距离领单时间已超过半小时,无法修改预约时间,请联系客服');
  124. }
  125. $work->work_status = 3;//待上门
  126. $work->appointment_time = strtotime($params['appointment_time']);
  127. $work->save();
  128. //添加变更日志
  129. $work_log = [
  130. 'work_id'=>$work->id,
  131. 'master_worker_id'=>$work->master_worker_id,
  132. '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'].'预约上门',
  133. ];
  134. ServiceWorkLogLogic::add($work_log);
  135. Db::commit();
  136. }
  137. catch (\Exception $e) {
  138. self::setError($e->getMessage());
  139. return false;
  140. }
  141. }
  142. /**
  143. * 工程师确认上门
  144. * @param $params
  145. * @return false|void
  146. */
  147. public static function confirmDoor($params)
  148. {
  149. Db::startTrans();
  150. try {
  151. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  152. if($work->isEmpty()){
  153. throw new Exception('工单不存在');
  154. }
  155. if($work->work_status != 3){
  156. throw new Exception('请勿重复点击');
  157. }
  158. $work->finally_door_time = time();//最后上门时间
  159. $work->work_status = 4;//已上门
  160. $work->save();
  161. //添加变更日志
  162. $work_log = [
  163. 'work_id'=>$work->id,
  164. 'master_worker_id'=>$work->master_worker_id,
  165. '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()).'已上门',
  166. ];
  167. ServiceWorkLogLogic::add($work_log);
  168. Db::commit();
  169. }
  170. catch (\Exception $e) {
  171. Db::rollback();
  172. self::setError($e->getMessage());
  173. return false;
  174. }
  175. }
  176. /**
  177. * 工程师确认报价单
  178. * @param $params
  179. * @return false|void
  180. */
  181. public static function confirmPrice($params)
  182. {
  183. Db::startTrans();
  184. try {
  185. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  186. if($work->isEmpty()){
  187. throw new Exception('工单不存在');
  188. }
  189. //搜索待支付订单
  190. $paid_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>1])->findOrEmpty()->toArray();
  191. if(empty($paid_order)){
  192. throw new Exception('订单错误');
  193. }
  194. if($work->work_status != 4){
  195. throw new Exception('请勿重复操作');
  196. }
  197. // 关联配件信息.计算配件总价 id company_price original_price offering_price number
  198. $spare_total = 0;
  199. isset($params['spare_parts']) && $params['spare_parts'] && $params['spare_parts'] = json_decode($params['spare_parts'], true);
  200. if(isset($params['spare_parts']) && $params['spare_parts']){
  201. $spare_parts = $params['spare_parts'];
  202. foreach ($spare_parts as $spare){
  203. $spare_total += $spare['company_price']*$spare['number'];
  204. }
  205. $work->spare_total = $spare_total;
  206. $service_work_spare = ServiceWorkSpare::where(['service_work_id'=>$work['id']])->findOrEmpty();
  207. if($service_work_spare->isEmpty()){
  208. //新增
  209. $service_work_spare = ServiceWorkSpare::create([
  210. 'service_work_id'=>$work['id'],
  211. 'spare_parts'=>$params['spare_parts'],
  212. 'remark'=>''
  213. ]);
  214. }else{
  215. //修改
  216. $service_work_spare->spare_parts = $params['spare_parts'];
  217. $service_work_spare->save();
  218. }
  219. $work->service_work_spare_id = $service_work_spare->id;
  220. }
  221. // order_amount 原 = $params['amount'] 修改为 = 配件总价 + 服务尾款
  222. $order_amount = $params['amount'] + $spare_total;
  223. //定金存在尾款结算功能,全款直接提交
  224. if($paid_order['payment_type']==1){
  225. $un_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>0])->findOrEmpty();
  226. if($un_order->isEmpty()){
  227. //新增待支付尾款
  228. $order_data = [
  229. 'order_type'=>$paid_order['order_type'],
  230. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  231. 'work_id'=>$paid_order['work_id'],
  232. 'user_id'=>$paid_order['user_id'],
  233. 'payment_type'=>2,
  234. 'order_total'=>$order_amount,
  235. //'order_amount'=>$params['amount'],
  236. 'order_amount'=>$order_amount,
  237. 'order_terminal'=>$paid_order['order_terminal']
  238. ];
  239. RechargeOrder::create($order_data);
  240. }else{
  241. //修改尾款信息
  242. $un_order->order_total = $order_amount;
  243. //$un_order->order_amount = $params['amount'];
  244. $un_order->order_amount = $order_amount;
  245. $un_order->save();
  246. }
  247. //更新服务费用
  248. $work->service_fee = $paid_order['paid_amount']+$params['amount'];
  249. }
  250. //总工单费用
  251. $work->work_total = $work->service_fee+$spare_total;
  252. $work->work_images = $params['work_images'];
  253. $work->explanation = $params['explanation']??'';
  254. $work->user_confirm_status = 1;//待确认报价
  255. $work->price_approval = 0;
  256. $work->save();
  257. //添加变更日志
  258. $work_log = [
  259. 'work_id'=>$work->id,
  260. 'master_worker_id'=>$work->master_worker_id,
  261. '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()).'提交了报价单',
  262. ];
  263. ServiceWorkLogLogic::add($work_log);
  264. Db::commit();
  265. }
  266. catch (\Exception $e) {
  267. Db::rollback();
  268. self::setError($e->getMessage());
  269. return false;
  270. }
  271. }
  272. /**
  273. * 工程师确认服务完成
  274. * @param $params
  275. * @return false|void
  276. */
  277. public static function confirmServiceFinish($params)
  278. {
  279. Db::startTrans();
  280. try {
  281. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  282. if($work->isEmpty()){
  283. throw new Exception('工单不存在');
  284. }
  285. if($work->user_confirm_status !=2){
  286. throw new Exception('请勿重复操作');
  287. }
  288. $work->finished_images = $params['finished_images'];
  289. $work->user_confirm_status = 3;//待确认服务完成
  290. $work->save();
  291. //添加变更日志
  292. $work_log = [
  293. 'work_id'=>$work->id,
  294. 'master_worker_id'=>$work->master_worker_id,
  295. '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()).'提交了待用户确认服务完成',
  296. ];
  297. ServiceWorkLogLogic::add($work_log);
  298. Db::commit();
  299. } catch (\Exception $e) {
  300. Db::rollback();
  301. self::setError($e->getMessage());
  302. return false;
  303. }
  304. }
  305. public static function allocateWorker($params,$userInfo){
  306. Db::startTrans();
  307. try {
  308. $work = ServiceWork::findOrEmpty($params['id']);
  309. if($work->isEmpty()){
  310. throw new Exception('工单不存在');
  311. }
  312. if($work->work_status >=6 ){
  313. throw new \Exception('工单状态只能修改待结算之前的');
  314. }
  315. if($work->master_worker_id == $params['master_worker_id']){
  316. throw new \Exception('分配的工程师相同');
  317. }
  318. $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'is_disable' =>0])->findOrEmpty();
  319. if($worker->isEmpty()){
  320. throw new \Exception('工程师不存在或被禁用');
  321. }
  322. if($worker->master_worker_id){
  323. MasterWorker::setWorktotal('dec',$worker->master_worker_id);
  324. }
  325. $work->master_worker_id = $params['master_worker_id'];
  326. $work->work_status = 1;
  327. $work->dispatch_time = time();
  328. MasterWorker::setWorktotal('inc',$params['master_worker_id']);
  329. $work->save();
  330. $work_log = [
  331. 'work_id'=>$work->id,
  332. 'master_worker_id'=>$work->master_worker_id,
  333. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'分配了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  334. ];
  335. ServiceWorkerAllocateWorkerLogic::add($work_log);
  336. Db::commit();
  337. return true;
  338. }catch(\Exception $e){
  339. Db::rollback();
  340. self::setError($e->getMessage());
  341. return false;
  342. }
  343. }
  344. /**
  345. * 工单详情
  346. * @param $params
  347. * @return array|false
  348. */
  349. public static function detail($params){
  350. $work_where = !empty($params['work_sn'])?['work_sn'=>$params['work_sn']]:['id'=>$params['id']];
  351. $result = ServiceWork::with([
  352. 'worker'=> function(Query $query) {
  353. $query->field('id,worker_number,real_name,mobile');
  354. },
  355. 'allocateWorkerLog' =>function(Query $query){
  356. $query->field('id,work_id,opera_log,create_time');
  357. },
  358. 'serviceWorkLog' =>function(Query $query){
  359. $query->field('id,work_id,opera_log,create_time');
  360. }
  361. ])->append(['id','work_status_text','service_status_text'])
  362. ->where($work_where)
  363. ->findOrEmpty()->toArray();
  364. if(isset($params['user_id']) && $params['user_id'] && ($params['user_id'] != $result['master_worker_id'])){
  365. throw new \Exception('您没有权限操作该工单');
  366. }
  367. //工程师工单按钮状态
  368. $work_service_status = 0;
  369. $work_service_status_text = '待派单';
  370. //工单状态
  371. if($result['work_status'] == 1){
  372. $work_service_status = 1;
  373. $work_service_status_text = '待领单';
  374. }
  375. if($result['work_status'] == 2){
  376. $work_service_status = 2;
  377. $work_service_status_text = '预约上门';
  378. }
  379. if($result['work_status'] == 3){
  380. $work_service_status = 3;
  381. $work_service_status_text = '等待上门';
  382. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  383. $work_service_status = 4;
  384. $work_service_status_text = '确认上门';
  385. }
  386. }
  387. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  388. $work_service_status = 5;
  389. $work_service_status_text = '确认报价';
  390. }
  391. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  392. $work_service_status = 6;
  393. $work_service_status_text = '用户确认报价中';
  394. }
  395. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  396. $work_service_status = 7;
  397. $work_service_status_text = '完成服务';
  398. }
  399. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  400. $work_service_status = 8;
  401. $work_service_status_text = '用户确认完成服务中';
  402. }
  403. if($result['work_status'] ==6){
  404. $work_service_status = 9;
  405. $work_service_status_text = '待结算';
  406. }
  407. if($result['work_status'] ==7){
  408. $work_service_status = 10;
  409. $work_service_status_text = '已完结';
  410. }
  411. if($result['work_status'] ==8){
  412. $work_service_status = 11;
  413. $work_service_status_text = '已评价';
  414. }
  415. if($result['work_status'] ==9){
  416. $work_service_status = 12;
  417. $work_service_status_text = '已退费';
  418. }
  419. $result['work_service_status'] = $work_service_status;
  420. $result['work_service_status_text'] = $work_service_status_text;
  421. //搜索当前工单下的所有订单记录
  422. $result['pay_orders'] = RechargeOrder::with(['orderGoods'=>function(Query $query){
  423. $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']);
  424. }])->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();
  425. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  426. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  427. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  428. $order_type_data = DictData::where('type_value','order_type')->column('name','value');
  429. $coupon_price = 0;
  430. foreach ($result['pay_orders'] as $k=>&$v){
  431. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  432. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  433. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  434. $v['order_type_name'] = $order_type_data[$v['order_type']];
  435. $v['pay_time'] = $v['pay_time'] && is_numeric($v['pay_time']) ? date('Y-m-d H:i:s',$v['pay_time']):'';
  436. if($v['payment_type']!=1 and !empty($result['spare_total'])){
  437. $v['order_total'] = $v['order_total'] - $result['spare_total'];
  438. $v['order_amount'] = $v['order_amount'] - $result['spare_total'];
  439. }
  440. $coupon_price += $v['coupon_price'];
  441. //服务支付类别
  442. if(!empty($v['orderGoods'][0]['goods_payment_type'])){
  443. $goods_payment_type = $v['orderGoods'][0]['goods_payment_type'];
  444. }
  445. }
  446. $result['goods_payment_type'] = !empty($goods_payment_type)?$goods_payment_type:1;
  447. //汇总优惠卷额度
  448. $result['coupon_price'] = $coupon_price;
  449. //工单总支付金额
  450. $result['worker_account'] = $result['work_amount'];
  451. // 配件信息
  452. $result['spare_parts'] = [];
  453. if($result['service_work_spare_id']){
  454. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$result['service_work_spare_id'])->value('spare_parts'),true);
  455. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  456. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  457. ->select()
  458. ->toArray();
  459. $spare_parts = array_column($spare_parts,null,'id');
  460. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  461. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  462. }
  463. $result['spare_parts'] = array_values($spare_parts)??[];
  464. }
  465. // 保修卡信息
  466. $result['order_effectives'] = OrderEffectiveLog::with(['goods'=>function ($query) {
  467. $query->with(['goodsCategory'=>function ($query1) {
  468. $query1->field(['name','picture']);
  469. }]);
  470. }])->where('work_id',$result['id'])
  471. ->field(['id','goods_id','sn','effective_unit','effective_num','remark','end_effective_time'])
  472. ->append(['effective_unit_text'])
  473. ->order('create_time desc')
  474. ->select()
  475. ->toArray();
  476. //查收工程师提成金额
  477. $change_amount = MasterWorkerAccountLog::where(['work_sn'=>$result['work_sn'],'action'=>1])->value('change_amount');
  478. $result['change_amount'] = !empty($change_amount)?$change_amount:0;
  479. //质保金相关金额
  480. $result['retention'] = MasterWorkerRetentionMoneyLog::where('work_id',$result['id'])->visible(['action','amount'])->select()
  481. ->each(function ($item){
  482. $item['amount'] = $item['action'] == 1 ? '+'.$item['amount'] : '-'.$item['amount'];
  483. })->toArray();
  484. return $result;
  485. }
  486. public static function getUserCouponDetails($params)
  487. {
  488. try{
  489. $work = ServiceWork::where('id',$params['id'])->findOrEmpty();
  490. if(!$work){
  491. throw new \Exception('工单不存在');
  492. }
  493. $coupon_all_ids =UserCoupon::where('user_id',$work['user_id'])
  494. ->where('voucher_count','>',0)
  495. ->where('voucher_status',0)
  496. ->where('expire_time','>',time())
  497. ->column('coupon_id');
  498. $coupon_ids = CouponCategory::where('goods_category_id',$work['goods_category_id'])->whereIn('coupon_id',$coupon_all_ids)->column('coupon_id');
  499. $data = UserCoupon::where('user_id',$work['user_id'])
  500. ->where('voucher_count','>',0)
  501. ->where('voucher_status',0)
  502. ->whereIn('coupon_id',$coupon_ids)
  503. ->append(['discount_ratio_text'])
  504. ->where('expire_time','>',time())
  505. ->visible(['id','coupon_id','amount','amount_require','begin_use','discount_ratio','event_name','expire_time','max_deductible_price','server_category_name','mold_type'])
  506. ->select()->toArray();
  507. foreach($data as $k => $v){
  508. $data[$k]['begin_use'] = date("Y-m-d H:i:s",$v['begin_use'] );
  509. $data[$k]['expire_time'] = date("Y-m-d H:i:s",$v['expire_time'] );
  510. $data[$k]['amount_require'] = '满'.$v['amount_require'].'可用';
  511. }
  512. return $data;
  513. } catch(\Exception $e){
  514. self::setError($e->getMessage());
  515. return false;
  516. }
  517. }
  518. public static function getDetailWorkServiceStatus($params)
  519. {
  520. $result = ServiceWork::where('id',$params['id'])->field('work_status,user_confirm_status,appointment_time,price_approval,appoint_approval')->findOrEmpty()->toArray();
  521. //工程师工单按钮状态
  522. $work_service_status = 0;
  523. //工单状态
  524. if($result['work_status'] == 1){
  525. $work_service_status = 1;
  526. }
  527. if($result['work_status'] == 2){
  528. $work_service_status = 2;
  529. }
  530. if($result['work_status'] == 3){
  531. $work_service_status = 3;
  532. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  533. $work_service_status = 4;
  534. }
  535. }
  536. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  537. $work_service_status = 5;
  538. }
  539. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  540. $work_service_status = 6;
  541. }
  542. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  543. $work_service_status = 7;
  544. }
  545. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  546. $work_service_status = 8;
  547. }
  548. if($result['work_status'] ==6){
  549. $work_service_status = 9;
  550. }
  551. if($result['work_status'] ==7){
  552. $work_service_status = 10;
  553. }
  554. if($result['work_status'] ==8){
  555. $work_service_status = 11;
  556. }
  557. if($result['work_status'] ==9){
  558. $work_service_status = 12;
  559. }
  560. return ['work_service_status'=>$work_service_status,'price_approval'=>$result['price_approval'],'appoint_approval'=>$result['appoint_approval']];
  561. }
  562. /**
  563. * @notes 取消操作
  564. * @param array $params
  565. * @return bool
  566. * @author likeadmin
  567. * @date 2024/09/19 10:48
  568. */
  569. public static function cancel(array $params): bool
  570. {
  571. Db::startTrans();
  572. try {
  573. // 04-11-12 不做任何限制强制取消,已支付的费用给工程师余额
  574. $serviceWorkInfo = ServiceWork::find($params['id']);
  575. if((int)$serviceWorkInfo['user_confirm_status'] === 5 || (int)$serviceWorkInfo['service_status'] > 2){
  576. throw new \Exception('用户已完结该工单或已取消,已退款,不可执行取消');
  577. }
  578. ServiceWork::where('id', $params['id'])->update([
  579. //'work_status' => 9,
  580. 'service_status' => 4,
  581. 'remark' => $params['remark']??''
  582. ]);
  583. /*// 对应订单状态修改
  584. RechargeOrder::where('work_id', $params['id'])->update([
  585. 'pay_status' => 2
  586. ]);*/
  587. ServiceWorkLog::create([
  588. 'work_id' => $params['id'],
  589. 'master_worker_id' => $serviceWorkInfo['master_worker_id'],
  590. 'opera_log' => "工单:{$serviceWorkInfo['work_sn']}已取消"
  591. ]);
  592. $paid_amount = RechargeOrder::where('work_id', $params['id'])->where('payment_type', 1)->where('order_type', 0)->where('pay_status', 1)->value('paid_amount');
  593. if($paid_amount > 0){
  594. // 4=已上门,5=服务中,6=待结算, 即工程师已上门服务过
  595. if(in_array((int)$serviceWorkInfo['work_status'], [4,5,6])){
  596. WorkerAccountLogLogic::addAccountLog($serviceWorkInfo,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC,$params['remark']??'');
  597. }
  598. }
  599. Db::commit();
  600. Log::info('取消工单'.json_encode([$serviceWorkInfo]));
  601. return true;
  602. } catch (\Exception $e) {
  603. Db::rollback();
  604. self::setError($e->getMessage());
  605. Log::info('取消工单-Error'.json_encode([$e->getMessage()]));
  606. return false;
  607. }
  608. }
  609. /**
  610. * @notes 工程师结算操作
  611. * @param array $params
  612. * @return bool
  613. * @author likeadmin
  614. * @date 2024/09/19 10:48
  615. */
  616. public static function settlement(array $params): bool
  617. {
  618. Db::startTrans();
  619. try {
  620. $serviceWorkInfo = ServiceWork::find($params['id']);
  621. if((int)$serviceWorkInfo['work_pay_status'] !== 3){
  622. throw new \Exception('该工单非待结算状态');
  623. }
  624. //获取工单对应的商品id
  625. $order_sns = \app\common\model\orders\RechargeOrder::where('work_id',$serviceWorkInfo->id)->column('sn');
  626. $goods_id = OrderGoods::whereIn('sn',$order_sns)->value('goods_id');
  627. $rule = PerformanceRules::whereFindInSet('goods_id',$goods_id)->findOrEmpty();
  628. if($rule->isEmpty()){
  629. throw new \Exception('请配置商品业绩规则');
  630. }
  631. PerformanceLogic::calculatePerformance($serviceWorkInfo);
  632. Db::commit();
  633. return true;
  634. } catch (\Exception $e) {
  635. Db::rollback();
  636. self::setError($e->getMessage());
  637. return false;
  638. }
  639. }
  640. /**
  641. * 获取所有改约通知
  642. * @param $userId
  643. * @return array|false
  644. */
  645. public static function getAppointmentNotice($userId)
  646. {
  647. return ServiceWork::where(['master_worker_id'=>$userId,'appoint_approval'=>1])
  648. ->where('work_status','>',1)
  649. ->where('work_status','<',7)
  650. ->field(['id', 'work_sn','real_name','mobile', 'address', 'title', 'appointment_time','work_status'])
  651. ->order(['appointment_time' => 'asc'])//上门时间排序
  652. ->select()->each(function (&$item){
  653. $item['last_appointment_time'] = date('Y-m-d H:i:s',ServiceWorkAppointmentLog::where('work_id',$item['id'])->order('id desc')->value('last_appointment_time'));
  654. })
  655. ->toArray();
  656. }
  657. /**
  658. * @param $params
  659. * @return bool
  660. */
  661. public static function submitAppointment($params)
  662. {
  663. Db::startTrans();
  664. try {
  665. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  666. if($serviceWork->isEmpty()){
  667. throw new \Exception('工单不存在');
  668. }
  669. $serviceWork->appoint_approval = 2;
  670. $serviceWork->save();
  671. Db::commit();
  672. return true;
  673. } catch (\Exception $e) {
  674. Db::rollback();
  675. self::setError($e->getMessage());
  676. return false;
  677. }
  678. }
  679. /**
  680. * @param $params
  681. * @return bool
  682. */
  683. public static function submitChangePrice($params)
  684. {
  685. Db::startTrans();
  686. try {
  687. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  688. if($serviceWork->isEmpty()){
  689. throw new \Exception('工单不存在');
  690. }
  691. $serviceWork->work_status = 4;
  692. $serviceWork->user_confirm_status = 0;
  693. $serviceWork->price_approval = 2;
  694. $serviceWork->save();
  695. Db::commit();
  696. return true;
  697. } catch (\Exception $e) {
  698. Db::rollback();
  699. self::setError($e->getMessage());
  700. return false;
  701. }
  702. }
  703. public static function notApproved($params)
  704. {
  705. Db::startTrans();
  706. try {
  707. $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
  708. if($serviceWork->isEmpty()){
  709. throw new \Exception('工单不存在');
  710. }
  711. $serviceWork->refund_approval = 3;
  712. $serviceWork->save();
  713. Db::commit();
  714. return true;
  715. } catch (\Exception $e) {
  716. Db::rollback();
  717. self::setError($e->getMessage());
  718. return false;
  719. }
  720. }
  721. public static function againDoor($params)
  722. {
  723. Db::startTrans();
  724. try {
  725. $serviceWork = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  726. if($serviceWork->isEmpty()){
  727. throw new \Exception('工单不存在');
  728. }
  729. //更新预约日志
  730. //更新工单未确认上门的状态
  731. $serviceWork->work_status = 3;
  732. $serviceWork->user_confirm_status = 0;
  733. ServiceWorkAppointmentLog::create([
  734. 'work_id'=>$serviceWork->id,
  735. 'last_appointment_time'=>$serviceWork->appointment_time,
  736. 'this_appointment_time'=>strtotime($params['appointment_time']),
  737. ]);
  738. $serviceWork->appointment_time = strtotime($params['appointment_time']);
  739. $serviceWork->save();
  740. Db::commit();
  741. return true;
  742. } catch (\Exception $e) {
  743. Db::rollback();
  744. self::setError($e->getMessage());
  745. return false;
  746. }
  747. }
  748. public static function cancelAllocation($params,$userInfo){
  749. Db::startTrans();
  750. try {
  751. $work = ServiceWork::findOrEmpty($params['id']);
  752. if($work->isEmpty()){
  753. throw new Exception('工单不存在');
  754. }
  755. if($work->work_status >=6 ){
  756. throw new \Exception('工单状态只能修改待结算之前的');
  757. }
  758. $worker = MasterWorker::where(['id'=>$work->master_worker_id])->findOrEmpty();
  759. if($worker->isEmpty()){
  760. throw new \Exception('工程师不存在');
  761. }
  762. MasterWorker::setWorktotal('dec',$work->master_worker_id);
  763. $work->master_worker_id = 0;
  764. $work->work_status = 0;
  765. $work->dispatch_time = 0;
  766. $work->save();
  767. $work_log = [
  768. 'work_id'=>$work->id,
  769. 'master_worker_id'=>$params['master_worker_id'],
  770. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'取消了工程师'.'编号['.$worker->worker_number.']'.$worker->real_name
  771. ];
  772. ServiceWorkerAllocateWorkerLogic::add($work_log);
  773. Db::commit();
  774. return true;
  775. }catch(\Exception $e){
  776. Db::rollback();
  777. dd($e->getMessage());
  778. self::setError($e->getMessage());
  779. return false;
  780. }
  781. }
  782. public static function addCustomerLog($params)
  783. {
  784. Db::startTrans();
  785. try {
  786. $serviceWork = ServiceWork::where('id',$params['id'])->findOrEmpty();
  787. if($serviceWork->isEmpty()){
  788. throw new \Exception('工单不存在');
  789. }
  790. ServiceWorkCustomerLog::create([
  791. 'work_id'=>$serviceWork->id,
  792. 'content'=>$params['content']??'',
  793. 'admin_id'=>$params['admin_id']??0,
  794. 'create_time'=>time(),
  795. ]);
  796. Db::commit();
  797. return true;
  798. } catch (\Exception $e) {
  799. Db::rollback();
  800. self::setError($e->getMessage());
  801. return false;
  802. }
  803. }
  804. }