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.DateUtils;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.coupon.dao.TbCouponDao;
import com.sqx.modules.coupon.dao.TbCouponUserDao;
import com.sqx.modules.coupon.entity.TbCoupon;
import com.sqx.modules.coupon.entity.TbCouponUser;
import com.sqx.modules.coupon.service.TbCouponUserService;
import com.sqx.modules.integral.dao.UserIntegralDao;
import com.sqx.modules.integral.dao.UserIntegralDetailsDao;
import com.sqx.modules.integral.entity.UserIntegral;
import com.sqx.modules.integral.entity.UserIntegralDetails;
import com.sqx.modules.integral.service.UserIntegralService;
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.util.Calendar;
import java.util.Date;

@Service
public class TbTbCouponUserServiceImpl extends ServiceImpl<TbCouponUserDao, TbCouponUser> implements TbCouponUserService {

    @Autowired
    private TbCouponUserDao tbCouponUserDao;
    @Autowired
    private UserIntegralDao userIntegralDao;
    @Autowired
    private TbCouponDao tbCouponDao;
    @Autowired
    private UserIntegralDetailsDao userIntegralDetailsDao;

    @Override
    public Result selectCouponList(Long userId, Integer page, Integer limit, BigDecimal minMoney,Long shopId) {
        Page<TbCouponUser> pages=new Page<>(page,limit);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        PageUtils pageUtils = new PageUtils(tbCouponUserDao.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<TbCouponUser> pages=new Page<>(page,limit);
        PageUtils pageUtils = new PageUtils(tbCouponUserDao.CouponList(pages, userId, status));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public void updateExpiration() {
        tbCouponUserDao.updateExpiration();
    }

    @Transactional
    @Override
    public Result buyCoupon(Long userId, Long couponId) {
        UserIntegral userIntegral = userIntegralDao.selectOne(new QueryWrapper<UserIntegral>().eq("user_id", userId));
        TbCoupon tbCoupon = tbCouponDao.selectById(couponId);
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        if(tbCoupon.getShopId()==0){
            if(userIntegral.getIntegralNum()<tbCoupon.getNeedIntegral()){
                return Result.error("您的积分不足！");
            }
            //扣除用户积分
            userIntegralDao.updateUserIntegral(userId, tbCoupon.getNeedIntegral());
            //添加优惠券购买记录
            UserIntegralDetails userIntegralDetails1 = new UserIntegralDetails();
            userIntegralDetails1.setContent("积分兑换优惠券");
            userIntegralDetails1.setClassify(2);
            userIntegralDetails1.setType(2);
            userIntegralDetails1.setNum(tbCoupon.getNeedIntegral());
            userIntegralDetails1.setCreateTime(format);
            userIntegralDetails1.setUserId(userId);
            userIntegralDetailsDao.insert(userIntegralDetails1);
        }else{
            //如果是商家优惠券 先判断是否有未使用的 在判断今天是否 领取过
            Integer count = tbCouponUserDao.selectCount(new QueryWrapper<TbCouponUser>().eq("user_id", userId)
                    .eq("shop_id", tbCoupon.getShopId()).eq("coupon_id",couponId).eq("status", 0));
            if(count>0){
                return Result.error("您已有该优惠券，还未使用！");
            }
            count = tbCouponUserDao.selectCount(new QueryWrapper<TbCouponUser>().eq("user_id", userId)
                    .eq("shop_id", tbCoupon.getShopId()).eq("coupon_id",couponId)
                    .like("create_time", DateUtils.format(new Date(),DateUtils.DATE_PATTERN)));
            if(count>0){
                return Result.error("您今日已经领取过了！");
            }
        }
        //将优惠券添加到用户优惠券仓库
        TbCouponUser tbCouponUser = new TbCouponUser();
        tbCouponUser.setUserId(userId);
        tbCouponUser.setCouponId(couponId);
        tbCouponUser.setCouponName(tbCoupon.getCouponName());
        tbCouponUser.setCouponPicture(tbCoupon.getCouponPicture());
        tbCouponUser.setCreateTime(format);
        //计算优惠券过期时间
        Integer endDate = tbCoupon.getEndDate();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, endDate);
        Date date = calendar.getTime();
        String format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        tbCouponUser.setExpirationTime(format1);
        tbCouponUser.setMinMoney(tbCoupon.getMinMoney());
        tbCouponUser.setGoodsType(tbCoupon.getGoodsType());
        tbCouponUser.setMoney(tbCoupon.getMoney());
        tbCouponUser.setStatus(0);
        tbCouponUser.setEndTime(tbCoupon.getEndDate().toString());
        if(tbCoupon.getNeedIntegral()==null){
            tbCouponUser.setNeedIntegral("0");
        }else{
            tbCouponUser.setNeedIntegral(tbCoupon.getNeedIntegral().toString());
        }

        tbCouponUser.setShopId(tbCoupon.getShopId());
        tbCouponUserDao.insert(tbCouponUser);

        return Result.success();
    }

    @Override
    public Result selectCouponByUserId(Integer page, Integer limit, Long userId, Integer status, String phone,String shopName,Long shopId) {
        Page<TbCouponUser> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(tbCouponUserDao.selectCouponByUserId(pages, userId, status, phone,shopName,shopId));
        return Result.success().put("data", pageUtils);
    }
}
