package com.xhpc.evcs.cdjgpc.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.xhpc.evcs.domain.EtOrderMapping;
import com.xhpc.evcs.domain.XhpcStatisticsTimeInterval;
import com.xhpc.evcs.dto.ChargeDetails;
import com.xhpc.evcs.utils.DateUtil;
import com.xhpc.order.domain.XhpcHistoryOrder;
import lombok.Data;

import javax.persistence.Column;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author yuyang
 * @date 2023-09-07 15:31
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "OperatorID",
        "EquipmentOwnerID",
        "StationID",
        "EquipmentID",
        "ConnectorID",
        "EquipmentClassification",
        "StartChargeSeq",
        "UserChargeType",
        "Money",
        "ElectMoney",
        "ServiceMoney",
        "Elect",
        "StartTime",
        "EndTime",
        "SwapOrderID",
        "PaymentAmount",
        "PayTime",
        "PayChannel",
        "DiscountInfo",
        "StopReason",
        "SumPeriod",
        "Power",
        "ChargeLast",
        "MeterValueStart",
        "MeterValueEnd",
        "ChargeDetails",
        "PushTimeStamp"
})
@Data
public class CDOrderInfo {
    /**
     * 运营平台 ID
     */
    @JsonProperty("OperatorID")
    public String operatorID;
    /**
     * 设备产权所 属单位 ID
     */
    @JsonProperty("EquipmentOwnerID")
    public String equipmentOwnerID;
    /**
     * 充电站 ID
     */
    @JsonProperty("StationID")
    public String stationID;
    /**
     * 设备编码
     */
    @JsonProperty("EquipmentID")
    public String equipmentID;
    /**
     * 充电设备接 口编码
     */
    @JsonProperty("ConnectorID")
    public String connectorID;
    /**
     * 设备接口分类
     */
    @JsonProperty("EquipmentClassification")
    public Integer equipmentClassification;
    /**
     * 充电订单号
     */
    @JsonProperty("StartChargeSeq")
    public String startChargeSeq;
    /**
     * 用户发起充 电类型
     */
    @JsonProperty("UserChargeType")
    public String userChargeType;
    /**
     * 用户手机号
     */
//    @JsonProperty("MobileNumber")
//    public String mobileNumber;
    /**
     * 本次充电消费总金额
     */
    @JsonProperty("Money")
    @Column(columnDefinition = "Decimal(10,2)")
    public Double money;
    /**
     * 本本次充电电费总金额
     */
    @JsonProperty("ElectMoney")
    @Column(columnDefinition = "Decimal(10,2)")
    public Double electMoney;
    /**
     * 本次充电 服务费金额
     */
    @JsonProperty("ServiceMoney")
    @Column(columnDefinition = "Decimal(10,2)")
    public Double serviceMoney;
    /**
     * 本次充电电量
     */
    @JsonProperty("Elect")
    @Column(columnDefinition = "Decimal(10,2)")
    public Double elect;

//    /**
//     * 尖阶段电量
//     */
//    @JsonProperty("CuspElect")
//    @Column(columnDefinition = "Decimal(10,4)")
//    public Double cuspElect;
//
//    /**
//     * 峰阶段电量
//     */
//    @JsonProperty("PeakElect")
//    @Column(columnDefinition = "Decimal(10,4)")
//    public Double peakElect;
//
//    /**
//     * 平阶段电量
//     */
//    @JsonProperty("FlatElect")
//    @Column(columnDefinition = "Decimal(10,4)")
//    public Double flatElect;
//
//    /**
//     * 谷阶段电量
//     */
//    @JsonProperty("ValleyElect")
//    @Column(columnDefinition = "Decimal(10,4)")
//    public Double valleyElect;

