package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.*;
import com.usky.fire.mapper.ReportConfigMapper;
import com.usky.fire.service.ReportConfigService;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.service.ReportIndexConfigService;
import com.usky.fire.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 报告配置表 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-06-27
 */
@Service
public class ReportConfigServiceImpl extends AbstractCrudService<ReportConfigMapper, ReportConfig> implements ReportConfigService {
    @Autowired
    private ReportIndexConfigService reportIndexConfigService;

    @Override
    public List<ReportConfigVO> configInfo() {
        List<ReportConfigVO> list = new ArrayList<>();
        LambdaQueryWrapper<ReportConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReportConfig::getDeleteFlag, 0)
                    .eq(ReportConfig::getTenantId, SecurityUtils.getTenantId());
        List<ReportConfig> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            List<ReportConfigVO> list2 = new ArrayList<>();
            List<ReportConfigVO> list3 = new ArrayList<>();
            List<ReportConfigVO> list4 = new ArrayList<>();
            List<ReportConfigVO> list5 = new ArrayList<>();
            list2 = baseMapper.configNumList1(SecurityUtils.getTenantId());
            list3 = baseMapper.configNumList(SecurityUtils.getTenantId(),1);
            list4 = baseMapper.configNumList(SecurityUtils.getTenantId(),2);
            list5 = baseMapper.configNumList(SecurityUtils.getTenantId(),3);
            if (CollectionUtils.isNotEmpty(list2)) {
                for (int i = 0; i < list2.size(); i++) {
                    for (int j = 0; j < list3.size(); j++) {
                        if (list2.get(i).getId().equals(list3.get(j).getId())){
                            list2.get(i).setIndexNum1(list3.get(j).getIndexNum1());
                        }
                    }
                    for (int k = 0; k < list4.size(); k++) {
                        if (list2.get(i).getId().equals(list4.get(k).getId())){
                            list2.get(i).setIndexNum2(list4.get(k).getIndexNum1());
                        }
                    }
                    for (int g = 0; g < list5.size(); g++) {
                        if (list2.get(i).getId().equals(list5.get(g).getId())){
                            list2.get(i).setIndexNum3(list5.get(g).getIndexNum1());
                        }
                    }
                    list.add(list2.get(i));
                }
            }
        }
        return list;
    }
    @Override
    public List<ReportIndexVo> reportIndexSelect(Integer id) {
        List<ReportIndexVo> list = new ArrayList<>();
        List<ReportIndex1Vo> list2 = new ArrayList<>();
        LambdaQueryWrapper<ReportConfig> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReportConfig::getId,id);
        List<ReportConfig> list1 = this.list(queryWrapper);
        ReportIndexVo reportIndexVo = new ReportIndexVo();
        if (CollectionUtils.isNotEmpty(list1)) {
            reportIndexVo.setId(list1.get(0).getId());
            reportIndexVo.setReportName(list1.get(0).getReportName());
            LambdaQueryWrapper<ReportIndexConfig> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1
                    .eq(ReportIndexConfig::getReportId,list1.get(0).getId())
                    .eq(ReportIndexConfig::getIndexGrade,1);
            List<ReportIndexConfig> list3 = reportIndexConfigService.list(queryWrapper1);
            if (CollectionUtils.isNotEmpty(list3)) {
                List<Integer> indexIdList1 = new ArrayList<>();
                for (int i = 0; i < list3.size(); i++) {
                    indexIdList1.add(list3.get(i).getId());
                }
                LambdaQueryWrapper<ReportIndexConfig> queryWrapper2 = Wrappers.lambdaQuery();
                queryWrapper2
                        .in(ReportIndexConfig::getSeniorIndex,indexIdList1);
                List<ReportIndexConfig> list4 = reportIndexConfigService.list(queryWrapper2);
                for (int k = 0; k < list3.size(); k++) {
                    ReportIndex1Vo reportIndex1Vo = new ReportIndex1Vo();
                    reportIndex1Vo.setId(list3.get(k).getId());
                    reportIndex1Vo.setIndexCode(list3.get(k).getIndexCode());
                    reportIndex1Vo.setIndexGrade(list3.get(k).getIndexGrade());
                    reportIndex1Vo.setIndexName(list3.get(k).getIndexName());
                    reportIndex1Vo.setIndexWeight(list3.get(k).getIndexWeight());
                    reportIndex1Vo.setIndexDesc(list3.get(k).getIndexDesc());
                if (CollectionUtils.isNotEmpty(list4)) {
                    List<Integer> indexIdList2 = new ArrayList<>();
                    for (int j = 0; j < list4.size(); j++) {
                        indexIdList2.add(list4.get(j).getId());
                    }
                    LambdaQueryWrapper<ReportIndexConfig> queryWrapper3 = Wrappers.lambdaQuery();
                    queryWrapper3
                            .in(ReportIndexConfig::getSeniorIndex,indexIdList2);
                    List<ReportIndexConfig> list5 = reportIndexConfigService.list(queryWrapper3);

                        List<ReportIndex2Vo> list6 = new ArrayList<>();
                        for (int l = 0; l < list4.size(); l++) {
                            List<ReportIndex3Vo> list7 = new ArrayList<>();
                            if (list4.get(l).getSeniorIndex().equals(list3.get(k).getId())){
                                ReportIndex2Vo reportIndex2Vo = new ReportIndex2Vo();
                                reportIndex2Vo.setId(list4.get(l).getId());
                                reportIndex2Vo.setIndexCode(list4.get(l).getIndexCode());
                                reportIndex2Vo.setIndexGrade(list4.get(l).getIndexGrade());
                                reportIndex2Vo.setIndexName(list4.get(l).getIndexName());
                                reportIndex2Vo.setIndexWeight(list4.get(l).getIndexWeight());
                                reportIndex2Vo.setIndexDesc(list4.get(l).getIndexDesc());
                                for (int m = 0; m < list5.size(); m++) {
                                    if (list5.get(m).getSeniorIndex().equals(list4.get(l).getId())){
                                        ReportIndex3Vo reportIndex3Vo = new ReportIndex3Vo();
                                        reportIndex3Vo.setId(list5.get(m).getId());
                                        reportIndex3Vo.setIndexCode(list5.get(m).getIndexCode());
                                        reportIndex3Vo.setIndexGrade(list5.get(m).getIndexGrade());
                                        reportIndex3Vo.setIndexName(list5.get(m).getIndexName());
                                        reportIndex3Vo.setIndexWeight(list5.get(m).getIndexWeight());
                                        reportIndex3Vo.setIndexDesc(list5.get(m).getIndexDesc());
                                        list7.add(reportIndex3Vo);
                                    }
                                }
                                reportIndex2Vo.setChildren(list7);
                                list6.add(reportIndex2Vo);
                            }
                        }
                        reportIndex1Vo.setChildren(list6);
                        list2.add(reportIndex1Vo);
                    }
                    reportIndexVo.setReportIndex(list2);
                    list.add(reportIndexVo);
                }
            }
        }
        return list;
    }
    @Override
    public void delReportConfig(Integer id) {
        ReportConfig reportConfig = new ReportConfig();
        reportConfig.setId(id);
        reportConfig.setDeleteFlag(1);
        reportConfig.setUpdateTime(LocalDateTime.now());
        reportConfig.setUpdateBy(SecurityUtils.getUsername());
        this.updateById(reportConfig);
    }

    @Transactional
    @Override
    public void addReportConfig(ReportIndexVo reportIndexVo) {
        ReportConfig reportConfig = new ReportConfig();
        reportConfig.setReportName(reportIndexVo.getReportName());
        reportConfig.setCreateBy(SecurityUtils.getUsername());
        reportConfig.setCreateTime(LocalDateTime.now());
        reportConfig.setTenantId(SecurityUtils.getTenantId());
        this.save(reportConfig);
        Integer configId = reportConfig.getId();
        List<ReportIndex1Vo> list = reportIndexVo.getReportIndex();
        if (CollectionUtils.isNotEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                ReportIndexConfig reportIndexConfig = new ReportIndexConfig();
                reportIndexConfig.setIndexCode(list.get(i).getIndexCode());
                reportIndexConfig.setCreateBy(SecurityUtils.getUsername());
                reportIndexConfig.setCreateTime(LocalDateTime.now());
                reportIndexConfig.setTenantId(SecurityUtils.getTenantId());
                reportIndexConfig.setIndexDesc(list.get(i).getIndexDesc());
                reportIndexConfig.setIndexGrade(list.get(i).getIndexGrade());
                reportIndexConfig.setIndexName(list.get(i).getIndexName());
                reportIndexConfig.setIndexWeight(list.get(i).getIndexWeight());
                reportIndexConfig.setReportId(configId);
                reportIndexConfig.setSeniorIndex(0);
                reportIndexConfigService.save(reportIndexConfig);
                Integer indexConfigId1 = reportIndexConfig.getId();
                List<ReportIndex2Vo> list1 = list.get(i).getChildren();
                if (CollectionUtils.isNotEmpty(list1)){
                    for (int j = 0; j < list1.size(); j++) {
                        ReportIndexConfig reportIndexConfig2 = new ReportIndexConfig();
                        reportIndexConfig2.setIndexCode(list1.get(j).getIndexCode());
                        reportIndexConfig2.setCreateBy(SecurityUtils.getUsername());
                        reportIndexConfig2.setCreateTime(LocalDateTime.now());
                        reportIndexConfig2.setTenantId(SecurityUtils.getTenantId());
                        reportIndexConfig2.setIndexDesc(list1.get(j).getIndexDesc());
                        reportIndexConfig2.setIndexGrade(list1.get(j).getIndexGrade());
                        reportIndexConfig2.setIndexName(list1.get(j).getIndexName());
                        reportIndexConfig2.setIndexWeight(list1.get(j).getIndexWeight());
                        reportIndexConfig2.setReportId(configId);
                        reportIndexConfig2.setSeniorIndex(indexConfigId1);
                        reportIndexConfigService.save(reportIndexConfig2);
                        Integer indexConfigId2 = reportIndexConfig2.getId();
                        List<ReportIndex3Vo> list2 = list1.get(j).getChildren();
                        if (CollectionUtils.isNotEmpty(list2)) {
                            for (int k = 0; k < list2.size(); k++) {
                                ReportIndexConfig reportIndexConfig3 = new ReportIndexConfig();
                                reportIndexConfig3.setIndexCode(list2.get(k).getIndexCode());
                                reportIndexConfig3.setCreateBy(SecurityUtils.getUsername());
                                reportIndexConfig3.setCreateTime(LocalDateTime.now());
                                reportIndexConfig3.setTenantId(SecurityUtils.getTenantId());
                                reportIndexConfig3.setIndexDesc(list2.get(k).getIndexDesc());
                                reportIndexConfig3.setIndexGrade(list2.get(k).getIndexGrade());
                                reportIndexConfig3.setIndexName(list2.get(k).getIndexName());
                                reportIndexConfig3.setIndexWeight(list2.get(k).getIndexWeight());
                                reportIndexConfig3.setReportId(configId);
                                reportIndexConfig3.setSeniorIndex(indexConfigId2);
                                reportIndexConfigService.save(reportIndexConfig3);
                            }
                        }
                    }
                }
            }
        }
    }

    @Transactional
    @Override
    public void updateReportConfig(ReportIndexVo reportIndexVo) {
        ReportConfig reportConfig = new ReportConfig();
        reportConfig.setReportName(reportIndexVo.getReportName());
        reportConfig.setId(reportIndexVo.getId());
        reportConfig.setUpdateBy(SecurityUtils.getUsername());
        reportConfig.setUpdateTime(LocalDateTime.now());
        this.updateById(reportConfig);
        List<ReportIndex1Vo> list = reportIndexVo.getReportIndex();
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                ReportIndexConfig reportIndexConfig = new ReportIndexConfig();
                reportIndexConfig.setIndexCode(list.get(i).getIndexCode());
                reportIndexConfig.setIndexDesc(list.get(i).getIndexDesc());
                reportIndexConfig.setIndexGrade(list.get(i).getIndexGrade());
                reportIndexConfig.setIndexName(list.get(i).getIndexName());
                reportIndexConfig.setIndexWeight(list.get(i).getIndexWeight());
                if (list.get(i).getId()==null){
                    reportIndexConfig.setCreateBy(SecurityUtils.getUsername());
                    reportIndexConfig.setCreateTime(LocalDateTime.now());
                    reportIndexConfig.setTenantId(SecurityUtils.getTenantId());
                    reportIndexConfig.setReportId(reportIndexVo.getId());
                    reportIndexConfig.setSeniorIndex(0);
                    reportIndexConfigService.save(reportIndexConfig);
                    Integer indexConfigId1 = reportIndexConfig.getId();
                    List<ReportIndex2Vo> list1 = list.get(i).getChildren();
                    if (CollectionUtils.isNotEmpty(list1)) {
                        for (int j = 0; j < list1.size(); j++) {
                            ReportIndexConfig reportIndexConfig2 = new ReportIndexConfig();
                            reportIndexConfig2.setIndexCode(list1.get(j).getIndexCode());
                            reportIndexConfig2.setCreateBy(SecurityUtils.getUsername());
                            reportIndexConfig2.setCreateTime(LocalDateTime.now());
                            reportIndexConfig2.setTenantId(SecurityUtils.getTenantId());
                            reportIndexConfig2.setIndexDesc(list1.get(j).getIndexDesc());
                            reportIndexConfig2.setIndexGrade(list1.get(j).getIndexGrade());
                            reportIndexConfig2.setIndexName(list1.get(j).getIndexName());
                            reportIndexConfig2.setIndexWeight(list1.get(j).getIndexWeight());
                            reportIndexConfig2.setReportId(reportIndexVo.getId());
                            reportIndexConfig2.setSeniorIndex(indexConfigId1);
                            reportIndexConfigService.save(reportIndexConfig2);
                            Integer indexConfigId2 = reportIndexConfig2.getId();
                            List<ReportIndex3Vo> list2 = list1.get(j).getChildren();
                            if (CollectionUtils.isNotEmpty(list2)) {
                                for (int k = 0; k < list2.size(); k++) {
                                    ReportIndexConfig reportIndexConfig3 = new ReportIndexConfig();
                                    reportIndexConfig3.setIndexCode(list2.get(k).getIndexCode());
                                    reportIndexConfig3.setCreateBy(SecurityUtils.getUsername());
                                    reportIndexConfig3.setCreateTime(LocalDateTime.now());
                                    reportIndexConfig3.setTenantId(SecurityUtils.getTenantId());
                                    reportIndexConfig3.setIndexDesc(list2.get(k).getIndexDesc());
                                    reportIndexConfig3.setIndexGrade(list2.get(k).getIndexGrade());
                                    reportIndexConfig3.setIndexName(list2.get(k).getIndexName());
                                    reportIndexConfig3.setIndexWeight(list2.get(k).getIndexWeight());
                                    reportIndexConfig3.setReportId(reportIndexVo.getId());
                                    reportIndexConfig3.setSeniorIndex(indexConfigId2);
                                    reportIndexConfigService.save(reportIndexConfig3);
                                }
                            }
                        }
                    }
                }else {
                    reportIndexConfig.setId(list.get(i).getId());
                    reportIndexConfig.setUpdateBy(SecurityUtils.getUsername());
                    reportIndexConfig.setUpdateTime(LocalDateTime.now());
                    reportIndexConfigService.updateById(reportIndexConfig);
                    List<ReportIndex2Vo> list1 = list.get(i).getChildren();
                    if (CollectionUtils.isNotEmpty(list1)) {
                        for (int j = 0; j < list1.size(); j++) {
                            ReportIndexConfig reportIndexConfig2 = new ReportIndexConfig();
                            reportIndexConfig2.setIndexCode(list1.get(j).getIndexCode());
                            reportIndexConfig2.setIndexDesc(list1.get(j).getIndexDesc());
                            reportIndexConfig2.setIndexGrade(list1.get(j).getIndexGrade());
                            reportIndexConfig2.setIndexName(list1.get(j).getIndexName());
                            reportIndexConfig2.setIndexWeight(list1.get(j).getIndexWeight());
                            if (list1.get(j).getId()==null){
                                reportIndexConfig2.setCreateBy(SecurityUtils.getUsername());
                                reportIndexConfig2.setCreateTime(LocalDateTime.now());
                                reportIndexConfig2.setTenantId(SecurityUtils.getTenantId());
                                reportIndexConfig2.setReportId(reportIndexVo.getId());
                                reportIndexConfig2.setSeniorIndex(list.get(i).getId());
                                reportIndexConfigService.save(reportIndexConfig2);
                                Integer indexConfigId2 = reportIndexConfig2.getId();
                                List<ReportIndex3Vo> list2 = list1.get(j).getChildren();
                                if (CollectionUtils.isNotEmpty(list2)) {
                                    for (int k = 0; k < list2.size(); k++) {
                                        ReportIndexConfig reportIndexConfig3 = new ReportIndexConfig();
                                        reportIndexConfig3.setIndexCode(list2.get(k).getIndexCode());
                                        reportIndexConfig3.setCreateBy(SecurityUtils.getUsername());
                                        reportIndexConfig3.setCreateTime(LocalDateTime.now());
                                        reportIndexConfig3.setTenantId(SecurityUtils.getTenantId());
                                        reportIndexConfig3.setIndexDesc(list2.get(k).getIndexDesc());
                                        reportIndexConfig3.setIndexGrade(list2.get(k).getIndexGrade());
                                        reportIndexConfig3.setIndexName(list2.get(k).getIndexName());
                                        reportIndexConfig3.setIndexWeight(list2.get(k).getIndexWeight());
                                        reportIndexConfig3.setReportId(reportIndexVo.getId());
                                        reportIndexConfig3.setSeniorIndex(indexConfigId2);
                                        reportIndexConfigService.save(reportIndexConfig3);
                                    }
                                }
                            }else {
                                reportIndexConfig2.setId(list1.get(j).getId());
                                reportIndexConfig2.setUpdateBy(SecurityUtils.getUsername());
                                reportIndexConfig2.setUpdateTime(LocalDateTime.now());
                                reportIndexConfigService.updateById(reportIndexConfig2);
                                List<ReportIndex3Vo> list2 = list1.get(j).getChildren();
                                if (CollectionUtils.isNotEmpty(list2)) {
                                    for (int k = 0; k < list2.size(); k++) {
                                        ReportIndexConfig reportIndexConfig3 = new ReportIndexConfig();
                                        reportIndexConfig3.setIndexCode(list2.get(k).getIndexCode());
                                        reportIndexConfig3.setIndexDesc(list2.get(k).getIndexDesc());
                                        reportIndexConfig3.setIndexGrade(list2.get(k).getIndexGrade());
                                        reportIndexConfig3.setIndexName(list2.get(k).getIndexName());
                                        reportIndexConfig3.setIndexWeight(list2.get(k).getIndexWeight());
                                        if (list2.get(k).getId()==null){
                                            reportIndexConfig3.setCreateBy(SecurityUtils.getUsername());
                                            reportIndexConfig3.setCreateTime(LocalDateTime.now());
                                            reportIndexConfig3.setTenantId(SecurityUtils.getTenantId());
                                            reportIndexConfig3.setReportId(reportIndexVo.getId());
                                            reportIndexConfig3.setSeniorIndex(list1.get(j).getId());
                                            reportIndexConfigService.save(reportIndexConfig3);
                                        }else {
                                            reportIndexConfig3.setId(list2.get(k).getId());
                                            reportIndexConfig3.setUpdateBy(SecurityUtils.getUsername());
                                            reportIndexConfig3.setUpdateTime(LocalDateTime.now());
                                            reportIndexConfigService.updateById(reportIndexConfig3);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
        }
    }
}
