package com.ruoyi.dxp.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.dxp.domain.CallLogParam;
import com.ruoyi.dxp.domain.UserResource;
import com.ruoyi.dxp.dto.CalllogAdd;
import com.ruoyi.dxp.dto.UserCompanyDto;
import com.ruoyi.dxp.mapper.CallLogParamMapper;
import com.ruoyi.dxp.mapper.UserCompanyMapper;
import com.ruoyi.dxp.mapper.UserResourceMapper;
import com.ruoyi.dxp.service.IUserResourceService;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dxp.mapper.CallLogMapper;
import com.ruoyi.dxp.domain.CallLog;
import com.ruoyi.dxp.service.ICallLogService;

/**
 * 接口监控Service业务层处理
 * 
 * @author jy
 * @date 2022-11-06
 */
@Service
public class CallLogServiceImpl implements ICallLogService 
{
    @Autowired
    private CallLogMapper callLogMapper;

    @Autowired
    private CallLogParamMapper callLogParamMapper;

    @Autowired
    private UserResourceMapper userResourceMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private UserCompanyMapper  userCompanyMapper;

    /**
     * 查询接口监控
     * 
     * @param uuid 接口监控主键
     * @return 接口监控
     */
    @Override
    public CallLog selectCallLogByUuid(String uuid)
    {
        return callLogMapper.selectCallLogByUuid(uuid);
    }

    /**
     * 查询接口监控列表
     * 
     * @param callLog 接口监控
     * @return 接口监控
     */
    @Override
    public List<CallLog> selectCallLogList(CallLog callLog)
    {
        return callLogMapper.selectCallLogList(callLog);
    }

    /**
     * 新增接口监控
     * 
     * @param callLogAdd 接口监控
     * @return 结果
     */
    @Override
    public AjaxResult insertCallLog(CalllogAdd callLogAdd)
    {
        if (StringUtils.isEmpty(callLogAdd.getAccessId())) {
            return AjaxResult.error("用户接入id不能为空");
        }

        // 查询用户订阅资源
        UserResource req = new UserResource();
        req.setCallId(callLogAdd.getAccessId());
        List<UserResource> userResources = userResourceMapper.selectUserResourceList(req);
        if (userResources == null || userResources.size() == 0) {
            return AjaxResult.error("用户接入id错误");
        }

        // 查询用户
        String userName = "";
        UserCompanyDto userCompanyDto = userCompanyMapper.selectUserCompanyByUserId(userResources.get(0).getUserId());
        if (userCompanyDto != null) {
            userName = userCompanyDto.getCompanyName();
        } else {
            SysUser sysUser = sysUserMapper.selectUserById(userResources.get(0).getUserId());
            userName = sysUser.getUserName();
        }

        CallLog callLog = new CallLog();
        BeanUtils.copyProperties(callLogAdd, callLog);
        callLog.setUuid(UUID.fastUUID().toString());
        callLog.setResourceName(userResources.get(0).getResourceName());
        callLog.setProvideOrganization(userResources.get(0).getProvideOrganization());
        callLog.setRequestUrl(callLogAdd.getRequestUrl());
        callLog.setCreateTime(DateUtils.getNowDate());
        callLog.setResourceType(userResources.get(0).getResourceType());
        callLog.setResourceId(userResources.get(0).getResourceId());
        callLog.setCallName(userName);
        callLogMapper.insertCallLog(callLog);

        // 插入参数
        CallLogParam callLogParam = new CallLogParam();
        callLogParam.setUuid(callLog.getUuid());
        callLogParam.setRequestParam(callLogAdd.getRequestParam());
        callLogParam.setReturnParam(callLogAdd.getReturnParam());
        callLogParam.setCreateTime(DateUtils.getNowDate());
        callLogParamMapper.insertCallLogParam(callLogParam);

        return AjaxResult.success();
    }

    /**
     * 修改接口监控
     * 
     * @param callLog 接口监控
     * @return 结果
     */
    @Override
    public int updateCallLog(CallLog callLog)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        callLog.setUpdateBy(loginUser.getUsername());
        callLog.setUpdateTime(DateUtils.getNowDate());
        return callLogMapper.updateCallLog(callLog);
    }

    /**
     * 批量删除接口监控
     * 
     * @param uuids 需要删除的接口监控主键
     * @return 结果
     */
    @Override
    public int deleteCallLogByUuids(String[] uuids)
    {
        return callLogMapper.deleteCallLogByUuids(uuids);
    }

    /**
     * 删除接口监控信息
     * 
     * @param uuid 接口监控主键
     * @return 结果
     */
    @Override
    public int deleteCallLogByUuid(String uuid)
    {
        return callLogMapper.deleteCallLogByUuid(uuid);
    }
}
