package com.oqpo.api.service.oper;



import com.oqpo.api.entity.oper.PopEntity;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.oper.PopMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.oper.pop.SavePopRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.oper.pop.PopResponse;
import com.oqpo.api.web.dto.response.oper.pop.PopSearchResponse;
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 PopService extends CommonService {
    @Autowired
    private PopMapper popMapper;

    /*
      팝업 검색 리스트 조회
     */
    public GridResponse selectPopGridList(String fromDt,String toDt, String sPopNm, GridRequest gridRequest) throws Exception {
        int gridPage = gridRequest.getGridPage();
        int gridSize = gridRequest.getGridSize();

        fromDt = fromDt.replace(".","");
        toDt = toDt.replace(".","");
        int gridRecords = popMapper.selectPopGridCnt( fromDt, toDt,  sPopNm);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<PopEntity> entities = popMapper.selectPopGridList(fromDt, toDt,  sPopNm, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> PopSearchResponse.builder()
                        .viewCd("R")
                        .popNo(m.getPopNo())
                        .popNm(m.getPopNm())
                        .viewTargetNm(m.getViewTargetNm())
                        .popStatNm(m.getPopStatNm())
                        .popStDay(m.getPopStDay())
                        .addDt(m.getAddDt())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

    /*
    팝업 저장
     */
    @Transactional
    public void savePop(String userId, SavePopRequest savePopRequest) throws Exception {
        try {
            // 기본정보 저장
            PopEntity entity = new PopEntity();
            entity.setPopNm(savePopRequest.getPopNm());
            entity.setPopStDay(savePopRequest.getPopStDay().replace(".",""));
            entity.setPopEdDay(savePopRequest.getPopEdDay().replace(".",""));
            entity.setDelYn(savePopRequest.getDelYn());
            entity.setPopTypeCd(savePopRequest.getPopTypeCd());
            entity.setPopDesc(savePopRequest.getPopDesc());
            entity.setPopFileNo(savePopRequest.getPopFileNo());
            entity.setViewTargetCd(savePopRequest.getViewTargetCd());
            entity.setPopHeight(savePopRequest.getPopHeight());
            entity.setPopWidth(savePopRequest.getPopWidth());
            entity.setMenuNo(savePopRequest.getMenuNo());

            if ("C".equals(savePopRequest.getViewCd())) {
                // 신규 공지번호 생성
                entity.setPopNo(fnGetKeyNo(5));
                popMapper.insertPop(userId, entity);
            } else if ("U".equals(savePopRequest.getViewCd())) {
                entity.setPopNo(savePopRequest.getPopNo());
                popMapper.updatePop(userId, entity);
            }
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

    /*
   팝업 정보 가져오기
    */
    public PopResponse selectPopInfo(String popNo) throws Exception {
        return PopResponse.toDTO(popMapper.selectPopInfo(popNo));
    }

    /*
    팝업 오픈 여부 정보 전달 정보
     */
    public List<PopEntity> selectOpenPop(String sViewTargetCd) throws Exception {
        List<PopEntity> entities = popMapper.selectOpenPop(sViewTargetCd);
        return entities;
    }


}
