OrderLogic.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LikeShop有特色的全开源社交分销电商系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 商业用途务必购买系统授权,以免引起不必要的法律纠纷
  7. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  8. // | 微信公众号:好象科技
  9. // | 访问官网:http://www.likemarket.net
  10. // | 访问社区:http://bbs.likemarket.net
  11. // | 访问手册:http://doc.likemarket.net
  12. // | 好象科技开发团队 版权所有 拥有最终解释权
  13. // +----------------------------------------------------------------------
  14. // | Author: LikeShopTeam-段誉
  15. // +----------------------------------------------------------------------
  16. namespace app\shopapi\logic\Order;
  17. use app\common\cache\HandleConcurrencyCache;
  18. use app\common\enum\AfterSaleEnum;
  19. use app\common\enum\AfterSaleLogEnum;
  20. use app\common\enum\BargainEnum;
  21. use app\common\enum\CouponEnum;
  22. use app\common\enum\DeliveryEnum;
  23. use app\common\enum\FreeShippingEnum;
  24. use app\common\enum\GoodsEnum;
  25. use app\common\enum\OrderEnum;
  26. use app\common\enum\OrderLogEnum;
  27. use app\common\enum\PayEnum;
  28. use app\common\enum\PresellEnum;
  29. use app\common\enum\TeamEnum;
  30. use app\common\enum\YesNoEnum;
  31. use app\common\logic\CommonPresellLogic;
  32. use app\common\logic\DiscountLogic;
  33. use app\common\model\AddressLibrary;
  34. use app\common\model\AfterSale;
  35. use app\common\model\AfterSaleGoods;
  36. use app\common\model\BargainInitiate;
  37. use app\common\model\Cart;
  38. use app\common\model\CouponList;
  39. use app\common\model\Delivery;
  40. use app\common\model\Express;
  41. use app\common\model\FreeShipping;
  42. use app\common\model\FreeShippingOrder;
  43. use app\common\model\Goods;
  44. use app\common\model\GoodsItem;
  45. use app\common\model\LuckyDrawRecord;
  46. use app\common\model\Order;
  47. use app\common\model\OrderGoods;
  48. use app\common\model\OrderLog;
  49. use app\common\model\PresellGoodsItem;
  50. use app\common\model\Region;
  51. use app\common\model\SeckillGoodsItem;
  52. use app\common\model\SelffetchShop;
  53. use app\common\model\TeamActivity;
  54. use app\common\model\TeamFound;
  55. use app\common\model\TeamGoods;
  56. use app\common\model\TeamGoodsItem;
  57. use app\common\model\TeamJoin;
  58. use app\common\model\User;
  59. use app\common\logic\BaseLogic;
  60. use app\common\model\UserAddress;
  61. use app\common\service\after_sale\AfterSaleService;
  62. use app\common\service\ConfigService;
  63. use app\common\service\FileService;
  64. use app\shopapi\logic\BargainLogic;
  65. use app\shopapi\logic\TeamLogic;
  66. use expressage\Kd100;
  67. use expressage\Kdniao;
  68. use think\Exception;
  69. use think\facade\Db;
  70. use think\facade\Validate;
  71. /**
  72. * 订单逻辑
  73. * Class OrderLogic
  74. * @package app\shopapi\logic
  75. */
  76. class OrderLogic extends BaseLogic
  77. {
  78. /**
  79. * 下单用户
  80. * @var
  81. */
  82. protected static $user;
  83. /**
  84. * 订单类型
  85. * @var int
  86. */
  87. protected static $OrderType = OrderEnum::NORMAL_ORDER;
  88. /**
  89. * 订单数量
  90. * @var int
  91. */
  92. protected static $totalNum = 0;
  93. /**
  94. * 订单金额
  95. * @var array
  96. */
  97. protected static $orderPrice = [
  98. 'total_goods_price' => 0,//订单商品金额
  99. 'express_price' => 0,//运费
  100. 'total_amount' => 0,//订单金额
  101. 'order_amount' => 0,//订单实付金额
  102. 'discount_amount' => 0,//优惠金额
  103. 'member_amount' => 0,//会员折扣价
  104. 'total_goods_original_price' => 0,//订单商品原价总价
  105. ];
  106. /**
  107. * 失效商品列表
  108. * @var array
  109. */
  110. protected static array $disabledGoods = [];
  111. /**
  112. * @notes 当前下单用户
  113. * @param $userId
  114. * @return array
  115. * @author 段誉
  116. * @date 2021/7/23 15:52
  117. */
  118. public static function setOrderUser($userId)
  119. {
  120. self::$user = User::findOrEmpty($userId)->toArray();
  121. return self::$user;
  122. }
  123. /**
  124. * @notes 订单结算详情
  125. * @param $params
  126. * @return array|bool
  127. * @author 段誉
  128. * @date 2021/7/23 15:52
  129. */
  130. public static function settlement($params)
  131. {
  132. try {
  133. //设置订单类型
  134. self::$OrderType = $params['order_type'];
  135. //设置用户信息
  136. $user = self::setOrderUser($params['user_id']);
  137. //设置用户地址
  138. $userAddress = UserAddress::getOneAddress($params['user_id'], $params['address_id'] ?? 0);
  139. //获取商品信息
  140. $goodsLists = self::getOrderGoodsData($params);
  141. //判断是否需要地址
  142. $is_address = 1;
  143. foreach ($goodsLists as $goods) {
  144. //非虚拟商品必填地址
  145. if ($goods['type'] != GoodsEnum::GOODS_VIRTUAL) {
  146. break;
  147. }
  148. $is_address = $goods['is_address'];
  149. }
  150. if ($is_address == 0) {
  151. $userAddress = [];
  152. }
  153. //计算运费(自提订单不需要运费)
  154. if ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) {
  155. self::$orderPrice['express_price'] = 0;
  156. } else {
  157. $express_data = FreightLogic::calculateFreight($goodsLists, $userAddress);
  158. self::$orderPrice['express_price'] = $express_data['express_price'];
  159. $goodsLists = $express_data['goods'];
  160. }
  161. // 普通订单、虚拟订单可使用优惠券
  162. if (!empty($params['coupon_list_id']) && in_array(self::$OrderType, [OrderEnum::NORMAL_ORDER, OrderEnum::VIRTUAL_ORDER])) {
  163. $discountData = OrderCouponLogic::calculateCouponDiscount($goodsLists, $params['coupon_list_id']);
  164. self::$orderPrice['discount_amount'] = $discountData['discount'];
  165. self::$orderPrice['order_amount'] -= $discountData['discount'];
  166. $goodsLists = $discountData['goods'];
  167. }
  168. // 订单金额
  169. self::$orderPrice['total_amount'] += self::$orderPrice['express_price'];
  170. //订单应付金额
  171. self::$orderPrice['order_amount'] += self::$orderPrice['express_price'];
  172. $result = [
  173. 'terminal' => $params['terminal'],
  174. 'delivery_type' => intval($params['delivery_type']),
  175. 'delivery_type_desc' => DeliveryEnum::getDeliveryTypeDesc($params['delivery_type']),
  176. 'cart_id' => $params['cart_id'] ?? [],
  177. 'order_type' => self::$OrderType,
  178. 'coupon_list_id' => intval($params['coupon_list_id'] ?? 0),
  179. 'total_num' => self::$totalNum,
  180. 'total_goods_price' => bcadd(0, self::$orderPrice['total_goods_price'], 2),
  181. 'total_amount' => bcadd(0, self::$orderPrice['total_amount'], 2),
  182. 'order_amount' => bcadd(0, self::$orderPrice['order_amount'], 2),
  183. 'discount_amount' => bcadd(0, self::$orderPrice['discount_amount'], 2),
  184. 'member_amount' => bcadd(0, self::$orderPrice['member_amount'],2),
  185. 'express_price' => bcadd(0, self::$orderPrice['express_price'], 2),
  186. 'total_goods_original_price' => bcadd(0, self::$orderPrice['total_goods_original_price'], 2),
  187. 'user_id' => $user['id'],
  188. 'user_money' => $user['user_money'],
  189. 'user_remark' => $params['user_remark'] ?? '',
  190. 'address' => $userAddress,
  191. 'goods' => $goodsLists,
  192. 'goods_disabled' => static::$disabledGoods,
  193. 'selffetch_shop_id' => $params['selffetch_shop_id'] ?? '',
  194. 'selffetch_info' => [],
  195. 'contact' => $params['contact'] ?? '',
  196. 'mobile' => $params['mobile'] ?? '',
  197. 'is_address' => $is_address,
  198. 'draw_record_id' => $params['draw_record_id'] ?? 0,
  199. ];
  200. //门店自提显示上次提货人信息
  201. if ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) {
  202. $selffetch_info = Order::where(['user_id' => $user['id'], 'delivery_type' => DeliveryEnum::SELF_DELIVERY])
  203. ->field('address,selffetch_shop_id')
  204. ->order('id desc')
  205. ->findOrEmpty()->toArray();
  206. //上一次提货人
  207. if ($selffetch_info) {
  208. $selffetch_field = [
  209. 'id', 'name', 'image', 'contact', 'mobile',
  210. 'province', 'city', 'district', 'longitude', 'address', 'latitude',
  211. 'business_start_time', 'business_end_time', 'weekdays', 'remark',
  212. 'status',
  213. ];
  214. $result['selffetch_info'] = [
  215. 'selffetch_shop_id' => $selffetch_info['selffetch_shop_id'] ?? '',
  216. 'contact' => $selffetch_info['address']->contact ?? '',
  217. 'mobile' => $selffetch_info['address']->mobile ?? '',
  218. 'selffetch_shop' => SelffetchShop::where('status', 1)
  219. ->field($selffetch_field)
  220. ->append([ 'detailed_address' ])
  221. ->find($selffetch_info['selffetch_shop_id'] ?? 0),
  222. ];
  223. }
  224. }
  225. // 营销活动附加参数: 秒杀、拼团、砍价
  226. switch ($params['order_type']) {
  227. // 拼团
  228. case OrderEnum::TEAM_ORDER:
  229. $result['team_id'] = $params['team_id'];
  230. $result['found_id'] = $params['found_id'] ?? null;
  231. $teamFound = (new TeamFound())
  232. ->where(['id' => $result['found_id'], 'user_id' => $params['user_id']])
  233. ->findOrEmpty();
  234. if (!$teamFound->isEmpty()) {
  235. self::$error = '不允许参与自己开的团';
  236. return false;
  237. }
  238. break;
  239. // 秒杀
  240. case OrderEnum::SECKILL_ORDER:
  241. $result['seckill_id'] = $params['seckill_id'];
  242. break;
  243. // 砍价
  244. case OrderEnum::BARGAIN_ORDER:
  245. $result['initiate_id'] = $params['initiate_id'];
  246. break;
  247. // 预售
  248. case OrderEnum::PRESELL_ORDER:
  249. $result['presell_id'] = $params['presell_id'];
  250. break;
  251. // 抽奖
  252. case OrderEnum::DRAW_ORDER:
  253. //订单金额等于运费
  254. $result['order_amount'] = $result['express_price'];
  255. $result['total_amount'] = $result['express_price'];
  256. break;
  257. }
  258. // 判断是否符合包邮活动条件
  259. if (self::isFreeShipping($result)) {
  260. $result['order_amount'] = $result['order_amount'] - $result['express_price'];
  261. $result['total_amount'] = $result['total_amount'] - $result['express_price'];
  262. $result['express_price'] = 0;
  263. // 商品的运费也清0
  264. foreach ($goodsLists as &$goodsList) {
  265. $goodsList['express_price'] = 0;
  266. $goodsList['express_money'] = 0;
  267. }
  268. $result['goods'] = $goodsLists;
  269. }
  270. $result['total_goods_price'] = bcadd($result['total_goods_price'], 0, 2);
  271. $result['total_amount'] = bcadd($result['total_amount'], 0, 2);
  272. $result['order_amount'] = bcadd($result['order_amount'], 0, 2);
  273. $result['discount_amount'] = bcadd($result['discount_amount'], 0, 2);
  274. $result['member_amount'] = bcadd($result['member_amount'], 0, 2);
  275. $result['express_price'] = bcadd($result['express_price'], 0, 2);
  276. $result['total_goods_original_price'] = bcadd($result['total_goods_original_price'], 0, 2);
  277. return $result;
  278. } catch (\Exception $e) {
  279. self::$error = $e->getMessage();
  280. return false;
  281. }
  282. }
  283. /**
  284. * @notes 是否符合包邮活动条件
  285. */
  286. public static function isFreeShipping(&$params) {
  287. $params['is_free_shipping'] = false;
  288. if (empty($params['address'])) {
  289. return false;
  290. }
  291. // 获取当前时间进行中的包邮活动
  292. $activity = FreeShipping::where([
  293. ['start_time', '<=', time()],
  294. ['end_time', '>', time()],
  295. ['status', '=', FreeShippingEnum::ING],
  296. ])->find();
  297. if (!$activity) {
  298. // 没有包邮活动
  299. return false;
  300. }
  301. if ($activity->getData('condition_type') == FreeShippingEnum::BY_AMOUNT) {
  302. // 去除优惠金额
  303. $threshold = $params['total_goods_price'] - $params['discount_amount'];
  304. } else {
  305. $threshold = $params['total_num'];
  306. }
  307. $nationThreshold = 0;
  308. foreach($activity->region as $item) {
  309. if ($item->region_id == "100000") {
  310. // 全国区域留在最后判断
  311. $nationThreshold = $item->threshold;
  312. continue;
  313. }
  314. if (strpos($item->region_id, $params['address']->district_id) !== false) {
  315. if ($threshold >= $item->threshold) {
  316. $params['is_free_shipping'] = true;
  317. $params['free_shipping_id'] = $activity->id;
  318. return true;
  319. } else {
  320. return false;
  321. }
  322. }
  323. if (strpos($item->region_id, $params['address']->city_id) !== false) {
  324. if ($threshold >= $item->threshold) {
  325. $params['is_free_shipping'] = true;
  326. $params['free_shipping_id'] = $activity->id;
  327. return true;
  328. } else {
  329. return false;
  330. }
  331. }
  332. if (strpos($item->region_id, $params['address']->province_id) !== false) {
  333. if ($threshold >= $item->threshold) {
  334. $params['is_free_shipping'] = true;
  335. $params['free_shipping_id'] = $activity->id;
  336. return true;
  337. } else {
  338. return false;
  339. }
  340. }
  341. }
  342. if ($threshold >= $nationThreshold) {
  343. $params['is_free_shipping'] = true;
  344. $params['free_shipping_id'] = $activity->id;
  345. return true;
  346. }
  347. return false;
  348. }
  349. /**
  350. * @notes 提交订单前验证
  351. * @param $params
  352. * @throws \Exception
  353. * @author 段誉
  354. * @date 2021/7/23 15:53
  355. */
  356. public static function submitBeforeCheck($params)
  357. {
  358. // 商品检测
  359. if (empty($params['goods'])) {
  360. throw new \Exception('提交的商品已不能购买,请重新选择商品');
  361. }
  362. //配送方式为快递配送时,检测地址
  363. if (empty($params['address']) && $params['delivery_type'] == DeliveryEnum::EXPRESS_DELIVERY && $params['is_address'] == 1) {
  364. throw new \Exception('请选择收货地址');
  365. }
  366. //配送方式为门店自提时
  367. if ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY && empty($params['selffetch_shop_id'])) {
  368. throw new \Exception('自提门店不能为空');
  369. }
  370. if ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY && !empty($params['selffetch_shop_id'])) {
  371. $selffetch_shop = SelffetchShop::where('id', $params['selffetch_shop_id'])->findOrEmpty();
  372. if ($selffetch_shop->isEmpty()) {
  373. throw new \Exception('自提门店不存在');
  374. }
  375. }
  376. if ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY && empty($params['contact'])) {
  377. throw new \Exception('取货人不能为空');
  378. }
  379. if ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY && empty($params['mobile'])) {
  380. throw new \Exception('联系电话不能为空');
  381. }
  382. if ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY && !Validate::mobile($params['mobile'])) {
  383. throw new \Exception('联系电话格式不正确');
  384. }
  385. // 校验商品数量
  386. $limit_arr = [];
  387. foreach ($params['goods'] as $goods) {
  388. if ($goods['goods_num'] <= 0) {
  389. throw new \Exception('请选择商品' . ($goods['goods_name'] ?? '') . '数量');
  390. }
  391. $limit_arr[$goods['goods_id']]['goods_num'] = ($limit_arr[$goods['goods_id']]['goods_num'] ?? 0) + $goods['goods_num'];
  392. $limit_arr[$goods['goods_id']]['limit_type'] = $goods['limit_type'];
  393. $limit_arr[$goods['goods_id']]['limit_value'] = $goods['limit_value'];
  394. $limit_arr[$goods['goods_id']]['goods_name'] = $goods['goods_name'];
  395. $limit_arr[$goods['goods_id']]['goods_id'] = $goods['goods_id'];
  396. }
  397. //校验限购商品
  398. foreach ($limit_arr as $limit_val) {
  399. if (static::isOutBuyNum($limit_val, $limit_val['goods_num'], $params['user_id'])) {
  400. throw new \Exception('商品:' . ($limit_val['goods_name'] ?? '') . ' 超过限购数量');
  401. }
  402. }
  403. //验证订单商品是否支持对应的配送方式
  404. $is_express = ConfigService::get('delivery_type', 'is_express', 1);
  405. $is_selffetch = ConfigService::get('delivery_type', 'is_selffetch', 0);
  406. $item_ids = implode(',', array_column($params['goods'], 'item_id'));
  407. $goods_ids = implode(',', GoodsItem::where('id', 'in', $item_ids)->column('goods_id'));
  408. $goods = Goods::where('id', 'in', $goods_ids)->select();
  409. $goods_name = [];
  410. //门店自提
  411. if ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) {
  412. if ($is_selffetch == 0) {
  413. throw new \Exception('系统未开启门店自提配送方式');
  414. }
  415. foreach ($goods as $val) {
  416. //虚拟商品不支持门店自提
  417. if (GoodsEnum::GOODS_VIRTUAL == $val['type']) {
  418. throw new \Exception($val['name'] . '不支持门店自提');
  419. }
  420. if ($val['is_selffetch'] == 0) {
  421. $goods_name[] = $val['name'];
  422. }
  423. }
  424. } elseif ($params['delivery_type'] == DeliveryEnum::EXPRESS_DELIVERY) { //快递配送
  425. if ($is_express == 0) {
  426. throw new \Exception('系统未开启快递配送方式');
  427. }
  428. foreach ($goods as $val) {
  429. //实物商品且不支持物流
  430. if ($val['is_express'] == 0 && GoodsEnum::GOODS_REALITY == $val['type']) {
  431. $goods_name[] = $val['name'];
  432. }
  433. }
  434. }
  435. if (!empty($goods_name)) {
  436. throw new \Exception(implode('、', $goods_name) . '不支持' . DeliveryEnum::getDeliveryTypeDesc($params['delivery_type']) . ',请重新选择配送方式');
  437. }
  438. }
  439. /**
  440. * @notes 提交订单
  441. * @param $params
  442. * @return array|bool
  443. * @author 段誉
  444. * @date 2021/7/23 15:53
  445. */
  446. public static function submitOrder($params)
  447. {
  448. Db::startTrans();
  449. try {
  450. //提交前验证
  451. self::submitBeforeCheck($params);
  452. //防重复下单 同一用户10秒内只能提交一次
  453. $HandleConcurrencyCache = new HandleConcurrencyCache();
  454. $submitOrderLimit = $HandleConcurrencyCache->getCache($HandleConcurrencyCache->getSubmitOrderLimitKey(self::$user['id']));
  455. if ($submitOrderLimit && time() - $submitOrderLimit < 10) {
  456. throw new \Exception('您有订单正在处理中,请稍后再试');
  457. } else {
  458. $HandleConcurrencyCache->setCache($HandleConcurrencyCache->getSubmitOrderLimitKey(self::$user['id']), time(), 10);
  459. }
  460. //删除购物车
  461. self::delCartByOrder($params);
  462. //下单扣除库存
  463. self::decStock($params['goods']);
  464. //提交订单
  465. $order = self::addOrder($params, self::$user['id']);
  466. //下单增加商品销量
  467. self::incSale($params['goods']);
  468. //有使用优惠券时更新coupon_list
  469. if ($params['coupon_list_id'] > 0) {
  470. self::handleCouponByOrder($params['coupon_list_id'], $order['id']);
  471. }
  472. // 营销活动下单后的操作
  473. switch ($params['order_type']) {
  474. // 拼团
  475. case OrderEnum::TEAM_ORDER:
  476. self::teamAfter($params, $order);
  477. break;
  478. // 秒杀
  479. case OrderEnum::SECKILL_ORDER:
  480. self::seckillAfter($params, $order);
  481. break;
  482. // 砍价
  483. case OrderEnum::BARGAIN_ORDER:
  484. self::bargainAfter($params, $order);
  485. break;
  486. // 预售
  487. case OrderEnum::PRESELL_ORDER:
  488. self::presellAfter($params, $order);
  489. break;
  490. }
  491. // 判断是否为包邮活动订单
  492. if ($params['is_free_shipping']) {
  493. FreeShippingOrder::create([
  494. 'free_shpping_id' => $params['free_shipping_id'],
  495. 'order_id' => $order['id'],
  496. 'amount' => $order['order_amount']
  497. ]);
  498. }
  499. //订单日志
  500. (new OrderLog())->record([
  501. 'type' => OrderLogEnum::TYPE_USER,
  502. 'channel' => OrderLogEnum::USER_ADD_ORDER,
  503. 'order_id' => $order['id'],
  504. 'operator_id' => self::$user['id'],
  505. ]);
  506. //抽奖记录结算
  507. LuckyDrawRecord::update([
  508. 'id' => $params['draw_record_id'] ?? 0,
  509. 'is_send' => YesNoEnum::YES,
  510. 'send_time' => time(),
  511. ]);
  512. //提交事务
  513. Db::commit();
  514. return ['order_id' => $order['id'], 'type' => 'order'];
  515. } catch (\Exception $e) {
  516. Db::rollback();
  517. self::$error = $e->getMessage();
  518. return false;
  519. }
  520. }
  521. /**
  522. * @notes 更新订单优惠券状态
  523. * @param $coupon_list_id
  524. * @param $order_id
  525. * @throws \think\db\exception\DataNotFoundException
  526. * @throws \think\db\exception\DbException
  527. * @throws \think\db\exception\ModelNotFoundException
  528. * @author ljj
  529. * @date 2021/9/14 11:39 上午
  530. */
  531. public static function handleCouponByOrder($coupon_list_id, $order_id)
  532. {
  533. $coupon_list = CouponList::find($coupon_list_id);
  534. $coupon_list->order_id = $order_id;
  535. $coupon_list->status = CouponEnum::USE_STATUS_OK;
  536. $coupon_list->use_time = time();
  537. $coupon_list->update_time = time();
  538. $coupon_list->save();
  539. }
  540. /**
  541. * @notes 扣减库存(使用悲观锁防止高并发超卖)
  542. * @param $goodLists
  543. * @throws \Exception
  544. * @author 段誉
  545. * @date 2021/8/9 17:47
  546. */
  547. public static function decStock($goodLists)
  548. {
  549. // 按商品ID排序,确保所有事务都按相同顺序更新,防止死锁
  550. $itemIds = array_column($goodLists, 'item_id');
  551. array_multisort($itemIds, SORT_ASC, $goodLists);
  552. foreach ($goodLists as $goods) {
  553. // 使用悲观锁锁定记录
  554. $goodsItem = GoodsItem::where('id', $goods['item_id'])
  555. ->lock(true) // 添加 FOR UPDATE 锁
  556. ->find();
  557. if (!$goodsItem) {
  558. throw new \Exception('商品不存在');
  559. }
  560. if ($goodsItem['stock'] < $goods['goods_num']) {
  561. throw new \Exception('商品库存不足');
  562. }
  563. // 更新库存
  564. $goodsItem->stock = $goodsItem->stock - $goods['goods_num'];
  565. $goodsItem->save();
  566. // 更新商品总库存 - 使用子查询直接更新,避免查询和更新之间的间隙
  567. Goods::where(['id' => $goods['goods_id']])
  568. ->update([
  569. 'total_stock' => Db::raw("(SELECT SUM(stock) FROM nf_goods_item WHERE goods_id = " . $goods['goods_id'] . ")")
  570. ]);
  571. }
  572. }
  573. /**
  574. * @notes 下单增加销量
  575. * @param $goodLists
  576. * @throws \Exception
  577. * @author ljj
  578. * @date 2021/9/22 5:30 下午
  579. */
  580. public static function incSale($goodLists)
  581. {
  582. foreach ($goodLists as $goods) {
  583. Goods::update(['sales_num' => ['inc', $goods['goods_num']]], ['id' => $goods['goods_id']]);
  584. }
  585. }
  586. /**
  587. * @notes 删除购物车(购物车下单情况)
  588. * @param $params
  589. * @author 段誉
  590. * @date 2021/7/23 15:54
  591. */
  592. public static function delCartByOrder($params)
  593. {
  594. if (!empty($params['cart_id'])) {
  595. Cart::where(['id' => $params['cart_id'], 'user_id' => self::$user['id']])->delete();
  596. }
  597. }
  598. /**
  599. * @notes 添加订单
  600. * @param $params
  601. * @param $user_id
  602. * @author 段誉
  603. * @date 2021/8/9 17:42
  604. */
  605. public static function addOrder($params, $user_id)
  606. {
  607. $order = Order::create([
  608. 'sn' => generate_sn((new Order()), 'sn'),
  609. 'order_type' => $params['order_type'],
  610. 'user_id' => $user_id,
  611. 'order_terminal' => $params['terminal'],
  612. 'coupon_list_id' => $params['coupon_list_id'],
  613. 'total_num' => $params['total_num'],
  614. 'total_amount' => $params['total_amount'],
  615. 'goods_price' => $params['total_goods_price'],
  616. 'order_amount' => $params['order_amount'],
  617. 'express_price' => $params['express_price'],
  618. 'discount_amount' => $params['discount_amount'],
  619. 'member_amount' => $params['member_amount'],
  620. 'user_remark' => $params['user_remark'],
  621. 'address' => [
  622. 'contact' => ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? $params['contact'] : ($params['address']['contact'] ?? ''),
  623. 'province' => ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? '' : ($params['address']['province_id'] ?? ''),
  624. 'city' => ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? '' : ($params['address']['city_id'] ?? ''),
  625. 'district' => ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? '' : ($params['address']['district_id'] ?? ''),
  626. 'address' => ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? '' : ($params['address']['address'] ?? ''),
  627. 'mobile' => ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? $params['mobile'] : ($params['address']['mobile'] ?? ''),
  628. ],
  629. 'delivery_type' => OrderEnum::VIRTUAL_ORDER == $params['order_type'] ? DeliveryEnum::DELIVERY_VIRTUAL : $params['delivery_type'],
  630. 'pickup_code' => ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? create_number_sn((new Order()), 'pickup_code', 6) : null,
  631. 'selffetch_shop_id' => ($params['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? $params['selffetch_shop_id'] : 0,
  632. 'draw_record_id' => $params['draw_record_id'] ?? 0,
  633. ]);
  634. $goodsData = [];
  635. foreach ($params['goods'] as $goods) {
  636. //商品实付价格
  637. $totalPayPrice = $goods['sub_price'] - ($goods['discount_price'] ?? 0) + ($goods['express_price'] ?? 0);
  638. $totalPayPrice = $totalPayPrice <= 0 ? 0 : $totalPayPrice;
  639. $goodsData[] = [
  640. 'order_id' => $order['id'],
  641. 'goods_id' => $goods['goods_id'],
  642. 'item_id' => $goods['item_id'],
  643. 'goods_name' => $goods['goods_name'],
  644. 'goods_num' => $goods['goods_num'],
  645. 'goods_price' => $goods['sell_price'],//商品价格单价(未扣减优惠和积分价格)
  646. 'total_price' => $goods['sub_price'],
  647. 'total_pay_price' => $totalPayPrice,//实际支付商品金额(扣除优惠金额、加上运费)
  648. 'spec_value_ids' => $goods['spec_value_ids'],
  649. 'discount_price' => $goods['discount_price'] ?? 0,//优惠券优惠金额
  650. 'original_price' => $goods['original_price'] ?? 0,//商品原始价格
  651. 'member_price' => $goods['member_price'] ?? 0,//商品会员价格
  652. 'goods_snap' => $goods,
  653. 'express_price' => $goods['express_price'] ?? 0,//运费
  654. ];
  655. }
  656. (new OrderGoods())->saveAll($goodsData);
  657. return $order;
  658. }
  659. /**
  660. * @notes 商品信息
  661. * @param $params
  662. * @return array
  663. * @author 段誉
  664. * @date 2021/7/23 15:54
  665. */
  666. public static function getOrderGoodsData($params)
  667. {
  668. // 购物车下单
  669. if ($params['source'] == 'cart') {
  670. $params['goods'] = Cart::where([
  671. ['id', 'in', $params['cart_id']],
  672. ['user_id', '=', $params['user_id']],
  673. ])->field('item_id,goods_id,goods_num')->select()->toArray();
  674. }
  675. // 砍价商品信息提取
  676. if ($params['order_type'] == OrderEnum::BARGAIN_ORDER) {
  677. $params['goods'] = self::initiateGoods($params);
  678. }
  679. $itemIds = array_column($params['goods'], 'item_id');
  680. $field = [
  681. 'gi.id', 'gi.id' => 'item_id', 'gi.image' => 'item_image',
  682. 'gi.spec_value_str', 'spec_value_ids', 'gi.sell_price',
  683. 'gi.volume', 'gi.stock', 'gi.weight', 'gi.bar_code', 'g.id' => 'goods_id',
  684. 'g.name' => 'goods_name', 'g.type', 'g.status', 'g.delete_time', 'g.image',
  685. 'g.express_type', 'g.express_money', 'g.express_template_id',
  686. 'g.is_express', 'g.is_selffetch', 'g.is_express', 'g.is_virtualdelivery',
  687. 'g.after_pay', 'g.after_delivery', 'g.delivery_content', 'g.delivery_type','g.delivery_template_id',
  688. 'g.code', 'g.is_address', 'g.limit_type', 'g.limit_value'
  689. ];
  690. $goodsData = (new Goods())->alias('g')
  691. ->join('goods_item gi', 'gi.goods_id = g.id')
  692. ->with([ 'goods_category_index2', 'delivery_template' ])
  693. ->where('gi.id', 'in', $itemIds)
  694. ->field($field)
  695. ->select()->toArray();
  696. $goodsData = array_column($goodsData, null, 'id');
  697. //处理图片路径
  698. foreach ($goodsData as &$val) {
  699. $val['item_image'] = trim($val['item_image']) ? FileService::getFileUrl($val['item_image']) : '';
  700. }
  701. return self::getOrderGoodsLists($params, $goodsData);
  702. }
  703. /**
  704. * @notes 结算商品列表
  705. * @param $goods
  706. * @param $goodsData
  707. * @return array
  708. * @author 段誉
  709. * @date 2021/7/23 15:55
  710. */
  711. public static function getOrderGoodsLists($params, $goodsData)
  712. {
  713. $goods = $params['goods'];
  714. $goodsIds = array_column($goodsData, 'goods_id');
  715. $levelGoodsItem = DiscountLogic::getGoodsDiscount(self::$user['id'], $goodsIds);
  716. $goodsLists = [];
  717. foreach ($goods as $k => $item) {
  718. //删除没找到商品信息的商品
  719. if (!isset($goodsData[$item['item_id']])) {
  720. unset($goods[$k]);
  721. static::$disabledGoods[] = [
  722. 'msg' => '找不到商品',
  723. 'goods' => $goodsData[$item['item_id']] ?? [],
  724. ];
  725. continue;
  726. }
  727. //组装商品数据
  728. $goodsInfo = $goodsData[$item['item_id']];
  729. $goodsInfo['goods_num'] = intval($item['goods_num'] ?? 0);
  730. //记录原价
  731. $goodsInfo['original_price'] = $goodsInfo['sell_price'];
  732. $goodsInfo['sub_price'] = round($goodsInfo['sell_price'] * $item['goods_num'], 2);
  733. //当前商品是否被删除或下架
  734. if ($goodsInfo['delete_time'] > 0 || $goodsInfo['status'] != 1) {
  735. unset($goods[$k]);
  736. static::$disabledGoods[] = [
  737. 'msg' => '商品不能购买',
  738. 'goods' => $goodsInfo,
  739. ];
  740. continue;
  741. }
  742. //当前库存是否足够
  743. if ($item['goods_num'] > $goodsInfo['stock']) {
  744. unset($goods[$k]);
  745. static::$disabledGoods[] = [
  746. 'msg' => '商品库存不足',
  747. 'goods' => $goodsInfo,
  748. ];
  749. continue;
  750. }
  751. // 是否限购
  752. if (static::isOutBuyNum($goodsInfo, $item['goods_num'], $params['user_id'])) {
  753. unset($goods[$k]);
  754. static::$disabledGoods[] = [
  755. 'msg' => '超过购买限制',
  756. 'goods' => $goodsInfo,
  757. ];
  758. continue;
  759. }
  760. $goodsInfo['member_price'] = 0;
  761. if(in_array(self::$OrderType,[OrderEnum::NORMAL_ORDER,OrderEnum::VIRTUAL_ORDER])){
  762. //会员折扣
  763. $goodsInfo['member_price'] = $levelGoodsItem[$goodsInfo['goods_id']][$goodsInfo['item_id']]['discount_price'] ?? '';
  764. }
  765. // 获取不同订单类型的规格单价
  766. $goodsInfo['sell_price'] = self::getSellPrice($params, $goodsInfo);
  767. $goodsInfo['sub_price'] = round($goodsInfo['sell_price'] * $item['goods_num'], 2);
  768. $goodsInfo['total_original_price'] = round($goodsInfo['original_price'] * $goodsInfo['goods_num'], 2);//商品原价合计
  769. $goodsLists[] = $goodsInfo;
  770. self::$totalNum += $item['goods_num'];
  771. self::$orderPrice['total_goods_price'] += $goodsInfo['sub_price'];
  772. //普通商品,计算折扣金额
  773. if(in_array(self::$OrderType,[OrderEnum::NORMAL_ORDER,OrderEnum::VIRTUAL_ORDER])){
  774. $memberAmount = round(($goodsInfo['original_price'] - $goodsInfo['sell_price']) * $item['goods_num'],2);
  775. self::$orderPrice['member_amount'] += $memberAmount;
  776. }
  777. //订单商品原价总价
  778. self::$orderPrice['total_goods_original_price'] += $goodsInfo['total_original_price'];
  779. }
  780. //订单金额
  781. self::$orderPrice['total_amount'] = self::$orderPrice['total_goods_price'] + self::$orderPrice['member_amount'];
  782. //订单应付金额
  783. self::$orderPrice['order_amount'] = self::$orderPrice['total_goods_price'];
  784. return $goodsLists;
  785. }
  786. /**
  787. * @notes 是否超出 购买限制
  788. * @param $goods
  789. * @param $num
  790. * @param $userId
  791. * @return bool
  792. * @author lbzy
  793. * @datetime 2023-08-16 15:13:47
  794. */
  795. static function isOutBuyNum($goods, $num, $userId) : bool
  796. {
  797. if ($goods['limit_type'] == GoodsEnum::LIMIT_TYPE_USER) {
  798. $order_goods_num = OrderGoods::alias('og')
  799. ->join('order o', 'o.id = og.order_id')
  800. ->where([
  801. 'og.goods_id'=>$goods['goods_id'],
  802. 'o.order_status'=>[
  803. OrderEnum::STATUS_WAIT_PAY,
  804. OrderEnum::STATUS_WAIT_DELIVERY,
  805. OrderEnum::STATUS_WAIT_RECEIVE,
  806. OrderEnum::STATUS_FINISH
  807. ],
  808. 'o.user_id' => $userId,
  809. 'o.order_type' => [ OrderEnum::NORMAL_ORDER, OrderEnum::VIRTUAL_ORDER] ]
  810. )
  811. ->sum('og.goods_num');
  812. return ($order_goods_num + $goods['goods_num']) > $goods['limit_value'];
  813. }
  814. if ($goods['limit_type'] == GoodsEnum::LIMIT_TYPE_ORDER) {
  815. return $num > $goods['limit_value'];
  816. }
  817. return false;
  818. }
  819. /**
  820. * @notes 订单详情
  821. * @param $params
  822. * @return array
  823. * @author 段誉
  824. * @date 2021/8/2 20:59
  825. */
  826. public static function getDetail($params)
  827. {
  828. $result = (new Order())->with(['order_goods' => function ($query) {
  829. $query->field([
  830. 'id', 'order_id', 'goods_id', 'item_id', 'goods_snap', 'original_price',
  831. 'goods_name', 'goods_price', 'goods_num', 'total_price', 'total_pay_price', 'express_price', 'change_price', 'discount_price', 'discount_price'=>'coupon_discount', 'member_price', 'integral_price'
  832. ])->append(['goods_image', 'spec_value_str','original_price'])->hidden(['goods_snap']);
  833. }])
  834. ->where(['id' => $params['id'], 'user_id' => $params['user_id']])
  835. ->append(['btn', 'delivery_address', 'cancel_unpaid_orders_time', 'show_pickup_code'])
  836. ->hidden(['user_id', 'order_terminal', 'delete_time', 'update_time'])
  837. ->findOrEmpty()->toArray();
  838. //订单类型
  839. $result['order_type_desc'] = ($result['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? '自提订单' : OrderEnum::getOrderTypeDesc($result['order_type']);
  840. //订单状态描述
  841. $result['order_status_desc'] = ($result['order_status'] == OrderEnum::STATUS_WAIT_DELIVERY && $result['delivery_type'] == DeliveryEnum::SELF_DELIVERY) ? '待取货' : OrderEnum::getOrderStatusDesc($result['order_status']);
  842. if ($result['order_type'] == OrderEnum::TEAM_ORDER && $result['is_team_success'] != TeamEnum::TEAM_FOUND_SUCCESS) {
  843. $result['order_status_desc'] = ($result['order_status'] == OrderEnum::STATUS_WAIT_DELIVERY) ? TeamEnum::getStatusDesc($result['is_team_success']) : OrderEnum::getOrderStatusDesc($result['order_status']);
  844. }
  845. // 预售信息
  846. if ($result['order_type'] == OrderEnum::PRESELL_ORDER) {
  847. $result['presell'] = CommonPresellLogic::orderInfo($result);
  848. }
  849. //自提门店
  850. $result['selffetch_shop'] = SelffetchShop::where('id', $result['selffetch_shop_id'])
  851. ->append(['detailed_address'])
  852. ->hidden([ 'create_time', 'update_time', 'delete_time' ])
  853. ->find();
  854. //地址 省市区分隔开
  855. $result['address']->province = Region::where('id', $result['address']->province)->value('name');
  856. $result['address']->city = Region::where('id', $result['address']->city)->value('name');
  857. $result['address']->district = Region::where('id', $result['address']->district)->value('name');
  858. //订单商品原价总价
  859. $result['total_original_price'] = 0;
  860. //订单商品售后按钮处理
  861. foreach ($result['order_goods'] as &$goods) {
  862. $goods['after_sale_btn'] = 0;//售后按钮关闭
  863. $after_sale = AfterSale::where(['order_goods_id' => $goods['id'], 'order_id' => $params['id']])->findOrEmpty();
  864. $after_sale_goods = AfterSaleGoods::where(['order_goods_id' => $goods['id'], 'after_sale_id' => $after_sale['id']])->findOrEmpty();
  865. $goods['after_sale_id'] = $after_sale_goods['id'] ?? 0;
  866. if ($result['order_status'] == OrderEnum::STATUS_FINISH && $result['after_sale_deadline'] > time() && $after_sale->isEmpty()) {
  867. $goods['after_sale_btn'] = 1;//售后按钮开启
  868. }
  869. if ($result['order_status'] == OrderEnum::STATUS_FINISH && $result['after_sale_deadline'] > time() && $after_sale['status'] == AfterSaleEnum::STATUS_ING) {
  870. $goods['after_sale_btn'] = 2;//售后中
  871. }
  872. if ($result['order_status'] == OrderEnum::STATUS_FINISH && $result['after_sale_deadline'] > time() && $after_sale['status'] == AfterSaleEnum::STATUS_SUCCESS) {
  873. $goods['after_sale_btn'] = 3;//售后成功
  874. }
  875. if ($result['order_status'] == OrderEnum::STATUS_FINISH && $result['after_sale_deadline'] > time() && $after_sale['status'] == AfterSaleEnum::STATUS_FAIL) {
  876. $goods['after_sale_btn'] = 4;//售后失败
  877. }
  878. //批量下单后,过了取消订单的时间,可对其中一件商品进行退款操作
  879. if (in_array($result['order_status'],[OrderEnum::STATUS_WAIT_DELIVERY,OrderEnum::STATUS_WAIT_RECEIVE])) {
  880. $ableCancelOrder = ConfigService::get('transaction', 'cancel_unshipped_orders');
  881. if ($ableCancelOrder == YesNoEnum::NO) {
  882. $goods['after_sale_btn'] = 1;//售后按钮开启
  883. }
  884. if ($ableCancelOrder == YesNoEnum::YES) {
  885. $configTime = ConfigService::get('transaction', 'cancel_unshipped_orders_times');
  886. $ableCancelTime = strtotime($result['pay_time']) + ($configTime * 60);
  887. if (time() > $ableCancelTime) {
  888. $goods['after_sale_btn'] = 1;//售后按钮开启
  889. }
  890. }
  891. if (isset($after_sale['status']) && $after_sale['status'] == AfterSaleEnum::STATUS_ING) {
  892. $goods['after_sale_btn'] = 2;//售后中
  893. }
  894. if (isset($after_sale['status']) && $after_sale['status'] == AfterSaleEnum::STATUS_SUCCESS) {
  895. $goods['after_sale_btn'] = 3;//售后成功
  896. }
  897. if (isset($after_sale['status']) && $after_sale['status'] == AfterSaleEnum::STATUS_FAIL) {
  898. $goods['after_sale_btn'] = 4;//售后失败
  899. }
  900. }
  901. //商品原价总价
  902. $goods['total_original_price'] = $goods['original_price'] * $goods['goods_num'];
  903. $result['total_original_price'] += $goods['original_price'] * $goods['goods_num'];
  904. //会员价优惠
  905. $goods['member_discount'] = 0;
  906. if($goods['member_price'] > 0){
  907. $goods['member_discount'] = round(($goods['original_price'] - $goods['member_price']) * $goods['goods_num'],2);
  908. }
  909. // unset($goods['member_price']);
  910. //积分优惠
  911. $goods['integral_discount'] = round($goods['integral_price'] * $goods['goods_num'],2);
  912. // unset($goods['integral_price']);
  913. //售后状态
  914. $goods['after_sale_status_desc'] = '无售后';
  915. if (!$after_sale->isEmpty()) {
  916. $goods['after_sale_status_desc'] = AfterSaleEnum::getStatusDesc($after_sale->status);
  917. }
  918. //订单商品总的优惠
  919. $goods['total_discount'] = round($goods['member_discount']+ $goods['coupon_discount'] +$goods['integral_discount'],2);
  920. }
  921. //订单商品原价总价
  922. $result['total_original_price'] = round($result['total_original_price'],2);
  923. //订单总优惠金额
  924. $result['total_discount'] = $result['discount_amount'] + $result['member_amount'] + $result['integral_amount'];
  925. // unset($result['discount_amount']);unset($result['member_amount']);unset($result['integral_amount']);
  926. //商品详情屏蔽查看内容按钮
  927. $result['btn']['content_btn'] = OrderEnum::BTN_HIDE;
  928. return $result;
  929. }
  930. static function wxReceiveDetail($id, $user_id)
  931. {
  932. $order = Order::where('id', $id)->where('user_id', $user_id)->findOrEmpty()->toArray();
  933. return [
  934. 'transaction_id' => $order['transaction_id'] ?? '',
  935. ];
  936. }
  937. /**
  938. * @notes 取消订单
  939. * @param $params
  940. * @return bool
  941. * @author 段誉
  942. * @date 2021/8/2 15:08
  943. */
  944. public static function cancelOrder($params)
  945. {
  946. Db::startTrans();
  947. try {
  948. $order = (new Order())->getUserOrderById($params['id'], $params['user_id']);
  949. // 如果是拼团订单特别处理
  950. if ($order['order_type'] == OrderEnum::TEAM_ORDER) {
  951. TeamLogic::signFailTeam($order['id']);
  952. } else {
  953. //处于已支付状态的发起整单售后
  954. if ($order['pay_status'] == PayEnum::ISPAID) {
  955. AfterSaleService::orderRefund([
  956. 'order_id' => $params['id'],
  957. 'scene' => AfterSaleLogEnum::BUYER_CANCEL_ORDER
  958. ]);
  959. }
  960. //更新订单为已关闭
  961. Order::update([
  962. 'order_status' => OrderEnum::STATUS_CLOSE,
  963. 'cancel_time' => time()
  964. ], ['id' => $order['id']]);
  965. $returnInventory = ConfigService::get('transaction', 'return_inventory');
  966. if ($returnInventory) {
  967. // 需退还库存
  968. AfterSaleService::returnInventory(['order_id' => $order['id']]);
  969. }
  970. $returnCoupon = ConfigService::get('transaction', 'return_coupon');
  971. if ($returnCoupon) {
  972. // 需退还优惠券
  973. AfterSaleService::returnCoupon($order);
  974. }
  975. //订单日志
  976. (new OrderLog())->record([
  977. 'type' => OrderLogEnum::TYPE_USER,
  978. 'channel' => OrderLogEnum::USER_CANCEL_ORDER,
  979. 'order_id' => $params['id'],
  980. 'operator_id' => $params['user_id'],
  981. ]);
  982. }
  983. Db::commit();
  984. return true;
  985. } catch (\Exception $e) {
  986. Db::rollback();
  987. self::$error = $e->getMessage();
  988. return false;
  989. }
  990. }
  991. /**
  992. * @notes 确认订单
  993. * @param $params
  994. * @author 段誉
  995. * @date 2021/8/2 14:59
  996. */
  997. public static function confirmOrder($params)
  998. {
  999. //更新订单状态
  1000. Order::update([
  1001. 'order_status' => OrderEnum::STATUS_FINISH,
  1002. 'confirm_take_time' => time(),
  1003. 'after_sale_deadline' => self::getAfterSaleDeadline(), //售后截止时间
  1004. ], ['id' => $params['id'], 'user_id' => $params['user_id']]);
  1005. //订单日志
  1006. (new OrderLog())->record([
  1007. 'type' => OrderLogEnum::TYPE_USER,
  1008. 'channel' => OrderLogEnum::USER_CONFIRM_ORDER,
  1009. 'order_id' => $params['id'],
  1010. 'operator_id' => $params['user_id']
  1011. ]);
  1012. }
  1013. /**
  1014. * @notes 获取当前售后
  1015. * @return float|int
  1016. * @author 段誉
  1017. * @date 2021/8/2 17:02
  1018. */
  1019. public static function getAfterSaleDeadline()
  1020. {
  1021. //是否关闭维权
  1022. $afterSale = ConfigService::get('transaction', 'after_sales');
  1023. //可维权时间
  1024. $afterSaleDays = ConfigService::get('transaction', 'after_sales_days');
  1025. if ($afterSale == YesNoEnum::NO) {
  1026. $afterSaleDeadline = time();
  1027. } else {
  1028. $afterSaleDeadline = ($afterSaleDays * 24 * 60 * 60) + time();
  1029. }
  1030. return $afterSaleDeadline;
  1031. }
  1032. /**
  1033. * @notes 查看物流
  1034. * @param $params
  1035. * @return array[]
  1036. * @author ljj
  1037. * @date 2021/8/13 6:07 下午
  1038. */
  1039. public static function orderTraces($params)
  1040. {
  1041. //订单信息
  1042. $order = Order::field('id,sn,pay_time,address,order_type,express_status,order_status')
  1043. ->append(['delivery_address'])
  1044. ->where('id',$params['id'])
  1045. ->find()
  1046. ->toArray();
  1047. //物流配置
  1048. $express_type = ConfigService::get('logistics_config', 'express_type', '');
  1049. $express_bird = unserialize(ConfigService::get('logistics_config', 'express_bird', ''));
  1050. $express_hundred = unserialize(ConfigService::get('logistics_config', 'express_hundred', ''));
  1051. $parcelInfo = Delivery::field('id,order_id,order_goods_info,express_name,express_id,invoice_no,send_type,remark,create_time,delivery_address_info,return_address_info')
  1052. ->where(['order_id'=>$order['id']])
  1053. ->append(['send_type_desc'])
  1054. ->select()->toArray();
  1055. foreach ($parcelInfo as $key=>$parcel) {
  1056. //查询物流信息
  1057. $logisticsInfo = [];
  1058. if ($parcel['send_type'] == DeliveryEnum::NO_EXPRESS) {
  1059. $logisticsInfo['traces'] = ['无需物流'];
  1060. } else {
  1061. if (empty($express_type)) {
  1062. $logisticsInfo['traces'] = ['暂无物流信息'];
  1063. }
  1064. //快递配置设置为快递鸟时
  1065. if($express_type === 'express_bird') {
  1066. $expressage = (new Kdniao($express_bird['ebussiness_id'], $express_bird['app_key']));
  1067. $express_field = 'codebird';
  1068. } elseif($express_type === 'express_hundred') {
  1069. $expressage = (new Kd100($express_hundred['customer'], $express_hundred['app_key']));
  1070. $express_field = 'code100';
  1071. }
  1072. $logisticsInfo['express_field'] = $express_field ?? 'codebird';
  1073. //快递编码
  1074. $express_code = Express::where('id',$parcel['express_id'])->value($express_field);
  1075. //获取物流轨迹
  1076. if (in_array(strtolower($express_code), [ 'sf', 'shunfeng' ])) {
  1077. if ($express_type === 'express_bird') {
  1078. $expressage->logistics($express_code, $parcel['invoice_no'], substr($order['address']->mobile, -4));
  1079. } else {
  1080. $expressage->logistics($express_code, $parcel['invoice_no'], $order['address']->mobile);
  1081. }
  1082. }else {
  1083. $expressage->logistics($express_code, $parcel['invoice_no']);
  1084. }
  1085. $logisticsInfo['traces'] = $expressage->logisticsFormat();
  1086. if ($logisticsInfo['traces'] == false) {
  1087. $logisticsInfo['traces'] = ['暂无物流信息'];
  1088. } else {
  1089. foreach ($logisticsInfo['traces'] as &$item) {
  1090. $item = array_values(array_unique($item));
  1091. }
  1092. if ($logisticsInfo['express_field'] == 'codebird') {
  1093. //倒序排序
  1094. $logisticsInfo['traces'] = array_reverse($logisticsInfo['traces']);
  1095. }
  1096. }
  1097. }
  1098. $parcelInfo[$key]['logistics_info'] = $logisticsInfo;
  1099. $parcelInfo[$key]['express_icon'] = Express::where('id',$parcel['express_id'])->value('icon');
  1100. }
  1101. $deliveryAddressInfo = !empty($parcelInfo[0]['delivery_address_info']) ? json_decode($parcelInfo[0]['delivery_address_info'],true) : '';
  1102. $returnAddressInfo = !empty($parcelInfo[0]['return_address_info']) ? json_decode($parcelInfo[0]['return_address_info'],true) : '';
  1103. //待发货商品
  1104. $waitDeliveryGoods = OrderGoods::where(['order_id'=>$order['id'],'express_status'=>[DeliveryEnum::NOT_SHIPPED,DeliveryEnum::PART_SHIPPED]])
  1105. ->field('id,goods_name,goods_num,goods_price,delivery_num,goods_snap')
  1106. ->append(['spec_value_str','goods_image'])
  1107. ->hidden(['goods_snap'])->select()->toArray();
  1108. foreach ($waitDeliveryGoods as $key=>$goods) {
  1109. $afterSale = AfterSale::where(['order_goods_id'=>$goods['id'],'status'=>AfterSaleEnum::STATUS_SUCCESS])->findOrEmpty();
  1110. if (!$afterSale->isEmpty()) {
  1111. unset($waitDeliveryGoods[$key]);
  1112. }
  1113. }
  1114. $waitDeliveryGoods = empty($waitDeliveryGoods) ? [] : array_values($waitDeliveryGoods);
  1115. return [
  1116. 'order_id' => $order['id'],
  1117. 'order_sn' => $order['sn'],
  1118. 'order_type' => $order['order_type'],
  1119. 'order_status' => $order['order_status'],
  1120. 'express_status' => $order['express_status'],
  1121. 'pay_time' => $order['pay_time'],
  1122. 'receipt_address_info' => [
  1123. 'addresss' => $order['delivery_address'],
  1124. 'contact' => $order['address']->contact,
  1125. 'mobile' => $order['address']->mobile,
  1126. ],
  1127. 'delivery_address_info' => [
  1128. 'addresss' => $deliveryAddressInfo['complete_address'] ?? '',
  1129. 'contact' => $deliveryAddressInfo['contact'] ?? '',
  1130. 'mobile' => $deliveryAddressInfo['mobile'] ?? '',
  1131. ],
  1132. 'return_address_info' => [
  1133. 'addresss' => $returnAddressInfo['complete_address'] ?? '',
  1134. 'contact' => $returnAddressInfo['contact'] ?? '',
  1135. 'mobile' => $returnAddressInfo['mobile'] ?? '',
  1136. ],
  1137. 'remark' => $parcelInfo[0]['remark'],
  1138. 'parcel_info' => $parcelInfo,
  1139. 'wait_delivery_goods' => $waitDeliveryGoods
  1140. ];
  1141. }
  1142. /**
  1143. * @notes 获取配送方式
  1144. * @return array
  1145. * @author ljj
  1146. * @date 2021/8/27 2:32 下午
  1147. */
  1148. public function getDeliveryType()
  1149. {
  1150. return [
  1151. 'express' => [
  1152. 'is_express' => ConfigService::get('delivery_type', 'is_express', 1),
  1153. 'express_name' => ConfigService::get('delivery_type', 'express_name', '快递发货'),
  1154. ],
  1155. 'selffetch' => [
  1156. 'is_selffetch' => ConfigService::get('delivery_type', 'is_selffetch', 0),
  1157. 'selffetch_name' => ConfigService::get('delivery_type', 'selffetch_name', '上门自提'),
  1158. ],
  1159. ];
  1160. }
  1161. /**
  1162. * @notes 删除订单
  1163. * @param $params
  1164. * @return bool
  1165. * @author ljj
  1166. * @date 2021/8/31 2:38 下午
  1167. */
  1168. public function del($params)
  1169. {
  1170. return Order::destroy($params['id']);
  1171. }
  1172. /**
  1173. * @notes 提取砍价商品信息
  1174. * @param $params
  1175. * @return array
  1176. * @author Tab
  1177. * @date 2021/10/9 11:13
  1178. */
  1179. public static function initiateGoods($params)
  1180. {
  1181. $bargainInitiate = BargainInitiate::findOrEmpty($params['initiate_id'])->toArray();
  1182. // 返回二维数组
  1183. return [
  1184. [
  1185. 'item_id' => $bargainInitiate['goods_snapshot']['item_id'],
  1186. 'goods_num' => $bargainInitiate['goods_snapshot']['goods_num'],
  1187. ]
  1188. ];
  1189. }
  1190. /**
  1191. * @notes 获取不同类型订单的规格单价
  1192. * @param $params
  1193. * @author Tab
  1194. * @date 2021/10/9 11:23
  1195. */
  1196. public static function getSellPrice($params, $goodsInfo)
  1197. {
  1198. switch ($params['order_type']) {
  1199. // 普通订单 虚拟订单 抽奖订单
  1200. case OrderEnum::VIRTUAL_ORDER:
  1201. case OrderEnum::NORMAL_ORDER:
  1202. case OrderEnum::DRAW_ORDER:
  1203. return self::getGoodsSellPrice($goodsInfo);
  1204. // 拼团订单
  1205. case OrderEnum::TEAM_ORDER:
  1206. return self::getTeamActivityPrice($params);
  1207. // 秒杀订单
  1208. case OrderEnum::SECKILL_ORDER:
  1209. return self::getSeckillActivityPrice($params);
  1210. // 砍价订单
  1211. case OrderEnum::BARGAIN_ORDER:
  1212. return self::getBargainActivityPrice($params);
  1213. // 预售订单
  1214. case OrderEnum::PRESELL_ORDER:
  1215. return self::getPresellActivityPrice($params);
  1216. }
  1217. }
  1218. /**
  1219. * @notes 获取预售价
  1220. * @param $params
  1221. * @return mixed
  1222. * @author lbzy
  1223. * @datetime 2024-04-26 16:33:58
  1224. */
  1225. static function getPresellActivityPrice($params)
  1226. {
  1227. return PresellGoodsItem::alias('pgi')
  1228. ->where('pgi.item_id', $params['goods'][0]['item_id'])
  1229. ->where('pgi.presell_id', $params['presell_id'])
  1230. ->value('price');
  1231. }
  1232. /**
  1233. * @notes 获取商品售价
  1234. * @param $params
  1235. * @return mixed
  1236. * @author cjhao
  1237. * @date 2022/5/7 18:40
  1238. */
  1239. public static function getGoodsSellPrice($params)
  1240. {
  1241. //会员价允许为零
  1242. if($params['member_price'] >= 0 && $params['member_price'] <= $params['sell_price']){
  1243. return $params['member_price'];
  1244. }
  1245. return $params['sell_price'];
  1246. }
  1247. /**
  1248. * @notes 获取砍价活动价格
  1249. * @param $params
  1250. * @return mixed|void
  1251. * @author Tab
  1252. * @date 2021/10/9 11:30
  1253. */
  1254. public static function getBargainActivityPrice($params)
  1255. {
  1256. $bargainInitiate = BargainInitiate::findOrEmpty($params['initiate_id'])->toArray();
  1257. // 任意金额可购买
  1258. if ($params['buy_condition'] == 'random') {
  1259. return $bargainInitiate['current_price'];
  1260. }
  1261. // 底价购买
  1262. if ($params['buy_condition'] == 'floor') {
  1263. return $bargainInitiate['floor_price'];
  1264. }
  1265. }
  1266. /**
  1267. * @notes 获取秒杀活动价格
  1268. * @param $params
  1269. * @return mixed
  1270. * @author Tab
  1271. * @date 2021/10/9 16:00
  1272. */
  1273. public static function getSeckillActivityPrice($params)
  1274. {
  1275. return SeckillGoodsItem::where([
  1276. 'seckill_id' => $params['seckill_id'],
  1277. 'item_id' => $params['goods'][0]['item_id'],
  1278. ])->value('seckill_price');
  1279. }
  1280. /**
  1281. * @notes 获取拼团活动价格
  1282. * @param $params
  1283. * @author Tab
  1284. * @date 2021/10/9 16:31
  1285. */
  1286. public static function getTeamActivityPrice($params)
  1287. {
  1288. return TeamGoodsItem::where([
  1289. 'team_id' => $params['team_id'],
  1290. 'item_id' => $params['goods'][0]['item_id'],
  1291. ])->value('team_price');
  1292. }
  1293. /**
  1294. * @notes 砍价下单后的操作
  1295. * @param $params
  1296. * @param $order
  1297. * @author Tab
  1298. * @date 2021/10/9 11:52
  1299. */
  1300. public static function bargainAfter($params, $order)
  1301. {
  1302. // 更新砍价记录
  1303. $bargainInitiate = BargainInitiate::findOrEmpty($params['initiate_id']);
  1304. $bargainInitiate->order_id = $order['id'];
  1305. // 标识砍价成功避免任意金额可下单的情况用户继续发起帮助砍价
  1306. $bargainInitiate->status = BargainEnum::STATUS_SUCCESS;
  1307. $bargainInitiate->save();
  1308. }
  1309. /**
  1310. * @notes 拼团下单后的操作
  1311. * @param $params
  1312. * @param $order
  1313. * @author Tab
  1314. * @date 2021/10/9 16:35
  1315. */
  1316. public static function teamAfter($params, $order)
  1317. {
  1318. $time = time();
  1319. $news_found_id = null;
  1320. $teamActivity = TeamActivity::findOrEmpty($params['team_id'])->toArray();
  1321. $teamGoods = TeamGoods::where([
  1322. 'team_id' => $params['team_id'],
  1323. 'goods_id' => $params['goods'][0]['goods_id'],
  1324. ])->findOrEmpty()->toArray();
  1325. $teamGoodsItem = TeamGoodsItem::where([
  1326. 'team_id' => $params['team_id'],
  1327. 'item_id' => $params['goods'][0]['item_id'],
  1328. ])->findOrEmpty()->toArray();
  1329. $goodsSnap = $teamGoods['goods_snap'];
  1330. $itemSnap = $teamGoodsItem['item_snap'];
  1331. $goodsInfo = [
  1332. 'id' => intval($params['goods'][0]['goods_id']),
  1333. 'item_id' => intval($params['goods'][0]['item_id']),
  1334. 'spec_value_ids' => $params['goods'][0]['spec_value_ids'],
  1335. 'name' => $goodsSnap['name'],
  1336. 'image' => $itemSnap['image'] ? FileService::getFileUrl($itemSnap['image']) : FileService::getFileUrl($goodsSnap['image']),
  1337. 'spec_value_str' => $itemSnap['spec_value_str'],
  1338. 'cost_price' => $teamGoodsItem['sell_price'],
  1339. 'sell_price' => $teamGoodsItem['team_price'],
  1340. 'total_price' => round($teamGoodsItem['team_price'] * $params['goods'][0]['goods_num'], 2),
  1341. 'count' => intval($params['goods'][0]['goods_num']),
  1342. 'goods_snap' => $goodsSnap,
  1343. ];
  1344. // 开团
  1345. if (!isset($params['found_id']) || empty($params['found_id'])) {
  1346. $found = TeamFound::create([
  1347. 'found_sn' => generate_sn((new TeamFound()), 'found_sn'),
  1348. 'team_id' => $params['team_id'],
  1349. 'user_id' => $params['user_id'],
  1350. 'order_id' => $order['id'],
  1351. 'people' => $teamActivity['people_num'],
  1352. 'join' => 0,
  1353. 'status' => 0,
  1354. 'goods_snap' => json_encode([
  1355. 'id' => $params['goods'][0]['goods_id'],
  1356. 'name' => $params['goods'][0]['goods_name'],
  1357. 'image' => $params['goods'][0]['image']
  1358. ], JSON_UNESCAPED_UNICODE),
  1359. 'kaituan_time' => $time,
  1360. 'invalid_time' => ($teamActivity['effective_time'] * 60) + time()
  1361. ]);
  1362. $news_found_id = $found['id'];
  1363. }
  1364. // 参团
  1365. TeamJoin::create([
  1366. 'join_sn' => generate_sn((new TeamJoin()), 'join_sn'),
  1367. 'team_id' => $params['team_id'],
  1368. 'found_id' => $news_found_id ?: $params['found_id'],
  1369. 'identity' => $news_found_id ? 1 : 2,
  1370. 'user_id' => $params['user_id'],
  1371. 'order_id' => $order['id'],
  1372. 'status' => 0,
  1373. 'team_snap' => json_encode($teamActivity, JSON_UNESCAPED_UNICODE),
  1374. 'goods_snap' => json_encode($goodsInfo, JSON_UNESCAPED_UNICODE),
  1375. 'invalid_time' => ($teamActivity['effective_time'] * 60) + time(),
  1376. 'create_time' => $time,
  1377. 'update_time' => $time
  1378. ]);
  1379. // 更新数据
  1380. TeamFound::update(['join' => ['inc', 1]], ['id' => $news_found_id ?: $params['found_id']]);
  1381. TeamActivity::update(['partake_number' => ['inc', 1]], ['id' => $params['team_id']]);
  1382. Order::update(['team_found_id' => $news_found_id ?: $params['found_id']], ['id' => $order['id']]);
  1383. }
  1384. /**
  1385. * @notes 秒杀下单后操作
  1386. * @param $params
  1387. * @param $order
  1388. * @author Tab
  1389. * @date 2021/10/13 14:49
  1390. */
  1391. public static function seckillAfter($params, $order)
  1392. {
  1393. Order::update([
  1394. 'id' => $order['id'],
  1395. 'seckill_id' => $params['seckill_id']
  1396. ]);
  1397. }
  1398. /**
  1399. * @notes 预售下单后操作
  1400. * @param $params
  1401. * @param $order
  1402. * @return void
  1403. * @author lbzy
  1404. * @datetime 2024-04-26 16:43:00
  1405. */
  1406. static function presellAfter($params, $order)
  1407. {
  1408. Order::update([
  1409. 'id' => $order['id'],
  1410. 'presell_id' => $params['presell_id']
  1411. ]);
  1412. }
  1413. }