package com.njcn.serviceimpl.user;

import com.njcn.mapper.configuration.DicDataMapper;
import com.njcn.mapper.configuration.SubStationMapper;
import com.njcn.mapper.user.TfLgPloyAssMapper;
import com.njcn.mapper.user.TransFormerMapper;
import com.njcn.mapper.user.UserMapper;
import com.njcn.pojo.configuration.DicData;
import com.njcn.pojo.configuration.SubStation;
import com.njcn.pojo.user.TfLgPloyAss;
import com.njcn.pojo.user.TransFormer;
import com.njcn.sso.pojo.user.User;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.service.user.TransFormerService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.redis.RedisCacheUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class TransFormerServiceImpl implements TransFormerService {
	private static final Logger logger = LoggerFactory.getLogger(TransFormerServiceImpl.class);
	
	@Autowired
	TransFormerMapper transf;
	
	@Resource
	DicDataMapper ddm;
	
	@Resource
	UserMapper um;
	
	@Resource
	SubStationMapper ssm;
	
	@Autowired
	UserLogDetailService userLogservice;
	
	@Autowired
	TfLgPloyAssMapper tflgAssMapper;
	
	@Autowired
	private RedisCacheUtil redisCacheUtil;
	
	//查询变压器的所有信息
	@Override
	public List<List> selectAll() {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<List> lists=new ArrayList<List>();
		List<TransFormer> trans=transf.selectAll();
		if(!trans.isEmpty()){
			for(TransFormer tr:trans){
				try{
					List<String> str=new ArrayList<String>();
					str.add(tr.getTfName());//名称
					Long subIndex=(long)tr.getSubIndex();
					SubStation station=new SubStation();
					station.setSubIndex(subIndex);
					str.add(ssm.selectOne(station).getName());
					//接线方式
					String wi=tr.getWiring();
					DicData data=ddm.selectId(wi);
					String wiring=data.getDicName();
					str.add(wiring);
					//更新人
					String user=tr.getUserIndex();
					String userIndex=um.selectIdName(user);
					str.add(userIndex);
					str.add(sdf.format(tr.getUpdatetime()));//更新时间
					str.add(tr.getTfDescribe());//描述
					str.add("<button type='button' class='btn  btn-primary  ' style='margin-right:10px;' href='javascript:void(0);' onclick='tfLgAssTransByIndex(\""+tr.getTf_Index()+"\")' ><i class='fa fa-code-fork'></i>节点维护</button>"
							+"<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateTransByIndex(\""+tr.getTf_Index()+"\")' ><i class='fa fa-edit'></i>修改</button>"
							+"<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteTransByIndex(\""+tr.getTf_Index()+"\")' ><i class='fa fa-trash'></i>删除</button>"
					);
					lists.add(str);
				}catch (Exception e){
					lists.add(null);
				}
			}
		}
		return lists;
	}
	@Override
	public int saveTrans(String tfName, int subIndex,String Wiring, String description) {
		// TODO Auto-generated method stub
		List<TransFormer> former=transf.selectOneName(tfName);
		if(former.isEmpty()){
			TransFormer transFormer=new TransFormer();
			String tf_Index=java.util.UUID.randomUUID().toString();
			//排序
			int rank=transf.selectRank();
			transFormer.setRank(rank+1);
			transFormer.setTf_Index(tf_Index);//guid
			transFormer.setTfName(tfName);//名称
			transFormer.setSubIndex(subIndex);//变电站guid
			transFormer.setWiring(Wiring);//接线方式
			User user = TokenManager.getToken();
			transFormer.setUserIndex(user.getUserIndex());//更新人
			transFormer.setTfDescribe(description);//描述
			transFormer.setUpdatetime(new Date());//更新时间
			transFormer.setState(1);
			try {
				int flag = transf.insert(transFormer);
				redisCacheUtil.clearConData();
				return 1;
			} catch (Exception e) {
				logger.error(e.getMessage());
				return 2;
			}
		}else{
			return 0;
		}
	}
	@Override
	public int selectRank() {
		// TODO Auto-generated method stub
		return transf.selectRank();
	}
	@Override
	public TransFormer selectId(String tfIndex) {
		// TODO Auto-generated method stub
		TransFormer transFormer=transf.selectId(tfIndex);
		//获取所有的区域
		List<DicData> wirings=redisCacheUtil.getWring();
		transFormer.setWritings(wirings);
		return transFormer;
	}
	@Override
	public int updateTrans(String tfIndex,String tfName,int subIndex,
    		String wiring,String description,Date updateTime,String userIndex) {
		// TODO Auto-generated method stub
		return transf.updateTrans(tfIndex, tfName,subIndex, wiring, description,updateTime,userIndex);
	}
	@Override
	public List<TransFormer> selectName(String tfIndex) {
		// TODO Auto-generated method stub
		return transf.selectName(tfIndex);
	}
	@Override
	public List<TransFormer> selectOneName(String tfName) {
		// TODO Auto-generated method stub
		return transf.selectOneName(tfName);
	}
	@Override
	public String deleteTransByIndex(String index) {
		   TransFormer former=new TransFormer();
		   former.setTf_Index(index);
		   TransFormer former2=transf.selectByPrimaryKey(index);
		   TfLgPloyAss lgPloyAss=new TfLgPloyAss();
		   lgPloyAss.setTfIndex(index);
			try{
				transf.deleteByPrimaryKey(index);
				tflgAssMapper.deleteTfLgByIndex(index);
				return "删除变压器成功！";
			}catch (Exception e) {
				return "删除变压器失败，请稍后再试";
			}
	}
	@Override
	public List<TransFormer> selectTwoName(String tfIndex) {
		// TODO Auto-generated method stub
		return transf.selectTwoName(tfIndex);
	}
	@Override
	public List<List> selectNameAll(String tfName,int node) {
		// TODO Auto-generated method stub
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				List<List> lists=new ArrayList<List>();
		        List<TransFormer> trans=new ArrayList<>();
		        //node==0：通过供电公司过滤；node==1：通过变电站过滤；node==2，通过变压器过滤
				if(node==0){
					trans=transf.selectgd(tfName);
				}else if(node==1){
					trans=transf.selectsub(tfName);
				}else {
					trans=transf.selectNameAll(tfName);
				}

				if(!trans.isEmpty()){
					for(TransFormer tr:trans){
						List<String> str=new ArrayList<String>();
						str.add(tr.getTfName());//名称
						Long subIndex=(long)tr.getSubIndex();
						SubStation station=new SubStation();
						station.setSubIndex(subIndex);
						str.add(ssm.selectOne(station).getName());
						//接线方式
						String wi=tr.getWiring();
						DicData data=ddm.selectId(wi);
						String wiring=data.getDicName();
						str.add(wiring);
						//更新人
						String user=tr.getUserIndex();
						String userIndex=um.selectIdName(user);
						str.add(userIndex);
						str.add(sdf.format(tr.getUpdatetime()));//更新时间
						str.add(tr.getTfDescribe());//描述
						str.add("<button type='button' class='btn  btn-primary ' style='margin-right:10px;' href='javascript:void(0);' onclick='tfLgAssTransByIndex(\""+tr.getTf_Index()+"\")' ><i class='fa fa-code-fork'></i>节点维护</button>"
								+"<button type='button' class='btn  btn-primary' style='margin-right:10px;' href='javascript:void(0);' onclick='updateTransByIndex(\""+tr.getTf_Index()+"\")' ><i class='fa fa-edit'></i>修改</button>"
							   +"<button type='button' class='btn  btn-danger' style='margin-right:10px;' href='javascript:void(0);' onclick='deleteTransByIndex(\""+tr.getTf_Index()+"\")' ><i class='fa fa-trash'></i>删除</button>"
							   );
						lists.add(str);
					}
				}
				return lists;
	}
}
