package com.jisco.ymtappoint.controller;


import com.alibaba.fastjson.JSON;
import com.jisco.ymtappoint.http.HttpResult;
import com.jisco.ymtappoint.medol.AppointToJL;
import com.jisco.ymtappoint.service.impl.GetYMTAppoint;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "获取预约信息", tags = "预约信息")
@RestController
@RequestMapping("/v1/getAppointData")
public class YMTGetController {

//    @Autowired
//    GetYMTAppoint getYMTAppoint;

//    @Value ("${lcdate}")
//    String lcdate;
    //    @RequestMapping("/server/{ip}")

//    @ApiOperation("获取token")
//    @ResponseBody
//    @RequestMapping("/getAppointData/{lcdate}")
//    public HttpRequest getAppointData(@PathVariable("lcdate") String lcdate) {
//        String ymtToken = getYMTToken.getAppointToken();
//
//
//        RestTemplate client = new RestTemplate();
//        String url = "http://10.1.213.157:8778/appoint/getAppointToJL?=" + lcdate; //质量一贯制测试机
//        //定义请求参数类型，这里用json所以是MediaType.APPLICATION_JSON
//        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
//         headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
//        headers.add("Authorization", ymtToken);//质量一贯制测试机
//        // headers.add("Authorization", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJJZGVTdHVkaW8iLCJ1c2VyVHlwZSI6IjEiLCJhY2NvdW50IjoiMTYzNTczMjAzNTc1MyJ9.qRBHRfEaHnKKZ3_7dwn5ImMg12en54bfk3vAdLqfSaQ");//质量一贯制生产机
//
////        HttpEntity<String> request = new HttpEntity<>(list.toString(), headers);
//
//        Map<String, Object> params = new HashMap<>();
//        params.put("lcdate", "甘B-10505");
//
//        List<JSONObject> list = new ArrayList<>();
//        HttpEntity<String> request = new HttpEntity<>(list.toString(), headers);
//
////        ResponseEntity<String> entity = client.postForEntity(url, request, String.class);
//        ResponseEntity<String> entity = client.postForEntity(url, params, String.class);
//        JSONObject jsonObject = JSONObject.parseObject(entity.getBody());
//        //获取3方接口返回的数据通过entity.getBody();它返回的是一个字符串；
////        //system.out.println(jsonObject.get("access_token"));
////        return jsonObject.get("access_token").toString();
////        if(jsonObject.get("code").toString().equals("200")){
////            return true;
////        }
////        else{
//////            throw new CustomException("置入货位的结果回写碳钢MES失败！"+jsonObject.get("msg").toString());
////            throw new NullPointerException("获取异常！");
////        }
//
//
//        return null;
//
//    }


    @ApiOperation("获取token")
    @ResponseBody
    @PostMapping("/getAppointData/{lcdate}")
    public HttpResult getAppointData(@PathVariable("lcdate") String lcdate) {

        Assert.isTrue(StringUtils.isNotEmpty(lcdate),"请输入车号！");

//       String appoint = new GetYMTAppoint.getAppointData(lcdate);
       String appointYMT = new GetYMTAppoint().getAppointData(lcdate);

//        JSONArray jsonArray = JSONArray.fromArray(new ArrayList[]{(ArrayList) appoint});

//        JSONObject jsonObject = JSONObject.parseObject(appointYMT);
//        String appoint = jsonObject.toString();

        List<AppointToJL> appoint = JSON.parseArray(appointYMT,AppointToJL.class);
//        return JSONObject.parseObject(appointYMT);
        return HttpResult.ok(appoint);
    }
}
