StoreCouponIssueDao.php 14 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. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  33. */
  34. public function search(array $where = [])
  35. {
  36. return parent::search($where)->when(isset($where['type']) && $where['type'] != '', function ($query) use ($where) {
  37. if ($where['type'] == 'send') {
  38. $query->where('receive_type', 3)->where(function ($query1) {
  39. $query1->where(function ($query2) {
  40. $query2->where('start_time', '<', time())->where('end_time', '>', time());
  41. })->whereOr(function ($query3) {
  42. $query3->where('start_time', 0)->where('end_time', 0);
  43. });
  44. })->where('status', 1);
  45. }
  46. })->when(isset($where['receive_type']) && $where['receive_type'], function ($query) use ($where) {
  47. $query->where('receive_type', $where['receive_type']);
  48. });
  49. }
  50. /**
  51. * 获取列表
  52. * @param array $where
  53. * @param int $page
  54. * @param int $limit
  55. * @param string $field
  56. * @return array
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function getList(array $where, int $page, int $limit, string $field = '*')
  62. {
  63. return $this->search($where)->field($field)
  64. ->page($page, $limit)->order('id desc')->select()->toArray();
  65. }
  66. /**
  67. * 获取优惠券列表
  68. * @param int $uid 用户ID
  69. * @param int $type 0通用,1分类,2商品
  70. * @param array|int $typeId 分类ID或商品ID
  71. * @param int $page
  72. * @param int $limit
  73. * @return array
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function getIssueCouponList(int $uid, int $type, $typeId, int $page, int $limit)
  79. {
  80. return $this->getModel()->where('status', 1)
  81. ->where('is_del', 0)
  82. ->where('remain_count > 0 OR is_permanent = 1')
  83. ->where(function ($query) {
  84. $query->where('receive_type', 1)->whereOr('receive_type', 4);
  85. })->where(function ($query) {
  86. $query->where(function ($query) {
  87. $query->where('start_time', '<', time())->where('end_time', '>', time());
  88. })->whereOr(function ($query) {
  89. $query->where('start_time', 0)->where('end_time', 0);
  90. });
  91. })
  92. ->with(['used' => function ($query) use ($uid) {
  93. $query->where('uid', $uid);
  94. }])
  95. ->where('type', $type)
  96. ->when($type == 1, function ($query) use ($typeId) {
  97. if ($typeId) {
  98. $query->where(function ($query) use ($typeId) {
  99. $query->where('id', 'in', function ($query) use ($typeId) {
  100. $query->name('store_coupon_product')->whereIn('category_id', $typeId)->field(['coupon_id'])->select();
  101. })->whereOr('category_id', 'in', $typeId);
  102. });
  103. }
  104. })
  105. ->when($type == 2, function ($query) use ($typeId) {
  106. if ($typeId) $query->whereFindinSet('product_id', $typeId);
  107. })
  108. ->when($page != 0, function ($query) use ($page, $limit) {
  109. $query->page($page, $limit);
  110. })->order('sort desc,id desc')->select()->toArray();
  111. }
  112. /**
  113. * PC端获取优惠券
  114. * @param int $uid
  115. * @param array $cate_ids
  116. * @param int $product_id
  117. * @return array
  118. * @throws \think\db\exception\DataNotFoundException
  119. * @throws \think\db\exception\DbException
  120. * @throws \think\db\exception\ModelNotFoundException
  121. */
  122. public function getPcIssueCouponList(int $uid, $cate_ids = [], $product_id = 0, int $limit = 0)
  123. {
  124. return $this->getModel()->where('status', 1)
  125. ->where('is_del', 0)
  126. ->where('remain_count > 0 OR is_permanent = 1')
  127. ->where(function ($query) {
  128. $query->where('receive_type', 1)->whereOr('receive_type', 4);
  129. })->where(function ($query) {
  130. $query->where(function ($query) {
  131. $query->where('start_time', '<', time())->where('end_time', '>', time());
  132. })->whereOr(function ($query) {
  133. $query->where('start_time', 0)->where('end_time', 0);
  134. });
  135. })->with(['used' => function ($query) use ($uid) {
  136. $query->where('uid', $uid);
  137. }])->where(function ($query) use ($product_id, $cate_ids) {
  138. if ($product_id != 0 && $cate_ids != []) {
  139. $query->whereFindinSet('product_id', $product_id)->whereOr('id', 'in', function ($query) use ($cate_ids) {
  140. $query->name('store_coupon_product')->whereIn('category_id', $cate_ids)->field(['coupon_id'])->select();
  141. })->whereOr('category_id', 'in', $cate_ids)->whereOr('type', 0);
  142. }
  143. })->when($limit > 0, function ($query) use ($limit) {
  144. $query->limit($limit);
  145. })->order('sort desc,id desc')->select()->toArray();
  146. }
  147. /**
  148. * 获取优惠券数量
  149. * @param $productId
  150. * @param $cateId
  151. * @return mixed
  152. */
  153. public function getIssueCouponCount($productId, $cateId)
  154. {
  155. $model = function ($query) {
  156. $query->where('status', 1)
  157. ->where('is_del', 0)
  158. ->where('remain_count > 0 OR is_permanent = 1')
  159. ->where(function ($query) {
  160. $query->where('receive_type', 4)->whereOr('receive_type', 1);
  161. })->where(function ($query) {
  162. $query->where(function ($query) {
  163. $query->where('start_time', '<', time())->where('end_time', '>', time());
  164. })->whereOr(function ($query) {
  165. $query->where('start_time', 0)->where('end_time', 0);
  166. });
  167. });
  168. };
  169. $count[0] = $this->getModel()->where($model)->where('type', 0)->count();
  170. $count[1] = $this->getModel()->where($model)->where('type', 1)
  171. ->when(count($cateId) != 0, function ($query) use ($cateId) {
  172. $query->where(function ($query) use ($cateId) {
  173. $query->where('id', 'in', function ($query) use ($cateId) {
  174. $query->name('store_coupon_product')->whereIn('category_id', $cateId)->field(['coupon_id'])->select();
  175. })->whereOr('category_id', 'in', $cateId);
  176. });
  177. })->count();
  178. $count[2] = $this->getModel()->where($model)->where('type', 2)
  179. ->when($productId != 0, function ($query) use ($productId) {
  180. if ($productId) $query->whereFindinSet('product_id', $productId);
  181. })->count();
  182. return $count;
  183. }
  184. /**
  185. * 获取优惠卷详情
  186. * @param int $id
  187. * @return array|\think\Model|null
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public function getInfo(int $id)
  193. {
  194. return $this->getModel()->where('status', 1)
  195. ->where('id', $id)
  196. ->where('is_del', 0)
  197. ->where('remain_count > 0 OR is_permanent = 1')
  198. ->where(function ($query) {
  199. $query->where(function ($query) {
  200. $query->where('start_time', '<', time())->where('end_time', '>', time());
  201. })->whereOr(function ($query) {
  202. $query->where('start_time', 0)->where('end_time', 0);
  203. });
  204. })->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->getModel()->where('status', 1)
  227. ->where('is_del', 0)
  228. ->where('remain_count > 0 OR is_permanent = 1')
  229. ->where('receive_type', 2)
  230. ->where(function ($query) {
  231. $query->where(function ($query) {
  232. $query->where('start_time', '<', time())->where('end_time', '>', time());
  233. })->whereOr(function ($query) {
  234. $query->where('start_time', 0)->where('end_time', 0);
  235. });
  236. })->select()->toArray();
  237. }
  238. /**
  239. * 获取一条优惠券信息
  240. * @param int $id
  241. * @return mixed
  242. */
  243. public function getCouponInfo(int $id)
  244. {
  245. return $this->getModel()->where('id', $id)->where('status', 1)->where('is_del', 0)->find();
  246. }
  247. /**
  248. * 获取满赠、下单、关注赠送优惠券
  249. * @param array $where
  250. * @param string $field
  251. * @return array
  252. * @throws \think\db\exception\DataNotFoundException
  253. * @throws \think\db\exception\DbException
  254. * @throws \think\db\exception\ModelNotFoundException
  255. */
  256. public function getGiveCoupon(array $where, string $field = '*')
  257. {
  258. return $this->getModel()->field($field)
  259. ->where('status', 1)
  260. ->where('is_del', 0)
  261. ->where('remain_count > 0 OR is_permanent = 1')
  262. ->where($where)
  263. ->where(function ($query) {
  264. $query->where(function ($query) {
  265. $query->where('start_time', '<', time())->where('end_time', '>', time());
  266. })->whereOr(function ($query) {
  267. $query->where('start_time', 0)->where('end_time', 0);
  268. });
  269. })->select()->toArray();
  270. }
  271. /**
  272. * 获取商品优惠卷列表
  273. * @param $where
  274. * @param $field
  275. * @return array
  276. * @throws \think\db\exception\DataNotFoundException
  277. * @throws \think\db\exception\DbException
  278. * @throws \think\db\exception\ModelNotFoundException
  279. */
  280. public function productCouponList($where, $field)
  281. {
  282. return $this->getModel()->where($where)->field($field)->select()->toArray();
  283. }
  284. /**
  285. * 获取优惠券弹窗列表
  286. * @return array
  287. * @throws \think\db\exception\DataNotFoundException
  288. * @throws \think\db\exception\DbException
  289. * @throws \think\db\exception\ModelNotFoundException
  290. */
  291. public function getTodayCoupon($uid)
  292. {
  293. // return $this->getModel()->where('receive_type', 1)->where('is_del', 0)->whereDay('add_time')->select()->toArray();
  294. return $this->getModel()->where('status', 1)
  295. ->where('is_del', 0)
  296. ->where('remain_count > 0 OR is_permanent = 1')
  297. ->where(function ($query) {
  298. $query->where('receive_type', 1)->whereOr('receive_type', 4);
  299. // $query->where('receive_type', 1);
  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. }