package com.oqpo.api.service.system;


import com.oqpo.api.entity.system.VerInfoEntity;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.system.VerInfoMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.VerInfo.SaveVerInfoRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.ver.VerInfoResponse;
import com.oqpo.api.web.dto.response.ver.VerInfoSearchResponse;
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 VerInfoService extends CommonService {
    @Autowired
    private VerInfoMapper verInfoMapper;

    /*
     버전 그리드 리스트 조회
    */
    public GridResponse appVerGridList(GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();
        int gridRecords = verInfoMapper.selectVerAppGridCnt();
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<VerInfoEntity> entities = verInfoMapper.selectVerAppGridList("",gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> VerInfoSearchResponse.builder()
                        .viewCd("R")
                        .appNo((m.getAppNo()))
                        .appVer(m.getAppVer())
                        .desVer(m.getDesVer())
                        .appVerDt(m.getAppVerDt())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }


    /*
      앱버전정보 - 저장
     */
    @Transactional
    public void saveAppVer(String userId, SaveVerInfoRequest entity) throws Exception {
        try {
            VerInfoEntity vientity = new VerInfoEntity();
            vientity.setAppVer(entity.getAppVer());
            vientity.setDesVer(entity.getDesVer());
            verInfoMapper.insertVerInfo(userId, vientity) ;
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

    /*
      앱버전정보 - 삭제
     */
    @Transactional
    public void delAppVer(String userId, String appNo) throws Exception {
        try {
            verInfoMapper.updateDelVerInfo(userId, appNo) ;
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

    /*
    버전 최신버전값 전송
    */
    public VerInfoResponse selectLastVerInfo( ) throws Exception {
        String appNo = verInfoMapper.selectLastVerInfo();
        VerInfoEntity entity = verInfoMapper.selectVerAppInfo(appNo);
        return VerInfoResponse.toDTO(entity );
    }

    /* 버전 정보*/
    public VerInfoResponse selectVerAppInfo(String appNo) throws Exception {

        VerInfoEntity entity = verInfoMapper.selectVerAppInfo(appNo);
        return VerInfoResponse.toDTO(entity );
    }


}
