StoreCouponIssue.php 7.6 KB

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