ServiceOrderLogic.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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(['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($user_coupon->isEmpty()){
  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. dd($order_coupon_amount);
  78. }else{
  79. $order_coupon_amount = $user_coupon['amount'];
  80. }
  81. if(!empty($user_coupon['max_deductible_price'])){
  82. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
  83. }else{
  84. $order_amount = $order_amount-$order_coupon_amount;
  85. }
  86. }
  87. //生成服务工单
  88. $work_data = [
  89. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  90. 'real_name' => $params['contact_people'],
  91. 'mobile' => $params['contact_number'],
  92. 'address' => $params['address'],
  93. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  94. 'category_type' => $goods['category_type'],
  95. 'goods_category_ids' => $goods['goods_category_ids'],
  96. 'goods_category_id' => $goods['goods_category_id'],
  97. 'base_service_fee' => $goods['base_service_fee'],
  98. 'service_fee' => $goods['service_fee'],
  99. 'work_pay_status'=>$work_pay_status,
  100. 'appointment_time' => strtotime($params['appointment_time']),
  101. 'user_id'=>$params['user_id']
  102. ];
  103. $service_work = ServiceWork::create($work_data);
  104. //生成服务订单
  105. $data = [
  106. 'work_id'=> $service_work['id'],
  107. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  108. 'order_type'=>0,//服务订单
  109. 'order_terminal' => $params['terminal'],
  110. 'payment_type'=>$goods['goods_payment_type']==1?1:0,
  111. 'user_id' => $params['user_id'],
  112. 'pay_status' => $pay_status,
  113. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  114. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  115. 'pay_way' => $params['pay_way'],
  116. 'order_total' => $order_total,
  117. 'order_amount' => $order_amount,
  118. ];
  119. $order = RechargeOrder::create($data);
  120. //生成订单服务详情
  121. OrderGoods::create([
  122. 'sn' => $order['sn'],
  123. 'goods_id' => $params['goods_id'],
  124. 'category_type' => $goods['category_type'],
  125. 'goods_category_ids' => $goods['goods_category_ids'],
  126. 'goods_category_id' => $goods['goods_category_id'],
  127. 'goods_name' => $goods['goods_name'],
  128. 'goods_image' => $goods['goods_image'],
  129. 'goods_video' => $goods['goods_video'],
  130. 'goods_number' => $goods['goods_number'],
  131. 'good_unit' => $goods['good_unit'],
  132. 'goods_size' => $goods['goods_size'],
  133. 'goods_type' => $goods['goods_type'],
  134. 'goods_brand' => $goods['goods_brand'],
  135. 'install_guide' => $goods['install_guide'],
  136. 'goods_payment_type'=>$goods['goods_payment_type'],
  137. 'base_service_fee' => $goods['base_service_fee'],
  138. 'service_total' => $goods['service_total'],
  139. 'service_fee' => $goods['service_fee'],
  140. 'service_image' => $goods['service_image'],
  141. 'warranty_period'=>$goods['warranty_period'],
  142. 'fee_schedule' => $goods['fee_schedule'],
  143. 'goods_status' => $goods['goods_status'],
  144. ]);
  145. Db::commit();
  146. } catch (\Exception $e) {
  147. self::setError($e->getMessage());
  148. return false;
  149. }
  150. return [
  151. 'order_id' => (int)$order['id'],
  152. ];
  153. }
  154. /**
  155. * 获取订单工程师信息
  156. * * @param $params
  157. * * @return array|false
  158. */
  159. public static function getMasterWorker($params)
  160. {
  161. try {
  162. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  163. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  164. },'service_work'=>function ($query) {
  165. $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']);
  166. }])
  167. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  168. ->where([
  169. 'order_type' => 0,
  170. 'user_id' => $params['user_id'],
  171. 'sn'=>$params['sn']
  172. ])->findOrEmpty()->toArray();
  173. $data = [];
  174. //获取师傅参数
  175. if(!empty($order_info['service_work']['master_worker_id'])){
  176. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  177. $data['avatar'] = $worker['avatar'];
  178. $data['real_name'] = $worker['real_name'];
  179. $data['worker_number'] = $worker['worker_number'];
  180. $data['mobile'] = $worker['mobile'];
  181. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  182. $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  183. $data['appointment_time'] = $order_info['service_work']['appointment_time'];
  184. }
  185. return $data;
  186. }
  187. catch (\Exception $e) {
  188. self::setError($e->getMessage());
  189. return false;
  190. }
  191. }
  192. /**
  193. * 获取订单详情
  194. * @param $params
  195. * @return array|false
  196. */
  197. public static function detail($params)
  198. {
  199. try {
  200. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  201. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  202. },'service_work'=>function ($query) {
  203. $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']);
  204. }])
  205. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  206. ->where([
  207. 'order_type' => 0,
  208. 'user_id' => $params['user_id'],
  209. 'sn'=>$params['sn']
  210. ])->findOrEmpty()->toArray();
  211. $order_info['master_worker'] = [
  212. 'avatar' => '',
  213. 'real_name'=>'',
  214. 'worker_number'=>'',
  215. 'mobile'=>'',
  216. 'worker_exp'=>''
  217. ];
  218. if(empty($order_info)){
  219. throw new Exception('订单不存在');
  220. }
  221. //获取师傅参数
  222. if(!empty($order_info['service_work']['master_worker_id'])){
  223. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  224. $order_info['master_worker']['avatar'] = $worker['avatar'];
  225. $order_info['master_worker']['real_name'] = $worker['real_name'];
  226. $order_info['master_worker']['worker_number'] = $worker['worker_number'];
  227. $order_info['master_worker']['mobile'] = $worker['mobile'];
  228. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  229. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  230. }
  231. //搜索当前工单下的所有订单记录
  232. $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();
  233. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  234. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  235. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  236. $coupon_price = 0;
  237. foreach ($order_info['pay_orders'] as $k=>&$v){
  238. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  239. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  240. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  241. $coupon_price += $v['coupon_price'];
  242. }
  243. //汇总优惠卷额度
  244. $order_info['coupon_price'] = $coupon_price;
  245. return $order_info;
  246. }
  247. catch (\Exception $e) {
  248. self::setError($e->getMessage());
  249. return false;
  250. }
  251. }
  252. /**
  253. * 取消订单
  254. * @param $params
  255. * @return false|void
  256. */
  257. public static function cancelOrder($params)
  258. {
  259. Db::startTrans();
  260. try {
  261. $work_id = \app\common\model\recharge\RechargeOrder::where([
  262. 'order_type' => 0,
  263. 'user_id' => $params['user_id'],
  264. 'sn'=>$params['sn']
  265. ])->value('work_id');
  266. if(empty($work_id)){
  267. throw new Exception('订单不存在');
  268. }
  269. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  270. if(!$payed_order->isEmpty()){
  271. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  272. }
  273. //软删除订单
  274. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  275. $cancel_order->delete();
  276. //更新工单状态为已取消
  277. $service_work = ServiceWork::find($work_id);
  278. $service_work->service_status = 4;
  279. $service_work->save();
  280. Db::commit();
  281. }
  282. catch (\Exception $e) {
  283. self::setError($e->getMessage());
  284. return false;
  285. }
  286. }
  287. /**
  288. * 用户确认尾款报价单
  289. * @param $params
  290. * @return false|void
  291. */
  292. public static function confirmOrder($params)
  293. {
  294. Db::startTrans();
  295. try {
  296. $work_id = \app\common\model\recharge\RechargeOrder::where([
  297. 'order_type' => 0,
  298. 'user_id' => $params['user_id'],
  299. 'sn'=>$params['sn']
  300. ])->value('work_id');
  301. if(empty($work_id)){
  302. throw new Exception('订单不存在');
  303. }
  304. //更新工单状态为已取消
  305. $service_work = ServiceWork::find($work_id);
  306. if($service_work->user_confirm_status==2){
  307. throw new Exception('请勿重复操作');
  308. }
  309. $service_work->work_status = 5;
  310. $service_work->user_confirm_status = 2;
  311. $service_work->save();
  312. $work_log = [
  313. 'work_id'=>$work_id,
  314. 'master_worker_id'=>$service_work->master_worker_id,
  315. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  316. ];
  317. ServiceWorkLogLogic::add($work_log);
  318. Db::commit();
  319. }
  320. catch (\Exception $e) {
  321. self::setError($e->getMessage());
  322. return false;
  323. }
  324. }
  325. /**
  326. * 用户确认服务完成
  327. * @param $params
  328. * @return false|void
  329. */
  330. public static function confirmServiceFinish($params)
  331. {
  332. Db::startTrans();
  333. try {
  334. $work_id = \app\common\model\recharge\RechargeOrder::where([
  335. 'order_type' => 0,
  336. 'user_id' => $params['user_id'],
  337. 'sn'=>$params['sn']
  338. ])->value('work_id');
  339. if(empty($work_id)){
  340. throw new Exception('订单不存在');
  341. }
  342. //更新工单状态为已取消
  343. $service_work = ServiceWork::find($work_id);
  344. if($service_work->user_confirm_status!=3){
  345. throw new Exception('请勿重复操作');
  346. }
  347. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  348. //确认所有订单总金额和结算金额
  349. //若订单是全款已支付订单
  350. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  351. $service_work->work_status = 7;
  352. $service_work->user_confirm_status = 5;
  353. $service_work->service_status = 3;
  354. $service_work->work_pay_status = 1;
  355. $service_work->work_total = $orders[0]['order_total'];
  356. $service_work->work_amount = $orders[0]['order_amount'];
  357. }else{
  358. $service_work->work_status = 6;
  359. $service_work->user_confirm_status = 4;
  360. $order_total = 0;
  361. foreach ($orders as $k=>$v){
  362. $order_total += $v['order_total'];
  363. }
  364. $service_work->work_total = $order_total;
  365. }
  366. $service_work->save();
  367. $work_log = [
  368. 'work_id'=>$work_id,
  369. 'master_worker_id'=>$service_work->master_worker_id,
  370. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  371. ];
  372. ServiceWorkLogLogic::add($work_log);
  373. Db::commit();
  374. }
  375. catch (\Exception $e) {
  376. self::setError($e->getMessage());
  377. return false;
  378. }
  379. }
  380. }