package cn.ysk.cashier.mybatis.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.ysk.cashier.mybatis.entity.*;
import cn.ysk.cashier.mybatis.mapper.*;
import cn.ysk.cashier.mybatis.service.TbCouponProductService;
import cn.ysk.cashier.pojo.shop.TbShopUser;
import cn.ysk.cashier.repository.shop.TbShopUserRepository;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ysk.cashier.mybatis.service.TbShopShareRecordService;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.ysk.cashier.dto.TbShopShareRecordQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import cn.ysk.cashier.utils.PageUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

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

/**
 * (TbShopShareRecord)表服务实现类
 *
 * @author ww
 * @since 2024-11-08 09:17:37
 */
@Service("tbShopShareRecordService")
public class TbShopShareRecordServiceImpl extends ServiceImpl<TbShopShareRecordMapper, TbShopShareRecord> implements TbShopShareRecordService {

    @Autowired
    private TbShopShareRecordMapper tbShopShareRecordmapper;
    @Autowired
    private TbShopShareMapper tbShopShareMapper;
    @Autowired
    private TbShopUserRepository shopUserRepository;
    @Autowired
    private TbShopCouponMapper couponMapper;
    @Autowired
    private TbCouponProductMapper couProductMapper;
    @Autowired
    private TbActivateInRecordMapper inRecordMapper;

    @Override
    public Map<String, Object> selectAllByShare(TbShopShareRecordQueryCriteria criteria) {
        Page<TbShopShareRecord> ipage = tbShopShareRecordmapper.selectAllByShare(criteria,new Page<>(criteria.getPage(), criteria.getSize()));
        return PageUtil.toPage(ipage.getRecords(), ipage.getTotal());
    }

    @Override
    public void give(TbShopShareRecord shareRecord, Integer userId) {
        TbShopShare shopShare = tbShopShareMapper.selectById(shareRecord.getShareId());
        TbShopUser tbShopUser = shopUserRepository.selectByUserIdAndShopId(userId.toString(), shopShare.getShopId().toString());
        if (userId.equals(shareRecord.getInvitedId())) {
            giveCoupon(shopShare, tbShopUser, shopShare.getRewardCoupons(),shareRecord.getId(), true);
        } else if (userId.equals(shareRecord.getBeInvitedId())) {
            giveCoupon(shopShare, tbShopUser, shopShare.getNewCoupons(),shareRecord.getId(),false);
        }
    }


    public void giveCoupon(TbShopShare shopShare, TbShopUser tbShopUser, List<TbShopShare.ShareCoupons> coupons ,Integer flowId, boolean isReward) {
        for (TbShopShare.ShareCoupons newCoupon : coupons) {
            TbShopCoupon tbShopCoupon = couponMapper.selectById(newCoupon.getCouponId());
            Date start = new Date();
            Date end = new Date();
            if ("fixed".equals(tbShopCoupon.getValidityType())) {
                //固定时间
                end = DateUtil.offsetDay(new Date(), tbShopCoupon.getValidDays());
            } else if ("custom".equals(tbShopCoupon.getValidityType())) {
                //自定义时间
                start = tbShopCoupon.getValidStartTime();
                end = tbShopCoupon.getValidEndTime();
            }
            if (tbShopCoupon != null) {
                List<TbActivateInRecord> actGiveRecords = new ArrayList<>();
                if (tbShopCoupon.getType() == 1) {
                    //满减
                    TbActivateInRecord record = new TbActivateInRecord();
                    record.setVipUserId(Integer.valueOf(tbShopUser.getId()));
                    record.setCouponId(tbShopCoupon.getId());
                    record.setName("满" + tbShopCoupon.getFullAmount() + "减" + tbShopCoupon.getDiscountAmount());
                    record.setFullAmount(tbShopCoupon.getFullAmount());
                    record.setDiscountAmount(tbShopCoupon.getDiscountAmount());
                    record.setType(1);
                    record.setNum(newCoupon.getCouponNum());
                    record.setOverNum(newCoupon.getCouponNum());
                    record.setShopId(Integer.valueOf(tbShopUser.getShopId()));
                    record.setSourceActId(shopShare.getId());
                    record.setSourceFlowId(flowId);
                    record.setUseStartTime(start);
                    record.setUseEndTime(end);
                    if(isReward){
                        record.setSource("invited-reward");
                    }else {
                        record.setSource("invited");
                    }
                    actGiveRecords.add(record);
                } else if (tbShopCoupon.getType() == 2) {
                    //商品卷
                    List<TbCouponProduct> tbCouponProducts = couProductMapper.queryAllByCouponId(tbShopCoupon.getId());
                    for (TbCouponProduct actPro : tbCouponProducts) {
                        TbActivateInRecord record = new TbActivateInRecord();
                        record.setVipUserId(Integer.valueOf(tbShopUser.getId()));
                        record.setCouponId(tbShopCoupon.getId());
                        record.setName("商品卷");
                        record.setType(2);
                        record.setProId(actPro.getProductId());
                        record.setNum(actPro.getNum() * tbShopCoupon.getNumber());
                        record.setOverNum(actPro.getNum() * tbShopCoupon.getNumber());
                        record.setShopId(Integer.valueOf(tbShopUser.getShopId()));
                        record.setSourceActId(shopShare.getId());
                        record.setSourceFlowId(flowId);
                        record.setUseStartTime(start);
                        record.setUseEndTime(end);
                        if(isReward){
                            record.setSource("invited-reward");
                        }else {
                            record.setSource("invited");
                        }
                        actGiveRecords.add(record);
                    }
                }
                inRecordMapper.insert(actGiveRecords);
                if(!isReward){
                    tbShopCoupon.setLeftNumber(tbShopCoupon.getLeftNumber()-newCoupon.getCouponNum());
                    couponMapper.updateById(tbShopCoupon);
                }
            }
        }

    }
}

