ServiceOrderLogic.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\enum\GoodsEnum;
  4. use app\common\enum\PayEnum;
  5. use app\common\enum\WorkEnum;
  6. use app\common\logic\BaseLogic;
  7. use app\common\logic\PaymentLogic;
  8. use app\common\model\coupon\UserCoupon;
  9. use app\common\model\dict\DictData;
  10. use app\common\model\goods\Goods;
  11. use app\common\model\master_worker\MasterWorker;
  12. use app\common\model\orders\RechargeOrder;
  13. use app\common\model\recharge\OrderGoods;
  14. use app\common\model\works\ServiceWork;
  15. use app\workerapi\logic\ServiceWorkLogLogic;
  16. use think\Exception;
  17. use think\facade\Db;
  18. /**
  19. * 订单逻辑层
  20. * Class ServiceOrderLogic
  21. * @package app\api\logic
  22. */
  23. class ServiceOrderLogic extends BaseLogic
  24. {
  25. /**
  26. * 提交订单
  27. * @param array $params
  28. * @return array|false
  29. */
  30. public static function submitOrder($params)
  31. {
  32. Db::startTrans();
  33. try {
  34. $goods = Goods::findOrEmpty($params['goods_id']);
  35. if($goods->isEmpty()){
  36. throw new Exception('产品不存在!');
  37. }
  38. if(empty($params['user_info']['mobile'])){
  39. throw new Exception('请先补充您的联系方式后在提交订单');
  40. }
  41. //根据服务工单计算当前订单应支付金额
  42. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  43. //一口价订单
  44. $order_total = $goods['service_fee'];
  45. $order_amount = $goods['service_fee'];
  46. }else{
  47. $order_total = $goods['base_service_fee'];
  48. $order_amount = $goods['service_fee'];
  49. }
  50. //优惠券验证
  51. if(!empty($params['coupon_id'])){
  52. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  53. ->where('voucher_count','>',0)
  54. ->where('expire_time','>=',time())
  55. ->where('begin_use','<',time())
  56. ->findOrEmpty();
  57. if($user_coupon->isEmpty()){
  58. throw new Exception('该优惠券无法使用');
  59. }
  60. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order_amount<$user_coupon['amount_require']){
  61. throw new Exception('该优惠劵不满足满减使用条件');
  62. }
  63. if($goods['goods_payment_type'] != GoodsEnum::ISGOODS_PAYMENT_TYPE){
  64. throw new Exception('请在支付尾款的时候使用该优惠券');
  65. }
  66. //优惠券折扣
  67. if($user_coupon['mold_type'] == 1){
  68. //按比例折扣
  69. if($user_coupon['discount_ratio']>=1){
  70. throw new Exception('优惠券有误,请联系客服');
  71. }
  72. $order_coupon_amount = intval($order_amount*(1-$user_coupon['discount_ratio']));
  73. }else{
  74. $order_coupon_amount = $user_coupon['amount'];
  75. }
  76. if(!empty($user_coupon['max_deductible_price'])){
  77. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
  78. }else{
  79. $order_amount = $order_amount-$order_coupon_amount;
  80. }
  81. $user_coupon->voucher_status = 1;
  82. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  83. $user_coupon->save();
  84. }
  85. //生成服务工单
  86. $work_data = [
  87. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  88. 'real_name' => $params['contact_people'],
  89. 'mobile' => $params['contact_number'],
  90. 'address' => $params['address'],
  91. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  92. 'category_type' => $goods['category_type'],
  93. 'goods_category_ids' => $goods['goods_category_ids'],
  94. 'goods_category_id' => $goods['goods_category_id'],
  95. 'base_service_fee' => $goods['base_service_fee'],
  96. 'service_fee' => $goods['service_fee'],
  97. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  98. 'appointment_time' => strtotime($params['appointment_time']),
  99. 'user_id'=>$params['user_id'],
  100. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  101. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  102. ];
  103. //判断是否是加单
  104. if(!empty($params['worker'])){
  105. $worker_id = MasterWorker::where('worker_number',$params['worker'])->value('id');
  106. $work_data['master_worker_id'] = $worker_id;
  107. $work_data['work_status'] = 1;
  108. $work_data['dispatch_time'] = time();
  109. $work_data['work_type'] = 2;
  110. }
  111. $service_work = ServiceWork::create($work_data);
  112. //生成服务订单
  113. $data = [
  114. 'work_id'=> $service_work['id'],
  115. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  116. 'order_type'=>0,//服务订单
  117. 'order_terminal' => $params['terminal'],
  118. 'payment_type'=>$goods['goods_payment_type']==1?1:0,
  119. 'user_id' => $params['user_id'],
  120. 'pay_status' => PayEnum::UNPAID,
  121. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  122. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  123. 'pay_way' => $params['pay_way'],
  124. 'order_total' => $order_total,
  125. 'order_amount' => $order_amount,
  126. ];
  127. $order = RechargeOrder::create($data);
  128. //生成订单服务详情
  129. OrderGoods::create([
  130. 'sn' => $order['sn'],
  131. 'goods_id' => $params['goods_id'],
  132. 'category_type' => $goods['category_type'],
  133. 'goods_category_ids' => $goods['goods_category_ids'],
  134. 'goods_category_id' => $goods['goods_category_id'],
  135. 'goods_name' => $goods['goods_name'],
  136. 'goods_image' => $goods['goods_image'],
  137. 'goods_video' => $goods['goods_video'],
  138. 'goods_number' => $goods['goods_number'],
  139. 'good_unit' => $goods['good_unit'],
  140. 'goods_size' => $goods['goods_size'],
  141. 'goods_type' => $goods['goods_type'],
  142. 'goods_brand' => $goods['goods_brand'],
  143. 'install_guide' => $goods['install_guide'],
  144. 'goods_payment_type'=>$goods['goods_payment_type'],
  145. 'base_service_fee' => $goods['base_service_fee'],
  146. 'service_total' => $goods['service_total'],
  147. 'service_fee' => $goods['service_fee'],
  148. 'service_image' => $goods['service_image'],
  149. 'warranty_period'=>$goods['warranty_period'],
  150. 'fee_schedule' => $goods['fee_schedule'],
  151. 'goods_status' => $goods['goods_status'],
  152. ]);
  153. Db::commit();
  154. } catch (\Exception $e) {
  155. self::setError($e->getMessage());
  156. return false;
  157. }
  158. return [
  159. 'order_id' => (int)$order['id'],
  160. 'work_id' => (int)$order['work_id'],
  161. ];
  162. }
  163. /**
  164. * 提交尾款订单
  165. * @param array $params
  166. * @return array|false
  167. */
  168. public static function submitFinalOrder($params)
  169. {
  170. Db::startTrans();
  171. try {
  172. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  173. if ($order->isEmpty()) {
  174. throw new Exception('订单不存在');
  175. }
  176. //判断订单类型.服务订单尾款处理
  177. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  178. {
  179. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  180. if($order->isEmpty()){
  181. throw new Exception('订单已支付');
  182. }
  183. }
  184. if ($order['pay_status'] == PayEnum::ISPAID) {
  185. throw new Exception('订单已支付');
  186. }
  187. $order_goods = OrderGoods::where('sn',$params['sn'])->findOrEmpty();
  188. $goods = Goods::findOrEmpty($order_goods['goods_id']);
  189. //判断是否存在优惠券
  190. //优惠券验证
  191. if(!empty($order['coupon_id'])){
  192. $order->coupon_id = 0;
  193. $order->coupon_price = 0;
  194. $order->order_amount = $order->order_total;
  195. $order->save();
  196. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  197. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  198. $user_coupon_ed->voucher_status = 0;
  199. $user_coupon_ed->save();
  200. $order = \app\common\model\recharge\RechargeOrder::where('sn',$order['sn'])->findOrEmpty();
  201. }
  202. if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
  203. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  204. ->where('voucher_count','>',0)
  205. ->where('expire_time','>=',time())
  206. ->where('begin_use','<',time())
  207. ->findOrEmpty();
  208. if($user_coupon->isEmpty()){
  209. throw new Exception('该优惠券无法使用');
  210. }
  211. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order['order_amount']<$user_coupon['amount_require']){
  212. throw new Exception('该优惠劵不满足满减使用条件');
  213. }
  214. //优惠券折扣
  215. if($user_coupon['mold_type'] == 1){
  216. //按比例折扣
  217. if($user_coupon['discount_ratio']>=1){
  218. throw new Exception('优惠券有误,请联系客服');
  219. }
  220. $order_coupon_amount = intval($order['order_amount']*(1-$user_coupon['discount_ratio']));
  221. }else{
  222. $order_coupon_amount = $user_coupon['amount'];
  223. }
  224. if(!empty($user_coupon['max_deductible_price'])){
  225. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order['order_amount']-$user_coupon['max_deductible_price']):($order['order_amount']-$order_coupon_amount);
  226. }else{
  227. $order_amount = $order['order_amount']-$order_coupon_amount;
  228. }
  229. $user_coupon->voucher_status = 1;
  230. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  231. $user_coupon->save();
  232. }
  233. $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
  234. $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
  235. $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
  236. $order->save();
  237. Db::commit();
  238. } catch (\Exception $e) {
  239. self::setError($e->getMessage());
  240. return false;
  241. }
  242. }
  243. /**
  244. * 获取订单工程师信息
  245. * * @param $params
  246. * * @return array|false
  247. */
  248. public static function getMasterWorker($params)
  249. {
  250. try {
  251. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  252. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  253. },'service_work'=>function ($query) {
  254. $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']);
  255. }])
  256. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  257. ->where([
  258. 'order_type' => 0,
  259. 'user_id' => $params['user_id'],
  260. 'sn'=>$params['sn']
  261. ])->findOrEmpty()->toArray();
  262. $data = [];
  263. //获取师傅参数
  264. if(!empty($order_info['service_work']['master_worker_id'])){
  265. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  266. $data['avatar'] = $worker['avatar'];
  267. $data['real_name'] = $worker['real_name'];
  268. $data['worker_number'] = $worker['worker_number'];
  269. $data['mobile'] = $worker['mobile'];
  270. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  271. $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  272. $data['appointment_time'] = $order_info['service_work']['appointment_time'];
  273. }
  274. return $data;
  275. }
  276. catch (\Exception $e) {
  277. self::setError($e->getMessage());
  278. return false;
  279. }
  280. }
  281. /**
  282. * 获取订单详情
  283. * @param $params
  284. * @return array|false
  285. */
  286. public static function detail($params)
  287. {
  288. try {
  289. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  290. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  291. },'service_work'=>function ($query) {
  292. $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']);
  293. }])
  294. ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
  295. ->where([
  296. 'order_type' => 0,
  297. 'user_id' => $params['user_id'],
  298. 'sn'=>$params['sn']
  299. ])->findOrEmpty()->toArray();
  300. $order_info['master_worker'] = [
  301. 'avatar' => '',
  302. 'real_name'=>'',
  303. 'worker_number'=>'',
  304. 'mobile'=>'',
  305. 'worker_exp'=>''
  306. ];
  307. if(empty($order_info)){
  308. throw new Exception('订单不存在');
  309. }
  310. //查询总价
  311. $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');
  312. $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');
  313. //获取师傅参数
  314. if(!empty($order_info['service_work']['master_worker_id'])){
  315. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  316. $order_info['master_worker']['avatar'] = $worker['avatar'];
  317. $order_info['master_worker']['real_name'] = $worker['real_name'];
  318. $order_info['master_worker']['worker_number'] = $worker['worker_number'];
  319. $order_info['master_worker']['mobile'] = $worker['mobile'];
  320. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  321. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  322. }
  323. //搜索当前工单下的所有订单记录
  324. $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,payment_type,pay_way,pay_time,order_amount,coupon_price')->order('id asc')->select()->toArray();
  325. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  326. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  327. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  328. $coupon_price = 0;
  329. foreach ($order_info['pay_orders'] as $k=>&$v){
  330. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  331. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  332. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  333. $coupon_price += $v['coupon_price'];
  334. }
  335. //汇总优惠卷额度
  336. $order_info['coupon_price'] = $coupon_price;
  337. return $order_info;
  338. }
  339. catch (\Exception $e) {
  340. self::setError($e->getMessage());
  341. return false;
  342. }
  343. }
  344. /**
  345. * 取消订单
  346. * @param $params
  347. * @return false|void
  348. */
  349. public static function cancelOrder($params)
  350. {
  351. Db::startTrans();
  352. try {
  353. $work_id = \app\common\model\recharge\RechargeOrder::where([
  354. 'order_type' => 0,
  355. 'user_id' => $params['user_id'],
  356. 'sn'=>$params['sn']
  357. ])->value('work_id');
  358. if(empty($work_id)){
  359. throw new Exception('订单不存在');
  360. }
  361. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  362. if(!$payed_order->isEmpty()){
  363. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  364. }
  365. //软删除订单
  366. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  367. $cancel_order->pay_status = 2;
  368. $cancel_order->save();
  369. //更新工单状态为已取消
  370. $service_work = ServiceWork::find($work_id);
  371. $service_work->service_status = 4;
  372. $service_work->save();
  373. //判断如果存在优惠券,返还优惠券
  374. if(!empty($payed_order->coupon_id)){
  375. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  376. $user_coupon->voucher_status = 0;
  377. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  378. $user_coupon->save();
  379. }
  380. Db::commit();
  381. }
  382. catch (\Exception $e) {
  383. self::setError($e->getMessage());
  384. return false;
  385. }
  386. }
  387. /**
  388. * 用户确认尾款报价单
  389. * @param $params
  390. * @return false|void
  391. */
  392. public static function confirmOrder($params)
  393. {
  394. Db::startTrans();
  395. try {
  396. $work_id = \app\common\model\recharge\RechargeOrder::where([
  397. 'order_type' => 0,
  398. 'user_id' => $params['user_id'],
  399. 'sn'=>$params['sn']
  400. ])->value('work_id');
  401. if(empty($work_id)){
  402. throw new Exception('订单不存在');
  403. }
  404. //更新工单状态为已取消
  405. $service_work = ServiceWork::find($work_id);
  406. if($service_work->user_confirm_status==2){
  407. throw new Exception('请勿重复操作');
  408. }
  409. $service_work->work_status = 5;
  410. $service_work->user_confirm_status = 2;
  411. $service_work->save();
  412. $work_log = [
  413. 'work_id'=>$work_id,
  414. 'master_worker_id'=>$service_work->master_worker_id,
  415. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  416. ];
  417. ServiceWorkLogLogic::add($work_log);
  418. Db::commit();
  419. }
  420. catch (\Exception $e) {
  421. self::setError($e->getMessage());
  422. return false;
  423. }
  424. }
  425. /**
  426. * 用户确认服务完成
  427. * @param $params
  428. * @return false|void
  429. */
  430. public static function confirmServiceFinish($params)
  431. {
  432. Db::startTrans();
  433. try {
  434. $work_id = \app\common\model\recharge\RechargeOrder::where([
  435. 'order_type' => 0,
  436. 'user_id' => $params['user_id'],
  437. 'sn'=>$params['sn']
  438. ])->value('work_id');
  439. if(empty($work_id)){
  440. throw new Exception('订单不存在');
  441. }
  442. $service_work = ServiceWork::find($work_id);
  443. if($service_work->user_confirm_status!=3){
  444. throw new Exception('请勿重复操作');
  445. }
  446. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  447. //确认所有订单总金额和结算金额
  448. //若订单是全款已支付订单
  449. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  450. $service_work->work_status = 7;
  451. $service_work->user_confirm_status = 5;
  452. $service_work->service_status = 3;
  453. $service_work->work_pay_status = 1;
  454. }else{
  455. $service_work->work_status = 6;
  456. $service_work->user_confirm_status = 4;
  457. }
  458. $service_work->finished_time = time();
  459. $service_work->save();
  460. //更新师傅的进行工单数量
  461. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  462. $work_log = [
  463. 'work_id'=>$work_id,
  464. 'master_worker_id'=>$service_work->master_worker_id,
  465. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  466. ];
  467. ServiceWorkLogLogic::add($work_log);
  468. Db::commit();
  469. }
  470. catch (\Exception $e) {
  471. self::setError($e->getMessage());
  472. return false;
  473. }
  474. }
  475. public static function firmOrderSave($params)
  476. {
  477. Db::startTrans();
  478. try {
  479. $goodIds = array_column($params['order_goods'],'id');
  480. $goodInfo = Goods::whereIn('id',$goodIds)->select();
  481. $ids = $goodInfo->column('id');
  482. if(!empty(array_diff($goodIds,$ids))){
  483. throw new Exception('产品不存在!');
  484. }
  485. if(empty($params['user_info']['mobile'])){
  486. throw new Exception('请先补充您的联系方式后在提交订单');
  487. }
  488. $order_amount = 0;
  489. $order_total = 0;
  490. $orderGoodsData = [];
  491. $goodsPaymentTypeArr = [];
  492. //计算订单总金额和结算金额
  493. foreach($params['order_goods'] as $val){
  494. $goods = $goodInfo->where('id',$val['id'])->toArray()[0];
  495. //根据服务工单计算当前订单应支付金额
  496. $order_total = $goods['service_total'];
  497. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  498. //一口价订单
  499. $order_amount = $goods['service_fee']*$val['goods_number'];
  500. }else{
  501. $order_total = $goods['base_service_fee']*$val['goods_number'];
  502. $order_amount = $goods['service_fee']*$val['goods_number'];
  503. }
  504. $goodsPaymentTypeArr[] = $goods['goods_payment_type'];
  505. $orderGoodsData[] = [
  506. 'goods_id' => $goods['id'],
  507. 'category_type' => $goods['category_type'],
  508. 'goods_category_ids' => $goods['goods_category_ids'],
  509. 'goods_category_id' => $goods['goods_category_id'],
  510. 'goods_name' => $goods['goods_name'],
  511. 'goods_image' => $goods['goods_image'],
  512. 'goods_video' => $goods['goods_video'],
  513. 'goods_number' => $val['goods_number'],
  514. 'good_unit' => $goods['good_unit'],
  515. 'goods_size' => $goods['goods_size'],
  516. 'goods_type' => $goods['goods_type'],
  517. 'goods_brand' => $goods['goods_brand'],
  518. 'install_guide' => $goods['install_guide'],
  519. 'goods_payment_type'=>$goods['goods_payment_type'],
  520. 'base_service_fee' => $goods['base_service_fee'],
  521. 'service_total' => $goods['service_total'],
  522. 'service_fee' => $goods['service_fee'],
  523. 'service_image' => $goods['service_image'],
  524. 'warranty_period'=>$goods['warranty_period'],
  525. 'fee_schedule' => $goods['fee_schedule'],
  526. 'goods_status' => $goods['goods_status'],
  527. ];
  528. }
  529. if(count(array_unique($goodsPaymentTypeArr))>1){
  530. throw new Exception('订单中存在多种支付方式');
  531. }
  532. //生成服务工单
  533. $work_data = [
  534. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  535. 'real_name' => $params['contact_people'],
  536. 'mobile' => $params['contact_number'],
  537. 'address' => $params['address'],
  538. 'title' => $goods['goods_name'] . '*' .$val['goods_number'].$goods['good_unit'],
  539. 'category_type' => $goods['category_type'],
  540. 'goods_category_ids' => $goods['goods_category_ids'],
  541. 'goods_category_id' => $goods['goods_category_id'],
  542. 'base_service_fee' => $goods['base_service_fee'],
  543. 'service_fee' => $goods['service_fee'],
  544. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  545. 'appointment_time' => strtotime($params['appointment_time']),
  546. 'user_id' => $params['user_id'],
  547. 'work_type' => 1,
  548. ];
  549. $service_work = ServiceWork::create($work_data);
  550. //生成服务订单
  551. $data = [
  552. 'work_id'=> $service_work['id'],
  553. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  554. 'order_type'=>0,//服务订单
  555. 'order_terminal' => $params['terminal'],
  556. 'payment_type'=>$goods['goods_payment_type']==1?1:0,
  557. 'user_id' => $params['user_id'],
  558. 'pay_status' => PayEnum::UNPAID,
  559. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  560. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  561. 'pay_way' => $params['pay_way'],
  562. 'order_total' => $order_total,
  563. 'order_amount' => $order_amount,
  564. ];
  565. $order = RechargeOrder::create($data);
  566. array_walk($orderGoodsData, function (&$value, $key, $data) {
  567. $value = array_merge($value, ['sn' => $data['sn']]);
  568. },$data);
  569. dd($orderGoodsData);
  570. $orderGoodsModel = new OrderGoods();
  571. $orderGoodsModel->saveAll($orderGoodsData);
  572. Db::commit();
  573. return [
  574. 'order_id' => (int)$order['id'],
  575. ];
  576. } catch (\Exception $e) {
  577. Db::rollback();
  578. self::setError($e->getMessage());
  579. return false;
  580. }
  581. }
  582. }