GiftPackage.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\api\model\plus\giftpackage;
  3. use app\common\model\plus\giftpackage\GiftPackage as GiftPackageModel;
  4. use app\api\model\plus\coupon\Coupon;
  5. use app\api\model\product\Product;
  6. use app\common\enum\settings\DeliveryTypeEnum;
  7. use app\common\model\store\Store as StoreModel;
  8. use app\common\model\settings\Setting as SettingModel;
  9. use app\api\model\plus\giftpackage\Order as OrderModel;
  10. /**
  11. * 礼包购模型
  12. */
  13. class GiftPackage extends GiftPackageModel
  14. {
  15. /**
  16. * 获取数据
  17. */
  18. public function getGiftPackage($id, $params, $user = false)
  19. {
  20. $data = self::detail($id);
  21. if ($data['is_delete'] == 1) {
  22. $this->error = '活动不存在';
  23. return false;
  24. }
  25. $data = $data->toArray();
  26. if ($data['is_coupon'] == '1') {
  27. $data['is_coupon'] = true;
  28. }
  29. if ($data['is_point'] == '1') {
  30. $data['is_point'] = true;
  31. }
  32. $this->buildData($data);
  33. return $data;
  34. }
  35. /**
  36. * 获取数据
  37. */
  38. public function checkGiftPackage($id, $params, $user)
  39. {
  40. $data = self::detail($id);
  41. $data = $data->toArray();
  42. $data['value1'][] = $data['start_time']['text'];
  43. $data['value1'][] = $data['end_time']['text'];
  44. $data['grade_ids'] = explode(',', $data['grade_ids']);
  45. if ($data['status'] == 1) {
  46. $this->error = '活动已终止';
  47. return false;
  48. }
  49. if ($data['is_coupon'] == '1') {
  50. $data['is_coupon'] = true;
  51. }
  52. if ($data['is_point'] == '1') {
  53. $data['is_point'] = true;
  54. }
  55. if ($data['start_time']['value'] > time()) {
  56. $this->error = '活动还未开始';
  57. return false;
  58. }
  59. if ($data['end_time']['value'] < time()) {
  60. $this->error = '活动已结束';
  61. return false;
  62. }
  63. if ($data['is_times'] == 1) {
  64. $where = [
  65. 'user_id' => $user['user_id'],
  66. 'pay_status' => 20,
  67. 'gift_package_id' => $id,
  68. ];
  69. //统计购买数量
  70. $count = (new OrderModel())->where($where)->count('order_id');
  71. //判断购买次数
  72. if ($count >= $data['times']) {
  73. $this->error = '已超过限购数量';
  74. return false;
  75. }
  76. }
  77. //二维码类型10,一码,20,不同码
  78. if ($data['code_type'] == 10) {
  79. //统计已购买数量
  80. $totalcount = (new OrderModel())->where('gift_package_id', '=', $id)
  81. ->where('pay_status', '=', 20)
  82. ->where('order_status', '<>', 20)
  83. ->count();
  84. if ($totalcount >= $data['total_num']) {
  85. $this->error = '礼包数量不足';
  86. return false;
  87. }
  88. else if($data['code_type'] == 20){
  89. //查询码是否使用
  90. $code = $this->where('gift_package_id', '=', $id)
  91. ->where('code', '=', $params['code'])
  92. ->count();
  93. if ($code > 0) {
  94. $this->error = '优惠码已使用';
  95. return false;
  96. }
  97. }
  98. }
  99. //判断是否符合等级
  100. if ($data['is_grade'] == 1 && !in_array($user['grade_id'], $data['grade_ids'])) {
  101. $this->error = '会员等级不符合';
  102. return false;
  103. }
  104. $this->buildData($data);
  105. // 设置默认配送方式
  106. if (!$params['delivery_type']) {
  107. $params['delivery_type'] = current(SettingModel::getItem('store')['delivery_type']);
  108. }
  109. $data['delivery_type'] = $params['delivery_type'];
  110. // 处理配送方式
  111. if ($params['delivery_type'] == DeliveryTypeEnum::EXPRESS && $user) {
  112. $data['address'] = $user['address_default'];
  113. $data['exist_address'] = $user['address_id'] > 0;
  114. } elseif ($params['delivery_type'] == DeliveryTypeEnum::EXTRACT && $params['store_id'] > 0 && $user) {
  115. $data['extract_store'] = StoreModel::detail($params['store_id']);
  116. }
  117. return $data;
  118. }
  119. private function buildData(&$data){
  120. if ($data['coupon_ids'] != '') {
  121. $model = new Coupon();
  122. $coupon = json_decode($data['coupon_ids'], true);
  123. foreach ($coupon as $key => &$value) {
  124. $couponInfo = $model->getCouponInfo($value['coupon_id']);
  125. $value['name'] = $couponInfo['name'];
  126. $value['reduce_price'] = $couponInfo['reduce_price'];
  127. $value['expire_type'] = $couponInfo['expire_type'];
  128. $value['expire_day'] = $couponInfo['expire_day'];
  129. $value['start_time'] = $couponInfo['start_time'];
  130. $value['end_time'] = $couponInfo['end_time'];
  131. }
  132. $data['coupon_list'] = $coupon;
  133. $data['coupon'] = explode(',', $data['coupon_ids']);
  134. }
  135. if ($data['product_ids'] != '') {
  136. $ProductModel = new Product();
  137. $product = $ProductModel->getProduct($data['product_ids']);
  138. $data['product_list'] = $product->toArray();
  139. $data['product'] = explode(',', $data['product_ids']);
  140. }
  141. }
  142. }