package cn.com.ty.lift.ud.coupon.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.com.ty.lift.ud.userCoupon.mapper.UserCouponMapper;
import cn.com.ty.lift.ud.userCoupon.mapper.entity.UserCoupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.coupon.mapper.CouponMapper;
import cn.com.ty.lift.ud.coupon.mapper.entity.Coupon;
import cn.com.ty.lift.ud.coupon.service.ICouponService;

/**
 * 卡券 -ServiceImpl
 *
 * @author Xson
 * @date 2020-05-08
 */
@Service
@Transactional
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private UserCouponMapper userCouponMapper;

    @Override
    public void updateByIds(List<Long> ids, Integer status) {
        for (Long id : ids) {
            Coupon bit = new Coupon();
            bit.setId(id);
            bit.setStatuz(status);
            couponMapper.updateById(bit);
        }
    }

    @Override
    public List<Coupon> getAllFailureByDate() {
        return couponMapper.getAllFailureByDate();
    }

    @Override
    public void chargeToCoupon(BigDecimal recharge, Long userId) {
        // Coupon coupon = new Coupon();
        // coupon.setCheckFalg(1);
        // coupon.setType(1);
        // coupon.setScenario(0);
        // coupon.setStatuz(1);
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setUsedFlag(1);
        if (recharge.compareTo(new BigDecimal("50")) >= 0 && recharge.compareTo(new BigDecimal("100")) < 0) {
            // coupon.setName("5元优惠券");
            // coupon.setPrice(new BigDecimal("5"));
            // couponMapper.insert(coupon);
            userCoupon.setCouponId(1l);
            userCoupon.setUserId(userId);
            userCouponMapper.insert(userCoupon);
        }
        if (recharge.compareTo(new BigDecimal("100")) >= 0 && recharge.compareTo(new BigDecimal("200")) < 0) {
            // coupon.setName("10元优惠券");
            // coupon.setPrice(new BigDecimal("10"));
            // couponMapper.insert(coupon);
            userCoupon.setCouponId(2l);
            userCoupon.setUserId(userId);
            userCouponMapper.insert(userCoupon);
        }
        if (recharge.compareTo(new BigDecimal("200")) >= 0 && recharge.compareTo(new BigDecimal("500")) < 0) {
            // coupon.setName("20元优惠券");
            // coupon.setPrice(new BigDecimal("20"));
            // couponMapper.insert(coupon);
            userCoupon.setCouponId(3l);
            userCoupon.setUserId(userId);
            userCouponMapper.insert(userCoupon);
        }
        if (recharge.compareTo(new BigDecimal("500")) >= 0 && recharge.compareTo(new BigDecimal("1000")) < 0) {
            // coupon.setName("50元优惠券");
            // coupon.setPrice(new BigDecimal("50"));
            // couponMapper.insert(coupon);
            userCoupon.setCouponId(4l);
            userCoupon.setUserId(userId);
            userCouponMapper.insert(userCoupon);
        }
        if (recharge.compareTo(new BigDecimal("1000")) >= 0) {
            // coupon.setName("100元优惠券");
            // coupon.setPrice(new BigDecimal("100"));
            // couponMapper.insert(coupon);
            userCoupon.setCouponId(5l);
            userCoupon.setUserId(userId);
            userCouponMapper.insert(userCoupon);
        }

    }

    @Override
    public void updateSatus(Long id, Integer CheckStatus,Integer status) {
        couponMapper.updateSatus(id,CheckStatus,status);
    }

    @Override
    public void updateCouponById(Long id) {
        couponMapper.updateCouponById(id);
    }

    @Override
    public Coupon getByCouponId(Long id) {
        return couponMapper.getByCouponId(id);
    }

}