    /**
     * 本次充电开始时间
     */
    @JsonProperty("StartTime")
    public String startTime;
    /**
     * 本次充电结束时间
     */
    @JsonProperty("EndTime")
    public String endTime;
    /**
     * 换电订单编号
     */
    @JsonProperty("SwapOrderID")
    public String swapOrderID;
    /**
     * 支付金额
     */
    @JsonProperty("PaymentAmount")
    @Column(columnDefinition = "Decimal(10,2)")
    public Double paymentAmount;
    /**
     * 支付时
     */
    @JsonProperty("PayTime")
    public String payTime;
    /**
     * 支付方式
     */
    @JsonProperty("PayChannel")
    public Integer payChannel;
    /**
     * 优惠信息描述
     */
    @JsonProperty("DiscountInfo")
    public String discountInfo;
    /**
     * 充电结束
     * 原因
     */
    @JsonProperty("StopReason")
    public Integer stopReason;
    /**
     * 时段数 N
     */
    @JsonProperty("SumPeriod")
    public Integer sumPeriod;
    /**
     * 接口额定功 率
     */
    @JsonProperty("Power")
    @Column(columnDefinition = "Decimal(10,1)")
    public Double power;
    /**
     * 充电时长
     */
    @JsonProperty("ChargeLast")
    public Integer chargeLast;
    /**
     * 电表总起值
     */
    @JsonProperty("MeterValueStart")
    @Column(columnDefinition = "Decimal(10,3)")
    public Double meterValueStart;
    /**
     * 电表总止值
     */
    @JsonProperty("MeterValueEnd")
    @Column(columnDefinition = "Decimal(10,3)")
    public Double meterValueEnd;
    /**
     * 充电明细信息
     */
    @JsonProperty("ChargeDetails")
    public  ChargeDetails[] chargeDetails;
    /**
     * 推送时间
     */
    @JsonProperty("PushTimeStamp")
    public String pushTimeStamp;


