You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoreCouponIssueDao.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\dao\activity\coupon;
  4. use app\dao\BaseDao;
  5. use app\model\activity\coupon\StoreCouponIssue;
  6. /**
  7. * 优惠卷
  8. * Class StoreCouponIssueDao
  9. * @package app\dao\activity\coupon
  10. */
  11. class StoreCouponIssueDao extends BaseDao
  12. {
  13. /**
  14. * 设置模型
  15. * @return string
  16. */
  17. protected function setModel(): string
  18. {
  19. return StoreCouponIssue::class;
  20. }
  21. /**
  22. * @param array $where
  23. */
  24. public function search(array $where = [])
  25. {
  26. return parent::search($where)->when(isset($where['type']) && $where['type'] != '', function ($query) use ($where) {
  27. if ($where['type'] == 'send') {
  28. $query->where('receive_type', 3)->where(function ($query1) {
  29. $query1->where(function ($query2) {
  30. $query2->where('start_time', '<=', time())->where('end_time', '>=', time());
  31. })->whereOr(function ($query3) {
  32. $query3->where('start_time', 0)->where('end_time', 0);
  33. });
  34. })->where('status', 1);
  35. }
  36. })->when(isset($where['receive_type']) && $where['receive_type'], function ($query) use ($where) {
  37. $query->where('receive_type', $where['receive_type']);
  38. });
  39. }
  40. /**
  41. * 有效优惠券搜索
  42. * @param array $where
  43. */
  44. public function validSearch(array $where = [])
  45. {
  46. return parent::search($where)
  47. ->where('status', 1)
  48. ->where('is_del', 0)
  49. ->where('remain_count > 0 OR is_permanent = 1')
  50. ->where(function ($query) {
  51. $query->where(function ($query) {
  52. $query->where('start_time', '<=', time())->where('end_time', '>=', time());
  53. })->whereOr(function ($query) {
  54. $query->where('start_time', 0)->where('end_time', 0);
  55. });
  56. });
  57. }
  58. /**
  59. * 获取列表
  60. * @param array $where
  61. * @param int $page
  62. * @param int $limit
  63. * @return array
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. */
  68. public function getList(array $where, int $page = 0, int $limit = 0)
  69. {
  70. return $this->search($where)->when($page && $limit, function ($query) use ($page, $limit) {
  71. $query->page($page, $limit);
  72. })->order('id desc')->select()->toArray();
  73. }
  74. /**
  75. * 获取有效的优惠券列表
  76. * @param array $where
  77. * @param int $page
  78. * @param int $limit
  79. * @param array $with
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function getValidList(array $where = [], int $page = 0, int $limit = 0, array $with = [])
  86. {
  87. return $this->validSearch($where)
  88. ->when(isset($where['not_id']) && $where['not_id'], function($query) use ($where) {
  89. $query->whereNotIn('id', $where['not_id']);
  90. })->when($page && $limit, function ($query) use ($page, $limit) {
  91. $query->page($page, $limit);
  92. })->when($with, function ($query) use ($with) {
  93. $query->with($with);
  94. })->order('id desc')->select()->toArray();
  95. }
  96. /**
  97. * 获取有效的赠送券
  98. * @param array $ids
  99. * @param string $field
  100. * @param int $page
  101. * @param int $limit
  102. * @return array
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. */
  107. public function getValidGiveCoupons(array $ids, string $field = '*' , int $page = 0, int $limit = 0)
  108. {
  109. return $this->validSearch()->field($field)
  110. ->where('receive_type',3)
  111. ->when(count($ids), function ($query) use ($ids) {
  112. $query->whereIn('id', $ids);
  113. })->when($page && $limit, function ($query) use ($page, $limit) {
  114. $query->page($page, $limit);
  115. })->select()->toArray();
  116. }
  117. /**
  118. * 获取优惠券列表
  119. * @param int $uid 用户ID
  120. * @param int $type 0通用,1分类,2商品
  121. * @param int $typeId 分类ID或商品ID
  122. * @param int $page
  123. * @param int $limit
  124. * @return array
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function getIssueCouponList(int $uid, int $type, $typeId, int $page = 0, int $limit = 0)
  130. {
  131. return $this->validSearch()
  132. ->whereIn('receive_type', [1, 4])
  133. ->with(['used' => function ($query) use ($uid) {
  134. $query->where('uid', $uid);
  135. }])
  136. ->where('type', $type)
  137. ->when($type == 1, function ($query) use ($typeId) {
  138. if ($typeId) $query->where('category_id', 'in', $typeId);
  139. })
  140. ->when($type == 2, function ($query) use ($typeId) {
  141. if ($typeId) $query->whereFindinSet('product_id', $typeId);
  142. })
  143. ->when($page && $limit, function ($query) use ($page, $limit) {
  144. $query->page($page, $limit);
  145. })->order('sort desc,id desc')->select()->toArray();
  146. }
  147. /**
  148. * PC端获取优惠券
  149. * @param int $uid
  150. * @param array $cate_ids
  151. * @param int $product_id
  152. * @param string $filed
  153. * @param int $page
  154. * @param int $limit
  155. * @param string $sort
  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 getPcIssueCouponList(int $uid, array $cate_ids = [], int $product_id = 0, string $filed = '*', int $page = 0, int $limit = 0, string $sort = 'sort desc,id desc')
  162. {
  163. return $this->validSearch()->field($filed)
  164. ->whereIn('receive_type', [1, 4])
  165. ->with(['used' => function ($query) use ($uid) {
  166. $query->where('uid', $uid);
  167. }])->where(function ($query) use ($product_id, $cate_ids) {
  168. if ($product_id != 0 && $cate_ids != []) {
  169. $query->whereFindinSet('product_id', $product_id)->whereOr('category_id', 'in', $cate_ids)->whereOr('type', 0);
  170. }
  171. })->when($page && $limit, function ($query) use ($page, $limit) {
  172. $query->page($page, $limit);
  173. })->when(!$page && $limit, function ($query) use ($page, $limit) {
  174. $query->limit($limit);
  175. })->order($sort)->select()->toArray();
  176. }
  177. /**
  178. * 获取优惠券数量
  179. * @param int $productId
  180. * @param int $cateId
  181. * @return mixed
  182. */
  183. public function getIssueCouponCount($productId = 0, $cateId = 0)
  184. {
  185. $count[0] = $this->validSearch()->whereIn('receive_type', [1, 4])->where('type', 0)->count();
  186. $count[1] = $this->validSearch()->whereIn('receive_type', [1, 4])->where('type', 1)->when($cateId != 0, function ($query) use ($cateId) {
  187. if ($cateId) $query->where('category_id', 'in', $cateId);
  188. })->count();
  189. $count[2] = $this->validSearch()->whereIn('receive_type', [1, 4])->where('type', 2)->when($productId != 0, function ($query) use ($productId) {
  190. if ($productId) $query->whereFindinSet('product_id', $productId);
  191. })->count();
  192. return $count;
  193. }
  194. /**
  195. * 获取优惠卷详情
  196. * @param int $id
  197. * @return array|\think\Model|null
  198. * @throws \think\db\exception\DataNotFoundException
  199. * @throws \think\db\exception\DbException
  200. * @throws \think\db\exception\ModelNotFoundException
  201. */
  202. public function getInfo(int $id)
  203. {
  204. return $this->validSearch()->where('id', $id)->find();
  205. }
  206. /**
  207. * 获取金大于额的优惠卷金额
  208. * @param string $totalPrice
  209. * @return float
  210. */
  211. public function getUserIssuePrice(string $totalPrice)
  212. {
  213. return $this->search(['status' => 1, 'is_full_give' => 1, 'is_del' => 0])
  214. ->where('full_reduction', '<=', $totalPrice)
  215. ->sum('coupon_price');
  216. }
  217. /**
  218. * 获取新人券
  219. * @return array
  220. * @throws \think\db\exception\DataNotFoundException
  221. * @throws \think\db\exception\DbException
  222. * @throws \think\db\exception\ModelNotFoundException
  223. */
  224. public function getNewCoupon()
  225. {
  226. return $this->validSearch()->where('receive_type', 2)->select()->toArray();
  227. }
  228. /**
  229. * 获取一条优惠券信息
  230. * @param int $id
  231. * @return mixed
  232. */
  233. public function getCouponInfo(int $id)
  234. {
  235. return $this->getModel()->where('id', $id)->where('status', 1)->where('is_del', 0)->find();
  236. }
  237. /**
  238. * 获取满赠、下单、关注赠送优惠券
  239. * @param array $where
  240. * @param string $field
  241. * @return array
  242. * @throws \think\db\exception\DataNotFoundException
  243. * @throws \think\db\exception\DbException
  244. * @throws \think\db\exception\ModelNotFoundException
  245. */
  246. public function getGiveCoupon(array $where, string $field = '*')
  247. {
  248. return $this->validSearch()->field($field)->where($where)->select()->toArray();
  249. }
  250. /**
  251. * 获取商品优惠卷列表
  252. * @param $where
  253. * @param $field
  254. * @return array
  255. * @throws \think\db\exception\DataNotFoundException
  256. * @throws \think\db\exception\DbException
  257. * @throws \think\db\exception\ModelNotFoundException
  258. */
  259. public function productCouponList($where, $field)
  260. {
  261. return $this->getModel()->where($where)->field($field)->select()->toArray();
  262. }
  263. /**
  264. * 获取优惠券弹窗列表
  265. * @return array
  266. * @throws \think\db\exception\DataNotFoundException
  267. * @throws \think\db\exception\DbException
  268. * @throws \think\db\exception\ModelNotFoundException
  269. */
  270. public function getTodayCoupon($uid)
  271. {
  272. return $this->validSearch()
  273. ->whereIn('receive_type', [1, 4])
  274. ->when($uid != 0, function ($query) use ($uid) {
  275. $query->with(['used' => function ($query) use ($uid) {
  276. $query->where('uid', $uid);
  277. }]);
  278. })->whereDay('add_time')->order('sort desc,id desc')->select()->toArray();
  279. }
  280. /**
  281. * api数据获取优惠券
  282. * @param array $where
  283. * @return array
  284. * @throws \think\db\exception\DataNotFoundException
  285. * @throws \think\db\exception\DbException
  286. * @throws \think\db\exception\ModelNotFoundException
  287. */
  288. public function getApiIssueList(array $where)
  289. {
  290. return $this->getModel()->where($where)->select()->toArray();
  291. }
  292. }