package com.fjhx.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

public class GetV3DateUtil {

    private static final String URL_PREFIX = "http://mes.cfmfactory.com:8087/";
    private static final RestTemplate restTemplate = new RestTemplate();

    /**
     * 获取token
     */
    public static String getToken() {
        String url = URL_PREFIX + "portal/getAccessTokenAction.do?AppId=1&AppSecret=a2b1c2d3";
        String result = restTemplate.getForEntity(url, String.class).getBody();
        if (ObjectUtil.isEmpty(result)) {
            return "";
        }
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject.getString("token");
    }

    /**
     * 获取排班数据
     */
    public static JSONArray getPlanData(Date beginDate, Date endDate) {
        // 请求v3接口获取token
        String token = getToken();

        String beginDateStr = DateUtil.format(beginDate, "yyyy-MM-dd HH:mm:ss");
        String endDateStr = DateUtil.format(endDate, "yyyy-MM-dd HH:mm:ss");

        String url = URL_PREFIX + "MesWeb/planPersonOutAction.do?action=list&beginDate="
                + beginDateStr + "&endDate=" + endDateStr + "&access_token=" + token;

        JSONObject result = JSONObject.parseObject(restTemplate.getForEntity(url, String.class).getBody());

        // 接收到的消息
        return result.getJSONArray("items");
    }

}
