StoreCouponIssueDao.php 15 KB

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