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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.coupon.dao.TbCouponDao;
import com.sqx.modules.coupon.dao.TbCouponUserDao;
import com.sqx.modules.coupon.entity.TbCoupon;
import com.sqx.modules.coupon.entity.TbCouponLog;
import com.sqx.modules.coupon.service.TbCouponLogService;
import com.sqx.modules.coupon.service.TbCouponService;
import com.sqx.modules.datacentre.entity.SysUserShop;
import com.sqx.modules.sys.entity.SysUserEntity;
import com.sqx.modules.sys.entity.SysUserRoleEntity;
import com.sqx.modules.sys.service.SysUserRoleService;
import com.sqx.modules.sys.service.SysUserService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
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.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;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private TbCouponLogService tbCouponLogService;

    @Autowired
    private SysUserService sysUserService;

    @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) {
        Long shopId = tbCoupon.getShopId();
        if (shopId==0) {
            Integer sysUserId = tbCoupon.getSysUserId();
//        判断是否是超级管理员
            LambdaQueryWrapper<SysUserRoleEntity> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(SysUserRoleEntity::getRoleId,3)
                    .eq(SysUserRoleEntity::getUserId,sysUserId);
            SysUserRoleEntity sysUserRoleEntity = sysUserRoleService.getOne(wrapper);
            if (ObjectUtils.isEmpty(sysUserRoleEntity)) {
                tbCoupon.setState(0);
            }else {
                tbCoupon.setState(1);
            }
            tbCoupon.setDeleteFlag(0);
            tbCouponDao.insert(tbCoupon);

            TbCouponLog tbCouponLog = new TbCouponLog();
            BeanUtils.copyProperties(tbCoupon,tbCouponLog);
            tbCouponLog.setContent("添加优惠券");
            tbCouponLog.setCreateTime(new Date());
            tbCouponLog.setCreateSysUserId(sysUserId);
            tbCouponLogService.save(tbCouponLog);

        }else {
            tbCoupon.setDeleteFlag(0);
            tbCouponDao.insert(tbCoupon);
        }


        return Result.success();
    }

    @Override
    public Result updateCoupon(TbCoupon tbCoupon) {
        Long shopId = tbCoupon.getShopId();
        if (shopId==0) {
            Integer sysUserId = tbCoupon.getSysUserId();

            TbCouponLog tbCouponLog = new TbCouponLog();
            BeanUtils.copyProperties(tbCoupon,tbCouponLog);
            tbCouponLog.setContent("修改优惠券");
            tbCouponLog.setCreateTime(new Date());
            tbCouponLog.setCreateSysUserId(sysUserId);
            tbCouponLogService.save(tbCouponLog);

        }

        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).eq("state",1)));
        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 TbCoupon selectCoupon(Long couponId) {
        QueryWrapper<TbCoupon> queryWrapper = new QueryWrapper();
        queryWrapper.eq("coupon_id",couponId);
        TbCoupon result = tbCouponDao.selectOne(queryWrapper);
        return result;
    }

    @Override
    public Result checkCoupon(Long couponId, Integer sysUserId, Integer state) {
        TbCoupon tbCoupon = tbCouponDao.selectById(couponId);
        if (ObjectUtils.isEmpty(tbCoupon)) {
            return Result.error("无该优惠券");
        }
        LambdaQueryWrapper<SysUserRoleEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRoleEntity::getRoleId,3)
                .eq(SysUserRoleEntity::getUserId,sysUserId);
        SysUserRoleEntity sysUserRoleEntity = sysUserRoleService.getOne(wrapper);
        if (ObjectUtils.isEmpty(sysUserRoleEntity)) {
            return Result.error("该用户不是超级管理，无审批权限");
        }
        tbCoupon.setState(state);
        tbCouponDao.updateById(tbCoupon);

        TbCouponLog tbCouponLog = new TbCouponLog();
        BeanUtils.copyProperties(tbCoupon,tbCouponLog);
        if (state==1) {
            tbCouponLog.setContent("发布优惠券");
        }else {
            tbCouponLog.setContent("撤销优惠券");
        }
        tbCouponLog.setCreateTime(new Date());
        tbCouponLog.setCreateSysUserId(sysUserId);
        tbCouponLogService.save(tbCouponLog);

        return Result.success();
    }

    @Override
    public Result selectAdminCoupon(Integer page, Integer limit, Integer state) {
        if(page == null || limit==null){
            LambdaQueryWrapper<TbCoupon> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(TbCoupon::getShopId,0)
                    .eq(ObjectUtils.isNotEmpty(state),TbCoupon::getState,state);
            List<TbCoupon> tbCoupons = baseMapper.selectList(wrapper);
            return Result.success().put("data",tbCoupons);
        }else{
            Page<TbCoupon> pages = new Page<>(page, limit);
            LambdaQueryWrapper<TbCoupon> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(TbCoupon::getShopId,0)
                    .eq(ObjectUtils.isNotEmpty(state),TbCoupon::getState,state);
            PageUtils pageUtils = new PageUtils(baseMapper.selectPage(pages,wrapper));
            return Result.success().put("data", pageUtils);
        }
    }

    @Override
    public Result deleteCoupon(Long couponId,Integer sysUserId) {
        TbCoupon tbCoupon = tbCouponDao.selectById(couponId);
        if (ObjectUtils.isEmpty(tbCoupon)) {
            return Result.error("无该优惠券");
        }
        Long shopId = tbCoupon.getShopId();
        if (shopId==0) {
            TbCouponLog tbCouponLog = new TbCouponLog();
            BeanUtils.copyProperties(tbCoupon,tbCouponLog);
            tbCouponLog.setContent("删除优惠券");
            tbCouponLog.setCreateTime(new Date());
            tbCouponLog.setCreateSysUserId(sysUserId);
            tbCouponLogService.save(tbCouponLog);

        }
        tbCouponDao.deleteById(couponId);
        return Result.success();
    }



}
