package com.zcloud.modules.map.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.common.utils.Tools;
import com.zcloud.modules.map.service.MapEightService;
import com.zcloud.modules.sys.dao.SysUserDao;
import com.zcloud.modules.sys.entity.PageData;
import com.zcloud.modules.sys.service.SysUserService;
import com.zcloud.util.PerLocUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

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

/**
 * 说明：实时地图八项工作内容
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/map/Eight")
public class MapEightController {
	@Autowired
	private MapEightService mapEightService;
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private PerLocUtil perLocUtil;
	@Value("${perLoc.url}")
	private String url;
	@Autowired
	private SysUserDao sysUserDao;

	/**
	 * 根据用户id查询八项作业
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/findEightsByUserId")
	public Object findEightsByUserId(@RequestBody Map<String, Object> params) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		String empNo = "";
		if(StringUtils.isEmpty(params.get("empNo").toString())){
			map.put("msg","缺少必填参数empNo");
			map.put("result","file");
			return map;
		}
		empNo = params.get("empNo").toString();
		PageData pd = new PageData();
		pd.put("empNo", empNo);
		//获取八项作业信息
		List<PageData> varList = mapEightService.findEightsByUserIds(pd);
		//获取人员信息
		PageData byCardNo = sysUserDao.findByCardNo(pd);
		//若本地查询不到调用Api接口查询
		if (Tools.isEmpty(byCardNo)) {
			HttpHeaders headers = new HttpHeaders();
			headers.set("Authorization", perLocUtil.getToken());
			HttpEntity<String> entity = new HttpEntity<>(headers);
			ResponseEntity<String> response = restTemplate.exchange(url + "/deploy/card/peopleDisplay?cardId="
							+ empNo
					, HttpMethod.GET, entity, String.class);
			if (response.getStatusCode().is2xxSuccessful()) {
				String responseBody = response.getBody(); // 获取响应体字符串
				JSONObject responseObject = JSONObject.parseObject(responseBody);
				if (responseObject.get("code") != null && responseObject.get("code").toString().equals("200")) {
					List<PageData> dataList = JSONObject.parseArray(JSON.toJSON(responseObject.get("data")).toString(), PageData.class);
					if (dataList.size()>0) {
						for (PageData pageData : dataList) {
							PageData perPd = new PageData();
							perPd.put("DEPARTMENT_NAME",pageData.get("deptName"));
							perPd.put("POST_NAME",pageData.get("officeName"));
							byCardNo = perPd;
						}
					}
				}
			}
		}
		//调用工单信息接口
		HttpHeaders headers = new HttpHeaders();
		headers.set("Authorization", perLocUtil.getToken());
		HttpEntity<String> entity = new HttpEntity<>(headers);
		ResponseEntity<String> response = restTemplate.exchange(url + "/region/access/aListOfTicketsCurrentlyInProgress?workNumber="
						+ empNo
				, HttpMethod.GET, entity, String.class);
		if (response.getStatusCode().is2xxSuccessful()) {
			String responseBody = response.getBody(); // 获取响应体字符串
			JSONObject responseObject = JSONObject.parseObject(responseBody);
			if (responseObject.get("code") != null && responseObject.get("code").toString().equals("200")) {
				List<PageData> dataList = JSONObject.parseArray(JSON.toJSON(responseObject.get("data")).toString(), PageData.class);
				if (dataList.size()>0) {
					map.put("data",dataList);
				}else {
					map.put("data",new ArrayList<>());
				}
			}
		}
		map.put("INFO",byCardNo);
		map.put("varList", varList);
		map.put("result", errInfo);
		return map;
	}

	/**获取动火防护措施
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getAllTickets")
	public Object getAllTickets(@RequestBody Map<String, Object> params) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "file";
		String workNumbers = "";
		if(StringUtils.isEmpty(params.get("workNumbers").toString())){
			map.put("msg","缺少必填参数empNo");
			map.put("result", errInfo);
			return map;
		}
		workNumbers = params.get("workNumbers").toString();
		return mapEightService.getAllTickets(workNumbers);	//根据ID读取
	}

}
