package com.pqs9900.serviceImpl.DataInfo;

import com.njcn.mapper.user.UserMapper;
import com.njcn.shiro.token.TokenManager;
import com.njcn.sso.pojo.user.User;
import com.njcn.utils.PubUtils;
import com.pqs9900.mapper.DataInfo.MiddRunMapper;
import com.pqs9900.mapper.DataInfo.MiddRunTreeMapper;
import com.pqs9900.pojo.DataInfo.MiddRun;
import com.pqs9900.pojo.DataInfo.MiddRunTree;
import com.pqs9900.service.DataInfo.MiddRunService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author denghuajun
 * @Description //TODO 中间件表impl
 * @Date 2019/3/15 13:43
 **/
@Service
public class MiddRunServiceImpl implements MiddRunService {
    @Autowired
    private MiddRunMapper middRunMapper;
    @Autowired
    private MiddRunTreeMapper middRunTreeMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<MiddRun> getMiddRun(String middIndex) {
        List<MiddRun> middRun = middRunMapper.getMiddRun(middIndex);
        if (!middRun.isEmpty()) {
            for (int i = 0; i < middRun.size(); i++) {
                middRun.get(i).setUserIndex(userMapper.selectIdName(middRun.get(i).getUserIndex()));
            }
        } else {
            middRun = null;
        }
        return middRun;
    }

    @Override
    public List<MiddRun> getMiddRuns() {
        List<MiddRun> middRuns = middRunMapper.selectAll();
        if (!middRuns.isEmpty()) {
            for (int i = 0; i < middRuns.size(); i++) {
                middRuns.get(i).setUserIndex(userMapper.selectIdName(middRuns.get(i).getUserIndex()));
            }
        } else {
            middRuns = null;
        }
        return middRuns;
    }

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

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

        if (one.isEmpty()) {
            MiddRunTree dt = new MiddRunTree();
            dt.setId(100);
            dt.setNodeType(0);
            dt.setName("中间件详情");
            dt.setRdIndex("0");
            dt.setpId(0);
            dt.setPpId(0);
            list.add(dt);
            return list;
        }

        MiddRunTree dt = new MiddRunTree();
        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++) {
            MiddRunTree dto = new MiddRunTree();
            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 String saveMiddRun(String middRunName, String ip, int port) {
        MiddRun middRun = new MiddRun();
        String https = null;
        try {
            //正则表达
            if (!StringUtils.isBlank(middRunName)) {
                if (PubUtils.patternIp(ip)) {
                    MiddRun middRuns = new MiddRun();
                    middRuns.setName(middRunName);
                    middRun.setIp(ip);
                    middRuns.setPort(port);
                    middRuns = middRunMapper.selectOne(middRuns);
                    if (middRuns == null) {
                        String middIndex = UUID.randomUUID().toString();
                        String userIndex = TokenManager.getUserId();
                        middRun.setMiddIndex(middIndex);
                        middRun.setName(middRunName);
                        middRun.setUserIndex(userIndex);
                        middRun.setIp(ip);
                        middRun.setPort(port);
                        middRun.setUpdateTime(new Date());
                        middRunMapper.insert(middRun);
                        https = "新增成功";
                    } else {
                        https = middRunName + "在ip为" + ip + "且端口号为" + port + "已存在";
                    }
                } else {
                    https = "ip地址格式不正确";
                }
            } else {
                https = "名称不能为空";
            }
        } catch (Exception e) {
            https = "新增失败";
        }
        return https;
    }

    @Override
    public String updateMiddRun(String middIndex, String middRunName, String ip, int port) {
        MiddRun middRun = new MiddRun();
        String https = null;
        try {
            //正则表达
            if (!StringUtils.isBlank(middRunName)) {
                if (PubUtils.patternIp(ip)) {
                    List<MiddRun> middRuns = new ArrayList<>();
                    middRuns = middRunMapper.getUpdateSelect(middIndex);
                    if (middRuns == null) {
                        String userIndex = TokenManager.getUserId();
                        middRun.setMiddIndex(middIndex);
                        middRun.setName(middRunName);
                        middRun.setUserIndex(userIndex);
                        middRun.setIp(ip);
                        middRun.setPort(port);
                        middRun.setUpdateTime(new Date());
                        middRunMapper.updateByPrimaryKey(middRun);
                        https = "修改成功";
                    } else {
                        MiddRun middRunss = new MiddRun();
                        middRunss.setName(middRunName);
                        middRunss.setIp(ip);
                        middRunss.setPort(port);
                        middRunss = middRunMapper.selectOne(middRunss);
                        if (middRunss == null) {
                            String userIndex = TokenManager.getUserId();
                            middRun.setMiddIndex(middIndex);
                            middRun.setName(middRunName);
                            middRun.setUserIndex(userIndex);
                            middRun.setIp(ip);
                            middRun.setPort(port);
                            middRun.setUpdateTime(new Date());
                            middRunMapper.updateByPrimaryKey(middRun);
                            https = "修改成功";
                        } else {
                            List<String> list = new ArrayList<>();
                            for (int i = 0; i < middRuns.size(); i++) {
                                list.add(middRuns.get(i).getMiddIndex());
                            }
                            if (list.contains(middRunss.getMiddIndex())) {
                                https = middRunName + "在ip为" + ip + "且端口号为" + port + "已存在";
                            } else {
                                String userIndex = TokenManager.getUserId();
                                middRun.setMiddIndex(middIndex);
                                middRun.setName(middRunName);
                                middRun.setUserIndex(userIndex);
                                middRun.setIp(ip);
                                middRun.setPort(port);
                                middRun.setUpdateTime(new Date());
                                middRunMapper.updateByPrimaryKey(middRun);
                                https = "修改成功";
                            }
                        }

                    }
                } else {
                    https = "ip地址格式不正确";
                }
            } else {
                https = "名称不能为空";
            }
        } catch (Exception e) {
            https = "修改失败";
        }
        return https;
    }

    @Override
    public String deleteMiddRun(String middIndex) {
        try {
            middRunMapper.deleteByPrimaryKey(middIndex);
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }

    @Override
    public MiddRun selectMiddRun(String middIndex) {
        MiddRun middRun = new MiddRun();
        try {
            middRun = middRunMapper.selectByPrimaryKey(middIndex);
        } catch (Exception e) {
            return middRun;
        }
        return middRun;
    }
}
