StoreCouponIssue.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. $lst2 = $model2->where('B.type', 1)
  72. ->where('is_give_subscribe',0)
  73. ->where('is_full_give',0)
  74. ->where('B.category_id', 'in', $cate_id)
  75. ->select()
  76. ->hidden(['is_del', 'status'])
  77. ->toArray();
  78. }
  79. }else{
  80. //通用券
  81. $lst3 = $model3->where('B.type', 0)
  82. ->where('is_give_subscribe',0)
  83. ->where('is_full_give',0)
  84. ->select()
  85. ->hidden(['is_del', 'status'])
  86. ->toArray();
  87. }
  88. $list = array_merge($lst1, $lst2, $lst3);
  89. $list = array_unique_fb($list);
  90. if ($page) $list = array_slice($list, ((int)$page - 1) * $limit, $limit);
  91. foreach ($list as $k => $v) {
  92. $v['is_use'] = $uid ? isset($v['used']) : false;
  93. if (!$v['end_time']) {
  94. $v['start_time'] = '';
  95. $v['end_time'] = '不限时';
  96. } else {
  97. $v['start_time'] = date('Y/m/d', $v['start_time']);
  98. $v['end_time'] = $v['end_time'] ? date('Y/m/d', $v['end_time']) : date('Y/m/d', time() + 86400);
  99. }
  100. $v['coupon_price'] = (int)$v['coupon_price'];
  101. $list[$k] = $v;
  102. }
  103. if ($list)
  104. return $list;
  105. else
  106. return [];
  107. }
  108. /**
  109. * @param string $prefix
  110. * @return $this
  111. */
  112. public static function validWhere($prefix = '')
  113. {
  114. $model = new self;
  115. if ($prefix) {
  116. $model->alias($prefix);
  117. $prefix .= '.';
  118. }
  119. $newTime = time();
  120. return $model->where("{$prefix}status", 1)
  121. ->where(function ($query) use ($newTime, $prefix) {
  122. $query->where(function ($query) use ($newTime, $prefix) {
  123. $query->where("{$prefix}start_time", '<', $newTime)->where("{$prefix}end_time", '>', $newTime);
  124. })->whereOr(function ($query) use ($prefix) {
  125. $query->where("{$prefix}start_time", 0)->where("{$prefix}end_time", 0);
  126. });
  127. })->where("{$prefix}is_del", 0)->where("{$prefix}remain_count > 0 OR {$prefix}is_permanent = 1");
  128. }
  129. public static function issueUserCoupon($id, $uid)
  130. {
  131. $issueCouponInfo = self::validWhere()->where('id', $id)->find();
  132. if (!$issueCouponInfo) return self::setErrorInfo('领取的优惠劵已领完或已过期!');
  133. if (StoreCouponIssueUser::be(['uid' => $uid, 'issue_coupon_id' => $id]))
  134. return self::setErrorInfo('已领取过该优惠劵!');
  135. if ($issueCouponInfo['remain_count'] <= 0 && !$issueCouponInfo['is_permanent']) return self::setErrorInfo('抱歉优惠卷已经领取完了!');
  136. self::beginTrans();
  137. $res1 = false != StoreCouponUser::addUserCoupon($uid, $issueCouponInfo['cid']);
  138. $res2 = false != StoreCouponIssueUser::addUserIssue($uid, $id);
  139. $res3 = true;
  140. if ($issueCouponInfo['total_count'] > 0) {
  141. $issueCouponInfo['remain_count'] -= 1;
  142. $res3 = false !== $issueCouponInfo->save();
  143. }
  144. $res = $res1 && $res2 && $res3;
  145. self::checkTrans($res);
  146. return $res;
  147. }
  148. /**
  149. * 优惠券名称
  150. * @param $id
  151. * @return mixed
  152. */
  153. public static function getIssueCouponTitle($id)
  154. {
  155. $cid = self::where('id', $id)->value('cid');
  156. return StoreCoupon::where('id', $cid)->value('title');
  157. }
  158. }