package com.njcn.serviceimpl.user;

import com.njcn.mapper.user.TfLgPloyAssMapper;
import com.njcn.mapper.user.TfLgPloyMapper;
import com.njcn.mapper.user.TransFormerMapper;
import com.njcn.pojo.configuration.RptPloy;
import com.njcn.pojo.user.*;
import com.njcn.service.user.TfLgPloyAssService;
import com.njcn.service.user.TfLgPloyService;
import com.njcn.serviceimpl.advanced.RelevanceServiceImpl;
import com.njcn.shiro.token.TokenManager;
import com.njcn.sso.pojo.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

//import com.njcn.serviceImpl.advanced.RelevanceServiceImpl;
@Service
public class TfLgPloyServiceImpl implements TfLgPloyService {

    @Autowired
    private TfLgPloyMapper tfpm;

    @Autowired
    TransFormerMapper trans;

    @Autowired
    private RelevanceServiceImpl relevanceService;

    @Autowired
    private TfLgPloyAssMapper tfpam;

//	@Autowired
//	private RelevanceServiceImpl relevanceServiceImpl;

    @Autowired
    private TfLgPloyAssService tflgAssService;

    @Override
    public List<List> selectAll() {
        // TODO Auto-generated method stub
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<List> deptsList = new ArrayList<List>();
        List<TfLgPloy> lgPloys = tfpm.selectAll();
        if (lgPloys.isEmpty()) {
            return deptsList;
        } else {
            for (TfLgPloy tf : lgPloys) {
                List<String> temp = new ArrayList<String>();
                temp.add(tf.getTpName());
                temp.add(sdf.format(tf.getUpdatetime()));
                temp.add(tf.getTpDescribe());
                temp.add(
                        "<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updatetpolyByIndex(\"" + tf.gettPIndex() + "\")' ><i class='fa fa-edit'></i>修改</button>"
                                + "<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deletetpolyByIndex(\"" + tf.gettPIndex() + "\")' ><i class='fa fa-trash'></i>删除</button>"
                );
                deptsList.add(temp);
            }

        }
        return deptsList;
    }

    @Override
    public String savePloy(String tPName, String describe, List<String> index) {
        // TODO Auto-generated method stub
        String result;

        TfLgPloy tmp = new TfLgPloy();
        tmp.setTpName(tPName);

        tmp = tfpm.selectOne(tmp);
        if (tmp != null) {
            return "该策略名已存在！";
        } else {
            TfLgPloy rpp = new TfLgPloy();
            String tpIndex = java.util.UUID.randomUUID().toString();
            rpp.settPIndex(tpIndex);
            rpp.setTpName(tPName);
            rpp.setTpDescribe(describe);

            User user = TokenManager.getToken();
            rpp.setState(Integer.valueOf("1"));
            rpp.setUserIndex(user.getUserIndex());
            rpp.setUpdatetime(new Date());

            try {
                int flag = tfpm.insert(rpp);

                for (int i = 0; i < index.size(); i++) {
                    TfLgPloyAss rpa = new TfLgPloyAss();
                    rpa.settPIndex(tpIndex);
                    rpa.setTfIndex(index.get(i));
                    rpa.setUpdatetime(new Date());
                    rpa.setUserIndex(user.getUserIndex());
                    int flag0 = tfpam.insert(rpa);
                }

                return "新增策略成功";
            } catch (Exception e) {
                return "新增策略失败";
            }

        }
    }

    public TfLgPloyInfo loadPloy(String rpIndex) {
        TfLgPloyInfo result = new TfLgPloyInfo();

        TfLgPloy rpp = new TfLgPloy();
        List<TfLgPloyAss> ploy = new ArrayList<>();
        List<String> rdIndex = new ArrayList<>();

        TfLgPloyAss tmp = new TfLgPloyAss();
        tmp.settPIndex(rpIndex);

        rpp = tfpm.selectByPrimaryKey(rpIndex);
        ploy = tfpam.select(tmp);

        for (int i = 0; i < ploy.size(); i++) {
            rdIndex.add(ploy.get(i).getTfIndex());
        }

        result.setTpName(rpp.getTpName());
        result.setTpDescribe(rpp.getTpDescribe());
        result.setRdIndex(rdIndex);

        return result;
    }

    @Override
    public String updatePloy(String rpIndex, String tPName, String describe, List<String> index) {
        String result;

        List<RptPloy> tmp = new ArrayList<>();

        tmp = tfpm.testName(rpIndex, tPName);
        if (tmp.size() > 0) {
            return "策略名称已存在";
        } else {
            TfLgPloy rpp = new TfLgPloy();
            rpp.settPIndex(rpIndex);
            rpp.setTpName(tPName);
            rpp.setTpDescribe(describe);

            User user = TokenManager.getToken();
            rpp.setState(Integer.valueOf("1"));
            rpp.setUserIndex(user.getUserIndex());
            rpp.setUpdatetime(new Date());

            try {
                int flag = tfpm.updateByPrimaryKey(rpp);

                TfLgPloyAss del = new TfLgPloyAss();
                del.settPIndex(rpIndex);
                int flag4 = tfpam.delete(del);

                for (int i = 0; i < index.size(); i++) {
                    TfLgPloyAss rpa = new TfLgPloyAss();
                    rpa.settPIndex(rpIndex);
                    rpa.setTfIndex(index.get(i));
                    rpa.setUserIndex(user.getUserIndex());
                    rpa.setUpdatetime(new Date());
                    int flag0 = tfpam.insert(rpa);
                }

                return "修改变压器策略成功";
            } catch (Exception e) {
                return "修改变压器策略失败";
            }

        }
    }

    @Override
    public int selfcheck() {
        int result = 0;
        List<TfLgPloyAss> temp = tflgAssService.getTfLgPloyAss();
        for (int i = 0; i <= temp.size(); i++) {
            String tfIndex = temp.get(i).getTfIndex();
            TransFormer list = trans.selectId(tfIndex);
            if (list.equals(0)) {
                result = 0;
                return result;
            } else {
                for (int j = 0; j <= temp.size(); j++) {
                    String tpIndex = temp.get(j).gettPIndex();
                    List<TfLgPloy> lgPloys = tfpm.selectId(tpIndex);
                    if (lgPloys.isEmpty()) {
                        result = 1;
                        return result;
                    } else {
                        result = 2;
                        relevanceService.setNodeInRedis();
                        return result;
                    }
                }
            }
        }
        return result;
    }

}
