package com.sooka.system.service.impl;

import com.business.controller.WebSocketServer;
import com.sooka.common.utils.CacheUtils;
import com.sooka.common.utils.ServletUtils;
import com.sooka.common.utils.uuid.UUID;
import com.sooka.system.domain.*;
import com.sooka.system.mapper.TUInterfaceCrashLogMapper;
import com.sooka.system.mapper.TUInterfaceinfoMapper;
import com.sooka.system.mapper.TULogMapper;
import com.util.HttpUtil;
import com.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

@Service
public class Guiji_Base_Service {

    @Resource
    TUInterfaceCrashLogMapper crashLogMapper;
    @Resource
    TULogMapper tuLogMapper;
    @Resource
    TUInterfaceinfoMapper interfaceinfoMapper;
    @Resource
    private UserInfoUtil userInfoUtil;

    public void add_log(BaseBusinessEntity baseBusinessEntity){
        HttpServletRequest request =ServletUtils.getRequest();
        String osAndBrowser[] = HttpUtil.getOsAndBrowserInfo(request).split("---");
        String os = osAndBrowser[0];
        String browser = osAndBrowser[1];
        String ipaddress = HttpUtil.getIPAddress(request);
        //添加操作日志
        SysUser sysUser = userInfoUtil.getLoginUserInfo();
        TULog tuLog = new TULog();
        tuLog.setId(UUID.fastUUID().toString());
        tuLog.setBrowser(browser);
        tuLog.setInterfaceinfoId(baseBusinessEntity.getInterfaceinfoId());
        tuLog.setInterfaceinfoName(baseBusinessEntity.getInterfaceinfoName());
        /** 写入调用者部门ID,名称 lm 20221101 add **/
        tuLog.setDeptId(baseBusinessEntity.getDeptId().toString());
        tuLog.setDeptName(baseBusinessEntity.getDeptName());
        tuLog.setIpaddress(ipaddress);
        tuLog.setLoginUser(sysUser.getUserId());
        tuLog.setLoginName(sysUser.getLoginName());
        /** 记录操作状态、请求参数、返回结果、异常记录 lm 20220810 add **/
        tuLog.setOperationStatus(baseBusinessEntity.getOperationStatus());
        tuLog.setParam(baseBusinessEntity.getParam());
        tuLog.setResults(baseBusinessEntity.getResults());
        tuLog.setExceptionLog(baseBusinessEntity.getExceptionLog());
        /** 记录操作状态、请求参数、返回结果、异常记录 lm 20220810 add **/
        tuLog.setOs(os);
        tuLog.setPlatformInterfacetype(baseBusinessEntity.getInterfacetype());
        /** 每次新增日志后要清空分页数据缓存，以达到更新缓存的效果 lm 20221011 add **/
        CacheUtils.remove(tuLog.getCacheName(),tuLog.getCacheKey());
        tuLogMapper.insertTULog(tuLog);

        //推送websocket
         Set set = WebSocketServer.webSocketMap.keySet();
         Iterator it = set.iterator();
         while(it.hasNext()){
             Object key = it.next();
             try {
                 WebSocketServer.sendInfo("abc",key.toString());
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }

    }

    /**
     * 远程接口调用日志信息新增日志
     * 接口调用日志不进行Websocket推送 不需要用户登录
     */
    public void addLog(BaseBusinessEntity baseBusinessEntity){
        HttpServletRequest request = ServletUtils.getRequest();
        String osAndBrowser[] = HttpUtil.getOsAndBrowserInfo(request).split("---");
        String os = osAndBrowser[0];
        String browser = osAndBrowser[1];
        String ipaddress = HttpUtil.getIPAddress(request);
        //添加操作日志
        //SysUser sysUser = userInfoUtil.getLoginUserInfo();
        TULog tuLog = new TULog();
        tuLog.setId(UUID.fastUUID().toString());
        tuLog.setOs(os);
        tuLog.setBrowser(browser);
        tuLog.setInterfaceinfoId(baseBusinessEntity.getInterfaceinfoId());
        tuLog.setInterfaceinfoName(baseBusinessEntity.getInterfaceinfoName());
        tuLog.setIpaddress(ipaddress);
        //tuLog.setLoginUser(sysUser.getUserId());
        //tuLog.setLoginName(sysUser.getLoginName());
        /** 记录操作状态、请求参数、返回结果、异常记录 lm 20220810 add **/
        tuLog.setOperationStatus(baseBusinessEntity.getOperationStatus());
        tuLog.setParam(baseBusinessEntity.getParam());
        tuLog.setResults(baseBusinessEntity.getResults());
        tuLog.setExceptionLog(baseBusinessEntity.getExceptionLog());
        /** 记录操作状态、请求参数、返回结果、异常记录 lm 20220810 add **/
//        tuLog.setOperationQuantity(new Long(ls.size()));
        //共享接口
//        tuLog.setPlatformInterfacetype("platform_interfacetype_1");
        //归集接口
//        tuLog.setPlatformInterfacetype("platform_interfacetype_2");
        String interfaceinfoName = tuLog.getInterfaceinfoName();
        if(interfaceinfoName != null && !interfaceinfoName.equals("")){
            String loginName = interfaceinfoName.split("-")[1];//共享-自来水公司-用水量检测 归集-市医药-种植采集数
            tuLog.setLoginName(loginName);
            tuLog.setPlatformInterfacetype(baseBusinessEntity.getInterfacetype());
        }
        TUInterfaceinfo tuInterfaceinfo = interfaceinfoMapper.selectTUInterfaceinfoById(baseBusinessEntity.getInterfaceinfoId());
        tuLog.setDeptId(tuInterfaceinfo.getDeptId());
        tuLog.setDeptName(tuInterfaceinfo.getDeptName());
        tuLogMapper.insertTULog(tuLog);
        TUInterfaceCrashLog crashLog = new TUInterfaceCrashLog();
        BeanUtils.copyProperties(tuLog, crashLog);
        crashLogMapper.insertTUInterfaceCrashLog(crashLog);
    }
}
