ServiceOrderLogic.php 17 KB

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