package com.zcloud.modules.map.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.modules.map.mapper.MapEightMapper;
import com.zcloud.modules.map.service.MapEightService;
import com.zcloud.modules.sys.entity.PageData;
import com.zcloud.util.HttpRequestUtil;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 说明：实时地图八项工作
 * 作者：luoxiaobao
 * 时间：2022-09-21
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class MapEightServiceImpl implements MapEightService {

	@Resource
	private MapEightMapper mapEightMapper;

	@Autowired
	private PerLocUtil perLocUtil;

	@Value("${perLoc.url}")
	private String url;

	@Override
	public List<PageData> findEightsByUserIds(PageData pd) throws Exception {
		return mapEightMapper.findEightsByUserIds(pd);
	}

	@Override
	public Object getAllTickets(String workNumbers) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		Set<String> redList = new HashSet<String>();
		Set<String> orangeList = new HashSet<String>();
		Set<String> yellowList = new HashSet<String>();
		String[] split = workNumbers.split(",");
		if (split.length > 0) {
			for (String workNumber : split) {
				String _response = HttpRequestUtil.doGetUser( url + "/region/access/aListOfTicketsCurrentlyInProgress?workNumber=" + workNumber, perLocUtil.getToken());
				if (StringUtils.isNotEmpty(_response)) {
					JSONObject response = JSONObject.parseObject(_response);
					if (response.get("data") != null){
						List<PageData> data = JSONObject.parseArray(JSON.toJSON(response.get("data")).toString(), PageData.class);
						for (PageData pageData : data) {
							if(StringUtils.isNotEmpty(pageData.getString("fcolor")) && StringUtils.isNotEmpty(pageData.getString("cardNo"))){
								if (pageData.getString("fcolor").equals("0")){
									yellowList.add(pageData.getString("cardNo"));
								}
								if (pageData.getString("fcolor").equals("1")){
									orangeList.add(pageData.getString("cardNo"));
								}
								if (pageData.getString("fcolor").equals("2")){
									redList.add(pageData.getString("cardNo"));
								}
							}
						}
					}
				}
			}
		}
		map.put("redList",redList);
		map.put("orangeList",orangeList);
		map.put("yellowList",yellowList);
		map.put("result", errInfo);
		return map;
	}
}

