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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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.util.Utils;
import com.yuanxd.tools.pagehelper.PageHelper;
import com.yuanxd.tools.pagehelper.PageInfo;
import com.yuanxd.tools.utils.string.StringUtils;

@Controller
public class AqGisCtl {
	@Autowired
	private AqcxService aqs;

	/**
	 * 根据码头ID查询码头详细信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("aqgis/gq/{id}")
	@ResponseBody
	public Map<String, Object> gq(@PathVariable String id) {
		if (!StringUtils.hasText(id)) {
			return null;
		}
		return aqs.selectGqxxByGisId(id);
	}

	@RequestMapping("aqgis/login")
	@ResponseBody
	public AQUser login(String username, String password) {
		AQUser user = aqs.selectUserByName(username);
		if (null != user) {
			boolean eq = Utils.pwdEquals(password, user.getPassword());
			if (eq) {
				// 隐藏密码
				user.setPassword("");
				return user;
			}
		}
		return null;
	}

	/**
	 * 经营人基础信息查询
	 * 
	 * @param id
	 *            经营人ID
	 * @return
	 */
	@RequestMapping("aqgis/jyr/{id}")
	@ResponseBody
	public Map<String, Object> jyrjcxx(@PathVariable String id) {
		if (!StringUtils.hasText(id)) {
			return null;
		}
		// 经营人基础信息
		Map<String, Object> jyrjcxx = aqs.selectJyrjcxxByGisId(id);
		// 经营人码头信息
		List<String> mtmcs = aqs.selectMtByJyrid(Long.parseLong(id));
		if (mtmcs != null && mtmcs.size() > 0) {
			StringBuilder sb = new StringBuilder();
			for (String mt : mtmcs) {
				sb.append(mt);
				sb.append(",");
			}
			if (sb.length() > 0) {
				sb.deleteCharAt(sb.length() - 1);
			}
			jyrjcxx.put("mtmc", sb.toString());
		}
		return jyrjcxx;
	}

	/**
	 * 根据码头ID查询码头详细信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("aqgis/mt/{id}")
	@ResponseBody
	public Map<String, Object> mt(@PathVariable String id) {
		if (!StringUtils.hasText(id)) {
			return null;
		}
		return aqs.selectMtxxByGisId(id);
	}

	/**
	 * 根据泊位ID查询泊位详细信息
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("aqgis/bw/{id}")
	@ResponseBody
	public Map<String, Object> bw(@PathVariable String id) {
		if (!StringUtils.hasText(id)) {
			return null;
		}
		return aqs.selectBwxxByGisId(id);
	}

	@RequestMapping("aqgis/qsk/{id}")
	@ResponseBody
	public Qsk qsk(@PathVariable String id) {
		if (!StringUtils.hasText(id)) {
			return null;
		}
		return aqs.selectQskByGisId(id);
	}

	/**
	 * 港口、港区、经营人树图<br>
	 * TODO 增加登录人权限控制
	 * 
	 * @return
	 */
	@RequestMapping("aqgis/gkgqjryTree")
	@ResponseBody
	public List<TreeNode> gkgqjryTree(boolean wh) {
		List<TreeNode> nodes = new ArrayList<>();
		TreeNode root;
		// 省级根节点
		List<ThreeColumn> cities = aqs.selectHbszdByFID(null);
		if (null == cities) {
			return nodes;
		}else {
			ThreeColumn rootCol = cities.get(0);
			root = new TreeNode(rootCol);
			root.setId(rootCol.getColumn1());
			root.setType("1");
			nodes.add(root);
			// 市级
			List<ThreeColumn> citySzds = aqs.selectHbszdByFID(rootCol.getColumn1());
			List<TreeNode> cityNodes = new ArrayList<>();
			if (null != citySzds) {
				for (ThreeColumn szd : citySzds) {
					TreeNode citySzd = new TreeNode(szd);
					citySzd.setId(szd.getColumn1());
					citySzd.setType("1");
					cityNodes.add(citySzd);
					citySzd.setChildren(getGqMapByCity(szd.getColumn1(), wh));
				}
			}
			root.setChildren(cityNodes);
		}
		return nodes;
	}

