StoreCouponIssueDao.php 13 KB

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