StoreCouponIssueDao.php 15 KB

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