package com.njcn.serviceimpl.user;

import com.njcn.mapper.configuration.DicDataMapper;
import com.njcn.mapper.configuration.NodeInformationMapper;
import com.njcn.mapper.user.ManageListMapper;
import com.njcn.mapper.user.UserMapper;
import com.njcn.pojo.configuration.DicData;
import com.njcn.pojo.configuration.NodeInformation;
import com.njcn.pojo.user.ManageList;
import com.njcn.sso.pojo.user.User;
import com.njcn.service.user.ManageListService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.UserUtil;
import com.njcn.utils.redis.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ManageListServiceImpl implements ManageListService {
    @Autowired
    ManageListMapper alm;
    @Autowired
    UserMapper um;
    @Autowired
    NodeInformationMapper nodeMapper;
    @Autowired
    DicDataMapper dicDataMapper;
    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private UserUtil userUtil;

    @Override
    public List<List> getManage() {
        // TODO Auto-generated method stub
        List<List> lists = new ArrayList<List>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ManageList manageLists = new ManageList();
        manageLists.setState(1);
        manageLists.setSysType(TokenManager.getSysType());
        List<ManageList> list = alm.select(manageLists);
        if (!list.isEmpty()) {
            for (ManageList manageList : list) {
                List<String> temp = new ArrayList<String>();
                temp.add(manageList.getName());//资源名称
                //资源类目
                String type=manageList.getType();
                DicData dicData=new DicData();
                dicData.setDicIndex(type);
                dicData=dicDataMapper.selectOne(dicData);
                temp.add(dicData.getDicName());
                temp.add(manageList.getDescription());//资源描述
                temp.add(sdf.format(manageList.getUpdateTime()));//更新时间
                temp.add(
                        "<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateManageByIndex(\"" + manageList.getRes_Index() + "\")' ><i class='fa fa-edit'></i>修改</button>"
                                + "<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteManageByIndex(\"" + manageList.getRes_Index() + "\")' ><i class='fa fa-trash'></i>删除</button>"
                );
                lists.add(temp);
            }
        }
        return lists;
    }

    @Override
    public List<List> getSourceManage() {
        // TODO Auto-generated method stub
        List<List> lists = new ArrayList<List>();
        ManageList manage = new ManageList();
        manage.setState(1);
        manage.setSysType(userUtil.getSysType());
        List<ManageList> list = alm.select(manage);
        if (!list.isEmpty()) {
            for (ManageList manageList : list) {
                List<String> temp = new ArrayList<String>();
                temp.add(manageList.getName());//资源名称
                temp.add(manageList.getDescription());//资源描述
                temp.add("<button type='button' class='btn themeBGColor fr mr15' style='margin-right:35%;' href='javascript:void(0);' onclick='sourceManageByIndex(\"" + manageList.getRes_Index() + "\")' ><i class='glyphicon glyphicon-download-alt'></i>下载资源</button>"
                );
                lists.add(temp);
            }
        }
        return lists;
    }

    public List<NodeInformation> getNodeInfo() {
        List<NodeInformation> result = new ArrayList<>();

        result = nodeMapper.selectAll();
        return result;
    }

    public String delNode(Long nodeIndex) {
        NodeInformation node = nodeMapper.selectByPrimaryKey(nodeIndex);
        node.setNodeStatus(Long.valueOf("0"));
        try {
            nodeMapper.updateByPrimaryKey(node);
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }

    @Override
    public List<List> getBasemang(String sysType) {
        List<List> b=new ArrayList<List>();
        //基本资源
        List<ManageList> getBasemang=alm.getBasemang(sysType);
        for(ManageList manage:getBasemang){
            List<String> temp = new ArrayList<String>();
            temp.add(manage.getName());//资源名称
            temp.add(manage.getDescription());//资源描述
            temp.add("<button type='button' class='btn themeBGColor fr mr15' style='margin-right:35%;' href='javascript:void(0);' onclick='sourceManageByIndex(\"" + manage.getRes_Index() + "\")' ><i class='glyphicon glyphicon-download-alt'></i>下载资源</button>"
            );
            b.add(temp);
        }
        return b;
    }

    @Override
    public List<List> getReport(String sysType) {
        List<List> r=new ArrayList<List>();
        List<ManageList> getReport=alm.getReport(sysType);
        for(ManageList manage:getReport){
            List<String> temp = new ArrayList<String>();
            temp.add(manage.getName());//资源名称
            temp.add(manage.getDescription());//资源描述
            temp.add("<button type='button' class='btn themeBGColor fr mr15' style='margin-right:35%;' href='javascript:void(0);' onclick='sourceManageByIndex(\"" + manage.getRes_Index() + "\")' ><i class='glyphicon glyphicon-download-alt'></i>下载资源</button>"
            );
            r.add(temp);
        }
        return r;
    }

    @Override
    public List<List> getSub(String sysType) {
        List<List> s=new ArrayList<List>();
        List<ManageList> getSub=alm.getSub(sysType);
        for(ManageList manage:getSub){
            List<String> temp = new ArrayList<String>();
            temp.add(manage.getName());//资源名称
            temp.add(manage.getDescription());//资源描述
            temp.add("<button type='button' class='btn themeBGColor fr mr15' style='margin-right:35%;' href='javascript:void(0);' onclick='sourceManageByIndex(\"" + manage.getRes_Index() + "\")' ><i class='glyphicon glyphicon-download-alt'></i>下载资源</button>"
            );
            s.add(temp);
        }
        return s;
    }

    public String saveNode(String nodeName, String nodeDesc, String ip4, String ip3, String ip2, String ip1) {
        NodeInformation node = new NodeInformation();
        node.setNodeName(nodeName);

        List<NodeInformation> tmp = nodeMapper.select(node);

        if (tmp.size() > 0) {
            return "添加失败a";
        } else {
            Long index = nodeMapper.selectMaxIndex();

            node.setNodeIndex(index + 1);
            node.setNodeDesc(nodeDesc);
            node.setNodeIP(ip4 + "." + ip3 + "." + ip2 + "." + ip1);
            node.setNodeStatus(Long.valueOf(1));

            try {
                int flag = nodeMapper.insert(node);
                return "添加成功";
            } catch (Exception e) {
                return "添加失败";
            }
        }
    }

    public List<String> loadNode(Long nodeIndex) {
        List<String> result = new ArrayList<>();
        NodeInformation tmp = new NodeInformation();

        tmp = nodeMapper.selectByPrimaryKey(nodeIndex);

        result.add(tmp.getNodeName());
        result.add(tmp.getNodeDesc());

        String ip = tmp.getNodeIP();
        String[] ipn = ip.split("\\.");
        result.add(ipn[0]);
        result.add(ipn[1]);
        result.add(ipn[2]);
        result.add(ipn[3]);

        return result;
    }

    public String updateNode(Long nodeIndex, String nodeName, String nodeDesc, String ip4, String ip3, String ip2,
                             String ip1) {
        List<NodeInformation> tmp = new ArrayList<>();

        tmp = nodeMapper.testName(nodeName, nodeIndex);
        if (tmp.size() > 0) {
            return "更新失败a";
        } else {
            NodeInformation node = new NodeInformation();
            node.setNodeIndex(nodeIndex);
            node.setNodeName(nodeName);
            node.setNodeDesc(nodeDesc);
            node.setNodeIP(ip4 + "." + ip3 + "." + ip2 + "." + ip1);
            node.setNodeStatus(Long.valueOf(1));
            try {
                int flag = nodeMapper.updateByPrimaryKey(node);
                return "更新成功";
            } catch (Exception e) {
                return "更新失败";
            }
        }
    }

    @Override
    public ManageList selectTypeId(String resIndex) {
        ManageList manageList=alm.selectTypeId(resIndex,TokenManager.getSysType());
        //获取所有的资源类目
        List<DicData> type=redisCacheUtil.getManType();
        manageList.setGetType(type);
        return manageList;
    }

    @Override
    public List<ManageList> selectOneByIndex(String res_Index) {
        // TODO Auto-generated method stub
        return alm.selectOneByIndex(res_Index);
    }

    @Override
    public int updateManageList(String resIndex, String name, String type,String description, String sysType, String
            updateUser) {
        return alm.updateManageList(resIndex,name,type,description,sysType,updateUser);
    }

    @Override
    public int deleteIndex(String res_Index) {
        // TODO Auto-generated method stub
        return alm.deleteIndex(res_Index);
    }
}
