package com.xt.hb.data.aq.svcimpl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xt.hb.data.aq.aqmap.AqcxMapper;
import com.xt.hb.data.aq.model.AQUser;
import com.xt.hb.data.aq.model.Qsk;
import com.xt.hb.data.aq.model.ThreeColumn;
import com.xt.hb.data.aq.model.TreeNode;
import com.xt.hb.data.aq.model.Whzysq;
import com.xt.hb.data.aq.svc.AqcxService;
import com.xt.hb.data.camera.mapper.CameraMapper;
import com.xt.hb.data.camera.model.CameraInfo;
import com.xt.hb.data.camera.svcimpl.CameraService;
import com.xt.hb.data.cbyj.mapper.CbyjAreaMapper;
import com.xt.hb.data.cbyj.model.Cbgg;
import com.xt.hb.data.cbyj.model.Cbyj;
import com.xt.hb.data.cbyj.model.CbyjArea;
import com.yuanxd.tools.utils.string.StringUtils;

@Service
public class AqcxServiceImpl implements AqcxService {
	@Autowired
	private AqcxMapper aqcxMapper;
	@Autowired
	private CbyjAreaMapper cbyjAreaMapper;
	@Autowired
	private CameraMapper cameraMapper;
	@Autowired
	private CameraService cameraService;

	@Override
	public void saveCbyjgg(Cbyj record) {
		Cbgg gg = new Cbgg();
		gg.setCm(record.getCm());
		gg.setYjsj(record.getYjsj());
		StringBuilder sb = new StringBuilder();
		CbyjArea area = cbyjAreaMapper.selectByPrimaryKey(Long.parseLong(record.getArea()));
		gg.setSzd(area.getSzd());
		sb.append("船舶").append(record.getCm());
		if (StringUtils.hasText(record.getMmsi())) {
			sb.append("[").append(record.getMmsi()).append("]");
		}
		sb.append("进入").append(area.getAreaname()).append("预警区域");
		gg.setTitle(sb.toString());
		aqcxMapper.insertCbgg(gg);
	}

	@Override
	public Map<String, Object> selectJyrjcxxByGisId(String id) {
		return aqcxMapper.selectJyrjcxxByGisId(id);
	}

	@Override
	public List<String> selectMtByJyrid(long jyrid) {
		return aqcxMapper.selectMtByJyrid(jyrid);
	}

	@Override
	public List<ThreeColumn> selectHbszd() {
		return aqcxMapper.selectHbszd();
	}

	/**
	 * 根据fid获取下级
	 * 
	 * @param fid
	 * @return
	 */
	@Override
	public List<ThreeColumn> selectHbszdByFID(String fid) {
		return aqcxMapper.selectHbszdByFID(fid);
	}

	@Override
	public List<ThreeColumn> selectGqBySzd(long szd) {
		return aqcxMapper.selectGqByCity(szd+"%");
	}

	@Override
	public List<ThreeColumn> selectJyrmcByGq(long gq) {
		return aqcxMapper.selectJyrmcByGq(gq);
	}

	/**
	 * 根据港区查找危货企业
	 * 
	 * @param gq
	 * @return
	 */
	@Override
	public List<ThreeColumn> selectWhJyrmcByGq(long gq) {
		return aqcxMapper.selectWhJyrmcByGq(gq);
	}

	@Override
	public Map<String, Object> selectMtxxByGisId(String id) {
		return aqcxMapper.selectMtxxByGisId(id);
	}

	@Override
	public Map<String, Object> selectGqxxByGisId(String id) {
		return aqcxMapper.selectGqxxByGisId(id);
	}

	@Override
	public Map<String, Object> selectBwxxByGisId(String id) {
		return aqcxMapper.selectBwxxByGisId(id);
	}

	@Override
	public int insertCbgg(Cbgg record) {
		return aqcxMapper.insertCbgg(record);
	}

	@Override
	public Qsk selectQskByGisId(String id) {
		return aqcxMapper.selectQskByGisId(id);
	}

	@Override
	public List<TreeNode> getCamerasByFid(String fid) {
		// 获取所有摄像头
		List<CameraInfo> cameraInfoList = cameraService.getAllCameraList();
		// 摄像头信息存放于MAP对象中
		Map<String, List<TreeNode>> mapObj = new HashMap<String, List<TreeNode>>();
		if(cameraInfoList.size() > 0) {
			for(CameraInfo info : cameraInfoList) {
				List<TreeNode> tempNodeList = null;
				if(mapObj.containsKey(info.getRegionId())) {
					tempNodeList = mapObj.get(info.getRegionId());
				} else {
					tempNodeList = new ArrayList<>();
					mapObj.put(info.getRegionId(), tempNodeList);
				}
				TreeNode node = new TreeNode();
				node.setId(info.getCameraId());
				node.setName(info.getName());
				node.setAttr("1");
				node.setOnline(info.getCurState());
				tempNodeList.add(node);
			}
		}
		// 获取摄像头目录结构
		List<TreeNode> nodes = getDirectoryList(fid, mapObj);
		return nodes;
	}
	
	/**
	 * 获取摄像头目录信息
	 * @param fid
	 * @return
	 */
	private List<TreeNode> getDirectoryList(String fid, Map<String, List<TreeNode>> mapObj) {
		List<ThreeColumn> cameras = cameraMapper.selectHbCameraByFid(fid);
		if (null == cameras) {
			return null;
		}
		List<TreeNode> nodes = new ArrayList<>();
		for (ThreeColumn cm : cameras) {
			TreeNode cmNode = new TreeNode(cm);
			List<TreeNode> childrenList = new ArrayList<TreeNode>();
			if(mapObj.containsKey(cmNode.getId())) {
				cmNode.setChildren(mapObj.get(cmNode.getId()));
				childrenList.addAll(mapObj.get(cmNode.getId()));
			}
			childrenList.addAll(getDirectoryList(cmNode.getId(), mapObj));
			cmNode.setChildren(childrenList);
			nodes.add(cmNode);
		}
		return nodes;
	}
	
	

	@Override
	public List<Whzysq> selectWhzysqByBwid(String id) {
		return aqcxMapper.selectWhzysqByBwid(id);
	}

	@Override
	public AQUser selectUserByName(String username) {
		return aqcxMapper.selectUserByName(username);
	}

}
