package com.sqx.modules.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.coupon.dao.CouponDao;
import com.sqx.modules.coupon.dao.CouponUserDao;
import com.sqx.modules.coupon.entity.Coupon;
import com.sqx.modules.coupon.entity.CouponUser;
import com.sqx.modules.coupon.service.CouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class CouponUserServiceImpl extends ServiceImpl<CouponUserDao, CouponUser> implements CouponUserService {

    @Autowired
    private CouponUserDao CouponUserDao;
    @Autowired
    private CouponDao CouponDao;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;

    @Override
    public Result selectCouponList(Long userId, Integer page, Integer limit, BigDecimal minMoney,Long shopId) {
        Page<CouponUser> pages=new Page<>(page,limit);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        PageUtils pageUtils = new PageUtils(CouponUserDao.selectCouponList(pages, userId, minMoney, format,shopId));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result couponList(Long userId, Integer page, Integer limit, Integer status) {
        Page<CouponUser> pages=new Page<>(page,limit);
        PageUtils pageUtils = new PageUtils(CouponUserDao.CouponList(pages, userId, status));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public void updateExpiration() {
        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        CouponUserDao.updateExpiration(date);
    }

    @Transactional
    @Override
    public Result buyCoupon(Long userId, Long couponId) {

        Coupon coupon = CouponDao.selectById(couponId);
        //将优惠券添加到用户优惠券仓库
        CouponUser couponUser = new CouponUser();
        couponUser.setUserId(userId);
        couponUser.setCouponName(coupon.getCouponName());
        couponUser.setCouponPicture(coupon.getCouponPicture());
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        couponUser.setCreateTime(format);
        //计算优惠券过期时间
        Integer endDate = coupon.getEndDate();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, endDate);
        calendar.add(Calendar.DATE, 1);
        Date date = calendar.getTime();
        String format1 = new SimpleDateFormat("yyyy-MM-dd").format(date);
        couponUser.setExpirationTime(format1);
        couponUser.setMinMoney(coupon.getMinMoney());
        couponUser.setMoney(coupon.getMoney());
        couponUser.setStatus(0);
        CouponUserDao.insert(couponUser);

        return Result.success();
    }

    @Override
    public Result selectCouponByUserId(Integer page, Integer limit, String id, Long userId, Integer status, String phone,String couponName) {
        String[] arr = null;
        if(id !=null && id.length()>0){
            arr = id.split(",");
        }
        Page<CouponUser> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(CouponUserDao.selectCouponByUserId(pages, arr, userId, status, phone,couponName));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result selectNewUserCoupon(Integer page,Integer limit){
        Page<Coupon> pages=new Page<>(page,limit);
        PageUtils pageUtils = new PageUtils(CouponDao.selectNewerCouponPage(pages));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result insertNewUserCoupon(UserEntity userEntity){
        if(userEntity.getIsCoupon()!=null && userEntity.getIsCoupon()==1){
            return Result.error("您已经领取！");
        }
        Long userId=userEntity.getUserId();
        UserEntity user = userService.getById(userId);
        if(user.getIsCoupon()!=null && user.getIsCoupon()==1){
            return Result.error("您已经领取！");
        }
        //当前时间
        String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        //新增判断是否是新人
        String userTime = user.getCreateTime();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date1 = LocalDate.parse(nowTime.substring(0,10), formatter);
        LocalDate date2 = LocalDate.parse(userTime.substring(0,10), formatter);
        // 计算两个日期之间相差的天数  data1小于date2
        long daysBetween = ChronoUnit.DAYS.between(date2, date1);
        if(daysBetween>7){
            return Result.error("已过新人优惠期");
        }
        //获取所有新人优惠券

        QueryWrapper<Coupon> couponQueryWrapper = new QueryWrapper<Coupon>();
        couponQueryWrapper.eq("cou_type",1);
        List<Coupon> list = CouponDao.selectList(couponQueryWrapper);


//        String value = commonInfoService.findOne(281).getValue();


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<Coupon> couponList=new ArrayList<>();
        for(Coupon coupon:list){
            Integer endDate = coupon.getEndDate();
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, endDate);
            calendar.add(Calendar.DATE, 1);
            Date date = calendar.getTime();
            String expirationTime = simpleDateFormat.format(date);
            CouponUser couponUser=new CouponUser();
            couponUser.setUserId(userId);
            couponUser.setCouponName(coupon.getCouponName());
            couponUser.setCouponPicture(coupon.getCouponPicture());
            couponUser.setCreateTime(nowTime);
            couponUser.setExpirationTime(expirationTime);
            couponUser.setMinMoney(coupon.getMinMoney());
            couponUser.setMoney(coupon.getMoney());
            couponUser.setStatus(0);
            baseMapper.insert(couponUser);
            couponList.add(coupon);
        }
        userEntity.setIsCoupon(1);
        userService.updateById(userEntity);
        return Result.success().put("data",couponList);
    }

    @Override
    public CouponUser selectCountByUserId(Long userId) {
        return CouponUserDao.selectCountByUserId(userId);
    }

}
