package com.oqpo.api.service.oper;


import com.oqpo.api.entity.oper.InterfaceEntity;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.oper.InterfaceMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.oper.iflink.SaveInterfaceRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.oper.iflink.InterfaceResponse;
import com.oqpo.api.web.dto.response.oper.iflink.InterfaceSearchResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Slf4j
public class InterfaceService extends CommonService {

    @Autowired
    private InterfaceMapper interfaceMapper;

    /*
      외부연동 리스트 조회
     */
    public GridResponse selectInterfaceGridList(String fromDt, String toDt, String sLinkInfo, String sIfId,
                                                String sUseYn, String sIfNm, String sComMethCd, GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();

        fromDt = fromDt.replace(".", "");
        toDt = toDt.replace(".", "");

        int gridRecords = interfaceMapper.selectInterfaceGridCnt(fromDt, toDt, sLinkInfo, sIfId, sUseYn, sIfNm, sComMethCd);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<InterfaceEntity> entities = interfaceMapper.selectInterfaceGridList(fromDt, toDt, sLinkInfo, sIfId, sUseYn, sIfNm, sComMethCd, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> InterfaceSearchResponse.builder()
                        .viewCd("R")
                        .ifNo(m.getIfNo())
                        .ifId(m.getIfId())
                        .ifNm(m.getIfNm())
                        .useYn(m.getUseYn())
                        .useYnNm(m.getUseYnNm())
                        .comMethNm(m.getComMethNm())
                        .linkInfo(m.getLinkInfo())
                        .srTpNm(m.getSrTpNm())
                        .addDt(m.getAddDt())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

    /*
    배치 저장
     */
    @Transactional
    public void saveInterface(String userId, SaveInterfaceRequest saveInterfaceRequest) throws Exception {
        try {
            // 기본정보 저장
            InterfaceEntity entity = new InterfaceEntity();
            entity.setIfId(saveInterfaceRequest.getIfId());
            entity.setIfNm(saveInterfaceRequest.getIfNm());
            entity.setUseYn(saveInterfaceRequest.getUseYn());
            entity.setLinkInfo(saveInterfaceRequest.getLinkInfo());
            entity.setComMethCd(saveInterfaceRequest.getComMethCd());
            entity.setSrTpCd(saveInterfaceRequest.getSrTpCd());
            entity.setIfUrl(saveInterfaceRequest.getIfUrl());
            entity.setApiInfo(saveInterfaceRequest.getApiInfo());
            entity.setRequestInfo(saveInterfaceRequest.getRequestInfo());
            entity.setResponseInfo(saveInterfaceRequest.getResponseInfo());
            entity.setAddId(userId);
            entity.setChgId(userId);

            if ("C".equals(saveInterfaceRequest.getViewCd())) {
                entity.setIfNo(fnGetKeyNo(13));
                interfaceMapper.insertInterfaceInfo(userId, entity);
            } else if ("U".equals(saveInterfaceRequest.getViewCd())) {
                entity.setIfNo(saveInterfaceRequest.getIfNo());
                interfaceMapper.updateInterfaceInfo(userId, entity);
            }
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

    /*
    배치 삭제
     */
    @Transactional
    public void removeInterface(String userId, String ifNo) throws Exception {
        try {
            InterfaceEntity entity = new InterfaceEntity();
            entity.setIfNo(ifNo);
            entity.setChgId(userId);

            interfaceMapper.deleteInterfaceInfo(userId, entity);
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

    /*
   외부연동 정보 가져오기
    */
    public InterfaceResponse selectInterfaceInfo(String ifNo) throws Exception {
        return InterfaceResponse.toDTO(interfaceMapper.selectInterfaceInfo(ifNo));
    }

}
