package com.ruoyi.rocketmq.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.rocketmq.domain.RmResourceRemote;
import com.ruoyi.rocketmq.mapper.RmResourceRemoteMapper;
import com.ruoyi.rocketmq.service.IRmResourceRemoteService;
import com.ruoyi.system.api.RemoteRevenueConfigService;
import com.ruoyi.system.api.domain.RmResourceRegistrationRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 资源远程管理Service业务层处理
 * 
 * @author gyt
 * @date 2025-09-15
 */
@Service
public class RmResourceRemoteServiceImpl implements IRmResourceRemoteService 
{
    @Autowired
    private RmResourceRemoteMapper rmResourceRemoteMapper;
    @Autowired
    private RemoteRevenueConfigService remoteRevenueConfigService;

    /**
     * 查询资源远程管理
     * 
     * @param id 资源远程管理主键
     * @return 资源远程管理
     */
    @Override
    public RmResourceRemote selectRmResourceRemoteById(Long id)
    {
        return rmResourceRemoteMapper.selectRmResourceRemoteById(id);
    }

    /**
     * 查询资源远程管理列表
     * 
     * @param rmResourceRemote 资源远程管理
     * @return 资源远程管理
     */
    @Override
    public List<RmResourceRemote> selectRmResourceRemoteList(RmResourceRemote rmResourceRemote)
    {
        return rmResourceRemoteMapper.selectRmResourceRemoteList(rmResourceRemote);
    }

    /**
     * 新增资源远程管理
     * 
     * @param rmResourceRemote 资源远程管理
     * @return 结果
     */
    @Override
    public int insertRmResourceRemote(RmResourceRemote rmResourceRemote)
    {
        return rmResourceRemoteMapper.insertRmResourceRemote(rmResourceRemote);
    }

    /**
     * 修改资源远程管理
     * 
     * @param rmResourceRemote 资源远程管理
     * @return 结果
     */
    @Override
    public int updateRmResourceRemote(RmResourceRemote rmResourceRemote)
    {
        rmResourceRemote.setUpdateTime(DateUtils.getNowDate());
        return rmResourceRemoteMapper.updateRmResourceRemote(rmResourceRemote);
    }

    /**
     * 批量删除资源远程管理
     * 
     * @param ids 需要删除的资源远程管理主键
     * @return 结果
     */
    @Override
    public int deleteRmResourceRemoteByIds(Long[] ids)
    {
        return rmResourceRemoteMapper.deleteRmResourceRemoteByIds(ids);
    }

    /**
     * 删除资源远程管理信息
     * 
     * @param id 资源远程管理主键
     * @return 结果
     */
    @Override
    public int deleteRmResourceRemoteById(Long id)
    {
        return rmResourceRemoteMapper.deleteRmResourceRemoteById(id);
    }

    @Override
    public Map<String, Object> getScriptResultBySn(RmResourceRemote rmResourceRemote) {
        // 构建返回结果
        Map<String, Object> resultMap = new HashMap<>();
        // 资源详情
        if(rmResourceRemote.getHardwareSn() != null){
            RmResourceRegistrationRemote queryParam = new RmResourceRegistrationRemote();
            queryParam.setHardwareSn(rmResourceRemote.getHardwareSn());
            R<RmResourceRegistrationRemote> registrationRemote = remoteRevenueConfigService.getListByHardwareSn(queryParam, SecurityConstants.INNER);
            if(registrationRemote != null && registrationRemote.getData() != null){
                RmResourceRegistrationRemote resourceMsg = registrationRemote.getData();
                resultMap.put("resourceMsg", resourceMsg);
            }
        }
        // 脚本执行结果构建
        List<Map<String, Object>> scriptList = new ArrayList<>();
        List<RmResourceRemote> reslutList = rmResourceRemoteMapper.selectRmResourceRemoteList(rmResourceRemote);
        for (RmResourceRemote resourceRemote : reslutList) {
            // 脚本执行返回结果
            JSONObject jsonObject = JSONObject.parseObject(resourceRemote.getDescription());
            String command = jsonObject.getString("command");
            String resOut = jsonObject.getString("resOut");
            String sucOrFail = (!"".equals(resOut) && !"脚本执行失败".equals(resOut)) ? "\n执行成功，\n":"\n执行失败，\n";
            StringBuilder resultSb = new StringBuilder();
            resultSb.append("【").append(resourceRemote.getResourceName()).append("】脚本执行命令：\n")
                    .append(command).append(sucOrFail).append("执行结果如下：\n").append(resOut);
            Map<String,Object> map = new HashMap<>();
            Date createTime = resourceRemote.getCreateTime();
            String createTimeStr = DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss", createTime);
            map.put("createTime", createTimeStr);
            map.put("content", resultSb);
            scriptList.add(map);
        }
        resultMap.put("scriptResult", scriptList);
        return resultMap;
    }
}
