StoreCouponDao.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. * 查询优惠券列表
  43. * @param int $merId
  44. * @param array $where
  45. * @return BaseQuery
  46. * @author xaboy
  47. * @day 2020-05-14
  48. */
  49. public function search(?int $merId, array $where)
  50. {
  51. if(isset($where['is_trader']) && $where['is_trader'] !== ''){
  52. $query = StoreCoupon::hasWhere('merchant',function($query)use($where){
  53. $query->where('is_trader',$where['is_trader']);
  54. });
  55. }else{
  56. $query = StoreCoupon::getDB()->alias('StoreCoupon');
  57. }
  58. $query->when(isset($where['status']) && $where['status'] !== '', function ($query) use ($where) {
  59. $query->where('StoreCoupon.status', (int)$where['status']);
  60. })->when(isset($where['coupon_name']) && $where['coupon_name'] !== '', function ($query) use ($where) {
  61. $query->whereLike('title', "%{$where['coupon_name']}%");
  62. })->when(isset($where['coupon_id']) && $where['coupon_id'] !== '', function ($query) use ($where) {
  63. $query->where('coupon_id', (int)$where['coupon_id']);
  64. })->when(isset($where['send_type']) && $where['send_type'] !== '', function ($query) use ($where) {
  65. $query->where('send_type', (int)$where['send_type']);
  66. })->when(isset($where['not_send_type']) && $where['not_send_type'] !== '', function ($query) use ($where) {
  67. $query->whereNotIn('send_type', $where['not_send_type']);
  68. })->when(isset($where['type']) && $where['type'] !== '', function ($query) use ($where) {
  69. $query->where('type', (int)$where['type']);
  70. })->when($merId !== null, function ($query) use ($merId) {
  71. $query->where('StoreCoupon.mer_id', $merId);
  72. })->when(isset($where['is_mer']) && $where['is_mer'] !== '', function ($query) use ($merId) {
  73. $query->where('type', '<', 10);
  74. })->when(isset($where['coupon_ids']) && $where['coupon_ids'] !== '', function ($query) use ($where) {
  75. $query->whereIn('coupon_id', $where['coupon_ids']);
  76. });
  77. return $query->where('StoreCoupon.is_del', 0)->order(($merId ? 'StoreCoupon.sort DESC,' : '') . 'coupon_id DESC');
  78. }
  79. /**
  80. * 验证查询参数
  81. * @param int|null $type
  82. * @param int $send_type
  83. * @return BaseQuery
  84. * @author xaboy
  85. * @day 2020/6/18
  86. */
  87. public function validCouponQuery($type = null, $send_type = 0)
  88. {
  89. $query = StoreCoupon::getDB()
  90. ->where('status', 1)
  91. ->where('send_type', $send_type)
  92. ->where('is_del', 0)
  93. ->order('sort DESC,coupon_id DESC')
  94. ->when(!is_null($type), function ($query) use ($type) {
  95. $query->where('type', $type);
  96. });
  97. $query->where(function (BaseQuery $query) {
  98. $query->where('is_limited', 0)->whereOr(function (BaseQuery $query) {
  99. $query->where('is_limited', 1)->where('remain_count', '>', 0);
  100. });
  101. })
  102. ->where(function (BaseQuery $query) {
  103. $query->where('is_timeout', 0)->whereOr(function (BaseQuery $query) {
  104. $time = date('Y-m-d H:i:s');
  105. $query->where('is_timeout', 1)->where('start_time', '<', $time)->where('end_time', '>', $time);
  106. });
  107. })
  108. ->where(function (BaseQuery $query) {
  109. $query->where('coupon_type', 0)->whereOr(function (BaseQuery $query) {
  110. $query->where('coupon_type', 1)->where('use_end_time', '>', date('Y-m-d H:i:s'));
  111. });
  112. });
  113. return $query;
  114. }
  115. /**
  116. * 验证带商户的查询参数
  117. * @param $where
  118. * @param null $uid
  119. * @return mixed
  120. * @author wuhaotian
  121. * @email 442384644@qq.com
  122. * @date 2024/7/13
  123. */
  124. public function validCouponQueryWithMerchant($where,$uid = null)
  125. {
  126. $query = StoreCoupon::alias('C')->leftJoin('Merchant M','C.mer_id = M.mer_id')
  127. ->where('C.status', 1)
  128. ->where('C.is_del', 0)
  129. ->when(isset($where['type']) && !is_null($where['type']), function ($query) use ($where) {
  130. if ($where['type'] == '') {
  131. $query->where('C.type', 'in', [0,10,11,12]);
  132. } else {
  133. $query->where('C.type', $where['type']);
  134. }
  135. })
  136. ->when(isset($where['send_type']) && $where['send_type'] !== '', function($query) use($where){
  137. $query->where('C.send_type', $where['send_type']);
  138. })
  139. ->when(isset($where['not_svip']) && $where['not_svip'] !== '', function($query) use($where){
  140. $query->where('C.send_type', '<>',StoreCouponRepository::GET_COUPON_TYPE_SVIP);
  141. })
  142. ->when($uid, function($query) use($uid){
  143. $couponId = StoreCouponUser::where('uid',$uid)->whereIn('status',[1,2])->column('coupon_id');
  144. $query->whereNotIn('C.coupon_id', $couponId);
  145. })
  146. ->when(isset($where['mer_id']) && $where['mer_id'] !== '', function($query) use($where){
  147. $query->where('C.mer_id', $where['mer_id']);
  148. })
  149. ->where(function (BaseQuery $query) {
  150. $query->where('is_limited', 0)->whereOr(function (BaseQuery $query) {
  151. $query->where('is_limited', 1)->where('remain_count', '>', 0);
  152. });
  153. })
  154. ->where(function (BaseQuery $query) {
  155. $query->where('is_timeout', 0)->whereOr(function (BaseQuery $query) {
  156. $time = date('Y-m-d H:i:s');
  157. $query->where('is_timeout', 1)->where('start_time', '<', $time)->where('end_time', '>', $time);
  158. });
  159. })
  160. ->where(function (BaseQuery $query) {
  161. $query->where('C.mer_id', 0)->whereOr(function (BaseQuery $query) {
  162. $query->where('M.is_del',0)->where('M.status',1)->where('M.mer_state',1);
  163. });
  164. })
  165. ->where(function (BaseQuery $query) {
  166. $query->where('coupon_type', 0)->whereOr(function (BaseQuery $query) {
  167. $query->where('coupon_type', 1)->where('use_end_time', '>', date('Y-m-d H:i:s'));
  168. });
  169. });
  170. return $query->order('C.sort DESC,C.create_time DESC');
  171. }
  172. /**
  173. * 获取优惠券详情
  174. * @param $id
  175. * @param $uid
  176. * @return array|Model|null
  177. * @throws DataNotFoundException
  178. * @throws DbException
  179. * @throws ModelNotFoundException
  180. * @author xaboy
  181. * @day 2020/6/19
  182. */
  183. public function validCoupon($id, $uid)
  184. {
  185. return $this->validCouponQuery()->when($uid, function (BaseQuery $query, $uid) {
  186. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  187. $query->where('uid', $uid);
  188. }]);
  189. })->where('coupon_id', $id)->find();
  190. }
  191. /**
  192. * 获取svip优惠券详情
  193. * @param $id
  194. * @param $uid
  195. * @return array|mixed|BaseQuery|Model|null
  196. * @throws DataNotFoundException
  197. * @throws DbException
  198. * @throws ModelNotFoundException
  199. * @author wuhaotian
  200. * @email 442384644@qq.com
  201. * @date 2024/7/13
  202. */
  203. public function validSvipCoupon($id, $uid)
  204. {
  205. return $this->validCouponQuery(null,StoreCouponRepository::GET_COUPON_TYPE_SVIP)->when($uid, function (BaseQuery $query, $uid) {
  206. $query->with(['svipIssue' => function (BaseQuery $query) use ($uid) {
  207. $query->where('uid', $uid);
  208. }]);
  209. })->where('coupon_id', $id)->find();
  210. }
  211. /**
  212. * 查询商户的优惠券列表
  213. * @param $merId
  214. * @param null $uid
  215. * @return Collection
  216. * @throws DbException
  217. * @throws DataNotFoundException
  218. * @throws ModelNotFoundException
  219. * @author xaboy
  220. * @day 2020/6/1
  221. */
  222. public function validMerCoupon($merId, $uid = null, $type = 0)
  223. {
  224. return $this->validCouponQuery($type)->when($uid, function (BaseQuery $query, $uid) {
  225. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  226. $query->where('uid', $uid);
  227. }]);
  228. })->where('mer_id', $merId)->select();
  229. }
  230. /**
  231. * 商户的优惠券是否存在
  232. * @param $merId
  233. * @param null $uid
  234. * @return int
  235. * @author xaboy
  236. * @day 2020/6/19
  237. */
  238. public function validMerCouponExists($merId, $uid = null)
  239. {
  240. return $this->validCouponQuery(0)->when($uid, function (BaseQuery $query, $uid) {
  241. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  242. $query->where('uid', $uid);
  243. }]);
  244. })->where('mer_id', $merId)->count();
  245. }
  246. /**
  247. * 查询商品的优惠券列表
  248. * @param array $couponIds
  249. * @param null $uid
  250. * @return Collection
  251. * @throws DbException
  252. * @throws DataNotFoundException
  253. * @throws ModelNotFoundException
  254. * @author xaboy
  255. * @day 2020/6/1
  256. */
  257. public function validProductCoupon(array $couponIds, $uid = null)
  258. {
  259. return $this->validCouponQuery(1)->when($uid, function (BaseQuery $query, $uid) {
  260. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  261. $query->where('uid', $uid);
  262. }]);
  263. })->whereIn('coupon_id', $couponIds)->select();
  264. }
  265. /**
  266. * 查询商品优惠券数量
  267. * @param array $couponIds
  268. * @param null $uid
  269. * @return int
  270. * @author Qinii
  271. */
  272. public function validProductCouponExists(array $couponIds, $uid = null)
  273. {
  274. return $this->validCouponQuery(1)->when($uid, function (BaseQuery $query, $uid) {
  275. $query->with(['issue' => function (BaseQuery $query) use ($uid) {
  276. $query->where('uid', $uid);
  277. }]);
  278. })->whereIn('coupon_id', $couponIds)->count();
  279. }
  280. /**
  281. * 删除优惠券
  282. * @param int $id
  283. * @return int
  284. * @throws DbException
  285. * @author xaboy
  286. * @day 2020-05-13
  287. */
  288. public function delete(int $id)
  289. {
  290. return StoreCoupon::getDB()->where($this->getPk(), $id)->update(['is_del' => 1]);
  291. }
  292. /**
  293. * 查询优惠券是否存在
  294. * @param int $id
  295. * @return bool
  296. * @author xaboy
  297. * @day 2020-05-13
  298. */
  299. public function exists(int $id)
  300. {
  301. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('is_del', 0)->count($this->getPk()) > 0;
  302. }
  303. /**
  304. * 删除商户的优惠券
  305. * @param int $merId
  306. * @param int $id
  307. * @return int
  308. * @throws DbException
  309. * @author xaboy
  310. * @day 2020-05-13
  311. */
  312. public function merDelete(int $merId, int $id)
  313. {
  314. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->update(['is_del' => 1]);
  315. }
  316. /**
  317. * 商户优惠券是否存在
  318. * @param int $merId
  319. * @param int $id
  320. * @return bool
  321. * @author xaboy
  322. * @day 2020-05-13
  323. */
  324. public function merExists(int $merId, int $id)
  325. {
  326. return StoreCoupon::getDB()->where($this->getPk(), $id)->where('mer_id', $merId)->where('is_del', 0)->count($this->getPk()) > 0;
  327. }
  328. /**
  329. * 新人券获取
  330. * @return Collection
  331. * @throws DataNotFoundException
  332. * @throws DbException
  333. * @throws ModelNotFoundException
  334. * @author xaboy
  335. * @day 2020/6/18
  336. */
  337. public function newPeopleCoupon()
  338. {
  339. if (!systemConfig('newcomer_status') || !systemConfig('register_coupon_status'))
  340. return [];
  341. $register_give_coupon = systemConfig('register_give_coupon');
  342. return $this->validCouponQuery(null, StoreCouponRepository::GET_COUPON_TYPE_ADMIN)
  343. ->whereIn('coupon_id', $register_give_coupon)->select();
  344. }
  345. /**
  346. * 查询赠送的优惠券
  347. * @param array|null $ids
  348. * @return Collection
  349. * @throws DataNotFoundException
  350. * @throws DbException
  351. * @throws ModelNotFoundException
  352. * @author xaboy
  353. * @day 2020/6/19
  354. */
  355. public function getGiveCoupon(array $ids = null)
  356. {
  357. return $this->validCouponQuery(null, 3)->when($ids, function ($query, $ids) {
  358. $query->whereIn('coupon_id', $ids);
  359. })->select();
  360. }
  361. /**
  362. * 清除优惠券
  363. * @param $id
  364. * @param $field
  365. * @throws DbException
  366. * @author wuhaotian
  367. * @email 442384644@qq.com
  368. * @date 2024/7/13
  369. */
  370. public function clear($id,$field)
  371. {
  372. $this->getModel()::getDB()->where($field, $id)->delete();
  373. }
  374. }