ServiceOrderLogic.php 52 KB

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