package com.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.dao.ApiCouponMapper;
import com.platform.entity.CouponVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class ApiCouponService {
    @Autowired
    private ApiCouponMapper apiCouponMapper;

    public CouponVo queryObject(Integer couponId) {
        return apiCouponMapper.queryObject(couponId);
    }

    public List<CouponVo> queryList(Map<String, Object> map) {
        return apiCouponMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiCouponMapper.queryTotal(map);
    }

    public CouponVo selectCouponForUpdate(int couponId){
        return apiCouponMapper.selectForUpdateCoupon(couponId);
    }

    public void save(CouponVo userVo) {
        apiCouponMapper.save(userVo);
    }

    public void update(CouponVo user) {
        apiCouponMapper.update(user);
    }

    public void delete(Long userId) {
        apiCouponMapper.delete(userId);
    }

    public void deleteBatch(Long[] userIds) {
        apiCouponMapper.deleteBatch(userIds);
    }

    @Transactional(rollbackFor = Exception.class)
    public List<CouponVo> queryUserCoupons(Map<String, Object> map) {
        // 检查优惠券是否过期
        List<CouponVo> couponVos = apiCouponMapper.queryUserCoupons(map);
        List<CouponVo> latestCouponVos = new ArrayList<>();
        if(!CollectionUtils.isEmpty(couponVos)){
            for (CouponVo couponVo : couponVos) {
                // 检查是否生效
                if(couponVo.getUseStartDate().compareTo(LocalDateTime.now()) < 0 && couponVo.getUseEndDate().compareTo(LocalDateTime.now()) > 0) {
                    if (couponVo.getCouponStatus().compareTo(1) == 0) {
                        //优惠券状态 1 未生效 2 生效中 3 已失效 4 停止发放
                        couponVo.setCouponStatus(2);
                        apiCouponMapper.update(couponVo);
                        log.info("优惠券[{}]原来为未生效，实际已经生效了，更新状态为2" , couponVo.getId());
                    }
                }

                // 检查是否过期
                if(couponVo.getUseEndDate().compareTo(LocalDateTime.now()) < 0) {
                    if (couponVo.getCouponStatus().compareTo(2) == 0) {
                        //优惠券状态 1 未生效 2 生效中 3 已失效 4 停止发放
                        couponVo.setCouponStatus(3);
                        apiCouponMapper.update(couponVo);
                        log.info("优惠券[{}]原为未过期，但当前时间已过期，更新优惠券状态为3" , couponVo.getId());

                        //这里更新用户优惠券使用状态 1. 可用 2. 已用 3. 过期 4、作废
                        apiCouponMapper.updateUserCoupon(couponVo);
                        log.info("优惠券[{}]原为未过期，但当前时间已过期，更新使用状态状态为3" , couponVo.getId());

                    }
                }
                latestCouponVos.add(couponVo);
            }

        }
        if(CollectionUtils.isEmpty(latestCouponVos)){
            return couponVos;
        }else{
            return latestCouponVos;
        }

    }

    public CouponVo queryMaxUserEnableCoupon(Map<String, Object> map) {
        return apiCouponMapper.queryMaxUserEnableCoupon(map);
    }

    public List<CouponVo> queryUserCouponList(Map<String, Object> map) {
        return apiCouponMapper.queryUserCouponList(map);
    }

    public IPage<CouponVo> getInvalidCouponByYear(long userId , long pageNum , long pageSize){
        IPage<CouponVo> page = new Page<>(pageNum , pageSize);
        QueryWrapper<CouponVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("b.user_id" , userId);
        //优惠券状态 1 未生效 2 生效中 3 已失效 4 停止发放，这里只给用户获取已经失效的
        queryWrapper.eq("b.coupon_status" , 3);
        queryWrapper.eq("a.enable" , 1);
        LocalDateTime now = LocalDateTime.now();
        //优惠券只获取当年失效的优惠券
        LocalDateTime start = LocalDateTime.of(now.getYear() , 1 , 1, 0, 0, 0);
        LocalDateTime end = LocalDateTime.of(now.getYear() , 12 , 31, 23, 59, 59);
        queryWrapper.between("a.use_end_date" , start , end);
        queryWrapper.orderByDesc("a.create_time");
        return apiCouponMapper.getInvalidCouponByYear(page , queryWrapper);
    }

    public static  void main(String[] args){
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now.getYear());
    }

    public IPage<CouponVo> getCouponPageByBrand(String brandId , Long pageNum , Long pageSize){
        IPage<CouponVo> resultPage = new Page<>();
        if(StringUtils.isBlank(brandId)){
            return resultPage;
        }
        IPage<CouponVo> page = new Page<>(pageNum , pageSize);
        QueryWrapper<CouponVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("brand_id" , brandId);
        //优惠券状态 1 未生效 2 生效中 3 已失效 4 停止发放，这里只给用户领取生效中的
        queryWrapper.eq("coupon_status" , 2);
        queryWrapper.eq("enable" , 1);
        queryWrapper.orderByDesc("create_time");
        //TODO：加上eanble过滤字段
        return  apiCouponMapper.getCouponByBrandId(page , queryWrapper);
    }
}
