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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zcloud.modules.map.service.UserPositionService;
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.math.BigDecimal;
import java.util.*;

/**
 * 说明：用户服务接口实现类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Service
@Transactional //开启事物
public class UsersPositionServiceImpl implements UserPositionService {
    @Autowired
    private PerLocUtil perLocUtil;

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


    @Override
    public Object getCurrentLocation() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        String _response = HttpRequestUtil.doGetUser(url + "/deploy/psnmgt/getTheCurrentLocationOfAllEmployeesWhosePersonnelCardsAreOnline?office=", perLocUtil.getToken());
        if (StringUtils.isNotEmpty(_response)) {
            JSONObject response = JSONObject.parseObject(_response);
            if (response.get("data") != null) {
                List<PageData> _varList = JSONObject.parseArray(JSON.toJSON(response.get("data")).toString(), PageData.class);
                List<PageData> varList = new ArrayList<>();
                if (!Objects.isNull(_varList)) {
                    for (PageData varData : _varList) {
                        List<String> position = JSONObject.parseArray(JSON.toJSON(varData.get("coordinate")).toString(), String.class);
                        if (position != null) {
                            varData.put("LONGITUDE", position.get(0));
                            varData.put("LATITUDE", position.get(1));
                        }
                        varData.put("MAP_POINT_NAME", varData.getString("name"));
                        varData.put("NAME", varData.getString("name"));
                        varList.add(varData);
                    }
                }
                map.put("varList", varList);
            }
        }
        map.put("result", errInfo);
        return map;
    }


    @Override
    public Object getCarCurrentLocation() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        String _response = HttpRequestUtil.doGetUser(url + "/region/access/getCarRealLocationDataInfor", perLocUtil.getToken());
        if (StringUtils.isNotEmpty(_response)) {
            JSONObject response = JSONObject.parseObject(_response);
            if (response.get("list") != null) {
                List<PageData> _varList = JSONObject.parseArray(JSON.toJSON(response.get("list")).toString(), PageData.class);
                List<PageData> varList = new ArrayList<>();
                if (!Objects.isNull(_varList)) {
                    for (PageData varData : _varList) {
                        varData.put("LONGITUDE", varData.get("flon"));
                        varData.put("LATITUDE", varData.get("flat"));
                        varData.put("MAP_POINT_NAME", varData.get("carsign"));
                        varData.put("NAME", varData.get("carsign"));
                        varList.add(varData);
                    }
                }
                map.put("varList", varList);
            }
        }
        map.put("result", errInfo);
        return map;
    }

    /**
     * 查询人员在线统计数据
     *
     * @param
     * @return
     */
    @Override
    public Object getCurrentPersonnelData(String route) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        String path = url + route + "?office=";
        String _response = HttpRequestUtil.doGetUser(path, perLocUtil.getToken());
        if (StringUtils.isNotEmpty(_response)) {
            JSONObject response = JSONObject.parseObject(_response);
            if (response.get("data") != null && StringUtils.isNotEmpty(response.get("data").toString())) {
                List<PageData> _varList = JSONObject.parseArray(JSON.toJSON(response.get("data")).toString(), PageData.class);
                PageData resultPd = new PageData();
                int internalPersonCount = 0; // 内部人员在线数量
                int outsourcingPersonCount = 0; // 外协人员在线数量
                int temporaryPersonCount = 0; // 临时人员在线数量
                if (!Objects.isNull(_varList)) {
                    for (PageData pageData : _varList) {
                        if ("内部员工".equals(pageData.getString("officeName"))) { // 内部员工
                            internalPersonCount++;
                            continue;
                        }
                        if ("外协员工".equals(pageData.getString("officeName"))) {
                            outsourcingPersonCount++;
                            continue;
                        }
                        if ("临时员工".equals(pageData.getString("officeName"))) {
                            temporaryPersonCount++;
                        }
                    }
                }
                resultPd.put("onlinePersonCount", _varList.size());
                resultPd.put("internalPersonCount", internalPersonCount);
                resultPd.put("outsourcingPersonCount", outsourcingPersonCount);
                resultPd.put("temporaryPersonCount", temporaryPersonCount);
                map.put("pd", resultPd);
            }

        }
        map.put("result", errInfo);
        return map;
    }

    /**
     * 查询人员在线统计数据
     *
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public Object getCurrentCarData(String route) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        String path = url + route + "?ftype=real";
        String _response = HttpRequestUtil.doGetUser(path, perLocUtil.getToken());
        if (StringUtils.isNotEmpty(_response)) {
            JSONObject response = JSONObject.parseObject(_response);
            if (response.get("list") != null && StringUtils.isNotEmpty(response.get("list").toString())) {
                List<PageData> _varList = JSONObject.parseArray(JSON.toJSON(response.get("list")).toString(), PageData.class);
                double onlineCarCount = 0;
                for (PageData pageData : _varList) {
                    if (pageData.getString("psign").equals("一期堆场") || pageData.getString("psign").equals("二期堆场")) {
                        if (pageData.get("num") != null) {
                            BigDecimal bigDecimal = (BigDecimal) pageData.get("num");
                            onlineCarCount += Double.parseDouble(bigDecimal.toString());
                        }
                    }
                }
                PageData resultPd = new PageData();
                resultPd.put("onlineCarCount", onlineCarCount);
                map.put("pd", resultPd);
            }

        }
        map.put("result", errInfo);
        return map;
    }


    @Override
    public Object getPersonPositioningStatistics(String route) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        String path = url + route;
        String _response = HttpRequestUtil.doGetUser(path, perLocUtil.getToken());
        if (StringUtils.isNotEmpty(_response)) {
            JSONObject response = JSONObject.parseObject(_response);
            List<PageData> _varList = JSONObject.parseArray(JSON.toJSON(response.get("data")).toString(), PageData.class);
            List<PageData> varList = new ArrayList<>();
            if (!Objects.isNull(_varList)) {
                for (PageData pageData : _varList) {
                    PageData data = new PageData();
                    data.put("cardNo", pageData.getString("cardNo"));
                    data.put("name", pageData.getString("name"));
                    data.put("cardType", (boolean) pageData.get("cardType") ? "在线" : "离线");
                    varList.add(data);
                }
            }
            if (varList.size() > 8) {
                map.put("varList", varList.subList(0, 8));
            } else {
                map.put("varList", varList);
            }
        }
        map.put("result", errInfo);
        return map;
    }


    /**
     * 获取车辆定位统计列表
     *
     * @param route
     * @return
     * @throws Exception
     */
    @Override
    public Object getCarPositioningStatistics(String route) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        String path = url + route;
        String _response = HttpRequestUtil.doGetUser(path, perLocUtil.getToken());
        if (StringUtils.isNotEmpty(_response)) {
            JSONObject response = JSONObject.parseObject(_response);
            if (response.get("list") != null) {
                List<PageData> _varList = JSONObject.parseArray(JSON.toJSON(response.get("list")).toString(), PageData.class);
                List<PageData> varList = new ArrayList<>();
                if (!Objects.isNull(_varList)) {
                    for (PageData pageData : _varList) {
                        PageData data = new PageData();
                        data.put("name", pageData.getString("carsign"));
                        varList.add(data);
                    }
                }
                if (varList.size() > 6) {
                    map.put("varList", varList.subList(0, 6));
                } else {
                    map.put("varList", varList);
                }
            }

        }
        map.put("result", errInfo);
        return map;
    }


    @Override
    public Object getCurrentLocationCount() {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";

        try {
            String _response = HttpRequestUtil.doGetUser(url + "/deploy/psnmgt/getTheCurrentLocationOfAllEmployeesWhosePersonnelCardsAreOnline?office=", perLocUtil.getToken());
            map.put("perCount", 0);
            if (StringUtils.isNotEmpty(_response)) {
                JSONObject response = JSONObject.parseObject(_response);
                if (response.get("data") != null) {
                    List<PageData> _varList = JSONObject.parseArray(JSON.toJSON(response.get("data")).toString(), PageData.class);
                    map.put("perCount", _varList.size());
                }
            }
            map.put("result", errInfo);
        } catch (Exception e) {
            map.put("result", "error");
            map.put("msg", "第三方登录失败");

        }

        return map;


    }



    @Override
    public Object getDianZiWeiLanList(String route) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String errInfo = "success";
        String path = url + route;
        String _response = HttpRequestUtil.doGetUser(path, perLocUtil.getToken());
        if (StringUtils.isNotEmpty(_response)) {
            JSONObject response = JSONObject.parseObject(_response);
            if (response.get("rows") != null) {
                List<PageData> _varList = JSONObject.parseArray(JSON.toJSON(response.get("rows")).toString(), PageData.class);
                map.put("varList", _varList);
            }
        }
        map.put("result", errInfo);
        return map;
    }


}

