package com.jsowell.amap;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
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.AMapCommonResult;
import com.jsowell.thirdparty.amap.domain.AMapStationInfo;
import com.jsowell.thirdparty.amap.service.AMapService;
import com.jsowell.thirdparty.amap.util.AMapUtils;
import org.springframework.beans.factory.annotation.Autowired;
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("/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);
                List<AMapStationInfo> stationInfos = aMapService.getStationInfosV2(dto);
                result = result.successResponse(stationInfos);
                // logger.info("高德拉取充电站静态数据 success  业务参数:{}", JSON.toJSONString(stationInfos));
                logger.info("高德拉取充电站静态数据 success  result:{}", JSON.toJSONString(result));
                return result;
            }

        } 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("商家推送充电设备动态数据 params:{}", JSONObject.toJSONString(dto));
        try {
            String pushResult = aMapService.pushChargingDeviceDynamics(dto.getStationId());
            return result.successResponse(pushResult);
        } catch (Exception e) {
            logger.error("商家推送充电设备动态数据 error", e);
        }
        return result.failedResponse();
    }
}
