StoreCouponDao.php 8.0 KB

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