StoreCouponIssueDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. $query->where('receive_type', $where['receive_type']);
  49. });
  50. }
  51. /**
  52. * 获取列表
  53. * @param array $where
  54. * @param int $page
  55. * @param int $limit
  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)
  62. {
  63. return $this->search($where)
  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('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,int $limit = 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(function ($query) {
  122. $query->where('receive_type', 1)->whereOr('receive_type', 4);
  123. })->where(function ($query) {
  124. $query->where(function ($query) {
  125. $query->where('start_time', '<', time())->where('end_time', '>', time());
  126. })->whereOr(function ($query) {
  127. $query->where('start_time', 0)->where('end_time', 0);
  128. });
  129. })->with(['used' => function ($query) use ($uid) {
  130. $query->where('uid', $uid);
  131. }])->where(function ($query) use ($product_id, $cate_ids) {
  132. if ($product_id != 0 && $cate_ids != []) {
  133. $query->whereFindinSet('product_id', $product_id)->whereOr('category_id', 'in', $cate_ids)->whereOr('type', 0);
  134. }
  135. })->when($limit > 0, function($query) use ($limit){
  136. $query->limit($limit);
  137. })->order('sort desc,id desc')->select()->toArray();
  138. }
  139. /**
  140. * 获取优惠券数量
  141. * @param int $productId
  142. * @param int $cateId
  143. * @return mixed
  144. */
  145. public function getIssueCouponCount($productId = 0, $cateId = 0)
  146. {
  147. $model = function ($query) {
  148. $query->where('status', 1)
  149. ->where('is_del', 0)
  150. ->where('remain_count > 0 OR is_permanent = 1')
  151. ->where(function ($query) {
  152. $query->where('receive_type', 4)->whereOr('receive_type', 1);
  153. })->where(function ($query) {
  154. $query->where(function ($query) {
  155. $query->where('start_time', '<', time())->where('end_time', '>', time());
  156. })->whereOr(function ($query) {
  157. $query->where('start_time', 0)->where('end_time', 0);
  158. });
  159. });
  160. };
  161. $count[0] = $this->getModel()->where($model)->where('type', 0)->count();
  162. $count[1] = $this->getModel()->where($model)->where('type', 1)->when($cateId != 0, function ($query) use ($cateId) {
  163. if ($cateId) $query->where('category_id', 'in', $cateId);
  164. })->count();
  165. $count[2] = $this->getModel()->where($model)->where('type', 2)->when($productId != 0, function ($query) use ($productId) {
  166. if ($productId) $query->whereFindinSet('product_id', $productId);
  167. })->count();
  168. return $count;
  169. }
  170. /**
  171. * 获取优惠卷详情
  172. * @param int $id
  173. * @return array|\think\Model|null
  174. * @throws \think\db\exception\DataNotFoundException
  175. * @throws \think\db\exception\DbException
  176. * @throws \think\db\exception\ModelNotFoundException
  177. */
  178. public function getInfo(int $id)
  179. {
  180. return $this->getModel()->where('status', 1)
  181. ->where('id', $id)
  182. ->where('is_del', 0)
  183. ->where('remain_count > 0 OR is_permanent = 1')
  184. ->where(function ($query) {
  185. $query->where(function ($query) {
  186. $query->where('start_time', '<', time())->where('end_time', '>', time());
  187. })->whereOr(function ($query) {
  188. $query->where('start_time', 0)->where('end_time', 0);
  189. });
  190. })->find();
  191. }
  192. /**
  193. * 获取金大于额的优惠卷金额
  194. * @param string $totalPrice
  195. * @return float
  196. */
  197. public function getUserIssuePrice(string $totalPrice)
  198. {
  199. return $this->search(['status' => 1, 'is_full_give' => 1, 'is_del' => 0])
  200. ->where('full_reduction', '<=', $totalPrice)
  201. ->sum('coupon_price');
  202. }
  203. /**
  204. * 获取新人券
  205. * @return array
  206. * @throws \think\db\exception\DataNotFoundException
  207. * @throws \think\db\exception\DbException
  208. * @throws \think\db\exception\ModelNotFoundException
  209. */
  210. public function getNewCoupon()
  211. {
  212. return $this->getModel()->where('status', 1)
  213. ->where('is_del', 0)
  214. ->where('remain_count > 0 OR is_permanent = 1')
  215. ->where('receive_type', 2)
  216. ->where(function ($query) {
  217. $query->where(function ($query) {
  218. $query->where('start_time', '<', time())->where('end_time', '>', time());
  219. })->whereOr(function ($query) {
  220. $query->where('start_time', 0)->where('end_time', 0);
  221. });
  222. })->select()->toArray();
  223. }
  224. /**
  225. * 获取一条优惠券信息
  226. * @param int $id
  227. * @return mixed
  228. */
  229. public function getCouponInfo(int $id)
  230. {
  231. return $this->getModel()->where('id', $id)->where('status', 1)->where('is_del', 0)->find();
  232. }
  233. /**
  234. * 获取满赠、下单、关注赠送优惠券
  235. * @param array $where
  236. * @param string $field
  237. * @return array
  238. * @throws \think\db\exception\DataNotFoundException
  239. * @throws \think\db\exception\DbException
  240. * @throws \think\db\exception\ModelNotFoundException
  241. */
  242. public function getGiveCoupon(array $where, string $field = '*')
  243. {
  244. return $this->getModel()->field($field)
  245. ->where('status', 1)
  246. ->where('is_del', 0)
  247. ->where('remain_count > 0 OR is_permanent = 1')
  248. ->where($where)
  249. ->where(function ($query) {
  250. $query->where(function ($query) {
  251. $query->where('start_time', '<', time())->where('end_time', '>', time());
  252. })->whereOr(function ($query) {
  253. $query->where('start_time', 0)->where('end_time', 0);
  254. });
  255. })->select()->toArray();
  256. }
  257. /**
  258. * 获取商品优惠卷列表
  259. * @param $where
  260. * @param $field
  261. * @return array
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\DbException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. */
  266. public function productCouponList($where, $field)
  267. {
  268. return $this->getModel()->where($where)->field($field)->select()->toArray();
  269. }
  270. /**
  271. * 获取优惠券弹窗列表
  272. * @return array
  273. * @throws \think\db\exception\DataNotFoundException
  274. * @throws \think\db\exception\DbException
  275. * @throws \think\db\exception\ModelNotFoundException
  276. */
  277. public function getTodayCoupon($uid)
  278. {
  279. // return $this->getModel()->where('receive_type', 1)->where('is_del', 0)->whereDay('add_time')->select()->toArray();
  280. return $this->getModel()->where('status', 1)
  281. ->where('is_del', 0)
  282. ->where('remain_count > 0 OR is_permanent = 1')
  283. ->where(function ($query) {
  284. $query->where('receive_type', 1)->whereOr('receive_type', 4);
  285. // $query->where('receive_type', 1);
  286. })->where(function ($query) {
  287. $query->where(function ($query) {
  288. $query->where('start_time', '<', time())->where('end_time', '>', time());
  289. })->whereOr(function ($query) {
  290. $query->where('start_time', 0)->where('end_time', 0);
  291. });
  292. })->when($uid != 0, function ($query) use ($uid) {
  293. $query->with(['used' => function ($query) use ($uid) {
  294. $query->where('uid', $uid);
  295. }]);
  296. })->whereDay('add_time')->order('sort desc,id desc')->select()->toArray();
  297. }
  298. /**api数据获取优惠券
  299. * @param array $where
  300. * @return array
  301. * @throws \think\db\exception\DataNotFoundException
  302. * @throws \think\db\exception\DbException
  303. * @throws \think\db\exception\ModelNotFoundException
  304. */
  305. public function getApiIssueList(array $where)
  306. {
  307. return $this->getModel()->where($where)->select()->toArray();
  308. }
  309. }