ServiceOrderLogic.php 18 KB

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