package com.kym.entity.enplus.response;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.annotation.JSONCreator;
import com.alibaba.fastjson2.annotation.JSONField;
import com.kym.entity.enplus.EnPolicyInfo;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author skyline
 * @description 设备策略信息
 * @date 2023-08-15 15:58
 */
@Data
public class EnBusinessPolicy {
    /**
     * 业务策略查询流水号
     */
    @JSONField(name = "EquipBizSeq")
    private String equipBizSeq;
    /**
     * 充电设备接口编码
     */
    @JSONField(name = "ConnectorID")
    private String connectorId;
    /**
     * 操作结果
     * 0：失败
     * 1：成功
     */
    @JSONField(name = "SuccStat")
    private Integer succStat;
    /**
     * 失败原因
     * 0：无
     * 1：此充电桩业务策略不存在
     */
    @JSONField(name = "FailReason")
    private Integer failReason;
    /**
     * 时段数N 0~32
     */
    @JSONField(name = "SumPeriod")
    private Integer sumPeriod;
    /**
     * 单项业务策略信息
     */
    private List<EnPolicyInfo> policyInfos;

    @JSONCreator
    EnBusinessPolicy(@JSONField(name = "PolicyInfos") JSONArray array) {
        this.policyInfos = array.toJavaList(EnPolicyInfo.class);
        // 将尖峰平谷信息填入EnPolicyInfo中
        var elecPriceSet = policyInfos.stream().map(EnPolicyInfo::getElecPrice).collect(Collectors.toSet()).stream().sorted().toList();
        policyInfos = policyInfos.stream().peek(price -> price.setPricePeriod(EnPolicyInfo.PRICE_PERIOD[elecPriceSet.indexOf(price.getElecPrice())])).toList();
    }


}
