package com.yfd.platform.modules.algorithm.service.impl;

import cn.hutool.core.net.NetUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.modules.algorithm.domain.AlgorithmClass;
import com.yfd.platform.modules.algorithm.domain.AlgorithmClassComponent;
import com.yfd.platform.modules.algorithm.domain.AlgorithmDevice;
import com.yfd.platform.modules.algorithm.domain.AlgorithmParams;
import com.yfd.platform.modules.algorithm.mapper.AlgorithmClassMapper;
import com.yfd.platform.modules.algorithm.mapper.AlgorithmDeviceMapper;
import com.yfd.platform.modules.algorithm.mapper.AlgorithmParamsMapper;
import com.yfd.platform.modules.algorithm.service.IAlgorithmParamsService;
import com.yfd.platform.utils.HttpRESTfulUtils;
import com.yfd.platform.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhengsl
 * @since 2025-05-13
 */
@Service
@Transactional
public class AlgorithmParamsServiceImpl extends ServiceImpl<AlgorithmParamsMapper, AlgorithmParams> implements IAlgorithmParamsService {

    @Resource
    private AlgorithmParamsMapper algorithmParamsMapper;
    @Resource
    private AlgorithmClassMapper algorithmClassMapper;

    @Resource
    private AlgorithmDeviceMapper algorithmDeviceMapper;
    @Resource
    private HttpRESTfulUtils httpRESTfulUtils;

    /**********************************
     * 用途说明: 查询算法参数
     * 参数说明 algorithmId 算法分类Id
     * 参数说明 componentId 部件Id
     * 返回值说明: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     ***********************************/
    @Override
    public List<Map<String, Object>> getAlgorithmParamsList(String algorithmId, String componentId) {
        return algorithmParamsMapper.getAlgorithmParamsList(algorithmId, componentId);
    }

    /**********************************
     * 用途说明: 新增算法参数
     * 参数说明 algorithmParams 算法参数
     * 返回值说明: boolean 操作状态
     ***********************************/
    @Override
    public boolean addAlgorithmParams(AlgorithmParams algorithmParams) {
        return this.saveOrUpdate(algorithmParams);
    }

    @Override
    public boolean batchAddAlgorithmParams(List<AlgorithmParams> algorithmParamsList) {
        return this.saveBatch(algorithmParamsList);
    }

    /**********************************
     * 用途说明: 批量修改算法参数
     * 参数说明 algorithmParamsList 算法参数集合
     * 返回值说明: boolean 操作状态
     ***********************************/
    @Override
    public boolean batchUpdateAlgorithmParams(List<AlgorithmParams> algorithmParamsList) {
        return this.saveOrUpdateBatch(algorithmParamsList);
    }

    /**********************************
     * 用途说明: 删除算法参数
     * 参数说明 id 参数id
     * 返回值说明: boolean
     ***********************************/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteAlgorithmParams(String id) {
        LambdaQueryWrapper<AlgorithmDevice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AlgorithmDevice::getParamId, id);
        algorithmDeviceMapper.delete(queryWrapper);
        return this.removeById(id);
    }

    /**********************************
     * 用途说明:
     * 参数说明 algorithmId 算法分类Id
     * 参数说明 componentId 部件Id
     * 返回值说明: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     ***********************************/
    @Override
    public List<Map<String, Object>> getAlgorithmParamsNameList(String algorithmId, String componentId) {
        return algorithmParamsMapper.getAlgorithmParamsNameList(algorithmId, componentId);
    }

    /**********************************
     * 用途说明:
     * 参数说明 id 算法分类Id
     * 参数说明 componentId 部件Id
     * 返回值说明: com.alibaba.fastjson.JSONObject
     ***********************************/
    @Override
    public JSONObject callAlgorithmAnalyse(String id, String componentId) {
        AlgorithmClass algorithmClass = algorithmClassMapper.selectById(id);
        if (algorithmClass == null) {
            return null;
        }
        String serviceAddress = algorithmClass.getServiceAddress();
        if (!StringUtils.isHttpOrHttps(serviceAddress)) {
            throw new RuntimeException("当前算法请求分析的url不是标准请求");
        }
        // 固定参数就是固定参数，点位数据是默认最新点位，信号也是找最新数据
        List<Map<String, String>> mapList = algorithmParamsMapper.getAnalyseParams(id, componentId);
        if (mapList.size() <= 0) {
            return null;
        }
        Map<String, Object> senData = new HashMap<>();
        for (Map<String, String> map : mapList) {
            senData.put(map.get("paramName"), map.get("paramValue"));
        }
        Map<String, Object> param = new HashMap<>();
        param.put("param", senData);
        JSONObject jsonObject = httpRESTfulUtils.sendHttpUrlPost("json", serviceAddress, "", param, null);
        String desc = algorithmDeviceMapper.getReturnDesc(componentId);
        jsonObject.put("mapList", mapList);
        jsonObject.put("desc", desc);
        return jsonObject;
    }
}
