StoreCouponIssue.php 6.3 KB

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