package com.pqs9900.serviceImpl.DataInfo;

import com.njcn.mapper.user.UserMapper;
import com.njcn.service.configuration.DicDataService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
import com.njcn.utils.ThreeDes;
import com.njcn.utils.UserUtil;
import com.pqs9900.mapper.DataInfo.ServerMapper;
import com.pqs9900.mapper.DataInfo.ServerTreeMapper;
import com.pqs9900.pojo.DataInfo.MiddRun;
import com.pqs9900.pojo.DataInfo.Server;
import com.pqs9900.pojo.DataInfo.ServerTree;
import com.pqs9900.service.DataInfo.ServerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author denghuajun
 * @Description //TODO 服务器表
 * @Date 2019/3/15 13:47
 **/
@Service
public class ServerServiceImpl implements ServerService {
    @Autowired
    private ServerMapper serverMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserUtil userUtil;
    @Autowired
    DicDataService dds;
    @Autowired
    private ServerTreeMapper serverTreeMapper;

    @Override
    public List<Server> getServer(String sevIndex) {
        List<Server> servers = new ArrayList<>();
        try {
            servers = serverMapper.getServer(sevIndex);
            if (!servers.isEmpty()) {
                for (int i = 0; i < servers.size(); i++) {
                    servers.get(i).setType(userUtil.getPreSysType(servers.get(i).getType()).getDicName());
                    servers.get(i).setVersion(userUtil.getPreSysType(servers.get(i).getVersion()).getDicName());
                    servers.get(i).setUserIndex(userMapper.selectIdName(servers.get(i).getUserIndex()));
                }
            }
        } catch (Exception e) {
            servers = null;
        }
        return servers;
    }

    @Override
    public List<ServerTree> getTreeType() {
        List<ServerTree> list = new ArrayList<>();

        Map<Integer, Integer> value = new HashMap<Integer, Integer>();
        int p = 1000;
        List<ServerTree> one = serverTreeMapper.getfirstData();

        if (one.isEmpty()) {
            ServerTree dt = new ServerTree();
            dt.setId(100);
            dt.setNodeType(0);
            dt.setName("服务器信息");
            dt.setRdIndex("0");
            dt.setpId(0);
            dt.setPpId(0);
            list.add(dt);
            return list;
        }

        ServerTree dt = new ServerTree();
        dt.setId(100);
        dt.setNodeType(0);
        dt.setName("服务器信息");
        dt.setRdIndex("0");
        dt.setpId(100);
        dt.setPpId(100);
        list.add(dt);

        List<Integer> first = new ArrayList<>();

        for (int i = 0; i < one.size(); i++) {
            ServerTree dto = new ServerTree();
            dto.setId(++p);
            dto.setName(one.get(i).getName());
            dto.setNodeType(1);
            dto.setpId(100);
            dto.setPpId(100);
            dto.setRdIndex(one.get(i).getRdIndex());
            list.add(dto);
        }
        return list;
    }

    @Override
    public List<Server> getServerDetails() {
        List<Server> servers = new ArrayList<>();
        try {
            servers = serverMapper.selectAll();
            if (!servers.isEmpty()) {
                for (int i = 0; i < servers.size(); i++) {
                    servers.get(i).setType(userUtil.getPreSysType(servers.get(i).getType()).getDicName());
                    servers.get(i).setVersion(userUtil.getPreSysType(servers.get(i).getVersion()).getDicName());
                    servers.get(i).setUserIndex(userMapper.selectIdName(servers.get(i).getUserIndex()));
                }
            } else {
                servers = null;
            }
        } catch (Exception e) {
            servers = null;
        }
        return servers;
    }

    @Override
    public String saveServer(String serverName, String type, String version, String hardware, String userName, String password, String ip) {
        String https = null;
        try {
            Server server = new Server();
            server.setName(serverName);
            server = serverMapper.selectOne(server);
            if (server != null) {
                https = "新增失败，该服务器已存在";
            } else {
                Server servers = new Server();
                String userIndex = TokenManager.getUserId();
                servers.setSevIndex(UUID.randomUUID().toString());
                servers.setName(serverName);
                servers.setType(type);
                servers.setVersion(version);
                servers.setHardware(hardware);
                servers.setUserName(userName);
                servers.setPassword(ThreeDes.encryptThreeDes(password));
                servers.setIp(ip);
                servers.setUserIndex(userIndex);
                servers.setUpdateTime(new Date());
                serverMapper.insert(servers);
                https = "新增成功";
            }
        } catch (Exception e) {
            https = "新增失败";
        }
        return https;
    }

