StoreCouponDao.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. namespace app\common\dao\store\coupon;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\coupon\StoreCoupon;
  15. use app\common\repositories\system\merchant\MerchantRepository;
  16. use think\Collection;
  17. use think\db\BaseQuery;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\Model;
  22. /**
  23. * Class StoreCouponIssueDao
  24. * @package app\common\dao\store\coupon
  25. * @author xaboy
  26. * @day 2020-05-14
  27. */
  28. class StoreCouponDao extends BaseDao
  29. {
  30. /**
  31. * @return BaseModel
  32. * @author xaboy
  33. * @day 2020-03-30
  34. */
  35. protected function getModel(): string
  36. {
  37. return StoreCoupon::class;
  38. }
  39. /**
  40. * @param int $merId
  41. * @param array $where
  42. * @return BaseQuery
  43. * @author xaboy
  44. * @day 2020-05-14
  45. */
  46. public function search(?int $merId, array $where)
  47. {
  48. if(isset($where['is_trader']) && $where['is_trader'] !== ''){
  49. $query = StoreCoupon::hasWhere('merchant',function($query)use($where){
  50. $query->where('is_trader',$where['is_trader']);
  51. });
  52. }else{
  53. $query = StoreCoupon::getDB()->alias('StoreCoupon');
  54. }
  55. $query->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  56. $query->where('StoreCoupon.status', (int)$where['status']);
  57. })->when(isset($where['coupon_name']) && $where['coupon_name'] !== '', function ($query) use ($where) {
  58. $query->whereLike('title', "%{$where['coupon_name']}%");
  59. })->when(isset($where['send_type']) && $where['send_type'] !== '', function ($query) use ($where) {
  60. $query->where('send_type', (int)$where['send_type']);
  61. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  62. $query->where('type', (int)$where['type']);
  63. })->when($merId !== null, function ($query) use ($merId) {
  64. $query->where('StoreCoupon.mer_id', $merId);
  65. });
  66. return $query->where('StoreCoupon.is_del', 0)->order(($merId ? 'StoreCoupon.sort DESC,' : '') . 'coupon_id DESC');
  67. }
  68. /**
  69. * @param int|null $type
  70. * @param int $send_type
  71. * @return BaseQuery
  72. * @author xaboy
  73. * @day 2020/6/18
  74. */
  75. public function validCouponQuery(int $type = null, $send_type = 0)
  76. {
  77. $query = StoreCoupon::getDB()->where('status', 1)->where('send_type', $send_type)->where('is_del', 0)->order('sort DESC,coupon_id DESC')->when(!is_null($type), function ($query) use ($type) {
  78. $query->where('type', $type);
  79. });
  80. $query->where(function (BaseQuery $query) {
  81. $query->where('is_limited', 0)->whereOr(function (BaseQuery $query) {
  82. $query->where('is_limited', 1)->where('remain_count', '>', 0);
  83. });
  84. })->where(function (BaseQuery $query) {
  85. $query->where('is_timeout', 0)->whereOr(function (BaseQuery $query) {
  86. $time = date('Y-m-d H:i:s');
  87. $query->where('is_timeout', 1)->where('start_time', '<', $time)->where('end_time', '>', $time);
  88. });
  89. })->where(function (BaseQuery $query) {
  90. $query->where('coupon_type', 0)->whereOr(function (BaseQuery $query) {
  91. $query->where('coupon_type', 1)->where('use_end_time', '>', date('Y-m-d H:i:s'));
  92. });
  93. });
  94. return $query;
  95. }
  96. /**
  97. * @param $id
  98. * @param $uid
  99. * @return array|Model|null
  100. * @throws DataNotFoundException
  101. * @throws DbException
  102. * @throws ModelNotFoundException
  103. * @author xaboy
  104. * @day 2020/6/19
  105. */
  106. public function validCoupon($id, $uid)
  107. {
  108. return $this->validCouponQuery()->when($uid, function (BaseQuery $query, $uid) {
  109. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  110. $query->where('uid', $uid);
  111. }]);
  112. })->where('coupon_id', $id)->find();
  113. }
  114. /**
  115. * @param $merId
  116. * @param null $uid
  117. * @return Collection
  118. * @throws DbException
  119. * @throws DataNotFoundException
  120. * @throws ModelNotFoundException
  121. * @author xaboy
  122. * @day 2020/6/1
  123. */
  124. public function validMerCoupon($merId, $uid = null, $type = 0)
  125. {
  126. return $this->validCouponQuery($type)->when($uid, function (BaseQuery $query, $uid) {
  127. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  128. $query->where('uid', $uid);
  129. }]);
  130. })->where('mer_id', $merId)->select();
  131. }
  132. /**
  133. * @param $merId
  134. * @param null $uid
  135. * @return int
  136. * @author xaboy
  137. * @day 2020/6/19
  138. */
  139. public function validMerCouponExists($merId, $uid = null)
  140. {
  141. return $this->validCouponQuery(0)->when($uid, function (BaseQuery $query, $uid) {
  142. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  143. $query->where('uid', $uid);
  144. }]);
  145. })->where('mer_id', $merId)->count();
  146. }
  147. /**
  148. * @param array $couponIds
  149. * @param null $uid
  150. * @return Collection
  151. * @throws DbException
  152. * @throws DataNotFoundException
  153. * @throws ModelNotFoundException
  154. * @author xaboy
  155. * @day 2020/6/1
  156. */
  157. public function validProductCoupon(array $couponIds, $uid = null)
  158. {
  159. return $this->validCouponQuery(1)->when($uid, function (BaseQuery $query, $uid) {
  160. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  161. $query->where('uid', $uid);
  162. }]);
  163. })->whereIn('coupon_id', $couponIds)->select();
  164. }
  165. /**
  166. * @param array $couponIds
  167. * @param null $uid
  168. * @return int
  169. * @author Qinii
  170. */
  171. public function validProductCouponExists(array $couponIds, $uid = null)
  172. {
  173. return $this->validCouponQuery(1)->when($uid, function (BaseQuery $query, $uid) {
  174. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  175. $query->where('uid', $uid);
  176. }]);
  177. })->whereIn('coupon_id', $couponIds)->count();
  178. }
  179. /**
  180. * @param int $id
  181. * @return int
  182. * @throws DbException
  183. * @author xaboy
  184. * @day 2020-05-13
  185. */
  186. public function delete(int $id)
  187. {
  188. return StoreCoupon::getDB()->where($this->getPk(), $id)->update(['is_del' => 1]);
  189. }
  190. /**
  191. * @param int $id
  192. * @return bool
  193. * @author xaboy
  194. * @day 2020-05-13
  195. */
  196. public function exists(int $id)
  197. {
  198. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('is_del', 0)->count($this->getPk()) > 0;
  199. }
  200. /**
  201. * @param int $merId
  202. * @param int $id
  203. * @return int
  204. * @throws DbException
  205. * @author xaboy
  206. * @day 2020-05-13
  207. */
  208. public function merDelete(int $merId, int $id)
  209. {
  210. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->update(['is_del' => 1]);
  211. }
  212. /**
  213. * @param int $merId
  214. * @param int $id
  215. * @return bool
  216. * @author xaboy
  217. * @day 2020-05-13
  218. */
  219. public function merExists(int $merId, int $id)
  220. {
  221. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->where('is_del', 0)->count($this->getPk()) > 0;
  222. }
  223. /**
  224. * @return Collection
  225. * @throws DataNotFoundException
  226. * @throws DbException
  227. * @throws ModelNotFoundException
  228. * @author xaboy
  229. * @day 2020/6/18
  230. */
  231. public function newPeopleCoupon()
  232. {
  233. return $this->validCouponQuery(null, 2)->select();
  234. }
  235. /**
  236. * @param array|null $ids
  237. * @return Collection
  238. * @throws DataNotFoundException
  239. * @throws DbException
  240. * @throws ModelNotFoundException
  241. * @author xaboy
  242. * @day 2020/6/19
  243. */
  244. public function getGiveCoupon(array $ids = null)
  245. {
  246. return $this->validCouponQuery(null, 3)->when($ids, function ($query, $ids) {
  247. $query->whereIn('coupon_id', $ids);
  248. })->select();
  249. }
  250. }