ServiceOrderLogic.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. $order = \app\common\model\recharge\RechargeOrder::where('sn',$order['sn'])->findOrEmpty();
  197. }
  198. if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
  199. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  200. ->where('voucher_count','>',0)
  201. ->where('expire_time','>=',time())
  202. ->where('begin_use','<',time())
  203. ->findOrEmpty();
  204. if($user_coupon->isEmpty()){
  205. throw new Exception('该优惠券无法使用');
  206. }
  207. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order['order_amount']<$user_coupon['amount_require']){
  208. throw new Exception('该优惠劵不满足满减使用条件');
  209. }
  210. //优惠券折扣
  211. if($user_coupon['mold_type'] == 1){
  212. //按比例折扣
  213. if($user_coupon['discount_ratio']>=1){
  214. throw new Exception('优惠券有误,请联系客服');
  215. }
  216. $order_coupon_amount = intval($order['order_amount']*(1-$user_coupon['discount_ratio']));
  217. }else{
  218. $order_coupon_amount = $user_coupon['amount'];
  219. }
  220. if(!empty($user_coupon['max_deductible_price'])){
  221. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order['order_amount']-$user_coupon['max_deductible_price']):($order['order_amount']-$order_coupon_amount);
  222. }else{
  223. $order_amount = $order['order_amount']-$order_coupon_amount;
  224. }
  225. $user_coupon->voucher_status = 1;
  226. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  227. $user_coupon->save();
  228. }
  229. $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
  230. $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
  231. $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
  232. $order->save();
  233. Db::commit();
  234. } catch (\Exception $e) {
  235. self::setError($e->getMessage());
  236. return false;
  237. }
  238. }
  239. /**
  240. * 获取订单工程师信息
  241. * * @param $params
  242. * * @return array|false
  243. */
  244. public static function getMasterWorker($params)
  245. {
  246. try {
  247. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  248. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  249. },'service_work'=>function ($query) {
  250. $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']);
  251. }])
  252. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  253. ->where([
  254. 'order_type' => 0,
  255. 'user_id' => $params['user_id'],
  256. 'sn'=>$params['sn']
  257. ])->findOrEmpty()->toArray();
  258. $data = [];
  259. //获取师傅参数
  260. if(!empty($order_info['service_work']['master_worker_id'])){
  261. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  262. $data['avatar'] = $worker['avatar'];
  263. $data['real_name'] = $worker['real_name'];
  264. $data['worker_number'] = $worker['worker_number'];
  265. $data['mobile'] = $worker['mobile'];
  266. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  267. $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  268. $data['appointment_time'] = $order_info['service_work']['appointment_time'];
  269. }
  270. return $data;
  271. }
  272. catch (\Exception $e) {
  273. self::setError($e->getMessage());
  274. return false;
  275. }
  276. }
  277. /**
  278. * 获取订单详情
  279. * @param $params
  280. * @return array|false
  281. */
  282. public static function detail($params)
  283. {
  284. try {
  285. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  286. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  287. },'service_work'=>function ($query) {
  288. $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']);
  289. }])
  290. ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
  291. ->where([
  292. 'order_type' => 0,
  293. 'user_id' => $params['user_id'],
  294. 'sn'=>$params['sn']
  295. ])->findOrEmpty()->toArray();
  296. $order_info['master_worker'] = [
  297. 'avatar' => '',
  298. 'real_name'=>'',
  299. 'worker_number'=>'',
  300. 'mobile'=>'',
  301. 'worker_exp'=>''
  302. ];
  303. if(empty($order_info)){
  304. throw new Exception('订单不存在');
  305. }
  306. //查询总价
  307. $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');
  308. $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');
  309. //获取师傅参数
  310. if(!empty($order_info['service_work']['master_worker_id'])){
  311. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  312. $order_info['master_worker']['avatar'] = $worker['avatar'];
  313. $order_info['master_worker']['real_name'] = $worker['real_name'];
  314. $order_info['master_worker']['worker_number'] = $worker['worker_number'];
  315. $order_info['master_worker']['mobile'] = $worker['mobile'];
  316. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  317. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  318. }
  319. //搜索当前工单下的所有订单记录
  320. $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();
  321. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  322. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  323. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  324. $coupon_price = 0;
  325. foreach ($order_info['pay_orders'] as $k=>&$v){
  326. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  327. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  328. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  329. $coupon_price += $v['coupon_price'];
  330. }
  331. //汇总优惠卷额度
  332. $order_info['coupon_price'] = $coupon_price;
  333. return $order_info;
  334. }
  335. catch (\Exception $e) {
  336. self::setError($e->getMessage());
  337. return false;
  338. }
  339. }
  340. /**
  341. * 取消订单
  342. * @param $params
  343. * @return false|void
  344. */
  345. public static function cancelOrder($params)
  346. {
  347. Db::startTrans();
  348. try {
  349. $work_id = \app\common\model\recharge\RechargeOrder::where([
  350. 'order_type' => 0,
  351. 'user_id' => $params['user_id'],
  352. 'sn'=>$params['sn']
  353. ])->value('work_id');
  354. if(empty($work_id)){
  355. throw new Exception('订单不存在');
  356. }
  357. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  358. if(!$payed_order->isEmpty()){
  359. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  360. }
  361. //软删除订单
  362. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  363. $cancel_order->pay_status = 2;
  364. $cancel_order->save();
  365. //更新工单状态为已取消
  366. $service_work = ServiceWork::find($work_id);
  367. $service_work->service_status = 4;
  368. $service_work->save();
  369. //判断如果存在优惠券,返还优惠券
  370. if(!empty($payed_order->coupon_id)){
  371. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  372. $user_coupon->voucher_status = 0;
  373. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  374. $user_coupon->save();
  375. }
  376. Db::commit();
  377. }
  378. catch (\Exception $e) {
  379. self::setError($e->getMessage());
  380. return false;
  381. }
  382. }
  383. /**
  384. * 用户确认尾款报价单
  385. * @param $params
  386. * @return false|void
  387. */
  388. public static function confirmOrder($params)
  389. {
  390. Db::startTrans();
  391. try {
  392. $work_id = \app\common\model\recharge\RechargeOrder::where([
  393. 'order_type' => 0,
  394. 'user_id' => $params['user_id'],
  395. 'sn'=>$params['sn']
  396. ])->value('work_id');
  397. if(empty($work_id)){
  398. throw new Exception('订单不存在');
  399. }
  400. //更新工单状态为已取消
  401. $service_work = ServiceWork::find($work_id);
  402. if($service_work->user_confirm_status==2){
  403. throw new Exception('请勿重复操作');
  404. }
  405. $service_work->work_status = 5;
  406. $service_work->user_confirm_status = 2;
  407. $service_work->save();
  408. $work_log = [
  409. 'work_id'=>$work_id,
  410. 'master_worker_id'=>$service_work->master_worker_id,
  411. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  412. ];
  413. ServiceWorkLogLogic::add($work_log);
  414. Db::commit();
  415. }
  416. catch (\Exception $e) {
  417. self::setError($e->getMessage());
  418. return false;
  419. }
  420. }
  421. /**
  422. * 用户确认服务完成
  423. * @param $params
  424. * @return false|void
  425. */
  426. public static function confirmServiceFinish($params)
  427. {
  428. Db::startTrans();
  429. try {
  430. $work_id = \app\common\model\recharge\RechargeOrder::where([
  431. 'order_type' => 0,
  432. 'user_id' => $params['user_id'],
  433. 'sn'=>$params['sn']
  434. ])->value('work_id');
  435. if(empty($work_id)){
  436. throw new Exception('订单不存在');
  437. }
  438. $service_work = ServiceWork::find($work_id);
  439. if($service_work->user_confirm_status!=3){
  440. throw new Exception('请勿重复操作');
  441. }
  442. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  443. //确认所有订单总金额和结算金额
  444. //若订单是全款已支付订单
  445. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  446. $service_work->work_status = 7;
  447. $service_work->user_confirm_status = 5;
  448. $service_work->service_status = 3;
  449. $service_work->work_pay_status = 1;
  450. $service_work->work_total = $orders[0]['order_total'];
  451. $service_work->work_amount = $orders[0]['order_amount'];
  452. }else{
  453. $service_work->work_status = 6;
  454. $service_work->user_confirm_status = 4;
  455. $order_total = 0;
  456. foreach ($orders as $k=>$v){
  457. $order_total += $v['order_total'];
  458. }
  459. $service_work->work_total = $order_total;
  460. }
  461. $service_work->finished_time = time();
  462. $service_work->save();
  463. //更新师傅的进行工单数量
  464. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  465. $work_log = [
  466. 'work_id'=>$work_id,
  467. 'master_worker_id'=>$service_work->master_worker_id,
  468. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  469. ];
  470. ServiceWorkLogLogic::add($work_log);
  471. Db::commit();
  472. }
  473. catch (\Exception $e) {
  474. self::setError($e->getMessage());
  475. return false;
  476. }
  477. }
  478. public static function firmOrderSave($params)
  479. {
  480. Db::startTrans();
  481. try {
  482. $goodIds = array_column($params['order_goods'],'id');
  483. $goodInfo = Goods::whereIn('id',$goodIds)->select();
  484. $ids = $goodInfo->column('id');
  485. if(!empty(array_diff($goodIds,$ids))){
  486. throw new Exception('产品不存在!');
  487. }
  488. if(empty($params['user_info']['mobile'])){
  489. throw new Exception('请先补充您的联系方式后在提交订单');
  490. }
  491. $order_amount = 0;
  492. $order_total = 0;
  493. $orderGoodsData = [];
  494. $goodsPaymentTypeArr = [];
  495. //计算订单总金额和结算金额
  496. foreach($params['order_goods'] as $val){
  497. $goods = $goodInfo->where('id',$val['id'])->toArray()[0];
  498. //根据服务工单计算当前订单应支付金额
  499. $order_total = $goods['service_total'];
  500. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  501. //一口价订单
  502. $order_amount = $goods['service_fee']*$val['goods_number'];
  503. }else{
  504. $order_total = $goods['base_service_fee']*$val['goods_number'];
  505. $order_amount = $goods['service_fee']*$val['goods_number'];
  506. }
  507. $goodsPaymentTypeArr[] = $goods['goods_payment_type'];
  508. $orderGoodsData[] = [
  509. 'goods_id' => $goods['id'],
  510. 'category_type' => $goods['category_type'],
  511. 'goods_category_ids' => $goods['goods_category_ids'],
  512. 'goods_category_id' => $goods['goods_category_id'],
  513. 'goods_name' => $goods['goods_name'],
  514. 'goods_image' => $goods['goods_image'],
  515. 'goods_video' => $goods['goods_video'],
  516. 'goods_number' => $val['goods_number'],
  517. 'good_unit' => $goods['good_unit'],
  518. 'goods_size' => $goods['goods_size'],
  519. 'goods_type' => $goods['goods_type'],
  520. 'goods_brand' => $goods['goods_brand'],
  521. 'install_guide' => $goods['install_guide'],
  522. 'goods_payment_type'=>$goods['goods_payment_type'],
  523. 'base_service_fee' => $goods['base_service_fee'],
  524. 'service_total' => $goods['service_total'],
  525. 'service_fee' => $goods['service_fee'],
  526. 'service_image' => $goods['service_image'],
  527. 'warranty_period'=>$goods['warranty_period'],
  528. 'fee_schedule' => $goods['fee_schedule'],
  529. 'goods_status' => $goods['goods_status'],
  530. ];
  531. }
  532. if(count(array_unique($goodsPaymentTypeArr))>1){
  533. throw new Exception('订单中存在多种支付方式');
  534. }
  535. //生成服务工单
  536. $work_data = [
  537. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  538. 'real_name' => $params['contact_people'],
  539. 'mobile' => $params['contact_number'],
  540. 'address' => $params['address'],
  541. 'title' => $goods['goods_name'] . '*' .$val['goods_number'].$goods['good_unit'],
  542. 'category_type' => $goods['category_type'],
  543. 'goods_category_ids' => $goods['goods_category_ids'],
  544. 'goods_category_id' => $goods['goods_category_id'],
  545. 'base_service_fee' => $goods['base_service_fee'],
  546. 'service_fee' => $goods['service_fee'],
  547. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  548. 'appointment_time' => strtotime($params['appointment_time']),
  549. 'user_id' => $params['user_id'],
  550. 'work_type' => 1,
  551. ];
  552. $service_work = ServiceWork::create($work_data);
  553. //生成服务订单
  554. $data = [
  555. 'work_id'=> $service_work['id'],
  556. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  557. 'order_type'=>0,//服务订单
  558. 'order_terminal' => $params['terminal'],
  559. 'payment_type'=>$goods['goods_payment_type']==1?1:0,
  560. 'user_id' => $params['user_id'],
  561. 'pay_status' => PayEnum::UNPAID,
  562. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  563. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  564. 'pay_way' => $params['pay_way'],
  565. 'order_total' => $order_total,
  566. 'order_amount' => $order_amount,
  567. ];
  568. $order = RechargeOrder::create($data);
  569. array_walk($orderGoodsData, function (&$value, $key, $data) {
  570. $value = array_merge($value, ['sn' => $data['sn']]);
  571. },$data);
  572. dd($orderGoodsData);
  573. $orderGoodsModel = new OrderGoods();
  574. $orderGoodsModel->saveAll($orderGoodsData);
  575. Db::commit();
  576. return [
  577. 'order_id' => (int)$order['id'],
  578. ];
  579. } catch (\Exception $e) {
  580. Db::rollback();
  581. self::setError($e->getMessage());
  582. return false;
  583. }
  584. }
  585. }