    @Override
    public String updateServer(String sevIndex,String serverName, String type, String version, String hardware, String userName, String password, String ip) {
        String https = null;
        try {
            //正则表达
            if (!StringUtils.isBlank(serverName)) {
                if (!StringUtils.isBlank(type)) {
                    if (!StringUtils.isBlank(version)) {
                        if (PubUtils.patternIp(ip)) {
                            List<Server> servers = new ArrayList<>();
                            servers = serverMapper.getUpdateServer(sevIndex);
                            if (servers == null) {
                                Server server = new Server();
                                String userIndex = TokenManager.getUserId();
                                server.setSevIndex(sevIndex);
                                server.setName(serverName);
                                server.setType(type);
                                server.setVersion(version);
                                server.setHardware(hardware);
                                server.setUserName(userName);
                                server.setPassword(ThreeDes.encryptThreeDes(password));
                                server.setUserIndex(userIndex);
                                server.setIp(ip);
                                server.setUpdateTime(new Date());
                                serverMapper.updateByPrimaryKey(server);
                                https = "修改成功";
                            } else {
                                Server server = new Server();
                                server.setName(serverName);
                                server = serverMapper.selectOne(server);
                                if (server == null) {
                                    Server server1 = new Server();
                                    String userIndex = TokenManager.getUserId();
                                    server1.setSevIndex(sevIndex);
                                    server1.setName(serverName);
                                    server1.setType(type);
                                    server1.setVersion(version);
                                    server1.setHardware(hardware);
                                    server1.setUserName(userName);
                                    server1.setPassword(ThreeDes.encryptThreeDes(password));
                                    server1.setUserIndex(userIndex);
                                    server1.setIp(ip);
                                    server1.setUpdateTime(new Date());
                                    serverMapper.updateByPrimaryKey(server1);
                                    https = "修改成功";
                                } else {
                                    List<String> list = new ArrayList<>();
                                    for (int i = 0; i < servers.size(); i++) {
                                        list.add(servers.get(i).getSevIndex());
                                    }
                                    if (list.contains(server.getSevIndex())) {
                                        https = serverName + "已存在";
                                    } else {
                                        String userIndex = TokenManager.getUserId();
                                        server.setSevIndex(sevIndex);
                                        server.setName(serverName);
                                        server.setType(type);
                                        server.setVersion(version);
                                        server.setHardware(hardware);
                                        server.setUserName(userName);
                                        server.setPassword(ThreeDes.encryptThreeDes(password));
                                        server.setUserIndex(userIndex);
                                        server.setIp(ip);
                                        server.setUpdateTime(new Date());
                                        serverMapper.updateByPrimaryKey(server);
                                        https = "修改成功";
                                    }
                                }
                            }
                        } else {
                            https = "ip地址格式不正确";
                        }
                    } else {
                        https = "系统版本不可为空";
                    }
                }else {
                    https = "前置类型不可为空";
                }
            } else {
                https = "名称不能为空";
            }
        } catch (Exception e) {
            https = "修改失败";
        }
        return https;
    }

    @Override
    public String deleteServer(String sevIndex) {
        try {
            serverMapper.deleteByPrimaryKey(sevIndex);
            return "删除成功";
        }catch (Exception e){
            return "删除失败";
        }
    }

    @Override
    public Server selectServer(String sevIndex) {
        Server server = new Server();
        try {
            server = serverMapper.selectByPrimaryKey(sevIndex);
            if(server.getPassword()!=null) {
                server.setPassword(ThreeDes.decryptThreeDes(server.getPassword()));
            }else {
                server.setPassword(server.getPassword());
            }
            server.setGetType(dds.getType());
            server.setGetSystemType(dds.getSystemType());
        }catch (Exception e){
            return server;
        }
        return server;
    }
}
