Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. <?php
  2. namespace app\api\model\plus\giftpackage;
  3. use app\api\service\order\PaymentService;
  4. use app\api\service\order\paysuccess\type\GiftPaySuccessService;
  5. use app\common\enum\order\OrderPayTypeEnum;
  6. use app\common\enum\order\OrderTypeEnum;
  7. use app\common\enum\settings\DeliveryTypeEnum;
  8. use app\common\exception\BaseException;
  9. use app\common\model\plus\giftpackage\Order as OrderModel;
  10. use app\common\model\plus\giftpackage\GiftPackage as GiftPackageModel;
  11. use app\api\model\order\OrderAddress as OrderAddress;
  12. use app\api\model\order\Order as OrdersModel;
  13. use app\api\service\user\UserService;
  14. use app\api\model\order\OrderExtract as OrderExtractModel;
  15. use app\api\model\product\Product as ProductModel;
  16. use app\api\model\order\OrderProduct;
  17. use app\api\model\plus\coupon\Coupon;
  18. use app\api\model\product\Product;
  19. use app\common\model\plus\giftpackage\Code as GiftCodeModel;
  20. /**
  21. * 礼包购模型
  22. */
  23. class Order extends OrderModel
  24. {
  25. /**
  26. * 创建礼包购订单
  27. * 返回订单id
  28. */
  29. public function createOrder($user, $gift_package_id, $params)
  30. {
  31. $detail = GiftPackageModel::detail($gift_package_id);
  32. if ($detail['status'] == 1) {
  33. $this->error = '活动已终止';
  34. return false;
  35. }
  36. if ($detail['start_time']['value'] > time()) {
  37. $this->error = '活动还未开始';
  38. return false;
  39. }
  40. if ($detail['end_time']['value'] < time()) {
  41. $this->error = '活动已结束';
  42. return false;
  43. }
  44. if ($detail['is_times'] == 1) {
  45. $where = [
  46. 'user_id' => $user['user_id'],
  47. 'pay_status' => 20,
  48. 'gift_package_id' => $gift_package_id,
  49. ];
  50. //统计购买数量
  51. $count = $this->where($where)->count('order_id');
  52. //判断购买次数
  53. if ($count >= $detail['times']) {
  54. $this->error = '已超过限购数量';
  55. return false;
  56. }
  57. }
  58. //二维码类型10,一码,20,不同码
  59. switch ($detail['code_type']) {
  60. case '10':
  61. //统计已购买数量
  62. $totalcount = $this->where('gift_package_id', '=', $gift_package_id)
  63. ->where('pay_status', '=', 20)
  64. ->where('order_status', '<>', 20)
  65. ->count();
  66. if ($totalcount >= $detail['total_num']) {
  67. $this->error = '礼包数量不足';
  68. return false;
  69. }
  70. break;
  71. case '20':
  72. //查询码是否使用
  73. $code = $this->where('gift_package_id', '=', $gift_package_id)
  74. ->where('code', '=', $params['code'])
  75. ->count();
  76. if ($code > 0) {
  77. $this->error = '优惠码已使用';
  78. return false;
  79. }
  80. break;
  81. }
  82. //选购商品数量
  83. $product_num = count(json_decode($params['product_ids'], true));
  84. if ($product_num > $detail['product_num']) {
  85. $this->error = '商品选购数量超过最大值' . $product_num;
  86. return false;
  87. }
  88. //判断是否符合等级
  89. if ($detail['is_grade'] == 1 && !in_array($user['grade_id'], explode(',', $detail['grade_ids']))) {
  90. return false;
  91. }
  92. if ($params['delivery_type'] == DeliveryTypeEnum::EXPRESS) {
  93. if (empty($params['address']) || $params['address'] == 'null') {
  94. $this->error = '请先选择收货地址';
  95. return false;
  96. }
  97. }
  98. if ($params['delivery_type'] == DeliveryTypeEnum::EXTRACT) {
  99. $extract = json_decode($params['extract'], true);
  100. if ($extract['store_id'] == 0) {
  101. $this->error = '请先选择自提门店';
  102. return false;
  103. }
  104. if (empty($extract['linkman']) || empty($extract['phone'])) {
  105. $this->error = '请填写联系人和电话';
  106. return false;
  107. }
  108. }
  109. $code = isset($params['code']) ? $params['code'] : '';
  110. // 如果是通码
  111. if($detail['code_type'] == 10){
  112. $code = GiftCodeModel::detail($gift_package_id)['code'];
  113. }
  114. //写入order表
  115. $status = $this->save([
  116. 'gift_package_id' => $gift_package_id,
  117. 'order_no' => $this->orderNo(),
  118. 'total_price' => $detail['money'],
  119. 'order_price' => $detail['money'],
  120. 'pay_price' => $detail['money'],
  121. 'user_id' => $user['user_id'],
  122. 'app_id' => self::$app_id,
  123. 'product_ids' => $params['product_ids'],
  124. 'delivery_type' => $params['delivery_type'],
  125. 'address' => isset($params['address']) ? $params['address'] : '',
  126. 'extract' => isset($params['extract']) ? $params['extract'] : '',
  127. 'coupon_ids' => $detail['coupon_ids'],
  128. 'point' => $detail['point'],
  129. 'code' => $code,
  130. ]);
  131. // 余额支付标记订单已支付
  132. if ($status && $params['pay_type'] == OrderPayTypeEnum::BALANCE) {
  133. if ($user['balance'] < $detail['money']) {
  134. $this->error = '用户余额不足,无法使用余额支付';
  135. return false;
  136. }
  137. $this->onPaymentByBalance($this['order_no']);
  138. }
  139. return $status;
  140. }
  141. /**
  142. * 余额支付标记订单已支付
  143. */
  144. public function onPaymentByBalance($orderNo)
  145. {
  146. // 获取订单详情
  147. $PaySuccess = new GiftPaySuccessService($orderNo);
  148. // 发起余额支付
  149. return $PaySuccess->onPaySuccess(OrderPayTypeEnum::BALANCE);
  150. return $status;
  151. }
  152. /**
  153. * 待支付订单详情
  154. */
  155. public static function getPayDetail($orderNo)
  156. {
  157. $model = new static();
  158. return $model->where(['order_no' => $orderNo, 'pay_status' => 10, 'is_delete' => 0])->with(['user'])->find();
  159. }
  160. /**
  161. * 构建支付请求的参数
  162. */
  163. public static function onOrderPayment($user, $order, $payType, $pay_source)
  164. {
  165. if ($payType == OrderPayTypeEnum::WECHAT) {
  166. return self::onPaymentByWechat($user, $order, $pay_source);
  167. }
  168. return [];
  169. }
  170. /**
  171. * 构建微信支付请求
  172. */
  173. protected static function onPaymentByWechat($user, $order, $pay_source)
  174. {
  175. return PaymentService::wechat(
  176. $user,
  177. $order['order_id'],
  178. $order['order_no'],
  179. $order['pay_price'],
  180. OrderTypeEnum::GIFT,
  181. $pay_source
  182. );
  183. }
  184. //支付完成添加订单
  185. public function addOrder($product_ids, $order_no, $app_id = null)
  186. {
  187. is_null($app_id) && $app_id = self::$app_id;
  188. $order = $this->where(['order_no' => $order_no])->find();
  189. $extract = json_decode($order['extract'], true);
  190. $productArray = json_decode($product_ids, true);
  191. foreach ($productArray as $key => $value) {
  192. // 商品详情
  193. $product = ProductModel::detail($value['product_id']);
  194. // 商品sku信息
  195. $product['product_sku'] = ProductModel::getProductSku($product, $value['product_sku_id']);
  196. // 订单数据
  197. $data = [
  198. 'user_id' => $order['user_id'],
  199. 'order_no' => $this->orderNo(),
  200. 'total_price' => $product['spec_type'] == 10 ? $product['product_price'] : $product['product_sku']['product_price'],
  201. 'order_price' => $product['spec_type'] == 10 ? $product['product_price'] : $product['product_sku']['product_price'],
  202. 'coupon_id' => 0,
  203. 'coupon_money' => 0,
  204. 'points_money' => 0,
  205. 'points_num' => 0,
  206. 'pay_price' => 0,
  207. 'delivery_type' => $order['delivery_type'],
  208. 'pay_type' => 20,
  209. 'buyer_remark' => '',
  210. 'order_source' => 60,
  211. 'activity_id' => $order['gift_package_id'],
  212. 'points_bonus' => 0,
  213. 'is_agent' => 0,
  214. 'app_id' => $app_id,
  215. 'pay_status' => 20,
  216. 'pay_time' => time(),
  217. 'pay_source' => 'wx',
  218. 'extract_store_id' => $order['delivery_type'] == 20 ? $extract['store_id'] : 0,
  219. ];
  220. // 保存订单记录
  221. $OrdersModel = new OrdersModel;
  222. $OrdersModel->save($data);
  223. $new_order_id = $OrdersModel->order_id;
  224. if ($order['delivery_type'] == 10) {
  225. $address = json_decode($order['address'], true);
  226. // 记录收货地址
  227. $this->saveOrderAddress($address, $new_order_id, $order['user_id'], $app_id);
  228. } elseif ($order['delivery_type'] == 20) {
  229. // 记录自提信息
  230. $this->saveOrderExtract($extract, $new_order_id, $order['user_id'], $app_id);
  231. }
  232. //添加商品
  233. $this->saveOrderProduct($order, $new_order_id, $product, $app_id);
  234. }
  235. }
  236. /**
  237. * 保存订单商品信息
  238. */
  239. private function saveOrderProduct($order, $status, $product, $app_id = null)
  240. {
  241. is_null($app_id) && $app_id = self::$app_id;
  242. // 订单商品列表
  243. $goods = [
  244. 'order_id' => $status,
  245. 'user_id' => $order['user_id'],
  246. 'app_id' => $app_id,
  247. 'product_id' => $product['product_id'],
  248. 'product_name' => $product['product_name'],
  249. 'image_id' => $product['image'][0]['image_id'],
  250. 'deduct_stock_type' => $product['deduct_stock_type'],
  251. 'spec_type' => $product['spec_type'],
  252. 'spec_sku_id' => $product['product_sku']['spec_sku_id'],
  253. 'product_sku_id' => $product['product_sku']['product_sku_id'],
  254. 'product_attr' => $product['product_sku']['product_attr'],
  255. 'content' => $product['content'],
  256. 'product_no' => $product['product_sku']['product_no'],
  257. 'product_price' => $product['product_sku']['product_price'],
  258. 'line_price' => $product['product_sku']['line_price'],
  259. 'product_weight' => $product['product_sku']['product_weight'],
  260. 'is_user_grade' => (int)$product['is_user_grade'],
  261. 'grade_ratio' => $product['grade_ratio'] ? $product['grade_ratio'] : 0,
  262. 'grade_product_price' => isset($product['grade_product_price']) ? $product['grade_product_price'] : 0,
  263. 'grade_total_money' => 0,
  264. 'coupon_money' => 0,
  265. 'points_money' => isset($product['points_money']) ? $product['points_money'] : 0,
  266. 'points_num' => isset($product['points_num']) ? $product['points_num'] : 0,
  267. 'points_bonus' => 0,
  268. 'total_num' => 1,
  269. 'total_price' => $product['spec_type'] == 10 ? $product['product_price'] : $product['product_sku']['product_price'],
  270. 'total_pay_price' => 0,
  271. 'is_ind_agent' => $product['is_ind_agent'],
  272. 'agent_money_type' => $product['agent_money_type'],
  273. 'first_money' => 0,
  274. 'second_money' => 0,
  275. 'third_money' => 0,
  276. 'fullreduce_money' => 0,
  277. ];
  278. // 记录订单商品来源id
  279. $goods['product_source_id'] = isset($product['product_source_id']) ? $product['product_source_id'] : 0;
  280. // 记录订单商品sku来源id
  281. $goods['sku_source_id'] = isset($product['sku_source_id']) ? $product['sku_source_id'] : 0;
  282. // 记录拼团类的商品来源id
  283. $goods['bill_source_id'] = isset($product['bill_source_id']) ? $product['bill_source_id'] : 0;
  284. $model = new OrderProduct();
  285. return $model->save($goods);
  286. }
  287. /**
  288. * 记录收货地址
  289. */
  290. private function saveOrderAddress($address, $order_id, $user_id, $app_id = null)
  291. {
  292. $model = new OrderAddress();
  293. is_null($app_id) && $app_id = self::$app_id;
  294. return $model->save([
  295. 'order_id' => $order_id,
  296. 'user_id' => $user_id,
  297. 'app_id' => $app_id,
  298. 'name' => $address['name'],
  299. 'phone' => $address['phone'],
  300. 'province_id' => $address['province_id'],
  301. 'city_id' => $address['city_id'],
  302. 'region_id' => $address['region_id'],
  303. 'detail' => $address['detail'],
  304. ]);
  305. }
  306. /**
  307. * 保存上门自提联系人
  308. */
  309. private function saveOrderExtract($extract, $order_id, $user_id, $app_id = null)
  310. {
  311. $OrderExtract = new OrderExtractModel;
  312. // 记忆上门自提联系人(缓存),用于下次自动填写
  313. UserService::setLastExtract($user_id, trim($extract['linkman']), trim($extract['phone']));
  314. is_null($app_id) && $app_id = self::$app_id;
  315. // 保存上门自提联系人(数据库)
  316. return $OrderExtract->save([
  317. 'order_id' => $order_id,
  318. 'linkman' => trim($extract['linkman']),
  319. 'phone' => trim($extract['phone']),
  320. 'user_id' => $user_id,
  321. 'app_id' => $app_id,
  322. ]);
  323. }
  324. //购买列表
  325. public function getList($user, $data)
  326. {
  327. $list = $this->with(['gift'])
  328. ->field('order_no,pay_price,pay_type,pay_time,coupon_ids,product_ids,gift_package_id,point')
  329. ->where('user_id', '=', $user['user_id'])
  330. ->where('pay_status', '=', 20)
  331. ->order('create_time', 'desc')
  332. ->paginate($data);
  333. foreach ($list as $key => &$value) {
  334. $value['product_num'] = count(json_decode($value['product_ids']));
  335. $coupon_num = 0;
  336. if ($value['coupon_ids']) {
  337. $coupon_ids = json_decode($value['coupon_ids'], true);
  338. foreach ($coupon_ids as $k => $v) {
  339. $coupon_num += $v['coupon_num'];
  340. }
  341. }
  342. $value['coupon_num'] = $coupon_num;
  343. }
  344. return $list;
  345. }
  346. //订单详情
  347. public function orderDetail($order_no)
  348. {
  349. $detail = $this->where('order_no', '=', $order_no)->find();
  350. if ($detail['coupon_ids']) {
  351. $Coupon = new Coupon();
  352. $coupon = json_decode($detail['coupon_ids'], true);
  353. foreach ($coupon as $key => &$value) {
  354. $couponInfo = $Coupon->getCouponInfo($value['coupon_id']);
  355. $value['name'] = $couponInfo['name'];
  356. $value['reduce_price'] = $couponInfo['reduce_price'];
  357. $value['expire_type'] = $couponInfo['expire_type'];
  358. $value['expire_day'] = $couponInfo['expire_day'];
  359. $value['start_time'] = $couponInfo['start_time'];
  360. $value['end_time'] = $couponInfo['end_time'];
  361. }
  362. $detail['coupon_list'] = $coupon;
  363. }
  364. if ($detail['product_ids']) {
  365. $ProductModel = new Product();
  366. $product = $ProductModel->getProductList($detail['product_ids']);
  367. $detail['product_list'] = $product->toArray();
  368. }
  369. $detail['address'] = $detail['address'] ? json_decode($detail['address'], true) : '';
  370. $detail['extract'] = $detail['extract'] ? json_decode($detail['extract'], true) : '';
  371. return $detail;
  372. }
  373. /**
  374. * 订单详情
  375. */
  376. public static function getUserOrderDetail($order_id, $user_id)
  377. {
  378. $model = new static();
  379. $order = $model->where(['order_id' => $order_id, 'user_id' => $user_id])->find();
  380. if (empty($order)) {
  381. throw new BaseException(['msg' => '订单不存在']);
  382. }
  383. return $order;
  384. }
  385. }