StoreCouponIssue.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\models\store;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\traits\ModelTrait;
  5. use think\db\Where;
  6. /**
  7. * TODO 发布优惠券Model
  8. * Class StoreCouponIssue
  9. * @package app\models\store
  10. */
  11. class StoreCouponIssue extends BaseModel
  12. {
  13. /**
  14. * 数据表主键
  15. * @var string
  16. */
  17. protected $pk = 'id';
  18. /**
  19. * 模型名称
  20. * @var string
  21. */
  22. protected $name = 'store_coupon_issue';
  23. use ModelTrait;
  24. public function used()
  25. {
  26. return $this->hasOne(StoreCouponIssueUser::class, 'issue_coupon_id', 'id')->field('issue_coupon_id');
  27. }
  28. public static function getIssueCouponList($uid, $limit, $page = 0, $type = 0, $product_id = 0)
  29. {
  30. $model1 = self::validWhere('A')->alias('A')
  31. ->join('store_coupon B', 'A.cid = B.id')
  32. ->field('A.*,B.type,B.coupon_price,B.use_min_price,B.title')
  33. ->order('B.sort DESC,A.id DESC');
  34. $model2 = self::validWhere('A')->alias('A')
  35. ->join('store_coupon B', 'A.cid = B.id')
  36. ->field('A.*,B.type,B.coupon_price,B.use_min_price,B.title')
  37. ->order('B.sort DESC,A.id DESC');
  38. $model3 = self::validWhere('A')->alias('A')
  39. ->join('store_coupon B', 'A.cid = B.id')
  40. ->field('A.*,B.type,B.coupon_price,B.use_min_price,B.title')
  41. ->order('B.sort DESC,A.id DESC');
  42. if ($uid) {
  43. $model1->with(['used' => function ($query) use ($uid) {
  44. $query->where('uid', $uid);
  45. }]);
  46. $model2->with(['used' => function ($query) use ($uid) {
  47. $query->where('uid', $uid);
  48. }]);
  49. $model3->with(['used' => function ($query) use ($uid) {
  50. $query->where('uid', $uid);
  51. }]);
  52. }
  53. $lst1 = $lst2 = $lst3 = [];
  54. if ($type) {
  55. if ($product_id) {
  56. //商品券
  57. $lst1 = $model1->where('B.type', 2)
  58. ->where('is_give_subscribe', 0)
  59. ->where('is_full_give', 0)
  60. ->whereFindinSet('B.product_id', $product_id)
  61. ->select()
  62. ->hidden(['is_del', 'status'])
  63. ->toArray();
  64. //品类券
  65. $cate_id = StoreProduct::where('id', $product_id)->value('cate_id');
  66. $category = explode(',', $cate_id);
  67. foreach ($category as $value) {
  68. $temp[] = StoreCategory::where('id', $value)->value('pid');
  69. }
  70. $temp = array_unique($temp);
  71. $cate_id = $cate_id . ',' . implode(',', $temp);
  72. $lst2 = $model2->where('B.type', 1)
  73. ->where('is_give_subscribe', 0)
  74. ->where('is_full_give', 0)
  75. ->where('B.category_id', 'in', $cate_id)
  76. ->select()
  77. ->hidden(['is_del', 'status'])
  78. ->toArray();
  79. }
  80. } else {
  81. //通用券
  82. $lst3 = $model3->where('B.type', 0)
  83. ->where('is_give_subscribe', 0)
  84. ->where('is_full_give', 0)
  85. ->select()
  86. ->hidden(['is_del', 'status'])
  87. ->toArray();
  88. }
  89. $list = array_merge($lst1, $lst2, $lst3);
  90. $list = array_unique_fb($list);
  91. if ($page) $list = array_slice($list, ((int)$page - 1) * $limit, $limit);
  92. foreach ($list as $k => $v) {
  93. $v['is_use'] = $uid ? isset($v['used']) : false;
  94. if (!$v['end_time']) {
  95. $v['start_time'] = '';
  96. $v['end_time'] = '不限时';
  97. } else {
  98. $v['start_time'] = date('Y/m/d', $v['start_time']);
  99. $v['end_time'] = $v['end_time'] ? date('Y/m/d', $v['end_time']) : date('Y/m/d', time() + 86400);
  100. }
  101. $v['coupon_price'] = $v['coupon_price'];
  102. $list[$k] = $v;
  103. }
  104. if ($list)
  105. return $list;
  106. else
  107. return [];
  108. }
  109. /**
  110. * @param string $prefix
  111. * @return $this
  112. */
  113. public static function validWhere($prefix = '')
  114. {
  115. $model = new self;
  116. if ($prefix) {
  117. $model->alias($prefix);
  118. $prefix .= '.';
  119. }
  120. $newTime = time();
  121. return $model->where("{$prefix}status", 1)
  122. ->where(function ($query) use ($newTime, $prefix) {
  123. $query->where(function ($query) use ($newTime, $prefix) {
  124. $query->where("{$prefix}start_time", '<', $newTime)->where("{$prefix}end_time", '>', $newTime);
  125. })->whereOr(function ($query) use ($prefix) {
  126. $query->where("{$prefix}start_time", 0)->where("{$prefix}end_time", 0);
  127. });
  128. })->where("{$prefix}is_del", 0)->where("{$prefix}remain_count > 0 OR {$prefix}is_permanent = 1");
  129. }
  130. public static function issueUserCoupon($id, $uid)
  131. {
  132. $issueCouponInfo = self::validWhere()->where('id', $id)->find();
  133. if (!$issueCouponInfo) return self::setErrorInfo('领取的优惠劵已领完或已过期!');
  134. if (StoreCouponIssueUser::be(['uid' => $uid, 'issue_coupon_id' => $id]))
  135. return self::setErrorInfo('已领取过该优惠劵!');
  136. if ($issueCouponInfo['remain_count'] <= 0 && !$issueCouponInfo['is_permanent']) return self::setErrorInfo('抱歉优惠卷已经领取完了!');
  137. self::beginTrans();
  138. $res1 = false != StoreCouponUser::addUserCoupon($uid, $issueCouponInfo['cid']);
  139. $res2 = false != StoreCouponIssueUser::addUserIssue($uid, $id);
  140. $res3 = true;
  141. if ($issueCouponInfo['total_count'] > 0) {
  142. $issueCouponInfo['remain_count'] -= 1;
  143. $res3 = false !== $issueCouponInfo->save();
  144. }
  145. $res = $res1 && $res2 && $res3;
  146. self::checkTrans($res);
  147. return $res;
  148. }
  149. /**
  150. * 优惠券名称
  151. * @param $id
  152. * @return mixed
  153. */
  154. public static function getIssueCouponTitle($id)
  155. {
  156. $cid = self::where('id', $id)->value('cid');
  157. return StoreCoupon::where('id', $cid)->value('title');
  158. }
  159. }