package com.dbnt.faisp.fpiMgt.affairResult.service;


import com.dbnt.faisp.config.BaseService;
import com.dbnt.faisp.config.FileInfo;
import com.dbnt.faisp.fpiMgt.affairResult.mapper.ResultMapper;
import com.dbnt.faisp.fpiMgt.affairResult.model.ResultApprv;
import com.dbnt.faisp.fpiMgt.affairResult.model.ResultBoard;
import com.dbnt.faisp.fpiMgt.affairResult.model.ResultFile;
import com.dbnt.faisp.fpiMgt.affairResult.repository.ResultApprvRepository;
import com.dbnt.faisp.fpiMgt.affairResult.repository.ResultBoardRepository;
import com.dbnt.faisp.fpiMgt.affairResult.repository.ResultFileRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class ResultService extends BaseService { // 월간계획
  private final ResultBoardRepository resultBoardRepository;
  private final ResultFileRepository resultFileRepository;
  private final ResultApprvRepository resultApprvRepository;
  private final ResultMapper resultMapper;

  public List<ResultBoard> selectResultBoardList(ResultBoard resultBoard) {
    return resultMapper.selectResultBoardList(resultBoard);
  }

  public Integer selectResultBoardListCnt(ResultBoard resultBoard) {
    return resultMapper.selectResultBoardListCnt(resultBoard);
  }

  public ResultBoard selectResultBoard(Integer resultKey) {
    ResultBoard savedResult = resultBoardRepository.findById(resultKey).orElse(null);
    if (savedResult != null) {
      savedResult.setFileList(resultFileRepository.findByResultKey(resultKey));
      savedResult.setApprvList(resultApprvRepository.findByResultKey(resultKey));
    }
    return savedResult;
  }

  @Transactional
  public Integer saveResultBoard(ResultBoard resultBoard, List<Integer> deleteFileSeq) {
    Integer resultKey = resultBoardRepository.save(resultBoard).getResultKey();
    if(deleteFileSeq != null && deleteFileSeq.size()>0){
      deleteResultFile(resultKey, deleteFileSeq);
    }
    if(resultBoard.getMultipartFileList()!=null){
      saveUploadFiles(resultKey, resultBoard.getMultipartFileList());
    }
    return resultKey;
  }

  private void saveUploadFiles(Integer resultKey, List<MultipartFile> multipartFileList){
    ResultFile lastFileInfo = resultFileRepository.findTopByResultKeyOrderByFileSeqDesc(resultKey).orElse(null);
    int fileSeq = lastFileInfo==null?1:(lastFileInfo.getFileSeq()+1);
    for(MultipartFile file : multipartFileList){
      String saveName = UUID.randomUUID().toString();
      String path = locationPath+File.separator+"affairResult"+File.separator;
      saveFile(file, new File(path+File.separator+saveName));

      String originalFilename = file.getOriginalFilename();
      int extnIdx = originalFilename.lastIndexOf(".");
      ResultFile fileInfo = new ResultFile();
      fileInfo.setResultKey(resultKey);
      fileInfo.setFileSeq(fileSeq++);
      fileInfo.setOrigNm(originalFilename.substring(0, extnIdx));
      fileInfo.setFileExtn(originalFilename.substring(extnIdx+1));
      fileInfo.setConvNm(saveName);
      fileInfo.setFileSize(calculationSize(file.getSize()));
      fileInfo.setSavePath(path);
      resultFileRepository.save(fileInfo);
    }
  }

  private void deleteResultFile(Integer resultKey, List<Integer> deleteFileSeq) {
    List<ResultFile> resultFileList = resultFileRepository.findByResultKey(resultKey);
    for(ResultFile file: resultFileList){
      if(deleteFileSeq.contains(file.getFileSeq())){
        deleteStoredFile(new File(file.getSavePath(), file.getConvNm()));
        resultFileRepository.delete(file);
      }
    }
  }

  public FileInfo selectResultFile(Integer resultKey, Integer fileSeq) {
    return resultFileRepository.findById(new ResultFile.ResultFileId(resultKey, fileSeq)).orElse(null);
  }

  @Transactional
  public Integer resultStateChange(ResultApprv apprv) {
    ResultBoard savedResult = resultBoardRepository.findById(apprv.getResultKey()).orElse(null);
    savedResult.setResultState(apprv.getState());
    ResultApprv lastApprv = resultApprvRepository.findTopByResultKeyOrderByApprvSeqDesc(apprv.getResultKey()).orElse(null);
    apprv.setApprvSeq(lastApprv==null?1:lastApprv.getApprvSeq()+1);
    resultApprvRepository.save(apprv);
    return apprv.getApprvSeq();
  }
}
