package org.springblade.third.iotdevice.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctg.ag.sdk.biz.AepProductManagementClient;
import com.ctg.ag.sdk.biz.aep_product_management.QueryProductListRequest;
import com.ctg.ag.sdk.biz.aep_product_management.QueryProductListResponse;
import com.ctg.ag.sdk.biz.aep_product_management.QueryProductRequest;
import com.ctg.ag.sdk.biz.aep_product_management.QueryProductResponse;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springblade.common.constant.AepConstant;
import org.springblade.core.tool.api.R;
import org.springblade.third.iotdevice.entity.IotAepProduct;
import org.springblade.third.iotdevice.service.IIotAepProductService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lidexi
 * @date 2021/5/28 16:46
 * aep产品设备对接控制层
 */

@RestController
@AllArgsConstructor
@RequestMapping("/third/aepproduct/butt")
public class IotAepProductButtController {

	private final IIotAepProductService iotAepProductService;

	@SneakyThrows
	@PostMapping("/productList")
	public R productList() {
//		AepProductManagementClient client = AepProductManagementClient.newClient()
//			.appKey(AepConstant.APPKEY).appSecret(AepConstant.APPSECRET)
//			.build();
//		QueryProductListRequest request = new QueryProductListRequest();
//		request.setParamPageNow(1);
//		request.setParamPageSize(20000);
//
//		QueryProductListResponse queryProductListResponse = client.QueryProductList(request);
//
//		client.shutdown();
//		JSONObject body = JSONUtil.parseObj(new String(queryProductListResponse.getBody()));
//		JSONObject result = JSONUtil.parseObj(body.get("result"));
//		JSONArray list1 = JSONUtil.parseArray(result.get("list"));
//		List<IotAepProduct> list = com.alibaba.fastjson.JSONObject.parseArray(list1.toString(), IotAepProduct.class);
//		list.stream().forEach(iotAepProduct -> {
//			IotAepProduct product = this.iotAepProductService.getBaseMapper().selectOne(new QueryWrapper<>(new IotAepProduct()).lambda()
//				.eq(IotAepProduct::getProductId, iotAepProduct.getProductId()));
//			if(product == null){
//				iotAepProduct.setPlatform("aep");
//				this.iotAepProductService.save(iotAepProduct);
//			}else {
//				iotAepProduct.setId(product.getId());
//				iotAepProduct.setPlatform("aep");
//				this.iotAepProductService.updateById(iotAepProduct);
//			}
//		});
		return R.data(new ArrayList<>());
//		return R.data(list);
	}

	@GetMapping("/product/{id}")
	public R queryOne(@PathVariable("id") Long id) {
//		AepProductManagementClient client = AepProductManagementClient.newClient()
//			.appKey(AepConstant.APPKEY).appSecret(AepConstant.APPSECRET)
//			.build();
//
//		QueryProductRequest request = new QueryProductRequest();
//		request.setParamProductId(id);
//		try {
//			QueryProductResponse queryProductResponse = client.QueryProduct(request);
//			JSONObject body = JSONUtil.parseObj(new String(queryProductResponse.getBody()));
//			System.out.println();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}

// more requests

//		client.shutdown();
		return null;
	}

}
