截流自动化的商城平台
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IntegralOrderLogic.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\basics\Logic;
  4. use app\common\enum\IntegralGoodsEnum;
  5. use app\common\enum\IntegralOrderEnum;
  6. use app\common\enum\PayEnum;
  7. use app\common\logic\AccountLogLogic;
  8. use app\common\logic\IntegralOrderRefundLogic;
  9. use app\common\logic\PayNotifyLogic;
  10. use app\common\model\AccountLog;
  11. use app\common\model\integral\IntegralGoods;
  12. use app\common\model\integral\IntegralOrder;
  13. use app\common\model\user\User;
  14. use app\common\server\ConfigServer;
  15. use app\common\server\UrlServer;
  16. use expressage\Kd100;
  17. use expressage\Kdniao;
  18. use think\facade\Env;
  19. use think\facade\Db;
  20. /**
  21. * 积分商城订单
  22. * Class IntegralOrderLogic
  23. * @package app\api\logic
  24. */
  25. class IntegralOrderLogic extends Logic
  26. {
  27. /**
  28. * @notes 结算订单
  29. * @param $params
  30. * @return array
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @author 段誉
  34. * @date 2022/3/2 9:50
  35. */
  36. public static function settlement($params)
  37. {
  38. // 地址
  39. if (empty($params['address_id']) || !$params['address_id']) {
  40. $address = UserAddressLogic::getDefaultAddress($params['user_id']);
  41. } else {
  42. $address = UserAddressLogic::getOneAddress($params['user_id'], ['id' => $params['address_id']]);
  43. }
  44. // 订单需支付总金额
  45. $order_amount = 0;
  46. $goods_price = 0;
  47. $goods = IntegralGoods::withoutField(['content', 'update_time'])->findOrEmpty($params['id'])->toArray();
  48. // 兑换方式为纯积分
  49. if ($goods['exchange_way'] == IntegralGoodsEnum::EXCHANGE_WAY_INTEGRAL) {
  50. // 订单需支付积分
  51. $order_integral = $goods['need_integral'] * $params['num'];
  52. } else {
  53. // 订单需支付总金额
  54. $goods_price = $goods['need_money'] * $params['num'];
  55. $order_amount = $goods_price;
  56. // 订单需支付总积分
  57. $order_integral = $goods['need_integral'] * $params['num'];
  58. }
  59. // 运费
  60. $shipping_price = 0;
  61. // 快递配送 && 快递统一运费 && 运费>0
  62. if ($goods['delivery_way'] == IntegralGoodsEnum::DELIVERY_EXPRESS
  63. && $goods['express_type'] == IntegralGoodsEnum::EXPRESS_TYPE_UNIFIED
  64. && $goods['express_money'] > 0) {
  65. $order_amount = $order_amount + $goods['express_money'];
  66. $shipping_price = $goods['express_money'];
  67. }
  68. return [
  69. 'address' => $address,
  70. 'goods' => $goods,
  71. 'need_pay' => $order_amount > 0 ? 1 : 0,
  72. 'exchange_way' => $goods['exchange_way'],
  73. 'delivery_way' => $goods['delivery_way'],
  74. 'total_num' => intval($params['num']),
  75. 'shipping_price' => $shipping_price, // 运费
  76. 'goods_price' => round($goods_price, 2), // 商品金额(不包含运费)
  77. 'order_amount' => round($order_amount, 2), // 订单需要的金额(包含运费)
  78. 'order_integral' => $order_integral, // 订单需要返回的积分
  79. ];
  80. }
  81. /**
  82. * @notes 提交订单
  83. * @param $params
  84. * @return array|false
  85. * @author 段誉
  86. * @date 2022/3/2 9:50
  87. */
  88. public static function submitOrder($params)
  89. {
  90. Db::startTrans();
  91. try {
  92. // 结算详情(支付积分,支付金额)
  93. $settle = self::settlement($params);
  94. // 提交前验证
  95. $user = User::findOrEmpty($params['user_id']);
  96. if ($user['user_integral'] < $settle['order_integral']) {
  97. throw new \Exception('积分不足');
  98. }
  99. if ($settle['total_num'] <= 0) {
  100. throw new \Exception('请选择商品数量');
  101. }
  102. $settle['goods']['image'] = UrlServer::setFileUrl($settle['goods']['image']);
  103. // 提交订单
  104. $order = IntegralOrder::create([
  105. 'order_sn' => createSn('integral_order', 'order_sn'),
  106. 'user_id' => $params['user_id'],
  107. 'order_source' => $params['client'],
  108. 'delivery_way' => $settle['goods']['delivery_way'],
  109. 'exchange_type' => $settle['goods']['type'],
  110. 'exchange_way' => $settle['goods']['exchange_way'],
  111. 'consignee' => $settle['address']['contact'],
  112. 'province' => $settle['address']['province_id'],
  113. 'city' => $settle['address']['city_id'],
  114. 'district' => $settle['address']['district_id'],
  115. 'address' => $settle['address']['address'],
  116. 'mobile' => $settle['address']['telephone'],
  117. 'order_amount' => $settle['order_amount'],
  118. 'order_integral' => $settle['order_integral'],
  119. 'total_num' => $settle['total_num'],
  120. 'goods_price' => $settle['goods_price'],
  121. 'shipping_price' => $settle['shipping_price'],
  122. 'user_remark' => $params['user_remark'] ?? '',
  123. 'goods_snap' => $settle['goods']
  124. ]);
  125. // 扣减应付积分
  126. if ($settle['order_integral'] > 0) {
  127. User::where(['id' => $params['user_id']])
  128. ->dec('user_integral', $settle['order_integral'])
  129. ->update();
  130. AccountLogLogic::AccountRecord(
  131. $params['user_id'],
  132. $settle['order_integral'], 2,
  133. AccountLog::pay_integral_order,
  134. '', $order['id'], $order['order_sn']
  135. );
  136. }
  137. // 兑换方式-积分 且没有运费 扣减积分后 直接支付完成
  138. if ($settle['goods']['exchange_way'] == IntegralGoodsEnum::EXCHANGE_WAY_INTEGRAL && $settle['order_amount'] <= 0) {
  139. PayNotifyLogic::handle('integral', $order['order_sn']);
  140. }
  141. Db::commit();
  142. return ['order_id' => $order['id'], 'type' => 'integral'];
  143. } catch (\Exception $e) {
  144. Db::rollback();
  145. self::$error = $e->getMessage();
  146. return false;
  147. }
  148. }
  149. /**
  150. * @notes 订单列表
  151. * @param $user_id
  152. * @param $type
  153. * @param $page
  154. * @param $size
  155. * @return array
  156. * @throws \think\db\exception\DataNotFoundException
  157. * @throws \think\db\exception\DbException
  158. * @throws \think\db\exception\ModelNotFoundException
  159. * @author 段誉
  160. * @date 2022/3/2 9:39
  161. */
  162. public static function lists($user_id, $type, $page, $size)
  163. {
  164. $order = new IntegralOrder();
  165. $where[] = ['del', '=', 0];
  166. $where[] = ['user_id', '=', $user_id];
  167. if (isset($type) && $type != '') {
  168. $where[] = ['order_status', '=', $type];
  169. }
  170. $field = [
  171. 'id', 'order_sn', 'order_status', 'pay_status', 'shipping_status', 'shipping_price',
  172. 'delivery_way', 'order_amount', 'total_num', 'order_integral', 'goods_snap',
  173. 'create_time', 'refund_status'
  174. ];
  175. $count = $order->where($where)->count();
  176. $lists = $order->where($where)->field($field)->page($page, $size)
  177. ->order('id desc')
  178. ->append(['btns'])
  179. ->select()->toArray();
  180. foreach ($lists as &$item) {
  181. $item['order_status'] = IntegralOrderEnum::getOrderStatus($item['order_status']);
  182. $goods = $item['goods_snap'];
  183. $item['goods'] = [
  184. 'image' => UrlServer::getFileUrl($goods['image']),
  185. 'name' => $goods['name'],
  186. 'need_integral' => $goods['need_integral'],
  187. 'need_money' => $goods['need_money'],
  188. 'exchange_way' => $goods['exchange_way'],
  189. ];
  190. unset($item['goods_snap']);
  191. }
  192. $data = [
  193. 'list' => $lists,
  194. 'page' => $page,
  195. 'size' => $size,
  196. 'count' => $count,
  197. 'more' => is_more($count, $page, $size)
  198. ];
  199. return $data;
  200. }
  201. /**
  202. * @notes 订单详情
  203. * @param $id
  204. * @return array
  205. * @author 段誉
  206. * @date 2022/3/2 10:22
  207. */
  208. public static function detail($id)
  209. {
  210. $order = IntegralOrder::where(['id' => $id])
  211. ->withoutField(['content', 'order_source', 'transaction_id', 'refund_amount'])
  212. ->append(['delivery_address', 'pay_time', 'btns'])
  213. ->findOrEmpty()->toArray();
  214. $order['pay_time'] = $order['pay_time'] ? date('Y-m-d H:i:s', $order['pay_time']) : '-' ;
  215. $order['shipping_time'] = $order['shipping_time'] ? date('Y-m-d H:i:s', $order['shipping_time']) : '-' ;
  216. $order['pay_way'] = PayEnum::getPayWay($order['pay_way']);
  217. $goods_snap = $order['goods_snap'];
  218. unset($order['goods_snap']);
  219. $order['goods'] = [
  220. 'image' => UrlServer::getFileUrl($goods_snap['image']),
  221. 'name' => $goods_snap['name'],
  222. 'exchange_way' => $goods_snap['exchange_way'],
  223. 'need_integral' => $goods_snap['need_integral'],
  224. 'need_money' => $goods_snap['need_money'],
  225. 'total_num' => $order['total_num'],
  226. ];
  227. return $order;
  228. }
  229. /**
  230. * @notes 确认收货
  231. * @param $id
  232. * @param $user_id
  233. * @author 段誉
  234. * @date 2022/3/2 10:59
  235. */
  236. public static function confirm($id, $user_id)
  237. {
  238. //更新订单状态
  239. IntegralOrder::update([
  240. 'order_status' => IntegralOrderEnum::ORDER_STATUS_COMPLETE,
  241. 'confirm_time' => time(),
  242. ], ['id' => $id, 'user_id' => $user_id]);
  243. }
  244. /**
  245. * @notes 删除订单
  246. * @param $id
  247. * @param $user_id
  248. * @author 段誉
  249. * @date 2022/3/2 10:59
  250. */
  251. public static function del($id, $user_id)
  252. {
  253. //更新订单状态
  254. IntegralOrder::update(['del' => 1], ['id' => $id, 'user_id' => $user_id]);
  255. }
  256. /**
  257. * @notes 取消订单
  258. * @param $id
  259. * @return bool
  260. * @throws \think\db\exception\DataNotFoundException
  261. * @throws \think\db\exception\DbException
  262. * @throws \think\db\exception\ModelNotFoundException
  263. * @author 段誉
  264. * @date 2022/3/3 14:56
  265. */
  266. public static function cancel($id)
  267. {
  268. Db::startTrans();
  269. try {
  270. $order = IntegralOrder::findOrEmpty($id);
  271. // 更新订单状态, 退回库存, 扣减销量
  272. IntegralOrderRefundLogic::cancelOrder($id);
  273. // 退回已支付积分
  274. IntegralOrderRefundLogic::refundOrderIntegral($id);
  275. // 退回订单已支付积分或已支付金额
  276. if ($order['pay_status'] == PayEnum::ISPAID) {
  277. IntegralOrderRefundLogic::refundOrderAmount($id);
  278. }
  279. Db::commit();
  280. return true;
  281. } catch (\Exception $e) {
  282. Db::rollback();
  283. self::$error = $e->getMessage();
  284. IntegralOrderRefundLogic::addRefundLog(
  285. $order, $order['order_amount'],
  286. 2, $e->getMessage()
  287. );
  288. return false;
  289. }
  290. }
  291. /**
  292. * @notes 订单物流轨迹
  293. * @param $id
  294. * @return array
  295. * @author 段誉
  296. * @date 2022/3/3 17:29
  297. */
  298. public static function orderTraces($id)
  299. {
  300. $order = IntegralOrder::alias('o')
  301. ->field('invoice_no, shipping_name, shipping_id, send_type, o.shipping_status,
  302. o.mobile, o.province, o.city, o.district, o.address,o.total_num,o.goods_snap')
  303. ->join('integral_delivery d', 'd.order_id = o.id')
  304. ->where(['o.id' => $id])
  305. ->findOrEmpty();
  306. if ($order->isEmpty() || $order['send_type'] != 1) {
  307. return [];
  308. }
  309. //数据合并
  310. $order_traces = [
  311. 'order' => [
  312. 'tips' => self::getTracesOrderTips($order),
  313. 'image' => UrlServer::getFileUrl($order['goods_snap']['image']),
  314. 'count' => $order['total_num'],
  315. 'invoice_no' => $order['invoice_no'],
  316. 'shipping_name' => empty($order['shipping_name']) ? '-' : $order['shipping_name'],
  317. ],
  318. 'take' => [
  319. 'contacts' => $order['consignee'],
  320. 'mobile' => $order['mobile'],
  321. 'address' => $order['delivery_address'],
  322. ],
  323. 'finish' => self::getTracesFinish($order),
  324. 'delivery' => [
  325. 'title' => '运输中',
  326. 'traces' => self::getTraces($order)
  327. ],
  328. 'shipment' => self::getTracesShipment($order),
  329. 'buy' => [
  330. 'title' => '已下单',
  331. 'tips' => '订单提交成功',
  332. 'time' => date('Y-m-d H:i:s', $order['pay_time'])
  333. ],
  334. ];
  335. return $order_traces;
  336. }
  337. /**
  338. * @notes 订单物流-备注信息
  339. * @param $order
  340. * @return string
  341. * @author 段誉
  342. * @date 2022/3/3 17:30
  343. */
  344. public static function getTracesOrderTips($order)
  345. {
  346. $order_tips = '已下单';
  347. //确认收货
  348. if ($order['order_status'] == IntegralOrderEnum::ORDER_STATUS_COMPLETE) {
  349. $order_tips = '交易完成';
  350. }
  351. return $order_tips;
  352. }
  353. /**
  354. * @notes 订单物流-待收货信息
  355. * @param $order
  356. * @return string[]
  357. * @author 段誉
  358. * @date 2022/3/3 17:30
  359. */
  360. public static function getTracesShipment($order)
  361. {
  362. $shipment = [
  363. 'title' => '已发货',
  364. 'tips' => '',
  365. 'time' => '',
  366. ];
  367. //待收货
  368. if ($order['order_status'] == IntegralOrderEnum::ORDER_STATUS_GOODS) {
  369. $shipment['tips'] = '商品已出库';
  370. $shipment['time'] = date('Y-m-d H:i:s', $order['shipping_time']);
  371. }
  372. return $shipment;
  373. }
  374. /**
  375. * @notes 订单物流-订单完成信息
  376. * @param $order
  377. * @return string[]
  378. * @author 段誉
  379. * @date 2022/3/3 17:30
  380. */
  381. public static function getTracesFinish($order)
  382. {
  383. $finish = [
  384. 'title' => '交易完成',
  385. 'tips' => '',
  386. 'time' => '',
  387. ];
  388. //确认收货
  389. if ($order['order_status'] == IntegralOrderEnum::ORDER_STATUS_COMPLETE) {
  390. $finish['tips'] = '订单交易完成';
  391. $finish['time'] = $order['confirm_time'] ? date('Y-m-d H:i:s', $order['confirm_time']) : $order['confirm_time'];
  392. }
  393. return $finish;
  394. }
  395. /**
  396. * @notes 订单物流-物流轨迹
  397. * @param $order
  398. * @return array|bool
  399. * @author 段誉
  400. * @date 2022/3/3 17:31
  401. */
  402. public static function getTraces($order)
  403. {
  404. $express = ConfigServer::get('express', 'way', '', '');
  405. $key = ConfigServer::get($express, 'appkey');
  406. $app = ConfigServer::get($express, 'appsecret');
  407. // 没有配置,没有发货
  408. if (empty($express) || empty($key) || empty($app) || $order['shipping_status'] != IntegralOrderEnum::SHIPPING_FINISH) {
  409. return [];
  410. }
  411. //快递配置设置为快递鸟时
  412. if ($express === 'kdniao') {
  413. $expressage = (new Kdniao($app, $key, Env::get('app.app_debug', 'true')));
  414. $shipping_field = 'codebird';
  415. } else {
  416. $expressage = (new Kd100($app, $key, Env::get('app.app_debug', 'true')));
  417. $shipping_field = 'code100';
  418. }
  419. //快递编码
  420. $shipping_code = Db::name('express')->where(['id' => $order['shipping_id']])->value($shipping_field);
  421. //获取物流轨迹
  422. if ($shipping_code === 'SF' && $express === 'kdniao') {
  423. $expressage->logistics($shipping_code, $order['invoice_no'], substr($order['mobile'], -4));
  424. } else {
  425. $expressage->logistics($shipping_code, $order['invoice_no']);
  426. }
  427. $traces = $expressage->logisticsFormat();
  428. //获取不到物流轨迹时
  429. if ($traces == false) {
  430. $error = $expressage->getError();
  431. $error = json_decode($error, true);
  432. if ($express === 'kdniao') {
  433. if ($error['Success'] == false) {
  434. $traces[] = [$error['Reason']];
  435. }
  436. } else {
  437. if ($error['result'] == false) {
  438. $traces[] = [$error['message']];
  439. }
  440. }
  441. } else {
  442. foreach ($traces as &$item) {
  443. $item = array_values(array_unique($item));
  444. }
  445. }
  446. return $traces;
  447. }
  448. }