package com.crunii.micro.service.sysmgr.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.crunii.micro.common.dto.Result;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.domain.entity.main.TsmWorkerMenuVisit;
import com.crunii.micro.domain.repo.main.TsmWorkerMenuVisitRepo;
import com.crunii.micro.domain.util.CurrWorkerUtil;
import com.crunii.micro.service.sysmgr.dto.sysmgr.FrequentVisitMenuRspDTO;
import com.crunii.micro.service.sysmgr.dto.sysmgr.MenuVisitReqDTO;
import com.crunii.micro.service.sysmgr.dto.sysmgr.VisitLogReq;
import com.crunii.micro.service.sysmgr.jdbc.FrequentVisitMenuJdbcDao;
import com.crunii.micro.service.sysmgr.service.FrequentVisitMenuService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author gongsongtao
 * @date 2023/9/18 16:07
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class FrequentVisitMenuServiceImpl implements FrequentVisitMenuService {

    private final FrequentVisitMenuJdbcDao frequentVisitMenuJdbcDao;
    private final TsmWorkerMenuVisitRepo tsmWorkerMenuVisitRepo;

    /**
     * 记录菜单访问记录
     * @param reqDTO 请求参数
     */
    @Override
    public Result  createTsmWorkerMenuVisit(MenuVisitReqDTO reqDTO,Worker worker) {
        TsmWorkerMenuVisit menuVisit=new TsmWorkerMenuVisit();
    //    if (reqDTO!=null && StringUtils.isNotEmpty(reqDTO.getMenuCode()) && StringUtils.isNotEmpty(reqDTO.getScope()) && worker!=null) {

        if (worker!=null) {
            menuVisit.setLoginId(worker.getLoginId());
        }

            menuVisit.setLoginId(worker.getLoginId());
            menuVisit.setMenuCode(reqDTO.getMenuCode());
            menuVisit.setCreateTime(new Date());
            menuVisit.setScope(reqDTO.getScope());
           menuVisit.setUrl(reqDTO.getUrl());
            menuVisit.setMenuModule(reqDTO.getMenuModule());
            tsmWorkerMenuVisitRepo.saveAndFlush(menuVisit);
    /*    }else {
            log.info("保存菜单访问记录失败,请求参数:{},当前登录用户:{}",reqDTO,worker);
        }*/
        return new Result<>();
    }

    @Override
    public Result saveVisitLog(VisitLogReq req, HttpServletRequest request) {
        TsmWorkerMenuVisit menuVisit=new TsmWorkerMenuVisit();

        menuVisit.setMenuCode(req.getMenuCode());
        menuVisit.setCreateTime(new Date());
        menuVisit.setScope(req.getScope());
        menuVisit.setUrl(req.getUrl());
        //菜单模块
        menuVisit.setMenuModule(req.getMenuModule());
        menuVisit.setVisitorCode(req.getVisitorCode());
        tsmWorkerMenuVisitRepo.saveAndFlush(menuVisit);
        return new Result<>();
    }


    /**
     * 查询常用菜单
     * @return FrequentVisitMenuRspDTO
     */
    @Override
    public Result<List<FrequentVisitMenuRspDTO>> findFrequentVisitMenu(MenuVisitReqDTO menuVisitReqDTO) {
        Worker worker= CurrWorkerUtil.getCurrWorker();
        if (worker==null){
            return new Result("获取登录用户失败");
        }
        String scope=StringUtils.isNotEmpty(menuVisitReqDTO.getScope())?menuVisitReqDTO.getScope():"web";//默认查询web
        return new Result(frequentVisitMenuJdbcDao.findFrequentVisitMenu(scope,worker));
    }
}
