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.UserMoneyDetails;
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.TbCouponService;
import com.sqx.modules.datacentre.entity.SysUserShop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class TbCouponServiceImpl extends ServiceImpl<TbCouponDao, TbCoupon> implements TbCouponService {

    @Autowired
    private TbCouponDao tbCouponDao;
    @Autowired
    private TbCouponUserDao tbCouponUserDao;

    @Override
    public Result seleteAllCoupon(Integer page, Integer limit,Long shopId,String shopName) {
        if(page == null || limit==null){
            List<TbCoupon> tbCoupons = baseMapper.selectCouponList(shopId, shopName);
            return Result.success().put("data",tbCoupons);
        }else{
            Page<TbCoupon> pages = new Page<>(page, limit);
            PageUtils pageUtils = new PageUtils(baseMapper.selectCouponPage(pages, shopId,shopName));
            return Result.success().put("data", pageUtils);
        }

    }

    @Override
    public Result issueCoupon(TbCoupon tbCoupon) {
        tbCoupon.setDeleteFlag(0);
        tbCouponDao.insert(tbCoupon);
        return Result.success();
    }

    @Override
    public Result updateCoupon(TbCoupon tbCoupon) {
        tbCouponDao.updateById(tbCoupon);
        return Result.success();
    }

    @Override
    public Result selectCouponList(Integer page, Integer limit,Long shopId) {
        Page<TbCoupon> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(baseMapper.selectPage(pages, new QueryWrapper<TbCoupon>().eq(shopId!=null,"shop_id",shopId).eq("delete_flag", 0)));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result selectCouponByShopId(Integer page, Integer limit, List<SysUserShop> userShops) {
        Page<TbCoupon> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(tbCouponDao.selectCouponByShopId(pages, userShops));
        return Result.success().put("data", pageUtils);
    }

    @Transactional
    @Override
    public Result giveCoupon(Long couponId, List<String> userIdList, Long shopId) {
        TbCoupon tbCoupon = tbCouponDao.selectById(couponId);
        //计算优惠券过期时间
        Integer endDate = tbCoupon.getEndDate();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, endDate);
        calendar.add(Calendar.DATE, 1);
        Date date = calendar.getTime();
        String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        String expirationTime = new SimpleDateFormat("yyyy-MM-dd").format(date);
        tbCouponUserDao.giveCoupon(userIdList, tbCoupon.getCouponName(), tbCoupon.getCouponPicture(), createTime, expirationTime, tbCoupon.getMinMoney(), tbCoupon.getGoodsType(),
                tbCoupon.getMoney(), 0,tbCoupon.getEndDate().toString(), shopId);
        return Result.success();
    }

    @Override
    public Result deleteCoupon(Long couponId) {
        tbCouponDao.deleteById(couponId);
        return Result.success();
    }


}
