package com.ruoyi.system.service.impl;

import java.util.*;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysInterfaceForeignMapper;
import com.ruoyi.system.domain.SysInterfaceForeign;
import com.ruoyi.system.service.ISysInterfaceForeignService;

/**
 * 对外接口调用配置Service业务层处理
 *
 * @author ruoyi
 * @date 2022-10-31
 */
@Service
public class SysInterfaceForeignServiceImpl implements ISysInterfaceForeignService {
    @Autowired
    private SysInterfaceForeignMapper sysInterfaceForeignMapper;

    /**
     * 查询对外接口调用配置
     *
     * @param id 对外接口调用配置主键
     * @return 对外接口调用配置
     */
    @Override
    public SysInterfaceForeign selectSysInterfaceForeignById(Long id) {
        SysInterfaceForeign interfaceForeign = sysInterfaceForeignMapper.selectSysInterfaceForeignById(id);
        List<String> ipLis = Arrays.asList(interfaceForeign.getIp().split(","));
        List<Map<Object,String>> ipList = new ArrayList<>();
        for (String ipLi : ipLis) {
            Map<Object,String> map = new HashMap<>();
            map.put("ip",ipLi);
            ipList.add(map);
        }
        interfaceForeign.setIpList(ipList);

        List<String> equipIds = Arrays.asList(interfaceForeign.getEquipId().split(","));
        List<Map<Object,String>> equipIdList = new ArrayList<>();
        for (String equip : equipIds) {
            Map<Object,String> map = new HashMap<>();
            map.put("equipId",equip);
            equipIdList.add(map);
        }
        interfaceForeign.setEquipIdList(equipIdList);

        return interfaceForeign;
    }

    /**
     * 查询对外接口调用配置列表
     *
     * @param sysInterfaceForeign 对外接口调用配置
     * @return 对外接口调用配置
     */
    @Override
    public List<SysInterfaceForeign> selectSysInterfaceForeignList(SysInterfaceForeign sysInterfaceForeign) {
        List<SysInterfaceForeign> sysInterfaceForeignList = sysInterfaceForeignMapper.selectSysInterfaceForeignList(sysInterfaceForeign);
        for (SysInterfaceForeign interfaceForeign : sysInterfaceForeignList) {
            List<String> ipLis = Arrays.asList(interfaceForeign.getIp().split(","));
            List<Map<Object,String>> ipList = new ArrayList<>();
            for (String ipLi : ipLis) {
                Map<Object,String> map = new HashMap<>();
                map.put("ip",ipLi);
                ipList.add(map);
            }
            interfaceForeign.setIpList(ipList);

            List<String> equipIds = Arrays.asList(interfaceForeign.getEquipId().split(","));
            List<Map<Object,String>> equipIdList = new ArrayList<>();
            for (String equip : equipIds) {
                Map<Object,String> map = new HashMap<>();
                map.put("equipId",equip);
                equipIdList.add(map);
            }
            interfaceForeign.setEquipIdList(equipIdList);
        }
        return sysInterfaceForeignList;
    }

    /**
     * 新增对外接口调用配置
     *
     * @param sysInterfaceForeign 对外接口调用配置
     * @return 结果
     */
    @Override
    public int insertSysInterfaceForeign(SysInterfaceForeign sysInterfaceForeign) {
        if (sysInterfaceForeign.getEquipIdList() != null && sysInterfaceForeign.getEquipIdList().size() > 0) {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < sysInterfaceForeign.getEquipIdList().size(); i++) {
                String equipId = sysInterfaceForeign.getEquipIdList().get(i).toString();
                if (i == 0) {
                    sb.append(equipId.substring(equipId.indexOf("=") + 1, equipId.length() - 1));
                } else {
                    sb.append(",").append(equipId.substring(equipId.indexOf("=") + 1, equipId.length() - 1));
                }
            }

            sysInterfaceForeign.setEquipId(sb.toString());
        }

        if (sysInterfaceForeign.getIpList() != null && sysInterfaceForeign.getEquipIdList().size() > 0) {

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < sysInterfaceForeign.getIpList().size(); i++) {
                String ip = sysInterfaceForeign.getIpList().get(i).toString();
                if (i == 0) {
                    sb.append(ip.substring(ip.indexOf("=") + 1, ip.length() - 1));
                } else {
                    sb.append(",").append(ip.substring(ip.indexOf("=") + 1, ip.length() - 1));
                }
            }
            sysInterfaceForeign.setIp(sb.toString());
        }

        //生成秘钥（32位UUID）
        sysInterfaceForeign.setSecretKey(IdUtils.simpleUUID());
        return sysInterfaceForeignMapper.insertSysInterfaceForeign(sysInterfaceForeign);
    }

    /**
     * 修改对外接口调用配置
     *
     * @param sysInterfaceForeign 对外接口调用配置
     * @return 结果
     */
    @Override
    public int updateSysInterfaceForeign(SysInterfaceForeign sysInterfaceForeign) {
        if (sysInterfaceForeign.getEquipIdList() != null && sysInterfaceForeign.getEquipIdList().size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < sysInterfaceForeign.getEquipIdList().size(); i++) {
                String equipId = sysInterfaceForeign.getEquipIdList().get(i).toString();
                if (i == 0) {
                    sb.append(equipId.substring(equipId.indexOf("=") + 1, equipId.length() - 1));
                } else {
                    sb.append(",").append(equipId.substring(equipId.indexOf("=") + 1, equipId.length() - 1));
                }
            }

            sysInterfaceForeign.setEquipId(sb.toString());
        }

        if (sysInterfaceForeign.getIpList() != null && sysInterfaceForeign.getEquipIdList().size() > 0) {

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < sysInterfaceForeign.getIpList().size(); i++) {
                String ip = sysInterfaceForeign.getIpList().get(i).toString();
                if (i == 0) {
                    sb.append(ip.substring(ip.indexOf("=") + 1, ip.length() - 1));
                } else {
                    sb.append(",").append(ip.substring(ip.indexOf("=") + 1, ip.length() - 1));
                }
            }
            sysInterfaceForeign.setIp(sb.toString());
        }
        return sysInterfaceForeignMapper.updateSysInterfaceForeign(sysInterfaceForeign);
    }

    /**
     * 批量删除对外接口调用配置
     *
     * @param ids 需要删除的对外接口调用配置主键
     * @return 结果
     */
    @Override
    public int deleteSysInterfaceForeignByIds(Long[] ids) {
        return sysInterfaceForeignMapper.deleteSysInterfaceForeignByIds(ids);
    }

    /**
     * 删除对外接口调用配置信息
     *
     * @param id 对外接口调用配置主键
     * @return 结果
     */
    @Override
    public int deleteSysInterfaceForeignById(Long id) {
        return sysInterfaceForeignMapper.deleteSysInterfaceForeignById(id);
    }
}
