package com.icomsys.main_vm.biz.rcp.monitoring.service.impl;

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.login.service.LoginService;
import com.icomsys.main_vm.biz.rcp.monitoring.service.MonitoringConversationMarkService;
import com.icomsys.main_vm.common.code.enumresource.ActionResource;
import com.icomsys.main_vm.common.code.enumresource.MenuResource;
import com.icomsys.main_vm.db.mybatis.alias.MonitoringConversationMarkVO;
import com.icomsys.main_vm.db.mybatis.mapper.ConversationMarkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class MonitoringConversationMarkServiceImpl implements MonitoringConversationMarkService {

    @Autowired
    private LoginService loginService;

    @Autowired
    private LogService logService;

    @Autowired
    private ConversationMarkMapper conversationMarkMapper;

    @Override
    public List<HashMap> selectBookmarkList(MonitoringConversationMarkVO vo) {
        return conversationMarkMapper.selectBookmarkList(vo);
    }

    @Override
    public int selectBookmarkListCnt(MonitoringConversationMarkVO vo) {
        return conversationMarkMapper.selectBookmarkListCnt(vo);
    }

    @Override
    public int insertBookmark(MonitoringConversationMarkVO vo) {
        int result = 0;

        HashMap resultMap;
        resultMap = conversationMarkMapper.checkBookmarkCnt(vo);

        String checkCnt = String.valueOf(resultMap.get("CNT"));

        if (!"0".equals(checkCnt)) {
            vo.setMeasureType("");
            vo.setSeq((Long) resultMap.get("SEQ"));
            vo.setUpdateId(loginService.getUserVo().getUserId());
            result = conversationMarkMapper.updateBookmark(vo);
        }
        else {
            vo.setRegistId(loginService.getUserVo().getUserId());
            vo.setUpdateId(loginService.getUserVo().getUserId());

            result = conversationMarkMapper.insertBookmark(vo);
        }

        if (result > 0) {
            logService.LogSave(LogVO.builder()
                    .menuAuth(MenuResource.valueOf(vo.getMenuVal()))
                    .actionCode(String.valueOf((ActionResource.insert)))
                    .serviceGroup(vo.getOprMngCode())
                    .remark("대화책갈피 등록")
                    .build());
        }

        return result;
    }

    @Override
    public int updateBookmark(MonitoringConversationMarkVO vo) {
        int result = 0;

        result = conversationMarkMapper.updateBookmark(vo);

        if (result > 0) {
            logService.LogSave(LogVO.builder()
                    .menuAuth(MenuResource.valueOf(vo.getMenuVal()))
                    .actionCode(String.valueOf((ActionResource.delete)))
                    .serviceGroup(vo.getOprMngCode())
                    .remark("[대화책갈피 삭제] - " + vo.getMeasureInfo())
                    .build());
        }

        return result;
    }

    @Override
    public HashMap checkBookmarkCnt(MonitoringConversationMarkVO vo) {
        vo.setChkMeasure("Y");

        return conversationMarkMapper.checkBookmarkCnt(vo);
    }
}
