StoreCouponIssueDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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\coupon;
  13. use app\dao\BaseDao;
  14. use app\model\coupon\StoreCouponIssue;
  15. use app\services\user\UserServices;
  16. /**
  17. *
  18. * Class StoreCouponIssueDao
  19. * @package app\dao\coupon
  20. */
  21. class StoreCouponIssueDao extends BaseDao
  22. {
  23. /**
  24. * 设置模型
  25. * @return string
  26. */
  27. protected function setModel(): string
  28. {
  29. return StoreCouponIssue::class;
  30. }
  31. /**
  32. * @param array $where
  33. * @return \crmeb\basic\BaseModel|mixed|\think\Model
  34. */
  35. public function search(array $where = [])
  36. {
  37. return parent::search($where)->when(isset($where['type']) && $where['type'] != '', function ($query) use ($where) {
  38. if ($where['type'] == 'send') {
  39. $query->where('receive_type', 3)->where(function ($query1) {
  40. $query1->where(function ($query2) {
  41. $query2->where('start_time', '<', time())->where('end_time', '>', time());
  42. })->whereOr(function ($query3) {
  43. $query3->where('start_time', 0)->where('end_time', 0);
  44. });
  45. })->where('status', 1);
  46. }
  47. })->when(isset($where['receive_type']) && $where['receive_type'], function ($query) use ($where) {
  48. if (in_array((int)$where['receive_type'], [1, 2, 3])) {
  49. $query->where('receive_type', $where['receive_type']);
  50. }
  51. });
  52. }
  53. /**
  54. * 获取列表
  55. * @param array $where
  56. * @param int $page
  57. * @param int $limit
  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)
  64. {
  65. return $this->search($where)
  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 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('receive_type', 1)->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('category_id', 'in', $typeId);
  98. })
  99. ->when($type == 2, function ($query) use ($typeId) {
  100. if ($typeId) $query->whereFindinSet('product_id', $typeId);
  101. })
  102. ->when($page != 0, function ($query) use ($page, $limit) {
  103. $query->page($page, $limit);
  104. })->order('sort desc,id desc')->select()->toArray();
  105. }
  106. /**
  107. * PC端获取优惠券
  108. * @param int $uid
  109. * @param array $cate_ids
  110. * @param int $product_id
  111. * @return array
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function getPcIssueCouponList(int $uid, $cate_ids = [], $product_id = 0)
  117. {
  118. return $this->getModel()->where('status', 1)
  119. ->where('is_del', 0)
  120. ->where('remain_count > 0 OR is_permanent = 1')
  121. ->where('receive_type', 1)->where(function ($query) {
  122. $query->where(function ($query) {
  123. $query->where('start_time', '<', time())->where('end_time', '>', time());
  124. })->whereOr(function ($query) {
  125. $query->where('start_time', 0)->where('end_time', 0);
  126. });
  127. })->with(['used' => function ($query) use ($uid) {
  128. $query->where('uid', $uid);
  129. }])->where(function ($query) use ($product_id, $cate_ids) {
  130. if ($product_id != 0 && $cate_ids != []) {
  131. $query->whereFindinSet('product_id', $product_id)->whereOr('category_id', 'in', $cate_ids)->whereOr('type', 0);
  132. }
  133. })->order('sort desc,id desc')->select()->toArray();
  134. }
  135. /**
  136. * 获取优惠券数量
  137. * @param int $productId
  138. * @param int $cateId
  139. * @return mixed
  140. */
  141. public function getIssueCouponCount($productId = 0, $cateId = 0)
  142. {
  143. $model = function ($query) {
  144. $query->where('status', 1)
  145. ->where('is_del', 0)
  146. ->where('remain_count > 0 OR is_permanent = 1')
  147. ->where('receive_type', 1)->where(function ($query) {
  148. $query->where(function ($query) {
  149. $query->where('start_time', '<', time())->where('end_time', '>', time());
  150. })->whereOr(function ($query) {
  151. $query->where('start_time', 0)->where('end_time', 0);
  152. });
  153. });
  154. };
  155. $count[0] = $this->getModel()->where($model)->where('type', 0)->count();
  156. $count[1] = $this->getModel()->where($model)->where('type', 1)->when($cateId != 0, function ($query) use ($cateId) {
  157. if ($cateId) $query->where('category_id', 'in', $cateId);
  158. })->count();
  159. $count[2] = $this->getModel()->where($model)->where('type', 2)->when($productId != 0, function ($query) use ($productId) {
  160. if ($productId) $query->whereFindinSet('product_id', $productId);
  161. })->count();
  162. return $count;
  163. }
  164. /**
  165. * 获取优惠卷详情
  166. * @param int $id
  167. * @return array|\think\Model|null
  168. * @throws \think\db\exception\DataNotFoundException
  169. * @throws \think\db\exception\DbException
  170. * @throws \think\db\exception\ModelNotFoundException
  171. */
  172. public function getInfo(int $id)
  173. {
  174. return $this->getModel()->where('status', 1)
  175. ->where('id', $id)
  176. ->where('is_del', 0)
  177. ->where('remain_count > 0 OR is_permanent = 1')
  178. ->where(function ($query) {
  179. $query->where(function ($query) {
  180. $query->where('start_time', '<', time())->where('end_time', '>', time());
  181. })->whereOr(function ($query) {
  182. $query->where('start_time', 0)->where('end_time', 0);
  183. });
  184. })->find();
  185. }
  186. /**
  187. * 获取金大于额的优惠卷金额
  188. * @param string $totalPrice
  189. * @return float
  190. */
  191. public function getUserIssuePrice(string $totalPrice)
  192. {
  193. return $this->search(['status' => 1, 'is_full_give' => 1, 'is_del' => 0])
  194. ->where('full_reduction', '<=', $totalPrice)
  195. ->sum('coupon_price');
  196. }
  197. /**
  198. * 获取新人券
  199. * @return array
  200. * @throws \think\db\exception\DataNotFoundException
  201. * @throws \think\db\exception\DbException
  202. * @throws \think\db\exception\ModelNotFoundException
  203. */
  204. public function getNewCoupon()
  205. {
  206. return $this->getModel()->where('status', 1)
  207. ->where('is_del', 0)
  208. ->where('remain_count > 0 OR is_permanent = 1')
  209. ->where('receive_type', 2)
  210. ->where(function ($query) {
  211. $query->where(function ($query) {
  212. $query->where('start_time', '<', time())->where('end_time', '>', time());
  213. })->whereOr(function ($query) {
  214. $query->where('start_time', 0)->where('end_time', 0);
  215. });
  216. })->select()->toArray();
  217. }
  218. /**
  219. * 获取一条优惠券信息
  220. * @param int $id
  221. * @return mixed
  222. */
  223. public function getCouponInfo(int $id)
  224. {
  225. return $this->getModel()->where('id', $id)->where('status', 1)->where('is_del', 0)->find();
  226. }
  227. /**
  228. * 获取满赠、下单、关注赠送优惠券
  229. * @param array $where
  230. * @param string $field
  231. * @return array
  232. * @throws \think\db\exception\DataNotFoundException
  233. * @throws \think\db\exception\DbException
  234. * @throws \think\db\exception\ModelNotFoundException
  235. */
  236. public function getGiveCoupon(array $where, string $field = '*')
  237. {
  238. return $this->getModel()->field($field)
  239. ->where('status', 1)
  240. ->where('is_del', 0)
  241. ->where('remain_count > 0 OR is_permanent = 1')
  242. ->where($where)
  243. ->where(function ($query) {
  244. $query->where(function ($query) {
  245. $query->where('start_time', '<', time())->where('end_time', '>', time());
  246. })->whereOr(function ($query) {
  247. $query->where('start_time', 0)->where('end_time', 0);
  248. });
  249. })->select()->toArray();
  250. }
  251. /**
  252. * 获取商品优惠卷列表
  253. * @param $where
  254. * @param $field
  255. * @return array
  256. * @throws \think\db\exception\DataNotFoundException
  257. * @throws \think\db\exception\DbException
  258. * @throws \think\db\exception\ModelNotFoundException
  259. */
  260. public function productCouponList($where, $field)
  261. {
  262. return $this->getModel()->where($where)->field($field)->select()->toArray();
  263. }
  264. /**
  265. * 获取优惠券弹窗列表
  266. * @return array
  267. * @throws \think\db\exception\DataNotFoundException
  268. * @throws \think\db\exception\DbException
  269. * @throws \think\db\exception\ModelNotFoundException
  270. */
  271. public function getTodayCoupon($uid)
  272. {
  273. // return $this->getModel()->where('receive_type', 1)->where('is_del', 0)->whereDay('add_time')->select()->toArray();
  274. return $this->getModel()->where('status', 1)
  275. ->where('is_del', 0)
  276. ->where('remain_count > 0 OR is_permanent = 1')
  277. ->where(function ($query) {
  278. $query->where('receive_type', 1)->whereOr('receive_type', 4);
  279. // $query->where('receive_type', 1);
  280. })->where(function ($query) {
  281. $query->where(function ($query) {
  282. $query->where('start_time', '<', time())->where('end_time', '>', time());
  283. })->whereOr(function ($query) {
  284. $query->where('start_time', 0)->where('end_time', 0);
  285. });
  286. })->when($uid != 0, function ($query) use ($uid) {
  287. $query->with(['used' => function ($query) use ($uid) {
  288. $query->where('uid', $uid);
  289. }]);
  290. })->whereDay('add_time')->order('sort desc,id desc')->select()->toArray();
  291. }
  292. /**api数据获取优惠券
  293. * @param array $where
  294. * @return array
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\DbException
  297. * @throws \think\db\exception\ModelNotFoundException
  298. */
  299. public function getApiIssueList(array $where)
  300. {
  301. return $this->getModel()->where($where)->select()->toArray();
  302. }
  303. }