package cn.cslg.report.service.business;

import cn.cslg.report.common.model.vo.CompareScenariosVO;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.report.entity.*;
import cn.cslg.report.entity.asso.AssoRecordsFeature;
import cn.cslg.report.mapper.CompareScenariosMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class CompareScenariosService extends ServiceImpl<CompareScenariosMapper, CompareScenarios> {
    private final CompareScenariosAndRecordsService compareScenariosAndRecordsService;
    private final AssoScenarIOSService assoScenarIOSService;
    private final CompareFilesService compareFilesService;
    private  final LoginUtils loginUtils;
    private final CompareRecordsService compareRecordsService;
    private final FeatureService featureService;
    private final AssoRecordsFeatureService assoRecordsFeatureService;
    public String add(CompareScenariosVO compareScenariosVO) throws IOException{
        //对比方案信息入表
        CompareScenarios compareScenarios = new CompareScenarios();
        compareScenarios.setContrastResult(compareScenariosVO.getContrastResult());
        compareScenarios.setReportId(compareScenariosVO.getReportId());
        compareScenarios.setRightName(compareScenariosVO.getRight());
        compareScenarios.setPersonId(loginUtils.getId());
        compareScenarios.setRightId(compareScenariosVO.getRightId());
        compareScenarios.setSignPatentNo(compareScenariosVO.getSignPatentNo());
        compareScenarios.insert();
        Integer id = compareScenarios.getId();
        List<Integer> FeaturesId = compareScenariosVO.getFeaturesIDList();
        for(Integer integer:FeaturesId){
            AssoScenarIOS compareScenariosAndRecords = new AssoScenarIOS();
            compareScenariosAndRecords.setScenariosID(id);
            compareScenariosAndRecords.setFeaturesID(integer);
            compareScenariosAndRecords.setFeatureRecordID(0);
            compareScenariosAndRecordsService.add(compareScenariosAndRecords);
        }
        List<Integer> recordIDList = compareScenariosVO.getRecordIDList();
        for(Integer integer:recordIDList) {
            AssoScenarIOS compareScenariosAndRecords = new AssoScenarIOS();
            compareScenariosAndRecords.setScenariosID(id);
            compareScenariosAndRecords.setFeatureRecordID(integer);
            compareScenariosAndRecords.setFeaturesID(0);
            compareScenariosAndRecordsService.add(compareScenariosAndRecords);
        }
       return Response.success();
    }

    public String select(int reportID)throws IOException{
       List<CompareScenariosVO> list=new ArrayList<>();
       //根据报告ID查询对比方案
        LambdaQueryWrapper<CompareScenarios> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CompareScenarios::getReportId, reportID);
        List<CompareScenarios> CompareScenarios = this.list(wrapper);
        //遍历对比方案
        for (CompareScenarios a:CompareScenarios){
            CompareScenariosVO compareScenariosVO=new CompareScenariosVO();
            compareScenariosVO.setRightId(a.getRightId());
            compareScenariosVO.setContrastResult( a.getContrastResult());
            compareScenariosVO.setReportId(a.getReportId());
            compareScenariosVO.setSignPatentNo(  a.getSignPatentNo());
            compareScenariosVO.setRight(a.getRightName());
            //根据对比方案ID获得s所有对比方案和对比记录关联信息
            List<AssoScenarIOS> select = assoScenarIOSService.select(a.getId());
            //获得Id不为0的对比记录和特征关联Id
            List<Integer> featureRecordIds = select.stream().filter(item->item.getFeatureRecordID()!=0).map(AssoScenarIOS::getFeatureRecordID).collect(Collectors.toList());
            //根据关联Id获得特征Id和对比记录ID
            LambdaQueryWrapper<AssoRecordsFeature> wrapper1 =new LambdaQueryWrapper<>();
            wrapper1.in(AssoRecordsFeature::getId,featureRecordIds);
            List<AssoRecordsFeature> assoRecordsFeatures =assoRecordsFeatureService.list(wrapper1);
            //特征Id
            List<Integer> featureIds= assoRecordsFeatures.stream().map(AssoRecordsFeature::getFeatureId).collect(Collectors.toList());
            //对比记录Id
            List<Integer> recordId  =assoRecordsFeatures.stream().map(AssoRecordsFeature::getRecordsId).collect(Collectors.toList());
            //根据特征Id查询特征
            LambdaQueryWrapper<Features> wrapper2 =new LambdaQueryWrapper<>();
            wrapper2.in(Features::getId,featureIds);
            List<Features> features =featureService.list(wrapper2);
            //根据记录ID查询记录
            LambdaQueryWrapper<CompareRecords> wrapper3 =new LambdaQueryWrapper<>();
            wrapper3.in(CompareRecords::getId,recordId);
            List<CompareRecords> compareRecords =compareRecordsService.list(wrapper3);
            assoRecordsFeatures.forEach(
                 item->{
                  Features features1 =features.stream().filter(tem->tem.getId().equals(item.getFeatureId())).findFirst().orElse(new Features());
                  CompareRecords compareRecords1 =compareRecords.stream().filter(tem->tem.getId().equals(item.getRecordsId())).findFirst().orElse(new CompareRecords());
                     item.setCompareRecords(compareRecords1);
                     item.setFeatures(features1);
                 }

            );

           List<Integer> featuresIds =select.stream().filter(item->item.getFeatureRecordID().equals(0)).map(AssoScenarIOS::getFeaturesID).collect(Collectors.toList());
            compareScenariosVO.setFeaturesIDList(featuresIds);
            compareScenariosVO.setAssoRecordsFeatures(assoRecordsFeatures);
         list.add(compareScenariosVO);
        }
        return Response.success(list);

    }



}
