ServiceOrderLogic.php 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. <?php
  2. namespace app\api\logic;
  3. use app\adminapi\service\DistributeLeafletsService;
  4. use app\common\enum\GoodsEnum;
  5. use app\common\enum\PayEnum;
  6. use app\common\enum\RefundEnum;
  7. use app\common\enum\WorkEnum;
  8. use app\common\enum\YesNoEnum;
  9. use app\common\logic\BaseLogic;
  10. use app\common\logic\PaymentLogic;
  11. use app\common\logic\RefundLogic;
  12. use app\common\model\coupon\CouponCategory;
  13. use app\common\model\coupon\UserCoupon;
  14. use app\common\model\dict\DictData;
  15. use app\common\model\goods\Goods;
  16. use app\common\model\master_worker\MasterWorker;
  17. use app\common\model\master_worker\MasterWorkerRule;
  18. use app\common\model\orders\RechargeOrder;
  19. use app\common\model\property\PropertyActivity;
  20. use app\common\model\recharge\OrderGoods;
  21. use app\common\model\refund\RefundRecord;
  22. use app\common\model\spare_part\SparePart;
  23. use app\common\model\works\ServiceWork;
  24. use app\common\model\works\ServiceWorkAppointmentLog;
  25. use app\common\model\works\ServiceWorkSpare;
  26. use app\workerapi\logic\ServiceWorkLogLogic;
  27. use think\Exception;
  28. use think\facade\Db;
  29. use think\facade\Log;
  30. /**
  31. * 订单逻辑层
  32. * Class ServiceOrderLogic
  33. * @package app\api\logic
  34. */
  35. class ServiceOrderLogic extends BaseLogic
  36. {
  37. /**
  38. * 判断是否在某服务区
  39. * @param $params
  40. * @return bool
  41. */
  42. public static function isService($params)
  43. {
  44. // 查询服务区所有的地点
  45. $rules = MasterWorkerRule::where('scene_id',MasterWorkerRule::SCENE_SERVICE)->column('description','id');
  46. //$masters = [];
  47. foreach ($rules as $key=>$value){
  48. $rule = explode(',',$value);
  49. //$masters[] = ['id'=>$key,'lon'=>$rule[0],'lat'=>$rule[1],'radius'=>$rule[2]];
  50. $distance = round(DistributeLeafletsService::haversineDistance($params['lat'], $params['lon'], $rule[1], $rule[0]), 2);
  51. if($distance <= (float)$rule[2]){
  52. return true;
  53. }
  54. }
  55. self::setError('已超出服务区域!');
  56. return false;
  57. }
  58. /**
  59. * 提交订单
  60. * @param array $params
  61. * @return array|false
  62. */
  63. public static function submitOrder($params)
  64. {
  65. Db::startTrans();
  66. try {
  67. // 订单位置是否在服务区内
  68. if(!self::isService($params)){
  69. throw new Exception('已超出服务区域!');
  70. }
  71. $goods = Goods::findOrEmpty($params['goods_id']);
  72. if($goods->isEmpty()){
  73. throw new Exception('产品不存在!');
  74. }
  75. if(empty($params['user_info']['mobile'])){
  76. throw new Exception('请先补充您的联系方式后在提交订单');
  77. }
  78. //根据服务工单计算当前订单应支付金额
  79. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  80. //一口价订单
  81. $order_total = $goods['service_fee'];
  82. $order_amount = $goods['service_fee'];
  83. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  84. $order_total = $goods['service_fee'];
  85. $order_amount = $goods['service_fee'];
  86. }
  87. else{
  88. $order_total = $goods['base_service_fee'];
  89. $order_amount = $goods['service_fee'];
  90. }
  91. //优惠券验证
  92. if(!empty($params['coupon_id'])){
  93. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  94. ->where('voucher_count','>',0)
  95. ->where('expire_time','>=',time())
  96. ->where('begin_use','<',time())
  97. ->findOrEmpty();
  98. if($user_coupon->isEmpty()){
  99. throw new Exception('该优惠券无法使用');
  100. }
  101. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order_amount<$user_coupon['amount_require']){
  102. throw new Exception('该优惠劵不满足满减使用条件');
  103. }
  104. if($goods['goods_payment_type'] != GoodsEnum::ISGOODS_PAYMENT_TYPE){
  105. throw new Exception('请在支付尾款的时候使用该优惠券');
  106. }
  107. //优惠券折扣
  108. if($user_coupon['mold_type'] == 1){
  109. //按比例折扣
  110. if($user_coupon['discount_ratio']>=1){
  111. throw new Exception('优惠券有误,请联系客服');
  112. }
  113. $order_coupon_amount = $order_amount*(1-$user_coupon['discount_ratio']);
  114. }else{
  115. $order_coupon_amount = $user_coupon['amount'];
  116. }
  117. if(!empty($user_coupon['max_deductible_price'])){
  118. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
  119. }else{
  120. $order_amount = $order_amount-$order_coupon_amount;
  121. }
  122. $user_coupon->voucher_status = 1;
  123. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  124. $user_coupon->save();
  125. }
  126. //生成服务工单
  127. $work_data = [
  128. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  129. 'real_name' => $params['contact_people'],
  130. 'mobile' => $params['contact_number'],
  131. 'address' => $params['address'],
  132. 'title' => $goods->goods_name,
  133. 'category_type' => $goods['category_type'],
  134. 'goods_category_ids' => $goods['goods_category_ids'],
  135. 'goods_category_id' => $goods['goods_category_id'],
  136. 'base_service_fee' => $goods['base_service_fee'],
  137. 'service_fee' => $goods['service_fee'],
  138. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  139. 'appointment_time' => strtotime($params['appointment_time']),
  140. 'user_id'=>$params['user_id'],
  141. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  142. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  143. 'property_activity_id'=>!empty($params['property_activity_id'])?$params['property_activity_id']:0,
  144. ];
  145. //判断是否是加单
  146. if(!empty($params['worker'])){
  147. $worker_id = MasterWorker::where('worker_number',$params['worker'])->value('id');
  148. $work_data['master_worker_id'] = $worker_id;
  149. $work_data['work_status'] = 1;
  150. $work_data['dispatch_time'] = time();
  151. $work_data['work_type'] = 2;
  152. $work_data['data_type'] = 1;
  153. }
  154. //判断是否是复购单
  155. $is_work = ServiceWork::where(['user_id'=>$params['user_id'],'service_status'=>3])->findOrEmpty();
  156. if(!$is_work->isEmpty()){
  157. $work_data['data_type'] = 1;
  158. }
  159. $service_work = ServiceWork::create($work_data);
  160. //生成服务订单
  161. $data = [
  162. 'work_id'=> $service_work['id'],
  163. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  164. 'order_type'=>0,//服务订单
  165. 'order_terminal' => $params['terminal'],
  166. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  167. 'user_id' => $params['user_id'],
  168. 'pay_status' => PayEnum::UNPAID,
  169. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  170. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  171. 'pay_way' => $params['pay_way'],
  172. 'order_total' => $order_total,
  173. 'order_amount' => $order_amount,
  174. ];
  175. $order = RechargeOrder::create($data);
  176. //生成订单服务详情
  177. OrderGoods::create([
  178. 'sn' => $order['sn'],
  179. 'goods_id' => $params['goods_id'],
  180. 'category_type' => $goods['category_type'],
  181. 'goods_category_ids' => $goods['goods_category_ids'],
  182. 'goods_category_id' => $goods['goods_category_id'],
  183. 'goods_name' => $goods['goods_name'],
  184. 'goods_image' => $goods['goods_image'],
  185. 'goods_video' => $goods['goods_video'],
  186. 'goods_number' => $goods['goods_number'],
  187. 'good_unit' => $goods['good_unit'],
  188. 'goods_size' => $goods['goods_size'],
  189. 'goods_type' => $goods['goods_type'],
  190. 'goods_brand' => $goods['goods_brand'],
  191. 'install_guide' => $goods['install_guide'],
  192. 'goods_payment_type'=>$goods['goods_payment_type'],
  193. 'base_service_fee' => $goods['base_service_fee'],
  194. 'service_total' => $goods['service_total'],
  195. 'service_fee' => $goods['service_fee'],
  196. 'service_image' => $goods['service_image'],
  197. 'warranty_period'=>$goods['warranty_period'],
  198. 'fee_schedule' => $goods['fee_schedule'],
  199. 'goods_status' => $goods['goods_status'],
  200. ]);
  201. Db::commit();
  202. } catch (\Exception $e) {
  203. self::setError($e->getMessage());
  204. return false;
  205. }
  206. return [
  207. 'order_id' => (int)$order['id'],
  208. 'work_id' => (int)$order['work_id'],
  209. ];
  210. }
  211. /**
  212. * 提交尾款订单
  213. * @param array $params
  214. * @return array|false
  215. */
  216. public static function submitFinalOrder($params)
  217. {
  218. Db::startTrans();
  219. try {
  220. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  221. if ($order->isEmpty()) {
  222. throw new Exception('订单不存在');
  223. }
  224. //判断订单类型.服务订单尾款处理
  225. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  226. {
  227. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  228. if($order->isEmpty()){
  229. throw new Exception('订单已支付');
  230. }
  231. }
  232. if ($order['pay_status'] == PayEnum::ISPAID) {
  233. throw new Exception('订单已支付');
  234. }
  235. $order_goods = OrderGoods::where('sn',$params['sn'])->findOrEmpty();
  236. $goods = Goods::findOrEmpty($order_goods['goods_id']);
  237. //判断是否存在优惠券
  238. //优惠券验证
  239. if(!empty($order['coupon_id'])){
  240. $order->coupon_id = 0;
  241. $order->coupon_price = 0;
  242. $order->order_amount = $order->order_total;
  243. $order->save();
  244. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  245. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  246. $user_coupon_ed->voucher_status = 0;
  247. $user_coupon_ed->save();
  248. $order = \app\common\model\recharge\RechargeOrder::where('sn',$order['sn'])->findOrEmpty();
  249. }
  250. if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
  251. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  252. ->where('voucher_count','>',0)
  253. ->where('expire_time','>=',time())
  254. ->where('begin_use','<',time())
  255. ->findOrEmpty();
  256. if($user_coupon->isEmpty()){
  257. throw new Exception('该优惠券不满足使用条件');
  258. }
  259. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order['order_amount']<$user_coupon['amount_require']){
  260. throw new Exception('该优惠劵不满足满减使用条件');
  261. }
  262. //优惠券折扣
  263. if($user_coupon['mold_type'] == 1){
  264. //按比例折扣
  265. if($user_coupon['discount_ratio']>=1){
  266. throw new Exception('优惠券有误,请联系客服');
  267. }
  268. $order_coupon_amount = $order['order_amount']*(1-$user_coupon['discount_ratio']);
  269. }else{
  270. $order_coupon_amount = $user_coupon['amount'];
  271. }
  272. if(!empty($user_coupon['max_deductible_price'])){
  273. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order['order_amount']-$user_coupon['max_deductible_price']):($order['order_amount']-$order_coupon_amount);
  274. }else{
  275. $order_amount = $order['order_amount']-$order_coupon_amount;
  276. }
  277. $user_coupon->voucher_status = 1;
  278. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  279. $user_coupon->save();
  280. }
  281. $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
  282. $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
  283. $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
  284. $order->save();
  285. Db::commit();
  286. } catch (\Exception $e) {
  287. self::setError($e->getMessage());
  288. return false;
  289. }
  290. return [
  291. 'order_id' => (int)$order['id'],
  292. 'work_id' => (int)$order['work_id'],
  293. ];
  294. }
  295. /**
  296. * 重置订单优惠券
  297. * * @param $params
  298. * * * @return array|false
  299. */
  300. public static function cancelOrderCoupon($params)
  301. {
  302. Db::startTrans();
  303. try {
  304. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  305. if ($order->isEmpty()) {
  306. throw new Exception('订单不存在');
  307. }
  308. //判断订单类型.服务订单尾款处理
  309. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  310. {
  311. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  312. if($order->isEmpty()){
  313. throw new Exception('订单已支付');
  314. }
  315. }
  316. if ($order['pay_status'] == PayEnum::ISPAID) {
  317. throw new Exception('订单已支付');
  318. }
  319. if(!empty($order['coupon_id'])){
  320. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  321. if(!$user_coupon_ed->isEmpty()){
  322. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  323. $user_coupon_ed->voucher_status = 0;
  324. $user_coupon_ed->save();
  325. $order->order_amount = $order->order_amount+$order->coupon_price;
  326. $order->coupon_id = 0;
  327. $order->coupon_price = 0;
  328. $order->save();
  329. }
  330. }
  331. Db::commit();
  332. } catch (\Exception $e) {
  333. self::setError($e->getMessage());
  334. return false;
  335. }
  336. }
  337. /**
  338. * 获取订单工程师信息
  339. * * @param $params
  340. * * @return array|false
  341. */
  342. public static function getMasterWorker($params)
  343. {
  344. try {
  345. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  346. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  347. },'service_work'=>function ($query) {
  348. $query->visible(['real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','finished_images','finished_time'])->append(['service_status_text','user_service_status','user_service_status_text']);
  349. }])
  350. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  351. ->where([
  352. 'order_type' => 0,
  353. 'user_id' => $params['user_id'],
  354. 'sn'=>$params['sn']
  355. ])->findOrEmpty()->toArray();
  356. $data = [];
  357. //获取工程师参数
  358. if(!empty($order_info['service_work']['master_worker_id'])){
  359. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  360. $data['avatar'] = $worker['avatar'];
  361. $data['real_name'] = $worker['real_name'];
  362. $data['worker_number'] = $worker['worker_number'];
  363. $data['mobile'] = $worker['mobile'];
  364. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  365. $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  366. $data['appointment_time'] = $order_info['service_work']['appointment_time'];
  367. }
  368. return $data;
  369. }
  370. catch (\Exception $e) {
  371. self::setError($e->getMessage());
  372. return false;
  373. }
  374. }
  375. /**
  376. * 获取订单详情
  377. * @param $params
  378. * @return array|false
  379. */
  380. public static function detail($params)
  381. {
  382. try {
  383. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  384. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  385. },'service_work'=>function ($query) {
  386. $query->visible(['work_sn','real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','explanation','finished_images','finished_time','spare_total','service_work_spare_id','refund_approval','property_activity_id'])->append(['service_status_text','user_service_status','user_service_status_text']);
  387. }])
  388. ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
  389. ->where([
  390. 'order_type' => 0,
  391. 'user_id' => $params['user_id'],
  392. 'sn'=>$params['sn']
  393. ])->findOrEmpty()->toArray();
  394. if(empty($order_info)){
  395. throw new Exception('订单不存在');
  396. }
  397. $order_info['master_worker'] = [
  398. 'avatar' => '',
  399. 'real_name'=>'',
  400. 'worker_number'=>'',
  401. 'mobile'=>'',
  402. 'worker_exp'=>''
  403. ];
  404. //查询总价
  405. $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
  406. $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
  407. //退款金额
  408. $order_refund_amount = 0;
  409. //获取工程师参数
  410. if(!empty($order_info['service_work']['master_worker_id'])){
  411. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  412. $order_info['master_worker']['avatar'] = !empty($worker)?$worker['avatar']:'';
  413. $order_info['master_worker']['real_name'] = !empty($worker)?$worker['real_name']:'';
  414. $order_info['master_worker']['worker_number'] = !empty($worker)?$worker['worker_number']:'';
  415. $order_info['master_worker']['mobile'] = !empty($worker)?$worker['mobile']:'';
  416. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  417. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  418. }
  419. //搜索当前工单下的所有订单记录
  420. $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status ,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_price')->order('id asc')->select()->toArray();
  421. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  422. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  423. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  424. $coupon_price = 0;
  425. foreach ($order_info['pay_orders'] as $k=>&$v){
  426. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  427. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  428. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  429. if($v['refund_status']==1){
  430. $order_refund_amount += $v['order_amount'];
  431. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  432. }
  433. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  434. $v['order_amount_total'] = $v['order_amount'];
  435. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'];
  436. }
  437. $coupon_price += $v['coupon_price'];
  438. }
  439. //汇总优惠卷额度
  440. $order_info['coupon_price'] = $coupon_price;
  441. //退款汇总
  442. $order_info['refund_amount'] = $order_refund_amount;
  443. if(isset($refund_status)){
  444. switch ($refund_status){
  445. case 0:
  446. $order_info['refund_status'] = '退款中';
  447. break;
  448. case 1:
  449. $order_info['refund_status'] = '退款成功';
  450. break;
  451. case 2:
  452. $order_info['refund_status'] = '退款失败';
  453. break;
  454. }
  455. }else{
  456. $order_info['refund_status'] = '未退款';
  457. }
  458. // 所有配件
  459. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  460. $order_info['spare_parts'] = [];
  461. if($order_info['service_work']['service_work_spare_id']){
  462. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  463. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  464. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  465. ->select()
  466. ->toArray();
  467. $spare_parts = array_column($spare_parts,null,'id');
  468. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  469. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  470. }
  471. $order_info['spare_parts'] = array_values($spare_parts)??[];
  472. }
  473. //获取所有的改约记录
  474. $order_info['appoint_list'] = [];
  475. $appoint_log = ServiceWorkAppointmentLog::where('work_id',$order_info['work_id'])->order('id desc')->select()->toArray();
  476. if(!empty($appoint_log)){
  477. $order_info['appoint_list'][0]['appointment_time'] = date('Y-m-d H:i:s',$appoint_log[0]['this_appointment_time']);
  478. foreach ($appoint_log as $k1=>$v1){
  479. $order_info['appoint_list'][$k1+1]['appointment_time'] = date('Y-m-d H:i:s',$v1['last_appointment_time']);
  480. }
  481. }
  482. //获取是否是活动工单
  483. if(!empty($order_info['service_work']['property_activity_id'])){
  484. $propertyActivity = PropertyActivity::findOrEmpty($order_info['service_work']['property_activity_id']);
  485. $order_info['service_work']['url_page'] = rawurlencode($params['domain'].'/static/wxapp/H5/'.$propertyActivity['url_page'].'/index.html?property_activity_id='.$propertyActivity['id'].'&token='.$params['user_info']['token']);
  486. }
  487. return $order_info;
  488. }
  489. catch (\Exception $e) {
  490. self::setError($e->getMessage());
  491. return false;
  492. }
  493. }
  494. public static function getDetailStatus($params)
  495. {
  496. try {
  497. $order_info = \app\common\model\recharge\RechargeOrder::with(['service_work'=>function ($query) {
  498. $query->visible(['work_sn'])->append(['service_status_text','user_service_status','user_service_status_text']);
  499. }])
  500. ->visible(['id','sn'])
  501. ->where([
  502. 'order_type' => 0,
  503. 'user_id' => $params['user_id'],
  504. 'sn'=>$params['sn']
  505. ])->findOrEmpty()->toArray();
  506. if(empty($order_info)){
  507. throw new Exception('订单不存在');
  508. }
  509. return $order_info;
  510. }
  511. catch (\Exception $e) {
  512. self::setError($e->getMessage());
  513. return false;
  514. }
  515. }
  516. /**
  517. * 获取订单支付详情
  518. * @param $params
  519. * @return array|false
  520. */
  521. public static function orderPayInfo($params)
  522. {
  523. try {
  524. //判断是否存在优惠券
  525. $is_coupon_used_work_id = \app\common\model\recharge\RechargeOrder::where([
  526. 'order_type' => 0,
  527. 'user_id' => $params['user_id'],
  528. 'sn'=>$params['sn']
  529. ])->value('work_id');
  530. $is_coupon_used = \app\common\model\recharge\RechargeOrder::where('work_id',$is_coupon_used_work_id)->where('pay_status',0)->findOrEmpty();
  531. if(!empty($is_coupon_used['coupon_id'])){
  532. $user_coupon = UserCoupon::where(['user_id'=>$is_coupon_used->user_id,'id'=>$is_coupon_used->coupon_id])->findOrEmpty();
  533. $user_coupon->voucher_status = 0;
  534. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  535. $user_coupon->save();
  536. $is_coupon_used->order_amount = $is_coupon_used->order_amount+$is_coupon_used->coupon_price;
  537. $is_coupon_used->coupon_price = 0;
  538. $is_coupon_used->coupon_id = 0;
  539. $is_coupon_used->save();
  540. }
  541. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  542. $query->visible(['goods_payment_type','goods_category_id']);
  543. },'service_work'=>function ($query) {
  544. $query->visible(['service_fee','spare_total','service_work_spare_id','goods_category_id']);
  545. }])
  546. ->visible(['id','sn','work_id'])
  547. ->where([
  548. 'order_type' => 0,
  549. 'user_id' => $params['user_id'],
  550. 'sn'=>$params['sn']
  551. ])->findOrEmpty()->toArray();
  552. if(empty($order_info)){
  553. throw new Exception('订单不存在');
  554. }
  555. //查询总价
  556. $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
  557. $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
  558. //搜索当前工单下的所有订单记录
  559. $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_price')->order('id asc')->select()->toArray();
  560. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  561. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  562. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  563. $coupon_price = 0;
  564. //退款金额
  565. $order_refund_amount = 0;
  566. $payment_type = 0;
  567. foreach ($order_info['pay_orders'] as $k=>&$v){
  568. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  569. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  570. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  571. if($v['refund_status']==1){
  572. $order_refund_amount += $v['order_amount'];
  573. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  574. }
  575. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  576. $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
  577. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
  578. }
  579. $coupon_price += $v['coupon_price'];
  580. $payment_type = $v['payment_type'];
  581. }
  582. $order_info['payment_type'] = $payment_type;
  583. //汇总优惠卷额度
  584. $order_info['coupon_price'] = $coupon_price;
  585. //退款汇总
  586. $order_info['refund_amount'] = $order_refund_amount;
  587. if(isset($refund_status)){
  588. switch ($refund_status){
  589. case 0:
  590. $order_info['refund_status'] = '退款中';
  591. break;
  592. case 1:
  593. $order_info['refund_status'] = '退款成功';
  594. break;
  595. case 2:
  596. $order_info['refund_status'] = '退款失败';
  597. break;
  598. }
  599. }else{
  600. $order_info['refund_status'] = '未退款';
  601. }
  602. // 所有配件
  603. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  604. $order_info['spare_parts'] = [];
  605. if($order_info['service_work']['service_work_spare_id']){
  606. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  607. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  608. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  609. ->select()
  610. ->toArray();
  611. $spare_parts = array_column($spare_parts,null,'id');
  612. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  613. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  614. }
  615. $order_info['spare_parts'] = array_values($spare_parts)??[];
  616. }
  617. //获取是否存在可使用优惠券
  618. $coupon_ids = CouponCategory::where('goods_category_id',$order_info['service_work']['goods_category_id'])
  619. ->whereIn('coupon_id',UserCoupon::where('user_id',$params['user_id'])
  620. ->where('voucher_count','>',0)
  621. ->where('voucher_status',0)
  622. ->where('expire_time','>',time())
  623. ->where('amount_require','<=',$order_info['order_amount'])
  624. ->column('coupon_id')
  625. )->column('coupon_id');
  626. $usable_coupon = UserCoupon::where('user_id',$params['user_id'])
  627. ->whereIn('coupon_id',$coupon_ids)
  628. ->append(['discount_ratio_text'])
  629. ->where('expire_time','>',time())
  630. ->count();
  631. $order_info['usable_coupon'] = $usable_coupon>0?true:false;
  632. return $order_info;
  633. }
  634. catch (\Exception $e) {
  635. self::setError($e->getMessage());
  636. return false;
  637. }
  638. }
  639. /**
  640. * 取消订单
  641. * @param $params
  642. * @return false|void
  643. */
  644. public static function cancelOrder($params)
  645. {
  646. Db::startTrans();
  647. try {
  648. $work_id = \app\common\model\recharge\RechargeOrder::where([
  649. 'order_type' => 0,
  650. 'user_id' => $params['user_id'],
  651. 'sn'=>$params['sn']
  652. ])->value('work_id');
  653. if(empty($work_id)){
  654. throw new Exception('订单不存在');
  655. }
  656. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  657. if(!$payed_order->isEmpty()){
  658. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  659. }
  660. //软删除订单
  661. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  662. $cancel_order->pay_status = 2;
  663. $cancel_order->save();
  664. //更新工单状态为已取消
  665. $service_work = ServiceWork::find($work_id);
  666. $service_work->service_status = 4;
  667. $service_work->save();
  668. //判断如果存在优惠券,返还优惠券
  669. if(!empty($payed_order->coupon_id)){
  670. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  671. $user_coupon->voucher_status = 0;
  672. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  673. $user_coupon->save();
  674. }
  675. Db::commit();
  676. if($cancel_order->pay_time){
  677. // 订单取消通知【给用户】
  678. event('Notice', [
  679. 'scene_id' => 122,
  680. 'params' => [
  681. 'user_id' => $params['user_id']
  682. ]
  683. ]);
  684. }
  685. }
  686. catch (\Exception $e) {
  687. self::setError($e->getMessage());
  688. return false;
  689. }
  690. }
  691. /**
  692. * 用户确认尾款报价单
  693. * @param $params
  694. * @return false|void
  695. */
  696. public static function confirmOrder($params)
  697. {
  698. Db::startTrans();
  699. try {
  700. $work_id = \app\common\model\recharge\RechargeOrder::where([
  701. 'order_type' => 0,
  702. 'user_id' => $params['user_id'],
  703. 'sn'=>$params['sn']
  704. ])->value('work_id');
  705. if(empty($work_id)){
  706. throw new Exception('订单不存在');
  707. }
  708. //更新工单状态为已取消
  709. $service_work = ServiceWork::find($work_id);
  710. if($service_work->user_confirm_status==2){
  711. throw new Exception('请勿重复操作');
  712. }
  713. $service_work->work_status = 5;
  714. $service_work->user_confirm_status = 2;
  715. $service_work->save();
  716. $work_log = [
  717. 'work_id'=>$work_id,
  718. 'master_worker_id'=>$service_work->master_worker_id,
  719. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  720. ];
  721. ServiceWorkLogLogic::add($work_log);
  722. Db::commit();
  723. }
  724. catch (\Exception $e) {
  725. self::setError($e->getMessage());
  726. return false;
  727. }
  728. }
  729. /**
  730. * 用户确认服务完成
  731. * @param $params
  732. * @return false|void
  733. */
  734. public static function confirmServiceFinish($params)
  735. {
  736. Db::startTrans();
  737. try {
  738. $work_id = \app\common\model\recharge\RechargeOrder::where([
  739. 'order_type' => 0,
  740. 'user_id' => $params['user_id'],
  741. 'sn'=>$params['sn']
  742. ])->value('work_id');
  743. if(empty($work_id)){
  744. throw new Exception('订单不存在');
  745. }
  746. $service_work = ServiceWork::find($work_id);
  747. if($service_work->user_confirm_status!=3){
  748. throw new Exception('请勿重复操作');
  749. }
  750. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  751. //确认所有订单总金额和结算金额
  752. //若订单是全款已支付订单
  753. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  754. $service_work->work_status = 7;
  755. $service_work->user_confirm_status = 5;
  756. $service_work->service_status = 3;
  757. $service_work->work_pay_status = 1;
  758. }else{
  759. $service_work->work_status = 6;
  760. $service_work->user_confirm_status = 4;
  761. }
  762. $service_work->finished_time = time();
  763. $service_work->save();
  764. //更新工程师的进行工单数量
  765. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  766. $work_log = [
  767. 'work_id'=>$work_id,
  768. 'master_worker_id'=>$service_work->master_worker_id,
  769. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  770. ];
  771. ServiceWorkLogLogic::add($work_log);
  772. Db::commit();
  773. }
  774. catch (\Exception $e) {
  775. self::setError($e->getMessage());
  776. return false;
  777. }
  778. }
  779. public static function approvalChangePrice($params)
  780. {
  781. Db::startTrans();
  782. try {
  783. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  784. if($order->isEmpty()){
  785. throw new Exception('订单不存在');
  786. }
  787. $work = ServiceWork::findOrEmpty($order->work_id);
  788. if($work->isEmpty()){
  789. throw new Exception('工单不存在');
  790. }
  791. $work->work_status = 4;
  792. $work->user_confirm_status = 0;
  793. $work->price_approval = 1;
  794. $work->save();
  795. Db::commit();
  796. }
  797. catch (\Exception $e) {
  798. self::setError($e->getMessage());
  799. return false;
  800. }
  801. }
  802. public static function approvalChangeAppointment($params)
  803. {
  804. Db::startTrans();
  805. try {
  806. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  807. if($order->isEmpty()){
  808. throw new Exception('订单不存在');
  809. }
  810. $work = ServiceWork::findOrEmpty($order->work_id);
  811. if($work->isEmpty()){
  812. throw new Exception('工单不存在');
  813. }
  814. //更新工单未确认上门的状态
  815. $work->work_status = 3;
  816. $work->user_confirm_status = 0;
  817. ServiceWorkAppointmentLog::create([
  818. 'work_id'=>$work->id,
  819. 'last_appointment_time'=>strtotime($work->appointment_time),
  820. 'this_appointment_time'=>strtotime($params['appointment_time']),
  821. ]);
  822. $work->appointment_time = strtotime($params['appointment_time']);
  823. $work->appoint_approval = 1;
  824. $work->save();
  825. Db::commit();
  826. }
  827. catch (\Exception $e) {
  828. self::setError($e->getMessage());
  829. return false;
  830. }
  831. return $work;
  832. }
  833. public static function approvalRefund($params)
  834. {
  835. Db::startTrans();
  836. try {
  837. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  838. if($order->isEmpty()){
  839. throw new Exception('订单不存在');
  840. }
  841. $work = ServiceWork::findOrEmpty($order->work_id);
  842. if($work->isEmpty()){
  843. throw new Exception('工单不存在');
  844. }
  845. $work->refund_approval = 1;
  846. $work->save();
  847. //判断是否已下单时间过了两小时,并且师傅暂未上门,距离预约时间两小时以上
  848. if(($order['pay_time']+7200)>time() and $work['work_status']<4 and (strtotime($work['appointment_time'])-7200)>time()){
  849. //生成退款订单
  850. \app\common\model\recharge\RechargeOrder::update([
  851. 'id' => $order['id'],
  852. 'refund_status' => YesNoEnum::YES,
  853. ]);
  854. // 生成退款记录
  855. $recordSn = generate_sn(RefundRecord::class, 'sn');
  856. $record = RefundRecord::create([
  857. 'sn' => $recordSn,
  858. 'user_id' => $order['user_id'],
  859. 'order_id' => $order['id'],
  860. 'order_sn' => $order['sn'],
  861. 'order_type' => RefundEnum::ORDER_TYPE_ORDER,
  862. 'order_amount' => $order['order_amount'],
  863. 'refund_amount' => $order['order_amount'],
  864. 'refund_type' => RefundEnum::TYPE_ADMIN,
  865. 'transaction_id' => $order['transaction_id'] ?? '',
  866. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  867. ]);
  868. //更新工单状态
  869. ServiceWork::update([
  870. 'id'=>$order['work_id'],
  871. 'service_status'=>5
  872. ]);
  873. // 退款
  874. RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
  875. }
  876. Db::commit();
  877. }
  878. catch (\Exception $e) {
  879. self::setError($e->getMessage());
  880. return false;
  881. }
  882. }
  883. /**
  884. * 订单完成通知【给用户】 - 全款 -通知
  885. * @param $params
  886. * @return bool
  887. */
  888. public static function serviceFinishNotice($params)
  889. {
  890. try {
  891. $order = RechargeOrder::where('sn', $params['sn'])
  892. ->where('payment_type','=',0)
  893. ->where('pay_status','=',1)
  894. ->findOrEmpty();
  895. if(!$order->isEmpty()){
  896. event('Notice', [
  897. 'scene_id' => 120,
  898. 'params' => [
  899. 'user_id' => $order['user_id']
  900. ]
  901. ]);
  902. }
  903. return true;
  904. }catch (\Exception $e) {
  905. return false;
  906. }
  907. }
  908. /**
  909. * 提交订单
  910. * @param array $params
  911. * @return array|false
  912. */
  913. public static function firmSubmitOrder($params)
  914. {
  915. Db::startTrans();
  916. try {
  917. $goods = Goods::findOrEmpty($params['goods_id']);
  918. if($goods->isEmpty()){
  919. throw new Exception('产品不存在!');
  920. }
  921. if(empty($params['user_info']['mobile'])){
  922. throw new Exception('请先补充您的联系方式后在提交订单');
  923. }
  924. //根据服务工单计算当前订单应支付金额
  925. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  926. //一口价订单
  927. $order_total = $goods['service_fee'];
  928. $order_amount = $goods['service_fee'];
  929. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  930. $order_total = $goods['service_fee'];
  931. $order_amount = $goods['service_fee'];
  932. }
  933. else{
  934. $order_total = $goods['base_service_fee'];
  935. $order_amount = $goods['service_fee'];
  936. }
  937. //生成服务工单
  938. $work_data = [
  939. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  940. 'real_name' => $params['contact_people'],
  941. 'mobile' => $params['contact_number'],
  942. 'address' => $params['address'],
  943. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  944. 'category_type' => $goods['category_type'],
  945. 'goods_category_ids' => $goods['goods_category_ids'],
  946. 'goods_category_id' => $goods['goods_category_id'],
  947. 'base_service_fee' => $goods['base_service_fee'],
  948. 'service_fee' => $goods['service_fee'],
  949. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  950. 'work_type'=> 1,
  951. 'appointment_time' => strtotime($params['appointment_time']),
  952. 'user_id'=>$params['user_id'],
  953. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  954. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  955. ];
  956. $service_work = ServiceWork::create($work_data);
  957. //生成服务订单
  958. $data = [
  959. 'work_id'=> $service_work['id'],
  960. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  961. 'order_type'=>0,//服务订单
  962. 'order_terminal' => $params['terminal'],
  963. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  964. 'user_id' => $params['user_id'],
  965. 'pay_status' => PayEnum::UNPAID,
  966. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  967. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  968. 'pay_way' => $params['pay_way'],
  969. 'order_total' => $order_total,
  970. 'order_amount' => $order_amount,
  971. ];
  972. $order = RechargeOrder::create($data);
  973. //生成订单服务详情
  974. OrderGoods::create([
  975. 'sn' => $order['sn'],
  976. 'goods_id' => $params['goods_id'],
  977. 'category_type' => $goods['category_type'],
  978. 'goods_category_ids' => $goods['goods_category_ids'],
  979. 'goods_category_id' => $goods['goods_category_id'],
  980. 'goods_name' => $goods['goods_name'],
  981. 'goods_image' => $goods['goods_image'],
  982. 'goods_video' => $goods['goods_video'],
  983. 'goods_number' => $goods['goods_number'],
  984. 'good_unit' => $goods['good_unit'],
  985. 'goods_size' => $goods['goods_size'],
  986. 'goods_type' => $goods['goods_type'],
  987. 'goods_brand' => $goods['goods_brand'],
  988. 'install_guide' => $goods['install_guide'],
  989. 'goods_payment_type'=>$goods['goods_payment_type'],
  990. 'base_service_fee' => $goods['base_service_fee'],
  991. 'service_total' => $goods['service_total'],
  992. 'service_fee' => $goods['service_fee'],
  993. 'service_image' => $goods['service_image'],
  994. 'warranty_period'=>$goods['warranty_period'],
  995. 'fee_schedule' => $goods['fee_schedule'],
  996. 'goods_status' => $goods['goods_status'],
  997. ]);
  998. Db::commit();
  999. } catch (\Exception $e) {
  1000. self::setError($e->getMessage());
  1001. return false;
  1002. }
  1003. return [
  1004. 'order_id' => (int)$order['id'],
  1005. 'work_id' => (int)$order['work_id'],
  1006. ];
  1007. }
  1008. /**
  1009. * 绑定工程师-设置为加单
  1010. * * @param array $params
  1011. * * @return array|false
  1012. */
  1013. public static function bindWorkerAndWork(array $params)
  1014. {
  1015. Db::startTrans();
  1016. try {
  1017. $order = RechargeOrder::where(['sn'=>$params['sn'],'user_id'=>$params['user_id']])->findOrEmpty();
  1018. if($order->isEmpty()){
  1019. throw new Exception('订单不存在');
  1020. }
  1021. $work = ServiceWork::where(['id'=>$order['work_id']])->findOrEmpty();
  1022. if($work->isEmpty()){
  1023. throw new Exception('工单不存在');
  1024. }
  1025. if($work['work_status']!=WorkEnum::WORK_STATUS_WAIT_SERVICE){
  1026. throw new Exception('工单状态不正确,无法绑定');
  1027. }
  1028. $worker = MasterWorker::where('id',$params['worker_id'])->findOrEmpty();
  1029. if($worker->isEmpty()){
  1030. throw new Exception('工程师不存在');
  1031. }
  1032. $work->master_worker_id = $params['worker_id'];
  1033. $work->work_status = WorkEnum::WORK_STATUS_WORKER;
  1034. $work->receive_time = time();
  1035. $work->work_type = 2;//加单状态
  1036. $work->save();
  1037. Db::commit();
  1038. } catch (\Exception $e) {
  1039. self::setError($e->getMessage());
  1040. return false;
  1041. }
  1042. }
  1043. }