    public CDOrderInfo(XhpcHistoryOrder xhpcHistoryOrder, EtOrderMapping etOrderMapping) {
        this.operatorID =xhpcHistoryOrder.getOperatorIdEvcs();
        //this.operatorID = xhpcHistoryOrder.getOperatorId3rdptyEvcs();
        this.stationID =xhpcHistoryOrder.getChargingStationId().toString();
        this.equipmentID = xhpcHistoryOrder.getSerialNumber().substring(0, 14);
        this.connectorID = xhpcHistoryOrder.getSerialNumber().substring(0, 16);
        this.startChargeSeq = etOrderMapping.getEvcsOrderNo();
        this.equipmentClassification =1;
        this.userChargeType ="1";
        this.money = xhpcHistoryOrder.getTotalPrice().doubleValue();
        this.electMoney =xhpcHistoryOrder.getTotalPrice().doubleValue();
        this.serviceMoney = xhpcHistoryOrder.getServicePriceTotal().doubleValue();
        this.elect = xhpcHistoryOrder.getTotalPower();
        this.endTime = DateUtil.date2String(xhpcHistoryOrder.getEndTime(), DateUtil.DATE_FORMAT_DATE_TIME);
        this.startTime = DateUtil.date2String(xhpcHistoryOrder.getStartTime(), DateUtil.DATE_FORMAT_DATE_TIME);
        this.pushTimeStamp = DateUtil.date2String(new Date(), DateUtil.DATE_FORMAT_DATE_TIME);
        this.paymentAmount = xhpcHistoryOrder.getActPrice().doubleValue();
        if(xhpcHistoryOrder.getTotalPrice().compareTo(xhpcHistoryOrder.getActPrice())!=0){
            this.discountInfo ="优惠金额："+xhpcHistoryOrder.getPromotionDiscount();
        }
        this.payTime =DateUtil.date2String(xhpcHistoryOrder.getEndTime(), DateUtil.DATE_FORMAT_DATE_TIME);
        if("支付宝".equals(xhpcHistoryOrder.getChargingMode())){
            this.payChannel =1;
        }else if("微信".equals(xhpcHistoryOrder.getChargingMode())){
            this.payChannel =2;
        }else{
            this.payChannel =6;
        }
        if(xhpcHistoryOrder.getStopReasonEvcs() !=null){
            String stopReasonEvcs = xhpcHistoryOrder.getStopReasonEvcs();
            if("40".equals(stopReasonEvcs)||"0".equals(stopReasonEvcs)||"00".equals(stopReasonEvcs)||"45".equals(stopReasonEvcs)||"APP 远程停止".equals(stopReasonEvcs)
                    ||"72".equals(stopReasonEvcs)){
                this.stopReason =0;
            }else if("41".equals(stopReasonEvcs) ||"42".equals(stopReasonEvcs)||"43".equals(stopReasonEvcs)||"44".equals(stopReasonEvcs)||"4E".equals(stopReasonEvcs)||"平台停止".equals(stopReasonEvcs)){
                this.stopReason =1;
            }else if("4A".equals(stopReasonEvcs) ||"4B".equals(stopReasonEvcs)||"4C".equals(stopReasonEvcs)||"4D".equals(stopReasonEvcs)||"4F".equals(stopReasonEvcs) ||
                    "55".equals(stopReasonEvcs)||"57".equals(stopReasonEvcs)||"62".equals(stopReasonEvcs)||"63".equals(stopReasonEvcs)||"74".equals(stopReasonEvcs)||"75".equals(stopReasonEvcs)
                    ||"78".equals(stopReasonEvcs)||"79".equals(stopReasonEvcs)||"7A".equals(stopReasonEvcs)||"7B".equals(stopReasonEvcs)||"7C".equals(stopReasonEvcs)||"7D".equals(stopReasonEvcs)
                    ||"7E".equals(stopReasonEvcs)||"7F".equals(stopReasonEvcs)||"83".equals(stopReasonEvcs)){
                this.stopReason =3;
            }else if("5A".equals(stopReasonEvcs)||"82".equals(stopReasonEvcs)){
                this.stopReason =2;
            }else if("6B".equals(stopReasonEvcs) ||"6D".equals(stopReasonEvcs)||"5E".equals(stopReasonEvcs)){
                this.stopReason =4;
            }else{
                this.stopReason =0;
            }
        }else{
            this.stopReason =0;
        }
        this.power = xhpcHistoryOrder.getConnectorPowerEvcs();
        Date starttime = xhpcHistoryOrder.getStartTime();
        Date endtime = xhpcHistoryOrder.getEndTime();
        int cl = 0;
        if (starttime != null && endtime != null) {
            cl = Math.toIntExact((endtime.getTime() - starttime.getTime()) / 1000);
        }
        this.chargeLast = Math.abs(cl);
        if(xhpcHistoryOrder.getMeterValueStartEvcs() !=null && xhpcHistoryOrder.getMeterValueStartEvcs()>0){
            double v = xhpcHistoryOrder.getMeterValueStartEvcs() / 10000;
            DecimalFormat df = new DecimalFormat("#.000");
            this.meterValueStart =Double.parseDouble(df.format(v));
        }else{
            this.meterValueStart = 0.00;
        }
        if(xhpcHistoryOrder.getMeterValueEndEvcs() !=null && xhpcHistoryOrder.getMeterValueEndEvcs()>0){
            double v = xhpcHistoryOrder.getMeterValueEndEvcs() / 10000;
            DecimalFormat df = new DecimalFormat("#.000");
            this.meterValueEnd =Double.parseDouble(df.format(v));
        } else{
            this.meterValueEnd = 0.00;
        }
        final List<XhpcStatisticsTimeInterval> xhpcStatisticsTimeIntervalList =
                xhpcHistoryOrder.getXhpcStatisticsTimeIntervalList();
        this.sumPeriod = xhpcStatisticsTimeIntervalList == null ? 0 : xhpcStatisticsTimeIntervalList.size();
        this.chargeDetails = translate(xhpcStatisticsTimeIntervalList);
    }
    private ChargeDetails[] translate(List< XhpcStatisticsTimeInterval > xhpcStatisticsTimeIntervalList) {

        List<ChargeDetails> details = new LinkedList<>();
        for (XhpcStatisticsTimeInterval statistics : xhpcStatisticsTimeIntervalList) {
            details.add(new ChargeDetails(statistics));
        }
        ChargeDetails[] a = new ChargeDetails[details.size()];
        return details.toArray(a);
    }
}
