package com.oqpo.api.service.oper;



import com.oqpo.api.entity.oper.MailFormEntity;
import com.oqpo.api.exception.GlobalException;
import com.oqpo.api.mapper.oper.MailFormMapper;
import com.oqpo.api.service.CommonService;
import com.oqpo.api.web.dto.request.GridRequest;
import com.oqpo.api.web.dto.request.oper.mailform.SaveMailFormRequest;
import com.oqpo.api.web.dto.response.GridResponse;
import com.oqpo.api.web.dto.response.oper.mailform.MailFormResponse;
import com.oqpo.api.web.dto.response.oper.mailform.MailFormSearchResponse;
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 MailFormService extends CommonService {
    @Autowired
    private MailFormMapper mailFormMapper;

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

        int gridRecords = mailFormMapper.selectMailFormGridCnt( sMailFormNm,sMailStatusCd);
        int gridTotal = fnCalculateGridTotal(gridSize, gridRecords);
        List<MailFormEntity> entities = mailFormMapper.selectMailFormGridList( sMailFormNm,sMailStatusCd, gridRequest);
        List<Object> gridRows = entities.stream()
                .map(m -> MailFormSearchResponse.builder()
                        .viewCd("R")
                        .mailFormNo(m.getMailFormNo())
                        .mailFormNm(m.getMailFormNm())
                        .mailStatusCd(m.getMailStatusCd())
                        .mailStatusNm(m.getMailStatusNm())
                        .addDt(m.getAddDt())
                        .build())
                .collect(Collectors.toList());
        return GridResponse.toDTO(gridPage, gridTotal, gridRecords, gridRows);
    }

    /*
    메일양식 저장
     */
    @Transactional
    public void saveMailForm(String userId, SaveMailFormRequest saveMailFormRequest) throws Exception {
        try {
            // 기본정보 저장
            MailFormEntity entity = new MailFormEntity();
            entity.setMailFormNm(saveMailFormRequest.getMailFormNm());
            entity.setMailSubject(saveMailFormRequest.getMailSubject());
            entity.setMailStatusCd(saveMailFormRequest.getMailStatusCd());
            entity.setMailDesc(saveMailFormRequest.getMailDesc());
            entity.setFileNo(saveMailFormRequest.getFileNo());


            if ("C".equals(saveMailFormRequest.getViewCd())) {
                // 신규 공지번호 생성
                entity.setMailFormNo(fnGetKeyNo(7));
                mailFormMapper.insertMailFormInfo(userId, entity);
            } else if ("U".equals(saveMailFormRequest.getViewCd())) {
                entity.setMailFormNo(saveMailFormRequest.getMailFormNo());
                mailFormMapper.updateMailFormInfo(userId, entity);
            }
        } catch (GlobalException e) {
            e.getStackTrace();
            throw new GlobalException(e.getSystemMessageCode());
        } catch (Exception e) {
            e.getStackTrace();
            throw new RuntimeException();
        }
    }

    /*
   메일양식 정보 가져오기
    */
    public MailFormResponse selectMailFormInfo(String mailFormNo) throws Exception {
        return MailFormResponse.toDTO(mailFormMapper.selectMailFormInfo(mailFormNo));
    }



}
