ServiceOrderLogic.php 22 KB

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