package com.kxs.system.biz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.enums.KindTypeEnum;
import com.kxs.common.core.exception.GlobalCustomerException;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.system.api.dto.admin.camp.CampReportAuditDTO;
import com.kxs.system.api.model.KxsCamp;
import com.kxs.system.api.model.KxsCampBonusLog;
import com.kxs.system.api.model.KxsCampReport;
import com.kxs.system.api.model.KxsCampUser;
import com.kxs.system.api.vo.admin.camp.ReportCampPageVO;
import com.kxs.system.biz.constant.enums.CampStatusEnum;
import com.kxs.system.biz.mapper.KxsCampBonusLogMapper;
import com.kxs.system.biz.mapper.KxsCampMapper;
import com.kxs.system.biz.mapper.KxsCampReportMapper;
import com.kxs.system.biz.mapper.KxsCampUserMapper;
import com.kxs.system.biz.service.KxsCampReportService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

/**
 * 训练营报备(KxsCampReport)表服务实现类
 *
 * @author 系统
 * @since 2024-07-02 15:10:29
 */
@Service("kxsCampReportService")
@RequiredArgsConstructor
public class KxsCampReportServiceImpl extends ServiceImpl<KxsCampReportMapper, KxsCampReport> implements KxsCampReportService {

    private final KxsCampMapper kxsCampMapper;

    private final KxsCampUserMapper kxsCampUserMapper;

    private final KxsCampBonusLogMapper kxsCampBonusLogMapper;
    @Override
    public Boolean report(KxsCampReport param) {

        Long userId = SecurityUtils.getUser().getId();
        param.setUserId(userId);

        //查询此用户数否参加训练营
        KxsCamp kxsCamp = kxsCampMapper.selectUserCamp(userId.intValue());
        Optional.ofNullable(kxsCamp).orElseThrow(() -> new GlobalCustomerException("您未参加训练营/训练营已结束"));
        if(!Objects.equals(kxsCamp.getId(), param.getCampId())){
            throw new GlobalCustomerException("训练营ID错误");
        }
        KxsCampUser kxsCampUser = kxsCampUserMapper.selectOne(Wrappers.<KxsCampUser>lambdaQuery()
                .eq(KxsCampUser::getCampId, kxsCamp.getId())
                .eq(KxsCampUser::getUserId, param.getUserId()));
        param.setUserCode(kxsCampUser.getUserCode());
        param.setUsername(kxsCampUser.getUsername());
        return save(param);
    }

    @Override
    public Boolean cancelReport(KxsCampReport param) {

        if(param.getId() == null){
            throw new GlobalCustomerException("id不能为空");
        }

        return removeById(param.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean reportAudit(CampReportAuditDTO param) {

        KxsCampReport campReport = getById(param.getId());
        Optional.ofNullable(campReport).orElseThrow(() -> new GlobalCustomerException("训练营报备不存在"));
        if(campReport.getStatus() == 1 || campReport.getStatus() == -1){
            throw new GlobalCustomerException("该报备已审核");
        }
        campReport.setCreateBy(param.getCreateBy());
        //审核拒绝
        if(param.getStatus() == -1) {
            campReport.setStatus(param.getStatus());
            campReport.setRemark(param.getRemark());
        }
        KxsCamp kxsCamp = kxsCampMapper.selectById(campReport.getCampId());
        Optional.ofNullable(kxsCamp).orElseThrow(() -> new GlobalCustomerException("训练营不存在"));
        //审核通过
        if(param.getStatus() == 1) {
            if(!Objects.equals(kxsCamp.getStatus(), CampStatusEnum.STATUS_NORMAL.getType())){

                throw new GlobalCustomerException("该报备已失效");
            }
            campReport.setStatus(param.getStatus());

            KxsCampUser kxsCampUser = kxsCampUserMapper.selectOne(Wrappers.<KxsCampUser>lambdaQuery()
                    .eq(KxsCampUser::getCampId, kxsCamp.getId())
                    .eq(KxsCampUser::getUserId, campReport.getUserId()));

            switch (param.getSourceType()) {
                case 0, 1, 2, 8 -> {
                    kxsCamp.setOrderNum(kxsCamp.getOrderNum() + param.getSourceNum());
                    kxsCampUser.setTeamOrderNum(kxsCampUser.getTeamOrderNum() + param.getSourceNum());
                }//电签券/组合包/盟主兑换券
                case 3, 4 -> {
                    kxsCamp.setOrderNum(kxsCamp.getOrderNum() + param.getSourceNum());
                    kxsCampUser.setTeamOrderNum(kxsCampUser.getTeamOpenNum() + param.getSourceNum());
                    kxsCampUser.setTeamLeaderNum(kxsCampUser.getTeamLeaderNum() + param.getSourceNum());
                }//盟主订单
                case 5,12 -> {
                    kxsCamp.setOpenNum(kxsCamp.getOrderNum() + param.getSourceNum());
                    kxsCampUser.setTeamOpenNum(kxsCampUser.getTeamOpenNum() + param.getSourceNum());
                    kxsCampUser.setTeamPosNum(kxsCampUser.getTeamPosNum() + param.getSourceNum());
                } //pos激活
                case 6 -> {
                    kxsCamp.setOpenNum(kxsCamp.getOrderNum() + param.getSourceNum());
                    kxsCampUser.setTeamOpenNum(kxsCampUser.getTeamOpenNum() + param.getSourceNum());
                    kxsCampUser.setTeamGdNum(kxsCampUser.getTeamGdNum() + param.getSourceNum());
                }//广电激活
                case 7,9,10,11 -> {
                    kxsCamp.setOpenNum(kxsCamp.getOrderNum() + param.getSourceNum());
                    kxsCampUser.setTeamOpenNum(kxsCampUser.getTeamOpenNum() + param.getSourceNum());
                    kxsCampUser.setTeamLkbNum(kxsCampUser.getTeamLkbNum() + param.getSourceNum());
                } //来客吧激活

            }
            kxsCampMapper.updateById(kxsCamp);

            kxsCampUserMapper.updateById(kxsCampUser);

            //添加日志
            KxsCampBonusLog kxsCampBonusLog = new KxsCampBonusLog();
            kxsCampBonusLog.setCampId(kxsCamp.getId());
            kxsCampBonusLog.setCampUserId(kxsCampUser.getUserId());
            kxsCampBonusLog.setGroupId(kxsCampUser.getGroupId());
            kxsCampBonusLog.setSourceId(kxsCampUser.getUserId());
            kxsCampBonusLog.setUsername(kxsCampUser.getUsername());
            kxsCampBonusLog.setUserCode(kxsCampUser.getUserCode());
            kxsCampBonusLog.setRemark("系统添加");
            kxsCampBonusLog.setKind(KindTypeEnum.ADD.getType());
            kxsCampBonusLog.setSourceNum(param.getSourceNum());
            kxsCampBonusLog.setSourceType(param.getSourceType());
            kxsCampBonusLog.setCreateTime(campReport.getCreateTime());
            kxsCampBonusLogMapper.insert(kxsCampBonusLog);
        }
        baseMapper.updateById(campReport);
        return Boolean.TRUE;
    }

    @Override
    public IPage<ReportCampPageVO> reportPage(Page<ReportCampPageVO> page, KxsCampReport param) {

        return baseMapper.selectReportPage(page, param);
    }
}

