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

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.common.TimeUtil;
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.userCoupon.controller.query.UserCouponQuery;
import cn.com.ty.lift.ud.userCoupon.mapper.UserCouponMapper;
import cn.com.ty.lift.ud.userCoupon.mapper.entity.CouponNumResp;
import cn.com.ty.lift.ud.userCoupon.mapper.entity.UserCoupon;
import cn.com.ty.lift.ud.userCoupon.service.IUserCouponService;
import cn.com.ty.lift.ud.userInfo.mapper.UserInfoMapper;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.xwy.boot.web.dto.RestResponse;

/**
 * 用户卡券 -ServiceImpl
 *
 * @author Xson
 * @date 2020-05-12
 */
@Service
@Transactional
public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements IUserCouponService {

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

    @Override
    public void updateUsedFlagByCouponId(Long couponId) {
        userCouponMapper.updateUsedFlagByCouponId(couponId);
    }

    @Override
    public CouponNumResp getCouponNum(Long userId) {
        CouponNumResp result = new CouponNumResp();
        UserInfoEntity byUserId = userInfoMapper.getByUserId(userId);
        Integer couponNum = userCouponMapper.getCouponNum(userId, 1);
        Integer hongBaoNum = userCouponMapper.getCouponNum(userId, 2);
        if(byUserId.getVipFlag() == 2 && byUserId.getVipEndTime() != null) {
            int dateDiff = TimeUtil.dateDiff(LocalDateTime.now(), byUserId.getVipEndTime());
            if(dateDiff <= 0) {
                result.setMemberDays("VIP已过期");
            }
            result.setMemberDays("VIP还剩"+dateDiff+"天");
        }else {
            result.setMemberDays("VIP未开通");
        }
        result.setIsVip(byUserId.getVipFlag());
        result.setBalance(byUserId.getBalance());
        result.setCouponNum(couponNum);
        result.setHongBaoNum(hongBaoNum);
        result.setFreezeBalance(byUserId.getFreezeBalance());
        return result;
    }

    @Override
    public List<UserCoupon> queryAllByType(Long userId, Integer type, BigDecimal amount) {
        List<UserCoupon> queryAllByType = null;
        if (type == 1) {
            queryAllByType = userCouponMapper.queryAllCoupon(userId, type, amount);
        } else {
            queryAllByType = userCouponMapper.queryAllRed(userId, type);
        }
        for (UserCoupon uc : queryAllByType) {
            Coupon coupon = couponMapper.selectById(uc.getCouponId());
            uc.setCoupon(coupon);
        }
        return queryAllByType;
    }

    @Override
    public RestResponse selectByExample(UserCouponQuery query) {
        IPage page = query.page();
        List<UserCoupon> list = userCouponMapper.selectByQuery(query.condition(), query);
        for (UserCoupon uc : list) {
            Coupon coupon = couponMapper.selectById(uc.getCouponId());
            uc.setCoupon(coupon);
        }
        int total = userCouponMapper.countByQuery(query.condition());
        page.setRecords(list);
        page.setTotal(total);
        return RestResponse.success(page, "Queried successfully");
    }

}
