StoreCouponIssueDao.php 13 KB

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