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

import cn.cslg.report.common.model.dto.invalidReReport.InvalidReasonFieldDTO;
import cn.cslg.report.common.model.dto.invalidReReport.InvalidReasonFieldValueDTO;
import cn.cslg.report.common.utils.Response;
import cn.cslg.report.entity.invalidReReport.InvalidReasonField;
import cn.cslg.report.entity.invalidReReport.InvalidReasonFieldValue;
import cn.cslg.report.mapper.InvalidReReport.InvalidReasonFiledMapper;
import cn.cslg.report.mapper.InvalidReReport.InvalidReasonFiledValueMapper;
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.beans.BeanUtils;
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 InvalidReasonFieldValueService extends ServiceImpl<InvalidReasonFiledValueMapper,InvalidReasonFieldValue> {

    public Boolean addOrUpdate(List<InvalidReasonFieldValueDTO> invalidReasonFieldValueDTOs) {
          //根据无效理由id和栏位id查询
        if(invalidReasonFieldValueDTOs==null||invalidReasonFieldValueDTOs.size()==0){
            return false;
        }
List<InvalidReasonFieldValue> invalidReasonFieldValues =this.baseMapper.getValues(invalidReasonFieldValueDTOs);
        List<InvalidReasonFieldValue> addValues =new ArrayList<>();
for (InvalidReasonFieldValueDTO fieldDTO:invalidReasonFieldValueDTOs){
   InvalidReasonFieldValue invalidReasonFieldValue= invalidReasonFieldValues.stream()
            .filter(item->item.getInvalidReasonId().equals(fieldDTO.getInvalidReasonId())&&item.getInvalidReasonFieldId().equals(fieldDTO.getFieldId()))
            .findFirst().orElse(new InvalidReasonFieldValue());
    if(invalidReasonFieldValue.getId()==null){
        invalidReasonFieldValue.setInvalidReasonId(fieldDTO.getInvalidReasonId());
        invalidReasonFieldValue.setInvalidReasonFieldId(fieldDTO.getFieldId());
    }
    invalidReasonFieldValue.setFieldValue(fieldDTO.getFieldValue());

    addValues.add(invalidReasonFieldValue);
}
this.saveOrUpdateBatch(addValues);
        return true;
    }



    public Boolean addOrUpdateLists(List<InvalidReasonFieldValueDTO> invalidReasonFieldValueDTOs) {
        //根据无效理由id和栏位id查询
        if(invalidReasonFieldValueDTOs==null||invalidReasonFieldValueDTOs.size()==0){
            return false;
        }
        List<InvalidReasonFieldValue> invalidReasonFieldValues =this.baseMapper.getValues(invalidReasonFieldValueDTOs);
        List<InvalidReasonFieldValue> addValues =new ArrayList<>();
        for (InvalidReasonFieldValueDTO fieldDTO:invalidReasonFieldValueDTOs){
            InvalidReasonFieldValue invalidReasonFieldValue= invalidReasonFieldValues.stream()
                    .filter(item->item.getInvalidReasonId().equals(fieldDTO.getInvalidReasonId())&&item.getInvalidReasonFieldId().equals(fieldDTO.getFieldId()))
                    .findFirst().orElse(new InvalidReasonFieldValue());
            if(invalidReasonFieldValue.getId()==null){
                invalidReasonFieldValue.setFieldValue(fieldDTO.getFieldValue());
                invalidReasonFieldValue.setInvalidReasonId(fieldDTO.getInvalidReasonId());
                invalidReasonFieldValue.setInvalidReasonFieldId(fieldDTO.getFieldId());
            }
            addValues.add(invalidReasonFieldValue);
        }
        this.saveOrUpdateBatch(addValues);
        return  true;
    }
}