StoreCouponUserDao.php 9.4 KB

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