package com.xhpc.order.dto;

import com.fasterxml.jackson.annotation.*;
import com.xhpc.common.core.utils.DateUtils;
import com.xhpc.common.util.DateUtil;
import com.xhpc.evcs.domain.XhpcStatisticsTimeInterval;
import com.xhpc.order.domain.XhpcHistoryOrder;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Transient;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

import static com.xhpc.common.core.utils.DateUtils.YYYY_MM_DD_HH_MM_SS;


@Setter
@Getter
public class ChargeOrderInfo {

    @Id
    @JsonProperty("StartChargeSeq")
    private String startChargeSeq;
    @JsonIgnore
    private Integer startChargeSeqStat;
    @JsonIgnore
    private Integer failReason;
    @JsonIgnore
    private String identCode;
    @JsonIgnore
    @Column(updatable = false)
    private String infraOperatorId;
    @JsonIgnore
    @Column(updatable = false)
    private String billerOperatorId;//biller
    @JsonProperty("ConnectorID")
    private String connectorID;
    @JsonProperty("StartTime")
    private String startTime;
    @JsonProperty("ChargeModel")
    private Integer chargeModel;
    @JsonProperty("Vin")
    private String vin;
    @JsonProperty("EndTime")
    private String endTime;
    @JsonProperty("TotalPower")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalPower = 0.0;
    @JsonProperty("TotalElecMoney")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalElecMoney = 0.0;
    @JsonProperty("TotalSeviceMoney")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalSeviceMoney = 0.0;
    @JsonProperty("TotalMoney")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double totalMoney = 0.0;
    @JsonProperty("StopReason")
    private Integer stopReason;
    @JsonProperty("SumPeriod")
    private Integer sumPeriod;
    @JsonProperty("MeterValueStart")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double meterValueStart = 0.0;
    @JsonProperty("MeterValueEnd")
    @Column(columnDefinition = "Decimal(10,2)")
    private Double meterValueEnd = 0.0;
    @JsonProperty("ChargeDetails")
    private ChargeDetails[] chargeDetails;
    @JsonIgnore
    @Transient
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();


    public ChargeOrderInfo(XhpcHistoryOrder xhpcHistoryOrder) {
        this.connectorID = xhpcHistoryOrder.getSerialNumber().substring(0, 16);
        this.startChargeSeq = xhpcHistoryOrder.getInternetSerialNumber();
        this.endTime = DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS, xhpcHistoryOrder.getEndTime());
        this.connectorID = xhpcHistoryOrder.getSerialNumber().substring(0, 16);
        this.endTime = DateUtil.date2String(xhpcHistoryOrder.getEndTime(), DateUtil.DATE_FORMAT_DATE_TIME);
        this.startTime = DateUtil.date2String(xhpcHistoryOrder.getStartTime(), DateUtil.DATE_FORMAT_DATE_TIME);
        this.chargeModel = xhpcHistoryOrder.getChargeModelEvcs();
        this.totalPower = xhpcHistoryOrder.getTotalPower();
        final BigDecimal powerPriceTotal = xhpcHistoryOrder.getPowerPriceTotal();
        this.totalElecMoney = powerPriceTotal == null ? 0.0 : powerPriceTotal.doubleValue();
        final BigDecimal servicePriceTotal = xhpcHistoryOrder.getServicePriceTotal();
        this.totalSeviceMoney = servicePriceTotal == null ? 0.0 : servicePriceTotal.doubleValue();
        final BigDecimal totalPrice = xhpcHistoryOrder.getTotalPrice();
        this.totalMoney = totalPrice == null ? 0.0 : totalPrice.doubleValue();
        //this.stopReason = xhpcHistoryOrder.getStopReasonEvcs();
        if(xhpcHistoryOrder.getStopReasonEvcs() !=null){
            String stopReasonEvcs = xhpcHistoryOrder.getStopReasonEvcs();
            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;
        }
        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<>();
        if (xhpcStatisticsTimeIntervalList != null)
            for (XhpcStatisticsTimeInterval statistics : xhpcStatisticsTimeIntervalList) {
                details.add(new ChargeDetails(statistics));
            }
        ChargeDetails[] a = new ChargeDetails[details.size()];
        return details.toArray(a);
    }

    public ChargeOrderInfo() {

    }

    @JsonProperty("StartChargeSeq")
    public String getStartChargeSeq() {

        return startChargeSeq;
    }

    @JsonProperty("StartChargeSeq")
    public void setStartChargeSeq(String startChargeSeq) {

        this.startChargeSeq = startChargeSeq;
    }

    public ChargeOrderInfo setStartChargeSeqAndReturn(String startChargeSeq) {

        this.startChargeSeq = startChargeSeq;
        return this;
    }

    @JsonProperty("ConnectorID")
    public String getConnectorID() {

        return connectorID;
    }

    @JsonProperty("ConnectorID")
    public void setConnectorID(String connectorID) {

        this.connectorID = connectorID;
    }

    @JsonProperty("StartTime")
    public String getStartTime() {

        return startTime;
    }

    @JsonProperty("StartTime")
    public void setStartTime(String startTime) {

        this.startTime = startTime;
    }

    @JsonProperty("EndTime")
    public String getEndTime() {

        return endTime;
    }

    @JsonProperty("EndTime")
    public void setEndTime(String endTime) {

        this.endTime = endTime;
    }

    @JsonProperty("TotalPower")
    public Double getTotalPower() {

        return totalPower;
    }

    @JsonProperty("TotalPower")
    public void setTotalPower(Double totalPower) {

        this.totalPower = totalPower;
    }

    @JsonProperty("TotalElecMoney")
    public Double getTotalElecMoney() {

        return totalElecMoney;
    }

    @JsonProperty("TotalElecMoney")
    public void setTotalElecMoney(Double totalElecMoney) {

        this.totalElecMoney = totalElecMoney;
    }

    @JsonProperty("TotalSeviceMoney")
    public Double getTotalSeviceMoney() {

        return totalSeviceMoney;
    }

    @JsonProperty("TotalSeviceMoney")
    public void setTotalSeviceMoney(Double totalSeviceMoney) {

        this.totalSeviceMoney = totalSeviceMoney;
    }

    @JsonProperty("TotalMoney")
    public Double getTotalMoney() {

        return totalMoney;
    }

    @JsonProperty("TotalMoney")
    public void setTotalMoney(Double totalMoney) {

        this.totalMoney = totalMoney;
    }

    @JsonProperty("StopReason")
    public Integer getStopReason() {

        return stopReason;
    }

    @JsonProperty("StopReason")
    public void setStopReason(Integer stopReason) {

        this.stopReason = stopReason;
    }

    @JsonProperty("SumPeriod")
    public Integer getSumPeriod() {

        return sumPeriod;
    }

    @JsonProperty("SumPeriod")
    public void setSumPeriod(Integer sumPeriod) {

        this.sumPeriod = sumPeriod;
    }

    @JsonAnyGetter
    @Transient
    public Map<String, Object> getAdditionalProperties() {

        return this.additionalProperties;
    }

    @JsonAnySetter
    @Transient
    public void setAdditionalProperty(String name, Object value) {

        this.additionalProperties.put(name, value);
    }


}
