package com.jsowell.amap;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.jsowell.common.annotation.Anonymous;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.pile.dto.QueryPileDTO;
import com.jsowell.pile.dto.amap.GetStationInfoDTO;
import com.jsowell.thirdparty.amap.common.AMapCommonParams;
import com.jsowell.thirdparty.amap.common.AMapCommonResult;
import com.jsowell.thirdparty.amap.domain.AMapStationInfo;
import com.jsowell.thirdparty.amap.service.AMapService;
import com.jsowell.thirdparty.amap.util.AMapUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author Lemon
 * @Date 2023/6/16 13:56
 */
@Anonymous
@RestController
@RequestMapping("/amap/charging")
public class AMapController extends BaseController {

    @Autowired
    private AMapService aMapService;

    @PostMapping("/queryStationInfo")
    public AMapCommonResult queryStationInfo(@RequestParam Map<String, String> requestMap) {
        AMapCommonResult result = new AMapCommonResult();
        String paramString = JSON.toJSONString(requestMap);
        logger.info("高德拉取充电站静态数据 params:{}", paramString);
        try {
            // Map map = JSON.parseObject(paramString, Map.class);
            // Map<String, String> paramMap = (Map<String, String>)JSON.parse(paramString);
            if (AMapUtils.checkSign(requestMap)) {
                // true 验签成功
                String bizContent = requestMap.get("biz_content");
                GetStationInfoDTO dto = JSON.parseObject(bizContent, GetStationInfoDTO.class);
                List<AMapStationInfo> stationInfos = aMapService.getStationInfos(dto);
                logger.info("高德拉取充电站静态数据 success");
                return result.successResponse(stationInfos);
            }

        } catch (Exception e) {
            logger.error("高德拉取充电站静态数据 error", e);
            return result.failedResponse();
        }
        logger.error("高德拉取充电站静态数据验签失败");
        return result.checkSignFailed();
        // logger.info("高德拉取充电站静态数据 result:{}", );
    }


    @PostMapping("/pushStationStatus")
    public AMapCommonResult pushStationStatus(@RequestBody QueryPileDTO dto) {
        AMapCommonResult result = new AMapCommonResult();
        logger.info("商家推送充电设备动态数据");
        try {
            String pushResult = aMapService.pushChargingDeviceDynamics(dto.getPileConnectorCode());
            return result.successResponse(pushResult);
        } catch (Exception e) {
            logger.error("商家推送充电设备动态数据 error", e);
        }
        return result.failedResponse();
    }
}
