| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <?php
- namespace app\api\logic;
- use app\common\logic\BaseLogic;
- use app\common\model\coupon\CouponCategory;
- use app\common\model\coupon\CouponRules;
- use app\common\model\coupon\UserCoupon;
- use think\db\Query;
- use think\db\Where;
- use think\facade\Db;
- /**
- * @author 林海涛
- * @date 2024/7/18 下午4:49
- */
- class UserCouponLogic extends BaseLogic
- {
- public static function add($params)
- {
- Db::startTrans();
- try{
- $errMsgArr = [];
- $rules = CouponRules::whereIn('id',$params['coupon_ids'])->lock(true)->select();
- $userCoupon = UserCoupon::where('user_id',$params['user_id'])
- ->whereIn('coupon_id',$params['coupon_ids'])
- ->where('voucher_count','>',0)
- ->select();
- $updateData = [];
- $createData = [];
- foreach($rules as $v) {
- if ($v->expire_time < time()) {
- $errMsgArr[] = $v->event_name .'已过期';
- continue;
- }
- if($v->voucher_status == 2){
- $errMsgArr[] = $v->event_name .'已设置为过期';
- continue;
- }
- if($userCoupon->where('coupon_id',$v->id)->count()){
- $errMsgArr[] = $v->event_name . '已领取';
- continue;
- }
- $updateData[$v->id] = ['voucher_count' => --$v->voucher_count];
- $createData[] = ['user_id'=>$params['user_id'],'coupon_id' => $v->id,'voucher_count' =>1];
- }
- CouponRules::updateWhenCase($updateData);
- if(!empty($createData)){
- (new UserCoupon())->saveAll($createData);
- }
- Db::commit();
- return $errMsgArr;
- }catch(\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 我的优惠卷列表
- * @param $params
- * @return array|false
- */
- public static function userCouponList($params)
- {
- try{
- $where = [];
- if(isset($params['voucher_status']) && is_array($params['voucher_status'])){
- $where[] = ['voucher_status','IN',$params['voucher_status']];
- }
- $where[] = ['user_id','=',$params['user_id']];
- $data = UserCoupon::with(['couponRules'=>function(Query $query){
- $query->withoutField(['voucher_status','voucher_count']);
- $query->with(['couponWithCategory'=>function(Query $query){
- $query->field('id,name');
- }]);
- }])
- ->where($where)
- ->field(['id', 'user_id', 'coupon_id', 'voucher_status','voucher_count'])
- ->order(['id' => 'desc'])
- ->select()
- ->toArray();
- $couponIds = [];
- foreach($data as $k => $v){
- if($v['couponRules']){
- if($v['voucher_status'] !=2 &&($v['couponRules']['begin_use'] <time() || $data['couponRules']['expire_time'] > time())){
- $couponIds[] = $v['coupon_id'];
- $v['voucher_status'] = 2;
- }
- $v['couponRules']['begin_use'] = date("Y-m-d H:i:s",$v['couponRules']['begin_use'] );
- $v['couponRules']['expire_time'] = date("Y-m-d H:i:s",$v['couponRules']['expire_time'] );
- $v['couponRules']['goods_category_ids'] = array_column($v['couponRules']['couponWithCategory'],'id');
- $data[$k] = $v;
- }
- }
- if(!empty($couponIds)){
- UserCoupon::whereIn('coupon_id',$couponIds)->update(['voucher_status'=>2]);
- }
- return $data;
- }catch(\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * 商品分类优惠卷领取
- * @param $params
- * @return array|false
- */
- public static function categoryCouponList($params)
- {
- try{
- $data = CouponRules::with(['couponWithCategory'=>function(Query $query) use($params){
- $query->where('goods_category_id',$params['goods_category_id']);
- }])->field(['id', 'amount', 'coupon_type','amount_require', 'discount_ratio', 'event_name', 'expire_time', 'max_deductible_price', 'mold_type', 'server_category_name', 'voucher_count'])
- ->order(['id' => 'desc'])
- ->select()
- ->toArray();
- return $data;
- }catch(\Exception $e){
- Db::rollback();
- self::setError($e->getMessage());
- return false;
- }
- }
- }
|