StoreCouponSendRepository.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\repositories\store\coupon;
  12. use app\common\dao\store\coupon\StoreCouponSendDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\user\UserMerchantRepository;
  15. use crmeb\jobs\MerchantSendCouponJob;
  16. use think\exception\ValidateException;
  17. use think\facade\Cache;
  18. use think\facade\Db;
  19. use think\facade\Queue;
  20. /**
  21. * @mixin StoreCouponSendDao
  22. */
  23. class StoreCouponSendRepository extends BaseRepository
  24. {
  25. public function __construct(StoreCouponSendDao $dao)
  26. {
  27. $this->dao = $dao;
  28. }
  29. public function getList(array $where, $page, $limit)
  30. {
  31. $query = $this->dao->search($where);
  32. $count = $query->count();
  33. $list = $query->setOption('field', [])->field('B.*,A.coupon_num,A.create_time,A.status as send_status, A.mark,A.coupon_send_id')
  34. ->append(['useCount', 'used_num'])->page($page, $limit)->order('coupon_send_id DESC')->select();
  35. return compact('count', 'list');
  36. }
  37. public function create($data, $merId)
  38. {
  39. $userMerchantRepository = app()->make(UserMerchantRepository::class);
  40. $query = null;
  41. $coupon = app()->make(StoreCouponRepository::class)->getWhere(['mer_id' => $merId, 'coupon_id' => $data['coupon_id'], 'is_del' => 0]);
  42. if (!$coupon) {
  43. throw new ValidateException('优惠券不存在');
  44. }
  45. if ($data['is_all']) {
  46. $query = $userMerchantRepository->search(['mer_id' => $merId] + $data['search']);
  47. } else {
  48. $query = $userMerchantRepository->search(['uids' => $data['uid'], 'mer_id' => $merId]);
  49. }
  50. $uid = $query->column('A.uid');
  51. $uTotal = count($uid);
  52. if (!$uTotal) {
  53. throw new ValidateException('请选择用户');
  54. }
  55. if ($coupon['is_limited'] && $coupon->remain_count < $uTotal) {
  56. throw new ValidateException('该优惠券可领取数不足' . $uTotal);
  57. }
  58. return Db::transaction(function () use ($uid, $merId, $data, $coupon, $uTotal) {
  59. $search = $data['mark'];
  60. if($coupon['is_limited']){
  61. $coupon->remain_count -= $uTotal;
  62. }
  63. $send = $this->dao->create([
  64. 'mer_id' => $merId,
  65. 'coupon_id' => $coupon->coupon_id,
  66. 'coupon_num' => $uTotal,
  67. 'status' => 0,
  68. 'mark' => [
  69. 'type' => $data['is_all'],
  70. 'search' => count($search) ? $search : null
  71. ]
  72. ]);
  73. $coupon->save();
  74. Cache::store('file')->set('_send_coupon' . $send->coupon_send_id, $uid);
  75. Queue::push(MerchantSendCouponJob::class, $send->coupon_send_id);
  76. return $send;
  77. });
  78. }
  79. }