package cn.cslg.report.service.business.InvalidReReport;

import cn.cslg.report.common.model.dto.invalidReReport.ProofStrDTO;
import cn.cslg.report.common.model.vo.invalidReReport.ArgumentsListVO;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.invalidReReport.Arguments;
import cn.cslg.report.entity.invalidReReport.ProofStr;
import cn.cslg.report.mapper.InvalidReReport.ProofStrMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProofStrService extends ServiceImpl<ProofStrMapper, ProofStr> {
    private final ArgumentsService argumentsService;
    public ArgumentsListVO queryProofStr(Integer proofId, Integer featureId) {
        ArgumentsListVO argumentsListVO =new ArgumentsListVO();
        LambdaQueryWrapper<ProofStr> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProofStr::getCompareFileId, proofId)
                .eq(ProofStr::getFeatureId, featureId);
        List<ProofStr> proofStrs = this.list(queryWrapper);
        ProofStr proofStr = new ProofStr();
        if (proofStrs.size() > 0) {
            proofStr = proofStrs.get(0);
            //根据无效理由id查询陈述意见
            LambdaQueryWrapper<Arguments> argumentsWrapper =new LambdaQueryWrapper<>();
            argumentsWrapper.eq(Arguments::getProofStrId,proofStr.getId());
            argumentsListVO.setProofStrId(proofStr.getId());
            argumentsListVO.setProofStr(proofStr.getProofStr());
            List<Arguments> arguments = argumentsService.list(argumentsWrapper);
            List<ArgumentsListVO.ArgumentIn> argumentIns =new ArrayList<>();
            arguments.forEach(item->{
                ArgumentsListVO.ArgumentIn argumentIn =new ArgumentsListVO.ArgumentIn();
                argumentIn.setArgumentStr(item.getArgumentStr());
                argumentIn.setId(item.getId());
                argumentIns.add(argumentIn);
            });
            argumentsListVO.setArguments(argumentIns);

        }
        return argumentsListVO;
    }

    public String addOrUpdateProofStr(ProofStrDTO proofStrDTO) {
        try {
            this.addOrUpdate(proofStrDTO);
            return Response.success("添加或更新成功");
        }
        catch (Exception e){
            return Response.error("添加或更新失败");
        }
    }

    /**
     * 添加或更新证据文字
     * @param proofStrDTO
     * @return
     */
    public ProofStr addOrUpdate(ProofStrDTO proofStrDTO) {
        ProofStr proofStr = new ProofStr();
        proofStr.setProofStr(proofStrDTO.getProofStr());
        proofStr.setFeatureId(proofStrDTO.getFeatureId());
        proofStr.setRightSort(proofStrDTO.getRightSort());
        proofStr.setReportId(proofStrDTO.getReportId());
        proofStr.setId(proofStrDTO.getProofStrId());
        proofStr.setCompareFileId(proofStrDTO.getCompareFileId());
        proofStr.insertOrUpdate();
        return proofStr;
    }

    public String deleteProofStr(List<Integer> ids) {
        try {
            if(ids==null||ids.size()==0){
                return Response.error("请选择无效证据");
            }
          this.removeByIds(ids);
            LambdaQueryWrapper<Arguments> argumentsWrapper=new LambdaQueryWrapper<>();
            argumentsWrapper.in(Arguments::getProofStrId,ids);
            argumentsService.remove(argumentsWrapper);
            return Response.success("删除无效证据成功");
        }
        catch (Exception e){
            return Response.error("删除无效证据失败");
        }
    }
}