StoreCouponDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\model\store\coupon\StoreCouponUser;
  16. use app\common\repositories\store\coupon\StoreCouponRepository;
  17. use app\common\repositories\system\merchant\MerchantRepository;
  18. use think\Collection;
  19. use think\db\BaseQuery;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\Model;
  24. /**
  25. * Class StoreCouponIssueDao
  26. * @package app\common\dao\store\coupon
  27. * @author xaboy
  28. * @day 2020-05-14
  29. */
  30. class StoreCouponDao extends BaseDao
  31. {
  32. /**
  33. * @return BaseModel
  34. * @author xaboy
  35. * @day 2020-03-30
  36. */
  37. protected function getModel(): string
  38. {
  39. return StoreCoupon::class;
  40. }
  41. /**
  42. * @param int $merId
  43. * @param array $where
  44. * @return BaseQuery
  45. * @author xaboy
  46. * @day 2020-05-14
  47. */
  48. public function search(?int $merId, array $where)
  49. {
  50. if(isset($where['is_trader']) && $where['is_trader'] !== ''){
  51. $query = StoreCoupon::hasWhere('merchant',function($query)use($where){
  52. $query->where('is_trader',$where['is_trader']);
  53. });
  54. }else{
  55. $query = StoreCoupon::getDB()->alias('StoreCoupon');
  56. }
  57. $query->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  58. $query->where('StoreCoupon.status', (int)$where['status']);
  59. })->when(isset($where['coupon_name']) && $where['coupon_name'] !== '', function ($query) use ($where) {
  60. $query->whereLike('title', "%{$where['coupon_name']}%");
  61. })->when(isset($where['coupon_id']) && $where['coupon_id'] !== '', function ($query) use ($where) {
  62. $query->where('coupon_id', (int)$where['coupon_id']);
  63. })->when(isset($where['send_type']) && $where['send_type'] !== '', function ($query) use ($where) {
  64. $query->where('send_type', (int)$where['send_type']);
  65. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  66. $query->where('type', (int)$where['type']);
  67. })->when($merId !== null, function ($query) use ($merId) {
  68. $query->where('StoreCoupon.mer_id', $merId);
  69. })->when(isset($where['is_mer']) && $where['is_mer'] !== '', function ($query) use ($merId) {
  70. $query->where('type', '<', 10);
  71. });
  72. return $query->where('StoreCoupon.is_del', 0)->order(($merId ? 'StoreCoupon.sort DESC,' : '') . 'coupon_id DESC');
  73. }
  74. /**
  75. * @param int|null $type
  76. * @param int $send_type
  77. * @return BaseQuery
  78. * @author xaboy
  79. * @day 2020/6/18
  80. */
  81. public function validCouponQuery($type = null, $send_type = 0)
  82. {
  83. $query = StoreCoupon::getDB()
  84. ->where('status', 1)
  85. ->where('send_type', $send_type)
  86. ->where('is_del', 0)
  87. ->order('sort DESC,coupon_id DESC')
  88. ->when(!is_null($type), function ($query) use ($type) {
  89. $query->where('type', $type);
  90. });
  91. $query->where(function (BaseQuery $query) {
  92. $query->where('is_limited', 0)->whereOr(function (BaseQuery $query) {
  93. $query->where('is_limited', 1)->where('remain_count', '>', 0);
  94. });
  95. })
  96. ->where(function (BaseQuery $query) {
  97. $query->where('is_timeout', 0)->whereOr(function (BaseQuery $query) {
  98. $time = date('Y-m-d H:i:s');
  99. $query->where('is_timeout', 1)->where('start_time', '<', $time)->where('end_time', '>', $time);
  100. });
  101. })
  102. ->where(function (BaseQuery $query) {
  103. $query->where('coupon_type', 0)->whereOr(function (BaseQuery $query) {
  104. $query->where('coupon_type', 1)->where('use_end_time', '>', date('Y-m-d H:i:s'));
  105. });
  106. });
  107. return $query;
  108. }
  109. public function validCouponQueryWithMerchant($where,$uid = null)
  110. {
  111. $query = StoreCoupon::alias('C')->leftJoin('Merchant M','C.mer_id = M.mer_id')
  112. ->where('C.status', 1)
  113. ->where('C.is_del', 0)
  114. ->when(isset($where['type']) && !is_null($where['type']), function ($query) use ($where) {
  115. if ($where['type'] == '') {
  116. $query->where('C.type', 'in', [0,10,11,12]);
  117. } else {
  118. $query->where('C.type', $where['type']);
  119. }
  120. })
  121. ->when(isset($where['send_type']) && $where['send_type'] != '', function($query) use($where){
  122. $query->where('C.send_type', $where['send_type']);
  123. })
  124. ->when(isset($where['not_svip']) && $where['not_svip'] != '', function($query) use($where){
  125. $query->where('C.send_type', '<>',StoreCouponRepository::GET_COUPON_TYPE_SVIP);
  126. })
  127. ->when($uid, function($query) use($uid){
  128. $couponId = StoreCouponUser::where('uid',$uid)->whereIn('status',[1,2])->column('coupon_id');
  129. $query->whereNotIn('C.coupon_id', $couponId);
  130. })
  131. ->when(isset($where['mer_id']) && $where['mer_id'] != '', function($query) use($where){
  132. $query->where('C.mer_id', $where['mer_id']);
  133. })
  134. ->where(function (BaseQuery $query) {
  135. $query->where('is_limited', 0)->whereOr(function (BaseQuery $query) {
  136. $query->where('is_limited', 1)->where('remain_count', '>', 0);
  137. });
  138. })
  139. ->where(function (BaseQuery $query) {
  140. $query->where('is_timeout', 0)->whereOr(function (BaseQuery $query) {
  141. $time = date('Y-m-d H:i:s');
  142. $query->where('is_timeout', 1)->where('start_time', '<', $time)->where('end_time', '>', $time);
  143. });
  144. })
  145. ->where(function (BaseQuery $query) {
  146. $query->where('C.mer_id', 0)->whereOr(function (BaseQuery $query) {
  147. $query->where('M.is_del',0)->where('M.status',1)->where('M.mer_state',1);
  148. });
  149. })
  150. ->where(function (BaseQuery $query) {
  151. $query->where('coupon_type', 0)->whereOr(function (BaseQuery $query) {
  152. $query->where('coupon_type', 1)->where('use_end_time', '>', date('Y-m-d H:i:s'));
  153. });
  154. });
  155. return $query->order('C.sort DESC,C.create_time DESC');
  156. }
  157. /**
  158. * @param $id
  159. * @param $uid
  160. * @return array|Model|null
  161. * @throws DataNotFoundException
  162. * @throws DbException
  163. * @throws ModelNotFoundException
  164. * @author xaboy
  165. * @day 2020/6/19
  166. */
  167. public function validCoupon($id, $uid)
  168. {
  169. return $this->validCouponQuery()->when($uid, function (BaseQuery $query, $uid) {
  170. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  171. $query->where('uid', $uid);
  172. }]);
  173. })->where('coupon_id', $id)->find();
  174. }
  175. public function validSvipCoupon($id, $uid)
  176. {
  177. return $this->validCouponQuery(null,StoreCouponRepository::GET_COUPON_TYPE_SVIP)->when($uid, function (BaseQuery $query, $uid) {
  178. $query->with(['svipIssue' => function (BaseQuery $query) use ($uid) {
  179. $query->where('uid', $uid);
  180. }]);
  181. })->where('coupon_id', $id)->find();
  182. }
  183. /**
  184. * @param $merId
  185. * @param null $uid
  186. * @return Collection
  187. * @throws DbException
  188. * @throws DataNotFoundException
  189. * @throws ModelNotFoundException
  190. * @author xaboy
  191. * @day 2020/6/1
  192. */
  193. public function validMerCoupon($merId, $uid = null, $type = 0)
  194. {
  195. return $this->validCouponQuery($type)->when($uid, function (BaseQuery $query, $uid) {
  196. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  197. $query->where('uid', $uid);
  198. }]);
  199. })->where('mer_id', $merId)->select();
  200. }
  201. /**
  202. * @param $merId
  203. * @param null $uid
  204. * @return int
  205. * @author xaboy
  206. * @day 2020/6/19
  207. */
  208. public function validMerCouponExists($merId, $uid = null)
  209. {
  210. return $this->validCouponQuery(0)->when($uid, function (BaseQuery $query, $uid) {
  211. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  212. $query->where('uid', $uid);
  213. }]);
  214. })->where('mer_id', $merId)->count();
  215. }
  216. /**
  217. * @param array $couponIds
  218. * @param null $uid
  219. * @return Collection
  220. * @throws DbException
  221. * @throws DataNotFoundException
  222. * @throws ModelNotFoundException
  223. * @author xaboy
  224. * @day 2020/6/1
  225. */
  226. public function validProductCoupon(array $couponIds, $uid = null)
  227. {
  228. return $this->validCouponQuery(1)->when($uid, function (BaseQuery $query, $uid) {
  229. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  230. $query->where('uid', $uid);
  231. }]);
  232. })->whereIn('coupon_id', $couponIds)->select();
  233. }
  234. /**
  235. * @param array $couponIds
  236. * @param null $uid
  237. * @return int
  238. * @author Qinii
  239. */
  240. public function validProductCouponExists(array $couponIds, $uid = null)
  241. {
  242. return $this->validCouponQuery(1)->when($uid, function (BaseQuery $query, $uid) {
  243. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  244. $query->where('uid', $uid);
  245. }]);
  246. })->whereIn('coupon_id', $couponIds)->count();
  247. }
  248. /**
  249. * @param int $id
  250. * @return int
  251. * @throws DbException
  252. * @author xaboy
  253. * @day 2020-05-13
  254. */
  255. public function delete(int $id)
  256. {
  257. return StoreCoupon::getDB()->where($this->getPk(), $id)->update(['is_del' => 1]);
  258. }
  259. /**
  260. * @param int $id
  261. * @return bool
  262. * @author xaboy
  263. * @day 2020-05-13
  264. */
  265. public function exists(int $id)
  266. {
  267. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('is_del', 0)->count($this->getPk()) > 0;
  268. }
  269. /**
  270. * @param int $merId
  271. * @param int $id
  272. * @return int
  273. * @throws DbException
  274. * @author xaboy
  275. * @day 2020-05-13
  276. */
  277. public function merDelete(int $merId, int $id)
  278. {
  279. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->update(['is_del' => 1]);
  280. }
  281. /**
  282. * @param int $merId
  283. * @param int $id
  284. * @return bool
  285. * @author xaboy
  286. * @day 2020-05-13
  287. */
  288. public function merExists(int $merId, int $id)
  289. {
  290. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->where('is_del', 0)->count($this->getPk()) > 0;
  291. }
  292. /**
  293. * @return Collection
  294. * @throws DataNotFoundException
  295. * @throws DbException
  296. * @throws ModelNotFoundException
  297. * @author xaboy
  298. * @day 2020/6/18
  299. */
  300. public function newPeopleCoupon()
  301. {
  302. return $this->validCouponQuery(null, 2)->select();
  303. }
  304. /**
  305. * @param array|null $ids
  306. * @return Collection
  307. * @throws DataNotFoundException
  308. * @throws DbException
  309. * @throws ModelNotFoundException
  310. * @author xaboy
  311. * @day 2020/6/19
  312. */
  313. public function getGiveCoupon(array $ids = null)
  314. {
  315. return $this->validCouponQuery(null, 3)->when($ids, function ($query, $ids) {
  316. $query->whereIn('coupon_id', $ids);
  317. })->select();
  318. }
  319. }