StoreCouponUserDao.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\dao\activity\coupon;
  13. use app\dao\BaseDao;
  14. use app\model\activity\coupon\StoreCouponUser;
  15. /**
  16. *
  17. * Class StoreCouponUserDao
  18. * @package app\dao\coupon
  19. */
  20. class StoreCouponUserDao extends BaseDao
  21. {
  22. /**
  23. * 设置模型
  24. * @return string
  25. */
  26. protected function setModel(): string
  27. {
  28. return StoreCouponUser::class;
  29. }
  30. /**
  31. * 获取列表
  32. * @param array $where
  33. * @param int $page
  34. * @param int $limit
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getList(array $where, string $field = '*', array $with = ['issue'], int $page, int $limit)
  41. {
  42. return $this->search($where)->field($field)->with($with)->page($page, $limit)->order('id desc')->select()->toArray();
  43. }
  44. /**
  45. * 使用优惠券修改优惠券状态
  46. * @param $id
  47. * @return \think\Model|null
  48. */
  49. public function useCoupon(int $id)
  50. {
  51. return $this->getModel()->where('id', $id)->update(['status' => 1, 'use_time' => time()]);
  52. }
  53. /**
  54. * 获取指定商品id下的优惠卷
  55. * @param array $productIds
  56. * @param int $uid
  57. * @param string $price
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function productIdsByCoupon(array $productIds, int $uid, string $price)
  63. {
  64. return $this->getModel()->whereIn('cid', function ($query) use ($productIds) {
  65. $query->name('store_coupon_issue')->whereIn('id', function ($q) use ($productIds) {
  66. $q->name('store_coupon_product')->whereIn('product_id', $productIds)->field('coupon_id')->select();
  67. })->field(['id'])->select();
  68. })->with('issue')->where(['uid' => $uid, 'status' => 0])->order('coupon_price DESC')
  69. ->where('use_min_price', '<=', $price)->select()
  70. ->where('start_time', '<', time())->where('end_time', '>', time())
  71. ->hidden(['status', 'is_fail'])->toArray();
  72. }
  73. /**
  74. * 根据商品id获取
  75. * @param array $cateIds
  76. * @param int $uid
  77. * @param string $price
  78. * @return mixed
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function cateIdsByCoupon(array $cateIds, int $uid, string $price)
  84. {
  85. return $this->getModel()->whereIn('cid', function ($query) use ($cateIds) {
  86. $query->name('store_coupon_issue')->whereIn('category_id', $cateIds)->where('type', 1)->field('id')->select();
  87. })->where(['uid' => $uid, 'status' => 0])->where('use_min_price', '<=', $price)
  88. ->where('start_time', '<', time())->where('end_time', '>', time())
  89. ->order('coupon_price DESC')->with('issue')->select()->hidden(['status', 'is_fail'])->toArray();
  90. }
  91. /**
  92. * 获取当前用户可用的优惠卷
  93. * @param array $ids
  94. * @param int $uid
  95. * @param string $price
  96. * @return mixed
  97. * @throws \think\db\exception\DataNotFoundException
  98. * @throws \think\db\exception\DbException
  99. * @throws \think\db\exception\ModelNotFoundException
  100. */
  101. public function getUserCoupon(array $ids, int $uid, string $price)
  102. {
  103. return $this->getModel()->where(['uid' => $uid, 'status' => 0])->when(count($ids) != 0, function ($query) use ($ids) {
  104. $query->whereNotIn('id', $ids);
  105. })->whereIn('cid', function ($query) {
  106. $query->name('store_coupon_issue')->where('type', 0)->field(['id'])->select();
  107. })->where('use_min_price', '<=', $price)
  108. ->where('start_time', '<', time())->where('end_time', '>', time())
  109. ->order('coupon_price DESC')->with('issue')->select()->hidden(['status', 'is_fail'])->toArray();
  110. }
  111. /**
  112. * 获取当前用户所有可用的优惠卷
  113. * @param int $uid
  114. * @return mixed
  115. * @throws \think\db\exception\DataNotFoundException
  116. * @throws \think\db\exception\DbException
  117. * @throws \think\db\exception\ModelNotFoundException
  118. */
  119. public function getUserAllCoupon(int $uid)
  120. {
  121. return $this->getModel()->where(['uid' => $uid, 'status' => 0, 'is_fail' => 0])
  122. ->where('start_time', '<', time())->where('end_time', '>', time())
  123. ->order('coupon_price DESC')->with('issue')->select()->hidden(['status', 'is_fail'])->toArray();
  124. }
  125. /**
  126. * 获取列表带排序
  127. * @param array $where
  128. * @param int $page
  129. * @param int $limit
  130. * @return array
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. public function getCouponListByOrder(array $where, $order, int $page = 0, int $limit = 0)
  136. {
  137. return $this->search($where, false)->with('issue')->when($page > 0 && $limit > 0, function ($qeury) use ($page, $limit) {
  138. $qeury->page($page, $limit);
  139. })->when($order != '', function ($query) use ($order) {
  140. $query->order($order);
  141. })->when(isset($where['coupon_ids']), function ($qeury) use ($where) {
  142. $qeury->whereIn('cid', $where['coupon_ids']);
  143. })->when(isset($where['status']), function ($qeury) use ($where) {
  144. if ($where['status'] == 1) {
  145. $qeury->where(function ($query) {
  146. $query->where('status', 1)->whereOr('status', 2);
  147. });
  148. } else {
  149. $qeury->where('status', $where['status']);
  150. }
  151. })->select()->toArray();
  152. }
  153. /**
  154. * 根据月份查询用户获得的优惠券
  155. * @param array $where
  156. * @return array
  157. * @throws \think\db\exception\DataNotFoundException
  158. * @throws \think\db\exception\DbException
  159. * @throws \think\db\exception\ModelNotFoundException
  160. */
  161. public function memberCouponUserGroupBymonth(array $where)
  162. {
  163. return $this->search($where, false)
  164. ->whereMonth('add_time')
  165. ->whereIn('cid', $where['couponIds'])
  166. ->field('count(id) as num,FROM_UNIXTIME(add_time, \'%Y-%m\') as time')
  167. ->group("FROM_UNIXTIME(add_time, '%Y-%m')")
  168. ->select()->toArray();
  169. }
  170. /**
  171. * 根据时间查询
  172. * @param array $where
  173. * @param string $field
  174. * @return array
  175. * @throws \think\db\exception\DataNotFoundException
  176. * @throws \think\db\exception\DbException
  177. * @throws \think\db\exception\ModelNotFoundException
  178. */
  179. public function getUserCounponByMonth(array $where, string $field = '*')
  180. {
  181. return $this->search($where)->field($field)->whereMonth('add_time')->select()->toArray();
  182. }
  183. /**
  184. * 获取本月领取的优惠券
  185. * @param $uid
  186. * @return array
  187. * @throws \think\db\exception\DataNotFoundException
  188. * @throws \think\db\exception\DbException
  189. * @throws \think\db\exception\ModelNotFoundException
  190. */
  191. public function getVipCouponList($uid)
  192. {
  193. return $this->getModel()->where('uid', $uid)->whereMonth('add_time')->select()->toArray();
  194. }
  195. /**
  196. * 删除用户获得的优惠券
  197. * @param $where
  198. * @return bool
  199. */
  200. public function delUserCoupon($where)
  201. {
  202. return $this->getModel()->where($where)->delete();
  203. }
  204. }