package com.lantone.security.facade;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lantone.common.dto.AbnormalLogDTO;
import com.lantone.common.dto.GetOperationLogDTO;
import com.lantone.common.dto.LoginLogDTO;
import com.lantone.common.dto.OperationLogDTO;
import com.lantone.common.enums.IsDeleteEnum;
import com.lantone.common.util.ListUtil;
import com.lantone.common.util.StringUtil;
import com.lantone.common.vo.BlockLossTypeGatherVO;
import com.lantone.common.vo.DataCompareVO;
import com.lantone.security.entity.OperationLog;
import com.lantone.security.facade.base.AbnormalLogFacade;
import com.lantone.security.facade.base.LoginLogFacade;
import com.lantone.security.facade.base.OperationLogFacade;
import com.lantone.security.vo.GetAbnormalLogVO;
import com.lantone.security.vo.GetAbnormalWayAndIpVO;
import com.lantone.security.vo.GetLoginLogVO;
import com.lantone.security.vo.GetLoginNameAndIpVO;
import com.lantone.security.vo.GetOperationLogVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:日志管理
 * @Author: cy
 * @time: 2021/9/8 10:45
 */
@Component
public class LogManagementFacade {
    @Autowired
    private LoginLogFacade loginLogFacade;
    @Autowired
    private OperationLogFacade operationLogFacade;
    @Autowired
    private AbnormalLogFacade abnormalLogFacade;

    /**
     * @Description: 查看登录日志
     * @Param: [getLoginLogVO]
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.lantone.common.dto.LoginLogDTO>
     * @Author: cy
     * @Date: 2021/9/8
     */
    public IPage<LoginLogDTO> getLoginLog(GetLoginLogVO getLoginLogVO) {
        return loginLogFacade.getBaseMapper().getLoginLog(getLoginLogVO);
    }

    /**
     * 查看登录日志登录用户|登录ip地址
     *
     * @Description:
     * @Param: [getLoginNameAndIpVO]
     * @return: java.util.List<java.lang.String>
     * @Author: cy
     * @Date: 2021/9/8
     */
    public List<String> getLoginLogNameAndIp(GetLoginNameAndIpVO getLoginNameAndIpVO) {
        return loginLogFacade.getBaseMapper().getLoginLogNameAndIp(getLoginNameAndIpVO);
    }

    public boolean addLoginLog(LoginLogDTO loginLog) {
        loginLogFacade.getBaseMapper().addLoginLog(loginLog);
        return true;
    }

    /**
     * @Description: 查看操作日志
     * @Param: [getOperationLogVO]
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.lantone.common.dto.OperationLogDTO>
     * @Author: cy
     * @Date: 2021/9/8
     */
    public IPage<OperationLogDTO> getOperationLog(GetOperationLogVO getOperationLogVO) {
        return operationLogFacade.getBaseMapper().getOperationLog(getOperationLogVO);
    }

    /**
     * @Description: 查看操作日志操作用户
     * @Param: [operationName]
     * @return: java.util.List<java.lang.String>
     * @Author: cy
     * @Date: 2021/9/8
     */
    public List<String> getOperationLogName(String operationName) {
        return operationLogFacade.lambdaQuery()
                .like(StringUtils.isNotBlank(operationName), OperationLog::getOperationName, operationName)
                .select(OperationLog::getOperationName)
                .list().stream().map(OperationLog::getOperationName).collect(Collectors.toList());
    }

    public boolean addOperLog(OperationLogDTO operationLog) {
        operationLogFacade.getBaseMapper().addOperLog(operationLog);
        return true;
    }

    /**
     * @Description: 查看异常日志
     * @Param: [getAbnormalLogVO]
     * @return: com.baomidou.mybatisplus.core.metadata.IPage<com.lantone.common.dto.AbnormalLogDTO>
     * @Author: cy
     * @Date: 2021/9/8
     */
    public IPage<AbnormalLogDTO> getAbnormalLog(GetAbnormalLogVO getAbnormalLogVO) {
        return abnormalLogFacade.getBaseMapper().getAbnormalLog(getAbnormalLogVO);
    }

