1
0

ThirdOrderLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\{enum\GoodsEnum,
  4. enum\PayEnum,
  5. enum\WorkEnum,
  6. logic\BaseLogic,
  7. model\goods\Goods,
  8. model\orders\RechargeOrder,
  9. model\recharge\OrderGoods,
  10. model\setting\PostageRegion,
  11. model\third\ThirdGoods,
  12. model\third\ThirdOrders,
  13. model\user\User,
  14. model\works\ServiceWork};
  15. use think\facade\Db;
  16. /**
  17. * 第三方平台下单逻辑层
  18. * Class UserLogic
  19. * @package app\shopapi\logic
  20. */
  21. class ThirdOrderLogic extends BaseLogic
  22. {
  23. /**
  24. * 处理订单
  25. * @param array $order
  26. * @return bool
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public static function orderhandle(array $order)
  32. {
  33. $message = json_decode($order['message'], true);
  34. //查询用户是否存在
  35. $userName = $message['userName'];
  36. $mobile = $message['mobile'];
  37. $userAddress = json_decode($message['userAddress']);
  38. $user = User::where(['mobile' =>$mobile])->findOrEmpty();
  39. $product_message = json_decode($message['products'],true);
  40. if($user->isEmpty()){
  41. $user = LoginLogic::register([
  42. 'password'=>'123456',
  43. 'account'=>$userName,
  44. 'mobile'=>$mobile,
  45. 'channel'=>1,
  46. 'user_type'=>0
  47. ]);
  48. }
  49. //查询美团商品和平台商品的对应关系
  50. $productId = $message['productId'];
  51. $product = ThirdGoods::where(['third_type'=>1,'product_id'=>$productId])->findOrEmpty();
  52. //生成第三方订单信息
  53. $orderData = [
  54. 'orderId'=>$message['orderId'],
  55. 'uni_order_id'=>$product_message[0]['uni_order_id'],
  56. 'productId'=>$message['productId'],
  57. 'productName'=>$message['productName'],
  58. 'request_msg'=>json_encode($order,JSON_UNESCAPED_UNICODE)
  59. ];
  60. //判断美团的预约订单是否生成
  61. if(!$product->isEmpty()){
  62. //查询美团预约订单是否生成
  63. $thirdOrder = ThirdOrders::where(['uni_order_id'=>$product_message[0]['uni_order_id']])->findOrEmpty();
  64. if(!$thirdOrder->isEmpty() and $thirdOrder['work_id'] != 0){
  65. //生成第三方订单信息
  66. $orderData['sync_status'] = 1;
  67. $orderData['work_id'] = $thirdOrder['work_id'];
  68. }
  69. }
  70. $thirdOrders = ThirdOrders::where('orderId',$message['orderId'])->findOrEmpty();
  71. //生成未同步预约订单信息
  72. if($thirdOrders->isEmpty()){
  73. $thirdOrders = ThirdOrders::create($orderData);
  74. }
  75. if(!$thirdOrders->isEmpty()){
  76. $orderData['work_id'] = $thirdOrders->work_id;
  77. }
  78. if(empty($orderData['work_id']) and !$product->isEmpty()){
  79. $goods = Goods::findOrEmpty($product['goods_id']);
  80. $pattern = '/(?:省|市)([\w\s]+市)/u';
  81. Db::startTrans();
  82. try {
  83. if (preg_match($pattern, $userAddress->receiver_address, $matches)) {
  84. $city = $matches[1];
  85. }
  86. $area_city = PostageRegion::where(['level'=>2,'name'=>$city])->value('id');
  87. //生成服务工单
  88. $work_data = [
  89. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  90. 'real_name' => $userName,
  91. 'mobile' => $mobile,
  92. 'address' => $userAddress->receiver_address,
  93. 'province' => 0,
  94. 'city' => $area_city??0,
  95. 'area_name' => $area_city??'',
  96. 'title' => $goods->goods_name,
  97. 'category_type' => $goods['category_type'],
  98. 'goods_category_ids' => $goods['goods_category_ids'],
  99. 'goods_category_id' => $goods['goods_category_id'],
  100. 'base_service_fee' => $goods['base_service_fee'],
  101. 'service_fee' => $message['amount'],
  102. 'work_pay_status'=>WorkEnum::IS_PAY_STATUS,
  103. 'appointment_time' => strtotime($message['begintime']),
  104. 'user_id'=>$user['id'],
  105. 'lon'=>!empty($userAddress->longitude)?$userAddress->longitude:0,
  106. 'lat'=>!empty($userAddress->latitude)?$userAddress->latitude:0,
  107. 'property_activity_id'=>0,
  108. 'user_equity_id'=>0,
  109. 'third_type'=>1,
  110. 'work_total'=>$goods['base_service_fee'],
  111. 'work_amount'=>$goods['base_service_fee']
  112. ];
  113. $service_work = ServiceWork::create($work_data);
  114. //生成服务订单
  115. $data = [
  116. 'work_id'=> $service_work['id'],
  117. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  118. 'order_type'=>0,//服务订单
  119. 'order_terminal' => 1,
  120. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  121. 'user_id' => $user['id'],
  122. 'pay_status' => PayEnum::ISPAID,
  123. 'coupon_id'=>0,
  124. 'coupon_price'=>0,
  125. 'pay_way' => 4,
  126. 'order_total' => $message['amount'],
  127. 'order_amount' => $message['amount'],
  128. ];
  129. $order = RechargeOrder::create($data);
  130. //生成订单服务详情
  131. OrderGoods::create([
  132. 'sn' => $order['sn'],
  133. 'goods_id' => $goods['id'],
  134. 'category_type' => $goods['category_type'],
  135. 'goods_category_ids' => $goods['goods_category_ids'],
  136. 'goods_category_id' => $goods['goods_category_id'],
  137. 'goods_name' => $goods['goods_name'],
  138. 'goods_image' => $goods['goods_image'],
  139. 'goods_video' => $goods['goods_video'],
  140. 'goods_number' => $goods['goods_number'],
  141. 'good_unit' => $goods['good_unit'],
  142. 'goods_size' => $goods['goods_size'],
  143. 'goods_type' => $goods['goods_type'],
  144. 'goods_brand' => $goods['goods_brand'],
  145. 'install_guide' => $goods['install_guide'],
  146. 'goods_payment_type'=>$goods['goods_payment_type'],
  147. 'base_service_fee' => $goods['base_service_fee'],
  148. 'service_total' => $goods['service_total'],
  149. 'service_fee' => $message['amount'],
  150. 'service_image' => $goods['service_image'],
  151. 'warranty_period'=>$goods['warranty_period'],
  152. 'fee_schedule' => $goods['fee_schedule'],
  153. 'goods_status' => $goods['goods_status'],
  154. ]);
  155. //判断是否是一口价预支付订单,支付成功后生成尾款订单
  156. $order_goods = OrderGoods::where('sn',$order->sn)->findOrEmpty();
  157. if(!$goods->isEmpty() and $goods->goods_payment_type == 3 and bcsub($goods->base_service_fee,$goods->service_fee,2)>=0){
  158. //新增待支付尾款
  159. $order_weikuan_data = [
  160. 'order_type'=>$order['order_type'],
  161. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  162. 'work_id'=>$order['work_id'],
  163. 'user_id'=>$order['user_id'],
  164. 'payment_type'=>2,
  165. 'order_total'=>bcsub($goods->base_service_fee,$message['amount'],2),
  166. 'order_amount'=>bcsub($goods->base_service_fee,$message['amount'],2),
  167. 'order_terminal'=>$order['order_terminal']
  168. ];
  169. \app\common\model\recharge\RechargeOrder::create($order_weikuan_data);
  170. }
  171. $thirdOrders->work_id = $service_work->id;
  172. $thirdOrders->save();
  173. Db::commit();
  174. } catch (\Exception $e) {
  175. Db::rollback();
  176. self::setError($e->getMessage());
  177. return false;
  178. }
  179. }
  180. return true;
  181. }
  182. //手动添加订单
  183. public static function submitOrders(array $message)
  184. {
  185. //查询用户是否存在
  186. $userName = $message['userName'];
  187. $mobile = $message['mobile'];
  188. $user = User::where(['mobile' =>$mobile])->findOrEmpty();
  189. if($user->isEmpty()){
  190. $user = LoginLogic::register([
  191. 'password'=>'123456',
  192. 'account'=>$userName,
  193. 'mobile'=>$mobile,
  194. 'channel'=>1,
  195. 'user_type'=>0,
  196. ]);
  197. }
  198. $goods = Goods::findOrEmpty($message['goods_id']);
  199. $pattern = '/(?:省|市)([\w\s]+市)/u';
  200. Db::startTrans();
  201. try {
  202. if (preg_match($pattern, $message['userAddress'], $matches)) {
  203. $city = $matches[1];
  204. }
  205. $area_city = PostageRegion::where(['level'=>2,'name'=>$city])->value('id');
  206. //生成服务工单
  207. $work_data = [
  208. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  209. 'real_name' => $userName,
  210. 'mobile' => $mobile,
  211. 'address' => $message['userAddress'],
  212. 'province' => 0,
  213. 'city' => $area_city??0,
  214. 'area_name' => $area_city??'',
  215. 'title' => $goods->goods_name,
  216. 'category_type' => $goods['category_type'],
  217. 'goods_category_ids' => $goods['goods_category_ids'],
  218. 'goods_category_id' => $goods['goods_category_id'],
  219. 'base_service_fee' => $goods['base_service_fee'],
  220. 'service_fee' => $message['amount'],
  221. 'work_pay_status'=>WorkEnum::IS_PAY_STATUS,
  222. 'appointment_time' => strtotime($message['appointment_time']),
  223. 'user_id'=>$user['id'],
  224. 'lon'=>$message['lon'],
  225. 'lat'=>$message['lat'],
  226. 'property_activity_id'=>0,
  227. 'user_equity_id'=>0,
  228. // 'third_type'=>1,
  229. 'work_total'=>$goods['base_service_fee'],
  230. 'work_amount'=>$goods['base_service_fee']
  231. ];
  232. $service_work = ServiceWork::create($work_data);
  233. //生成服务订单
  234. $data = [
  235. 'work_id'=> $service_work['id'],
  236. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  237. 'order_type'=>0,//服务订单
  238. 'order_terminal' => 1,
  239. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  240. 'user_id' => $user['id'],
  241. 'pay_status' => PayEnum::ISPAID,
  242. 'coupon_id'=>0,
  243. 'coupon_price'=>0,
  244. 'pay_way' => 4,
  245. 'order_total' => $message['amount'],
  246. 'order_amount' => $message['amount'],
  247. ];
  248. $order = RechargeOrder::create($data);
  249. //生成订单服务详情
  250. OrderGoods::create([
  251. 'sn' => $order['sn'],
  252. 'goods_id' => $goods['id'],
  253. 'category_type' => $goods['category_type'],
  254. 'goods_category_ids' => $goods['goods_category_ids'],
  255. 'goods_category_id' => $goods['goods_category_id'],
  256. 'goods_name' => $goods['goods_name'],
  257. 'goods_image' => $goods['goods_image'],
  258. 'goods_video' => $goods['goods_video'],
  259. 'goods_number' => $goods['goods_number'],
  260. 'good_unit' => $goods['good_unit'],
  261. 'goods_size' => $goods['goods_size'],
  262. 'goods_type' => $goods['goods_type'],
  263. 'goods_brand' => $goods['goods_brand'],
  264. 'install_guide' => $goods['install_guide'],
  265. 'goods_payment_type'=>$goods['goods_payment_type'],
  266. 'base_service_fee' => $goods['base_service_fee'],
  267. 'service_total' => $goods['service_total'],
  268. 'service_fee' => $message['amount'],
  269. 'service_image' => $goods['service_image'],
  270. 'warranty_period'=>$goods['warranty_period'],
  271. 'fee_schedule' => $goods['fee_schedule'],
  272. 'goods_status' => $goods['goods_status'],
  273. ]);
  274. //判断是否是一口价预支付订单,支付成功后生成尾款订单
  275. if(!$goods->isEmpty() and $goods->goods_payment_type == 3 and bcsub($goods->base_service_fee,$goods->service_fee,2)>=0){
  276. //新增待支付尾款
  277. $order_weikuan_data = [
  278. 'order_type'=>$order['order_type'],
  279. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  280. 'work_id'=>$order['work_id'],
  281. 'user_id'=>$order['user_id'],
  282. 'payment_type'=>2,
  283. 'order_total'=>bcsub($goods->base_service_fee,$message['amount'],2),
  284. 'order_amount'=>bcsub($goods->base_service_fee,$message['amount'],2),
  285. 'order_terminal'=>$order['order_terminal']
  286. ];
  287. \app\common\model\recharge\RechargeOrder::create($order_weikuan_data);
  288. }
  289. Db::commit();
  290. } catch (\Exception $e) {
  291. Db::rollback();
  292. self::setError($e->getMessage());
  293. return false;
  294. }
  295. return true;
  296. }
  297. }