package com.icomsys.main_vm.biz.common.common.service;

import com.google.gson.Gson;
import com.icomsys.main_vm.biz.common.login.service.LoginService;
import com.icomsys.main_vm.common.util.ClientIp;
import com.icomsys.main_vm.db.jpa.entity.conversation.TbIcsLog;
import com.icomsys.main_vm.db.jpa.repo.system.TbIcsLogRepo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
@Slf4j
@RequiredArgsConstructor
public class LogService {

    private final TbIcsLogRepo tbIcsLogRepo;
    private final LoginService loginService;
    private final ClientIp clientIp;
    private final HttpServletRequest request;

    public void LogSave(LogVO lv) {
        log.info("vo chk - {}", new Gson().toJson(lv));
        TbIcsLog til = new TbIcsLog();
        til.setUserId(loginService.getUserVo().getUserId());
//        til.setUserId("sysadmin");
        til.setIp(clientIp.getClientIp());
        til.setActionCode(lv.getActionCode().toLowerCase());
        til.setMenuAuth(lv.getMenuAuth().getName());
        til.setRemark(lv.getRemark());
        til.setServiceGroup(lv.getServiceGroup());
        til.setAttribute1(lv.getAttribute1());
        til.setAttribute2(lv.getAttribute2());
        til.setAttribute3(lv.getAttribute3());
        til.setAttribute4(lv.getAttribute4());

        if (lv.getRegistDate() == null) {
            til.setRegistDate(LocalDateTime.now());
        } else {
            til.setRegistDate(lv.getRegistDate());
        }

        tbIcsLogRepo.save(til);


    }


}
