You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Order.php 12 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace addons\unishop\behavior;
  3. use addons\unishop\extend\Ali;
  4. use addons\unishop\extend\Hashids;
  5. use addons\unishop\extend\Wechat;
  6. use addons\unishop\model\Address;
  7. use addons\unishop\model\Config;
  8. use addons\unishop\model\DeliveryRule as DeliveryRuleModel;
  9. use addons\unishop\model\OrderProduct;
  10. use addons\unishop\model\Product;
  11. use app\admin\model\unishop\Coupon;
  12. use think\Db;
  13. use think\Exception;
  14. /**
  15. * 订单相关行为
  16. * Class Order
  17. * @package addons\unishop\behavior
  18. */
  19. class Order
  20. {
  21. /**
  22. * 创建订单之后
  23. * 行为一:根据订单减少商品库存 增加"已下单未支付数量"
  24. * 行为二:如果选了购物车的就删除购物车的信息
  25. * @param array $params 商品属性
  26. * @param array $extra [specNumber] => ['spec1' => 'number1','spec2' => 'number2']
  27. */
  28. public function createOrderAfter(&$params, $extra)
  29. {
  30. // 行为一
  31. $key = 0;
  32. $productExtend = new \addons\unishop\extend\Product;
  33. $prefix = \think\Config::get('database.prefix');
  34. if (Config::isPessimism()) {
  35. // 悲观锁
  36. foreach ($extra['specNumber'] as $spec => $number) {
  37. $result = 0;
  38. if (is_numeric($spec) && $params[$key]['use_spec'] == Product::SPEC_OFF) {
  39. $result = Db::execute('UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number} WHERE id = {$params[$key]['id']}");
  40. } else if ($params[$key]['use_spec'] == Product::SPEC_ON) {
  41. $info = $productExtend->getBaseData($params[$key], $spec);
  42. // mysql<5.7.13时用
  43. //if (mysql < 5.7.13) {
  44. $spec = str_replace(',', '","', $spec);
  45. $search = '"stock":"' . $info['stock'] . '","value":["' . $spec . '"]';
  46. $stock = $info['stock'] - $number;
  47. $replace = '"stock":\"' . $stock . '\","value":["' . $spec . '"]';
  48. $sql = 'UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, stock = stock-{$number}, real_sales = real_sales+{$number} ,`specTableList` = REPLACE(specTableList,'$search','$replace') WHERE id = {$params[$key]['id']}";
  49. $result = Db::execute($sql);
  50. //}
  51. //下面语句直接操作JSON
  52. //if (mysql >= 5.7.13) {
  53. //$info['stock'] -= $number;
  54. //$result = Db::execute("UPDATE fa_unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number},specTableList = JSON_REPLACE(specTableList, '$[{$info['key']}].stock', {$info['stock']}) WHERE id = {$params[$key]['id']}");
  55. //}
  56. }
  57. if ($result == 0) { // 锁生效
  58. throw new Exception('下单失败,请重试');
  59. }
  60. $key++;
  61. }
  62. } else {
  63. // 乐观锁
  64. foreach ($extra['specNumber'] as $spec => $number) {
  65. $result = 0;
  66. if (is_numeric($spec) && $params[$key]['use_spec'] == Product::SPEC_OFF) {
  67. $result = Db::execute('UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number} WHERE id = {$params[$key]['id']} AND stock = {$params[$key]['stock']}");
  68. } else if ($params[$key]['use_spec'] == Product::SPEC_ON) {
  69. $info = $productExtend->getBaseData($params[$key], $spec);
  70. // mysql<5.7.13时用
  71. //if (mysql < 5.7.13) {
  72. $spec = str_replace(',', '","', $spec);
  73. $search = '"stock":"' . $info['stock'] . '","value":["' . $spec . '"]';
  74. $stock = $info['stock'] - $number;
  75. $replace = '"stock":\"' . $stock . '\","value":["' . $spec . '"]';
  76. $sql = 'UPDATE ' . $prefix . "unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number},`specTableList` = REPLACE(specTableList,'$search','$replace') WHERE id = {$params[$key]['id']} AND stock = {$params[$key]['stock']}";
  77. $result = Db::execute($sql);
  78. //}
  79. //下面语句直接操作JSON
  80. //if (mysql >= 5.7.13) {
  81. //$info['stock'] -= $number;
  82. //$result = Db::execute("UPDATE fa_unishop_product SET no_buy_yet = no_buy_yet+{$number}, real_sales = real_sales+{$number}, stock = stock-{$number},specTableList = JSON_REPLACE(specTableList, '$[{$info['key']}].stock', {$info['stock']}) WHERE id = {$params[$key]['id']} AND stock = {$params[$key]['stock']}");
  83. //}
  84. }
  85. if ($result == 0) { // 锁生效
  86. throw new Exception('下单失败,请重试');
  87. }
  88. $key++;
  89. }
  90. }
  91. // 行为二
  92. if (!empty($extra['cart'])) {
  93. $cart = $extra['cart'];
  94. Db::execute('DELETE FROM ' . $prefix . "unishop_cart WHERE id IN ($cart) AND user_id = $extra[userId]");
  95. }
  96. // More ...
  97. }
  98. /**
  99. * 检查是否符合创建订单的条件
  100. * 条件1:商品是否存在
  101. * 条件2:商品库存情况
  102. * 条件3:收货地址是否在范围内
  103. * 条件4:是否使用优惠券,优惠券能否可用
  104. * @param array $params
  105. * @param array $extra
  106. * @throws Exception
  107. * @throws \think\exception\DbException
  108. */
  109. public function createOrderBefore(&$params, $extra)
  110. {
  111. $specs = explode(',', $extra['spec']);
  112. foreach ($specs as &$spec) {
  113. $spec = str_replace('|', ',', $spec);
  114. }
  115. $numbers = explode(',', $extra['number']);
  116. $productIds = explode(',', $extra['product_id']);
  117. if (count($specs) !== count($numbers) || count($specs) !== count($productIds)) {
  118. throw new Exception(__('Parameter error'));
  119. }
  120. // 订单价格
  121. $orderPrice = 0;
  122. // 条件一
  123. $products = [];
  124. foreach ($productIds as $key => &$productId) {
  125. $productId = Hashids::decodeHex($productId);
  126. $products[$key] = Db::name('unishop_product')
  127. ->where(['id' => $productId, 'switch' => Product::SWITCH_ON])
  128. ->lock(Config::isPessimism()) // Todo 是否使用悲观锁
  129. ->find();
  130. if (!$products[$key]) {
  131. throw new Exception(__('There are not exist or Offline'));
  132. }
  133. }
  134. if (count($products) == 0 || count($productIds) != count($products)) {
  135. throw new Exception(__('There are offline product'));
  136. }
  137. // 从购物车下单多个商品时,有同一个商品的不同规格导致减库存问题
  138. if (count($productIds) > 0) {
  139. $reduceStock = [];
  140. foreach ($products as $key => $value) {
  141. if (!isset($reduceStock[$value['id']])) {
  142. $reduceStock[$value['id']] = $numbers[$key];
  143. } else {
  144. $products[$key]['stock'] -= $reduceStock[$value['id']];
  145. $reduceStock[$value['id']] += $numbers[$key];
  146. }
  147. }
  148. }
  149. // 条件二
  150. foreach ($products as $key => $product) {
  151. $productInfo = (new \addons\unishop\extend\Product())->getBaseData($product, $specs[$key] ? $specs[$key] : '');
  152. if ($productInfo['stock'] < $numbers[$key]) {
  153. throw new Exception(__('Insufficient inventory,%s pieces left', $productInfo['stock']));
  154. }
  155. $orderPrice = bcadd($orderPrice, bcmul($productInfo['sales_price'], $numbers[$key], 2), 2);
  156. $baseProductInfo[] = $productInfo;
  157. }
  158. // 条件三
  159. $delivery = (new DeliveryRuleModel())->cityInScopeOfDelivery($extra['city_id'], $extra['delivery_id']);
  160. if (!$delivery) {
  161. throw new Exception(__('Your receiving address is not within the scope of delivery'));
  162. } else {
  163. if ($delivery['min'] > array_sum($numbers)) {
  164. throw new Exception(__('You must purchase at least %s item to use this shipping method', $delivery['min']));
  165. }
  166. }
  167. $address = (new Address)->where(['id' => $extra['address_id'], 'user_id' => $extra['userId']])->find();
  168. if (!$address) {
  169. throw new Exception(__('Address not exist'));
  170. }
  171. // 条件四
  172. if ($extra['coupon_id']) {
  173. $coupon = Coupon::get($extra['coupon_id']);
  174. if ($coupon['switch'] == Coupon::SWITCH_OFF || $coupon['deletetime'] || $coupon['starttime'] > time() || $coupon['endtime'] < time()) {
  175. throw new Exception('此优惠券不可用');
  176. }
  177. // 至少消费多少钱
  178. if ($coupon['least'] > $orderPrice) {
  179. throw new Exception('选中的优惠券不满足使用条件');
  180. }
  181. } else {
  182. $coupon = [];
  183. }
  184. $params = [$products, $delivery, $coupon, $baseProductInfo, $address, $orderPrice, $specs, $numbers];
  185. }
  186. /**
  187. * 支付成功
  188. * 行为一:更改订单的支付状态,更新支付时间。
  189. * 行为二:减少商品的已下单但未支付的数量
  190. * @param $params
  191. * @param $extra
  192. * @throws \think\db\exception\DataNotFoundException
  193. * @throws \think\db\exception\ModelNotFoundException
  194. * @throws \think\exception\DbException
  195. */
  196. public function paidSuccess(&$params, $extra)
  197. {
  198. $order = &$params;
  199. $order->have_paid = time();// 更新支付时间为当前时间
  200. $order->pay_type = $extra['pay_type'];
  201. $order->save();
  202. $orderProductModel = new OrderProduct();
  203. $orderProducts = $orderProductModel
  204. ->with('product')
  205. ->where(['order_id' => $order->id])
  206. ->select();
  207. foreach ($orderProducts as $product) {
  208. if (isset($product->product)) {
  209. $product->product->no_buy_yet -= $product->number;
  210. $product->product->save();
  211. }
  212. }
  213. // More ...
  214. }
  215. /**
  216. * 支付失败
  217. * @param $params
  218. */
  219. public function paidFail(&$params)
  220. {
  221. $order = &$params;
  222. $order->have_paid = \addons\unishop\model\Order::PAID_NO;
  223. $order->save();
  224. // More ...
  225. }
  226. /**
  227. * 订单退款
  228. * 行为一:退款
  229. * @param array $params 订单数据
  230. */
  231. public function orderRefund(&$params)
  232. {
  233. $order = &$params;
  234. // 行为一
  235. switch ($order['pay_type']) {
  236. case \addons\unishop\model\Order::PAY_WXPAY:
  237. $app = Wechat::initEasyWechat('payment');
  238. $result = $app->refund->byOutTradeNumber($params['out_trade_no'], $params['out_trade_no'], bcmul($params['total_price'], 100), bcmul($params['refund']['amount'], 100), [
  239. // 可在此处传入其他参数,详细参数见微信支付文档
  240. 'refund_desc' => $params['refund']['reason_type'],
  241. ]);
  242. break;
  243. case \addons\unishop\model\Order::PAY_ALIPAY:
  244. $alipay = Ali::initAliPay();
  245. $order = [
  246. 'out_trade_no' => $params['out_trade_no'],
  247. 'refund_amount' => $params['total_price'],
  248. ];
  249. $result = $alipay->refund($order);
  250. break;
  251. }
  252. // More ...
  253. }
  254. }