package easydo.technology.controller;

import com.alibaba.fastjson.JSONObject;
import easydo.technology.service.GLDCostService;
import easydo.technology.util.HttpClientUtil;
import easydo.technology.annotation.WebLog;
import easydo.technology.exception.BizException;
import easydo.technology.utils.StringUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@RequestMapping("/costServer")
public class CostController {
    @Resource
    GLDCostService gldCostService;

    @WebLog
    @RequestMapping(value = "/getHYGHList/getHY")
    public Object getHY(@RequestBody Map<String, Object> map) throws Exception {
        List<String> projectCodeList = (List<String>) map.get("projectCodes");
        if (projectCodeList.isEmpty()) {
            throw new BizException("参数缺失");
        }
        JSONObject resObj = gldCostService.getHY(projectCodeList);
        return new ResponseEntity<>(resObj, HttpStatus.OK);
    }

    @WebLog
    @RequestMapping(value = "/getHYGHList/getKM")
    public Object getKM(@RequestBody Map<String, Object> map) throws Exception {
        List<String> projectCodeList = (List<String>) map.get("projectCodes");
        JSONObject resObj = gldCostService.getKM(projectCodeList);
        return new ResponseEntity<>(resObj, HttpStatus.OK);
    }

    // public static void main(String[] args) {
    //     JSONObject object = new JSONObject();
    //     List<String> codes = new ArrayList<>();
    //     codes.add("ygxny01");
    //     object.put("projectCodes", codes);
    //     Map<String, String> map = new HashMap<>();
    //     map.put("Authorization", "Basic a0ZkMkE4UkhSM1l1ZlplQ1lVUGNjVWFMblJWQW5ZZ1c6VTJNakoycVZiaGNVYlI5VlVyUUdYeVFQekp1WkdoNm4=");
    //     map.put("X-CORAL-TENANT", "865509948108800");
    //     long time1 = new Date().getTime();
    //     JSONObject res = HttpClientUtil.sendPOSTWithHeader("http://jsf-int.glodon.com/cost-management/openapi/contractPlanItems/list",
    //             object.toJSONString(), map);
    //     long time2 = new Date().getTime();
    //     System.out.println(res);
    //     System.out.println(time2-time1);
    // }

//    public static void main(String[] args) {
//
//        JSONObject data = new JSONObject();
//        List<Long> contractPlanIds = new ArrayList<>();
//        contractPlanIds.add((long)5600079);
//        data.put("contractPlanItemIds", contractPlanIds);
//        data.put("useState", "In_Use");
//        String url = "http://jsf-int.glodon.com" + "/cost-management/openapi/contractPlanItems/replace";
//        Map<String, String> header = new HashMap<>();
//        header.put("Authorization", "Basic a0ZkMkE4UkhSM1l1ZlplQ1lVUGNjVWFMblJWQW5ZZ1c6VTJNakoycVZiaGNVYlI5VlVyUUdYeVFQekp1WkdoNm4=");
//        header.put("X-CORAL-TENANT", "865509948108800");
//        JSONObject resObj = HttpClientUtil.sendPOSTWithHeader(url, data.toJSONString(), header);
//        System.out.println("修改合约规划状态req========>" + data.toJSONString() + "res========>" + resObj.toJSONString());
//        if (!"success".equals(resObj.getString("code"))) {
//            throw new BizException("修改合约规划状态异常");
//        }
//    }
}
