package com.fzm.mall.server.front.user.service.impl;

import com.fzm.mall.server.front.goods.model.MarketingCoupon;
import com.fzm.mall.server.front.goods.model.MarketingCouponSku;
import com.fzm.mall.server.front.goods.model.vo.CouponVo;
import com.fzm.mall.server.front.goods.model.vo.PageVo;
import com.fzm.mall.server.front.goods.model.vo.PageVoWithOne;
import com.fzm.mall.server.front.goods.model.vo.UserCouponVo;
import com.fzm.mall.server.front.user.model.Coupon;
import com.fzm.mall.server.front.user.mapper.CouponMapper;
import com.fzm.mall.server.front.user.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户与优惠券对应表 服务实现类
 * </p>
 *
 * @author fzm
 * @since 2021-03-19
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Autowired CouponMapper couponMapper;

    @Override
    public int updateUserCoupon(String uid, String couponId) {
        return couponMapper.updateUserCoupon(uid,couponId);
    }

    @Override
    public List<UserCouponVo> getUserCoupon(String uid, Integer type) {
        return couponMapper.getUserCoupon(uid,type,System.currentTimeMillis());
    }

    @Override
    public List<UserCouponVo> getCouponByGoodsId(String uid, String goodsId,Integer type,String pingtaiId,String marchentId) {
        if(StringUtil.isNullOrEmpty(uid)){
            return   couponMapper.getCouponByGoodsId(goodsId,type,System.currentTimeMillis(),pingtaiId,marchentId);
        }
        return couponMapper.getCouponByGoodsId(goodsId,type,System.currentTimeMillis(),pingtaiId,marchentId);
    }

    @Override
    @Transactional
    public int addCoupon(String uid, Integer type, String couponId) {
        //优惠券是否有效
        boolean a = check(couponId,type);
        if(!a){
            //优惠券失效了
            throw  new RuntimeException("coupon_invalid");
        }
        //判断是否已经领取
        boolean b = checkHave(uid,couponId);
        if(b){
            //优惠券已经领取
            throw  new RuntimeException("coupon_received");
        }
        int i = delirculation(couponId);
        if(i != 1){
            //优惠券已经领完
            throw  new RuntimeException("coupon_over");
        }
        Coupon coupon = new Coupon();
        coupon.setCouponId(couponId);
        coupon.setUid(uid);
        save(coupon);
        return  1;
    }

    private int delirculation(String couponId) {
        MarketingCoupon marketingCoupon = couponMapper.getByCouponId(couponId);
        if(marketingCoupon == null){
            return  0;
        }
        if(marketingCoupon.getCirculation() == 0){
            return  1;
        }else {
           int i =  couponMapper.delAmt(couponId);
           return  i;
        }
    }

    @Override
    public List<MarketingCoupon> getUserCouponAndStatus(String uid, Integer type, PageVoWithOne pageVo) {
        List<MarketingCoupon> list = new ArrayList<>();
        Integer start = (pageVo.getPage()-1)*pageVo.getSize();
        if(("1").equals(pageVo.getParam())){
            list = couponMapper.getUseful(uid,type,System.currentTimeMillis(),start,pageVo.getSize());
        }else if(("2").equals(pageVo.getParam())){
            list = couponMapper.getUsed(uid,type,start,pageVo.getSize());
        }else  if(("3").equals(pageVo.getParam())){
            list = couponMapper.getOld(uid,type,System.currentTimeMillis(),start,pageVo.getSize());
        }
        return list;
    }

    @Override
    public Integer getCount(String uid, Integer type, String status) {
        Integer count = 0;
        if(("1").equals(status)){
            count = couponMapper.getUsefulCount(uid,type,System.currentTimeMillis());
        }else if(("2").equals(status)){
            count = couponMapper.getUsedCount(uid,type);
        }else  if(("3").equals(status)){
            count = couponMapper.getOldCount(uid,type,System.currentTimeMillis());
        }
        return  count ;
    }

    @Override
    public Boolean checkHave(String uid, String couponId) {
        Coupon coupon = couponMapper.checkHave(uid,couponId);
        if(coupon == null){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public List<MarketingCoupon> getAllUserCoupon(String uid, Integer type) {
        return couponMapper.getAllUserCoupon(uid,type,System.currentTimeMillis());
    }

    @Override
    public List<String> getCategorys(String couponId) {
        return couponMapper.getCategorys(couponId);
    }

    @Override
    public CouponVo getAll(String couponId, Integer type) {
        return couponMapper.getAll(couponId,type,System.currentTimeMillis());
    }

    @Override
    public List<String> getCouponIdByUid(String uid, Integer type) {
        return couponMapper.getCouponIdByUid(uid,type,System.currentTimeMillis());
    }


    private boolean check(String couponId, Integer type) {
       MarketingCoupon marketingCoupon =  couponMapper.check(couponId,type,System.currentTimeMillis());
       if(marketingCoupon.getCirculation()!=0){
            if(marketingCoupon.getAmt() <= 0){
                return false;
            }
       }
       if(marketingCoupon == null){
           return  false;
       }else{
           return  true;
       }
    }


}
