package com.dbnt.faisp.main.faStatistics.internationalCrimeArrest.service;


import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.main.faStatistics.internationalCrimeArrest.mapper.InternationalCrimeArrestMapper;
import com.dbnt.faisp.main.faStatistics.internationalCrimeArrest.model.InternationalCrimeArrest;
import com.dbnt.faisp.main.faStatistics.internationalCrimeArrest.model.SuspectPersonInfo;
import com.dbnt.faisp.main.faStatistics.internationalCrimeArrest.repository.InternationalCrimeArrestRepository;
import com.dbnt.faisp.main.faStatistics.internationalCrimeArrest.repository.SuspectPersonInfoRepository;
import com.dbnt.faisp.util.ParamMap;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class InternationalCrimeArrestService extends BaseService {
  private final InternationalCrimeArrestRepository internationalCrimeArrestRepository;
  private final InternationalCrimeArrestMapper internationalCrimeArrestMapper;
  private final SuspectPersonInfoRepository suspectPersonInfoRepository;

  public List<InternationalCrimeArrest> selectInternationalCrimeArrestList(InternationalCrimeArrest InternationalCrimeArrest) {
    return internationalCrimeArrestMapper.selectInternationalCrimeArrestList(InternationalCrimeArrest);
  }

  public List<ParamMap> selectInternationalCrimeArrestParamList(InternationalCrimeArrest InternationalCrimeArrest) {
    return internationalCrimeArrestMapper.selectInternationalCrimeArrestParamList(InternationalCrimeArrest);
  }

  public Integer selectInternationalCrimeArrestListCnt(InternationalCrimeArrest internationalCrimeArrest) {
    return internationalCrimeArrestMapper.selectInternationalCrimeArrestListCnt(internationalCrimeArrest);
  }

  public Integer icaDuplicateCount(InternationalCrimeArrest internationalCrimeArrest) {
    return internationalCrimeArrestMapper.icaDuplicateCount(internationalCrimeArrest);
  }

  public InternationalCrimeArrest selectInternationalCrimeArrest(Integer icaKey) {
    InternationalCrimeArrest savedInternationalCrimeArrest = internationalCrimeArrestRepository.findById(icaKey).orElse(null);
    if (savedInternationalCrimeArrest != null) {
      savedInternationalCrimeArrest.setSuspectPersonInfoList(suspectPersonInfoRepository.findByIcaKey(icaKey));
    }
    return savedInternationalCrimeArrest;
  }

  @Transactional
  public Integer savedInternationalCrimeArrest(InternationalCrimeArrest internationalCrimeArrest) {
    if (internationalCrimeArrest.getMeansEtc() != null) {
      internationalCrimeArrest.setMeans(internationalCrimeArrest.getMeansEtc());
    }
    if (internationalCrimeArrest.getPurposesEtc() != null) {
      internationalCrimeArrest.setPurpose(internationalCrimeArrest.getPurposesEtc());
    }
    if (internationalCrimeArrest.getSmuggledGoodsEtc() != null) {
      internationalCrimeArrest.setSmuggledGoods(internationalCrimeArrest.getSmuggledGoodsEtc());
    }
    if (internationalCrimeArrest.getProcessResultEtc() != null) {
      internationalCrimeArrest.setProcessResult(internationalCrimeArrest.getProcessResultEtc());
    }

    Integer icaKey = internationalCrimeArrestRepository.save(internationalCrimeArrest).getIcaKey();

    if (internationalCrimeArrest.getDeleteSpiKeyList() != null) {
      suspectPersonInfoRepository.deleteAllByIdInQuery(internationalCrimeArrest.getDeleteSpiKeyList());
    }

    if (internationalCrimeArrest.getSuspectPersonInfoList() != null) {
      for(SuspectPersonInfo info: internationalCrimeArrest.getSuspectPersonInfoList()){
        if (info.getCountryEtc() != null) {
          info.setCountry(info.getCountryEtc());
        }
        info.setIcaKey(icaKey);
      }
      suspectPersonInfoRepository.saveAll(internationalCrimeArrest.getSuspectPersonInfoList());
    }

    return icaKey;
  }
}