	@Deprecated
	private List<TreeNode> getSzdNodes(boolean wh, List<ThreeColumn> szds) {
		List<TreeNode> nodes = new ArrayList<>();
		for (ThreeColumn szd : szds) {
			// 获取此所在地下的所有港区
			TreeNode szdNode = new TreeNode(szd);
			szdNode.setId(szd.getColumn1());
			szdNode.setType("1");
			List<ThreeColumn> subSzd = aqs.selectHbszdByFID(szd.getColumn1());
			if (null != subSzd) {
				List<TreeNode> subnodes = getSzdNodes(wh, subSzd);
				szdNode.setChildren(subnodes);
			}
			if (szdNode.getChildren() == null) {
				szdNode.setChildren(getGqMapByCity(szd.getColumn1(), wh));
			} else {
				szdNode.getChildren().addAll(getGqMapByCity(szd.getColumn1(), wh));
			}
			nodes.add(szdNode);

		}
		return nodes;
	}

	@RequestMapping("aqgis/cameraTree")
	@ResponseBody
	public List<TreeNode> cameraTree() {
		List<TreeNode> nodes = aqs.getCamerasByFid("1");
		return nodes;
	}

	/**
	 * 根据泊位ID查询危货作业申请
	 * 
	 * @param id
	 *            泊位ID
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "aqgis/whzysq")
	@ResponseBody
	public PageInfo<Whzysq> getWhzysqByBwid(String bwGisid, int page, int size) {
		if (StringUtils.isEmpty(bwGisid)) {
			return null;
		}
		// 初始化参数
		if (page < 1) {
			page = 1;
		}
		if (size < 1) {
			size = 1;
		}
		Map<String, Object> bwxx = aqs.selectBwxxByGisId(bwGisid);
		if (null == bwxx) {
			return null;
		}
		String id = String.valueOf(bwxx.get("ID"));
		if (StringUtils.isEmpty(id)) {
			return null;
		}
		PageHelper.startPage(page, size);
		List<Whzysq> whzysqs = aqs.selectWhzysqByBwid(id);
		PageInfo<Whzysq> pageResult = new PageInfo<>(whzysqs);
//		if(pageResult.getSize()==0) {
//			whzysqs = aqs.selectWhzysqByBwid(bwGisid);
//			pageResult = new PageInfo<>(whzysqs);
//		}
		return pageResult;
	}

	/**
	 * 根据所在地查询港区
	 * 
	 * @param obj
	 * @return
	 */
	private List<TreeNode> getGqMapByCity(String szdId, boolean wh) {
		List<ThreeColumn> gqs = aqs.selectGqBySzd(Long.parseLong(szdId));
		if (null == gqs) {
			return null;
		}
		List<TreeNode> nodes = new ArrayList<>();
		for (ThreeColumn gq : gqs) {
			TreeNode gqNode = new TreeNode(gq);
			gqNode.setType("2");
			List<ThreeColumn> jyrs = null;
			if (wh) {
				jyrs = aqs.selectWhJyrmcByGq(Long.parseLong(gq.getColumn1()));
			} else {
				jyrs = aqs.selectJyrmcByGq(Long.parseLong(gq.getColumn1()));
			}
			if (null != jyrs && jyrs.size() > 0) {
				List<TreeNode> jyrNodes = new ArrayList<>();
				for (ThreeColumn jyr : jyrs) {
					TreeNode j =new TreeNode(jyr);
					j.setType("3");
					jyrNodes.add(j);
				}
				gqNode.setChildren(jyrNodes);
			}
			nodes.add(gqNode);
		}
		return nodes;
	}
}
