package com.icomsys.main_vm.biz.rcp.oprManage.service;

import com.icomsys.main_vm.biz.common.common.service.LogService;
import com.icomsys.main_vm.biz.common.common.service.LogVO;
import com.icomsys.main_vm.biz.common.system.vo.SystemBotTtsTagVo;
import com.icomsys.main_vm.common.code.enumresource.ActionResource;
import com.icomsys.main_vm.common.code.enumresource.MenuResource;
import com.icomsys.main_vm.db.jpa.entity.oprManage.TbBotTtsTag;
import com.icomsys.main_vm.db.jpa.repo.oprManage.TbBotTtsTagRepo;
import com.icomsys.main_vm.db.mybatis.alias.LoginVO;
import com.icomsys.main_vm.db.mybatis.mapper.BotTTSTagMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class OprManageTTSTagService {

    private final BotTTSTagMapper botTTSTagMapper;
    private final MappingJackson2JsonView ajaxMainView;
    private final TbBotTtsTagRepo tbBotTtsTagRepo;
    private final LogService logService;


    public List<HashMap> selectTtsTagListService(Map<String, String> dataMap) {
        HashMap eMap = new HashMap();
        eMap.putAll(dataMap);
        return botTTSTagMapper.selectTtsTagList(eMap);
    }

    public ModelAndView updateTtsTagListService(HttpServletRequest request, SystemBotTtsTagVo vo) {
        ModelAndView mv = new ModelAndView(ajaxMainView);

        try {
            LoginVO resultVO = (LoginVO) request.getSession().getAttribute("LoginVO");
            vo.setUpdateId(resultVO.getUserId());
            SystemBotTtsTagVo getVo = tbBotTtsTagRepo.existsById(vo);
            if (getVo == null) {
                vo.setRegistId(resultVO.getUserId());
                vo.setRegistDate(LocalDateTime.now());
            } else {
                vo.setRegistId(getVo.getRegistId());
                vo.setRegistDate(getVo.getRegistDate());
            }
            tbBotTtsTagRepo.save(TbBotTtsTag.builder()
                    .ttsProdCode(vo.getTtsProdCode())
                    .tagCode(vo.getTagCode())
                    .ttsStartTag(vo.getTtsStartTag())
                    .ttsEndTag(vo.getTtsEndTag())
                    .registId(vo.getRegistId())
                    .registDate(vo.getRegistDate())
                    .updateId(vo.getUpdateId())
                    .updateDate(LocalDateTime.now())
                    .build());

/*            logService.LogSave(LogVO.builder()
                    .menuAuth(MenuResource.rcpoprmanagettstagmanage)
                    .actionCode(String.valueOf((ActionResource.update)))
                    .serviceGroup("-")
                    .build());*/

            mv.addObject("result", true);
            mv.addObject("message", "정상적으로 저장되었습니다.");
        } catch (Exception e) {
            mv.addObject("result", false);
            mv.addObject("message", "실패하였습니다.");
        }

        return mv;
    }
}
