package com.jsowell.thirdparty.xindiantu.service.impl;

import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
import com.jsowell.pile.domain.ThirdPartyStationRelation;
import com.jsowell.pile.dto.*;
import com.jsowell.pile.service.PileStationInfoService;
import com.jsowell.pile.service.ThirdPartyStationRelationService;
import com.jsowell.pile.vo.base.ThirdPartyStationRelationVO;
import com.jsowell.thirdparty.common.CommonService;
import com.jsowell.pile.thirdparty.CommonParamsDTO;
import com.jsowell.thirdparty.xindiantu.service.XDTService;
import com.jsowell.thirdparty.zhongdianlian.dto.ZDLGetTokenDTO;
import com.jsowell.thirdparty.zhongdianlian.service.ZDLService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 新电途Service
 *
 * @author Lemon
 * @Date 2023/12/28 11:02:42
 */
@Service
public class XDTServiceImpl implements XDTService {
    @Autowired
    private ZDLService zdlService;

    @Autowired
    private PileStationInfoService pileStationInfoService;

    @Autowired
    private ThirdPartyStationRelationService thirdPartyStationRelationService;

    @Autowired
    private CommonService commonService;

    /**
     * 获取令牌
     * @param dto
     * @return
     */
    @Override
    public String getToken(ZDLGetTokenDTO dto) {
        return zdlService.ZDLGetToken(dto);
    }

    /**
     * 生成token
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> generateToken(CommonParamsDTO dto) throws UnsupportedEncodingException {
        return zdlService.generateToken(dto);
    }

    @Override
    public Map<String, String> checkoutSign(CommonParamsDTO dto) {
        return zdlService.checkoutSign(dto);
    }

    /**
     * 查询站点信息
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryStationsInfo(QueryStationInfoDTO dto) {
        dto.setThirdPlatformType(ThirdPlatformTypeEnum.XIN_DIAN_TU.getTypeCode());
        return zdlService.queryStationsInfo(dto);
    }

    /**
     * 查询统计信息
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryStationStatus(QueryStationInfoDTO dto) {
        return zdlService.queryStationStatus(dto);
    }

    /**
     * 请求设备认证
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryEquipAuth(QueryEquipmentDTO dto) {
        return zdlService.queryEquipAuth(dto);
    }

    /**
     * 请求开始充电
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryStartCharge(QueryStartChargeDTO dto) {
        dto.setAccountBalance(dto.getChargingAmt()); // 新电途扩展字段，充电金额字段为 chargingAmt
        return zdlService.queryStartCharge(dto);
    }

    /**
     * 查询业务策略信息结果
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryEquipBusinessPolicy(QueryStartChargeDTO dto) {
        return zdlService.queryEquipBusinessPolicy(dto);
    }

    /**
     * 设备状态变化推送
     * @param dto
     * @return
     */
    @Override
    public String notificationStationStatus(PushRealTimeInfoDTO dto) {
        return zdlService.notificationStationStatus(dto);
    }

    /**
     * 推送充电订单信息
     * @param orderCode
     * @return
     */
    @Override
    public String pushChargeOrderInfo(String orderCode) {
        return zdlService.pushChargeOrderInfo(orderCode);
    }

    /**
     * 查询充电状态
     * @param dto
     */
    @Override
    public Map<String, String> queryEquipChargeStatus(QueryEquipChargeStatusDTO dto) {
        return zdlService.queryEquipChargeStatus(dto);
    }

    /**
     * 请求停止充电
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryStopCharge(QueryStartChargeDTO dto) {
        return zdlService.queryStopCharge(dto);
    }

    /**
     * 推送启动充电结果
     * @return
     */
    @Override
    public String notificationStartChargeResult(String orderCode) {
        return zdlService.notificationStartChargeResult(orderCode);
    }

    /**
     * 推送停止充电结果(仅在 0x19的帧类型中调用)
     * @param orderCode
     * @return
     */
    @Override
    public String notificationStopChargeResult(String orderCode) {
        return zdlService.notificationStopChargeResult(orderCode);
    }

    /**
     * 推送充电状态
     * @param orderCode
     * @return
     */
    @Override
    public String notificationEquipChargeStatus(String orderCode) throws UnsupportedEncodingException {
        return zdlService.notificationEquipChargeStatus(orderCode);
    }


    @Override
    public String pushStationInfo(PushStationInfoDTO dto) {
        // 通过站点id查询相关配置信息
        ThirdPartyStationRelation relation = new ThirdPartyStationRelation();
        relation.setStationId(dto.getStationId());
        ThirdPartyStationRelationVO relationInfo = thirdPartyStationRelationService.selectRelationInfo(relation);
        if (relationInfo == null) {
            // 新增
            relation.setThirdPartyType(dto.getThirdPartyType());
            thirdPartyStationRelationService.insertThirdPartyStationRelation(relation);

            return "OK";
        }
        return "Error";
    }

    public String pushStationInfoV2(PushStationInfoDTO dto) {
        // 新增数据库
        commonService.insertInfo2DataBase(dto);
        return dto.getThirdPartyType() + "：" + "OK";
    }
}