    /**
     * @Description: 查看异常日志请求方式|ip地址
     * @Param: [getAbnormalWayAndIpVO]
     * @return: java.util.List<java.lang.String>
     * @Author: cy
     * @Date: 2021/9/8
     */
    public List<String> getAbnormalLogWayAndIp(GetAbnormalWayAndIpVO getAbnormalWayAndIpVO) {
        return abnormalLogFacade.getBaseMapper().getAbnormalLogWayAndIp(getAbnormalWayAndIpVO);
    }

    public boolean addOperErrorLog(AbnormalLogDTO abnormalLogDTO) {
        abnormalLogFacade.getBaseMapper().addOperErrorLog(abnormalLogDTO);
        return true;
    }

    /**
     * 获取操作记录数据
     *
     * @param dataCompareVO
     * @Return java.util.List<com.lantone.common.dto.GetOperationLogDTO>
     */
    public List<GetOperationLogDTO> getOperationLogs(DataCompareVO dataCompareVO) {
        List<GetOperationLogDTO> out = new ArrayList<>();
        List<String> operationParams = operationLogFacade.list(new QueryWrapper<OperationLog>()
                .select("operation_param")
                .eq("is_deleted", IsDeleteEnum.N.getKey())
                .ge(dataCompareVO.getStartDate() != null && StringUtil.isBlank(dataCompareVO.getBehospitalCode()) && ListUtil.isEmpty(dataCompareVO.getBehospitalCodes()), "gmt_create", dataCompareVO.getStartDate())
                .le(dataCompareVO.getEndDate() != null && StringUtil.isBlank(dataCompareVO.getBehospitalCode()) && ListUtil.isEmpty(dataCompareVO.getBehospitalCodes()), "gmt_create", dataCompareVO.getEndDate())
                .eq(StringUtil.isNotBlank(dataCompareVO.getBehospitalCode()), "behospital_code", dataCompareVO.getBehospitalCode())
                .in(ListUtil.isNotEmpty(dataCompareVO.getBehospitalCodes()), "remark", dataCompareVO.getBehospitalCodes())
                .eq("operation_url", dataCompareVO.getOperationUrl()))
                .stream().map(OperationLog::getOperationParam).collect(Collectors.toList());
        if (ListUtil.isNotEmpty(operationParams)) {
            operationParams.stream().forEach(StrOperationParam -> {
                GetOperationLogDTO getOperationLogDTO = new GetOperationLogDTO();
                if (StringUtil.isNotEmpty(StrOperationParam) && "{".equals(StrOperationParam.substring(0, 1))) {
                    JSONObject temp = JSONObject.parseObject(StrOperationParam);
                    getOperationLogDTO.setBehospitalCode(temp.getString("behospitalCode"));
                    getOperationLogDTO.setHomePageId(temp.getString("homePageId"));
                    getOperationLogDTO.setRecId(temp.getString("recId"));
                    getOperationLogDTO.setRecTitle(temp.getString("recTitle"));
                    out.add(getOperationLogDTO);
                }
            });
        }
        return out;
    }

    /**
     * 获取该时间段的日志数量
     *
     * @param blockLossTypeGatherVO
     * @Return java.lang.Integer
     */
    public Integer getOperationLogNum(BlockLossTypeGatherVO blockLossTypeGatherVO) {
        return operationLogFacade.count(new QueryWrapper<OperationLog>()
                .ge(blockLossTypeGatherVO.getStartDate() != null, "gmt_create", blockLossTypeGatherVO.getStartDate())
                .le(blockLossTypeGatherVO.getStartDate() != null, "gmt_create", blockLossTypeGatherVO.getEndDate())
                .in("operation_url", blockLossTypeGatherVO.getOperationUrls()));
    }
}