StoreCouponIssueDao.php 14 KB

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