StoreCouponIssueDao.php 13 KB

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