package com.bonus.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.log.enums.OperaType;
import com.bonus.system.api.domain.SysLogsVo;
import com.bonus.system.domain.SysLogsMenuHead;
import com.bonus.system.warning.SysWarning;
import com.bonus.system.warning.WaringLogEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import com.bonus.system.api.domain.SysOperLog;
import com.bonus.system.mapper.SysOperLogMapper;
import com.bonus.system.service.ISysOperLogService;

import javax.annotation.Resource;

/**
 * 操作日志 服务层处理
 * 
 * @author bonus
 */
@Service
@Slf4j
public class SysOperLogServiceImpl implements ISysOperLogService
{
    @Resource
    private SysOperLogMapper operLogMapper;
    public static final String MENU_TYPE_FILE = "F";

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    /**
     * 新增操作日志
     * 
     * @param operLog 操作日志对象
     * @return 结果
     */
    @Override
    public int insertOperlog(SysOperLog operLog)
    {
        return operLogMapper.insertOperlog(operLog);
    }

    /**
     * 查询系统操作日志集合
     * 
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
    @Override
    public List<SysOperLog> selectOperLogList(SysOperLog operLog)
    {
        return operLogMapper.selectOperLogList(operLog);
    }

    /**
     * 批量删除系统操作日志
     * 
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
    @Override
    public int deleteOperLogByIds(Long[] operIds)
    {
        return operLogMapper.deleteOperLogByIds(operIds);
    }

    /**
     * 查询操作日志详细
     * 
     * @param operId 操作ID
     * @return 操作日志对象
     */
    @Override
    public SysOperLog selectOperLogById(Long operId)
    {
        return operLogMapper.selectOperLogById(operId);
    }

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog()
    {
        operLogMapper.cleanOperLog();
    }

    /**
     * 新增
     * @param sysLogsVo
     * @return
     */
    @Override
    public int addLogs(SysLogsVo sysLogsVo) {
        if (sysLogsVo.getLogType() == 2) {
            sysLogsVo.setWarningStatus("0");
            eventPublisher.publishEvent(new WaringLogEvent(new SysWarning(sysLogsVo.getLogId(), sysLogsVo.getErrType(), sysLogsVo.getIp(), sysLogsVo.getOperaUserName(), sysLogsVo.getOperTime(), "0")));
        }
        if (sysLogsVo.getOperaUserName() != null) {
            String str = sysLogsVo.getOperaUserName().replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_");
            sysLogsVo.setOperaUserName(str);
        }
        if (sysLogsVo.getIp() != null) {
            String str = sysLogsVo.getIp().replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_");
            sysLogsVo.setIp(str);
        }
        return operLogMapper.addLogs(sysLogsVo);
    }

    /**
     * 查询
     * @param sysLogsVo
     * @return
     */
    @Override
    public Map<String, String> getLogsErrorModule(SysLogsVo sysLogsVo) {
           Map<String, String> maps= Maps.newHashMap();
            Map result=Maps.newHashMap();
            result.put("code","403");
            result.put("data","");
            result.put("msg","数据接口未授权");
           try{
               List<SysLogsMenuHead> list =operLogMapper.getLogsErrorModule(sysLogsVo);
               if(StringUtils.isNotEmpty(list)){
                   SysLogsMenuHead  vo=list.get(0);
                   StringBuffer sb=new StringBuffer();
                    String  type=vo.getMenuType();
                       if(StringUtils.isNotEmpty(vo.getMenuName4())){
                           sb.append(vo.getMenuName4());
                       }
                       if(StringUtils.isNotEmpty(vo.getMenuName3())){
                           sbAppend(sb);
                           sb.append(vo.getMenuName3());
                       }
                       if(StringUtils.isNotEmpty(vo.getMenuName2())){
                           sbAppend(sb);
                           sb.append(vo.getMenuName2());
                       }
                    if(	MENU_TYPE_FILE .equals(type)){
                        if(vo.getMenuName().contains(OperaType.INSERT)){
                            maps.put("bussType",OperaType.INSERT);
                        }else if(vo.getMenuName().contains(OperaType.UPDATE)){
                            maps.put("bussType",OperaType.UPDATE);
                        }else if(vo.getMenuName().contains(OperaType.DELETE)){
                            maps.put("bussType",OperaType.DELETE);
                        }else if(vo.getMenuName().contains(OperaType.IMPORT)){
                            maps.put("bussType",OperaType.IMPORT);
                        }else if(vo.getMenuName().contains(OperaType.DOWNLOAD)){
                            maps.put("bussType",OperaType.DOWNLOAD);
                        }else if(vo.getMenuName().contains(OperaType.EXPORT)){
                            maps.put("bussType",OperaType.EXPORT);
                        }else if(vo.getMenuName().contains(OperaType.FLASH)){
                            maps.put("bussType",OperaType.FLASH);
                        }else if(vo.getMenuName().contains(OperaType.GRANT)){
                            maps.put("bussType",OperaType.GRANT);
                        }  else {
                            maps.put("bussType",OperaType.OTHER);
                        }
                        maps.put("title",vo.getMenuName2());
                    }else{
                        maps.put("bussType",OperaType.QUERY);
                        sbAppend(sb);
                        sb.append(vo.getMenuName());
                        maps.put("title",vo.getMenuName());
                    }
                      maps.put("detail", "进行数据的"+maps.get("bussType"));
                      maps.put("module",sb.toString());
                   maps.put("resultData", JSON.toJSONString(result));
               }

           }catch (Exception e){
               maps.put("resultData", JSON.toJSONString(result));
               maps.put("title","智慧工地");
               maps.put("module","智慧工地->数据接口");
               maps.put("detail","进行数据访问");
               maps.put("bussType", "未知");
            log.error(e.toString(),e);
           }
        return maps;
    }

    public void sbAppend(StringBuffer sb){
        if(StringUtils.isNotEmpty(sb.toString())){
            sb.append("->");
        }
    }
}
