StoreCouponDao.php 7.9 KB

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