package com.engine.custom.sl.entity;

import weaver.general.StringUtil;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class TravelToEsbBean {
    private String oaTrvlBnsExpnsAcctNo;
    private String operatorNo;
    private String operatorName;
    private String acsryNums;
    private String expnsAmt;
    private String expnsRsn;
    private String remark;
    private String costDeptCode;
    private String costDeptName;


    private List<ExpenseItem> expndArray;
    private List<JourneyInfo> jrnyInfoArray;
    private List<acsryItem> acsryArray;

    public String toXMLString() {

        StringBuilder xmlString = new StringBuilder(
                "<oaTrvlBnsExpnsAcctNo>" + oaTrvlBnsExpnsAcctNo + "</oaTrvlBnsExpnsAcctNo>" +
                        "<operatorNo>" + operatorNo + "</operatorNo>" +
                        "<operatorName>" + operatorName + "</operatorName>" +
                        "<acsryNums>" + acsryNums + "</acsryNums>" +
                        "<expnsAmt>" + expnsAmt + "</expnsAmt>" +
                        "<expnsRsn>" + expnsRsn + "</expnsRsn>" +
                        "<remark>" + remark + "</remark>" +
                        "<costDeptCode>" + costDeptCode + "</costDeptCode>" +
                        "<costDeptName>" + costDeptName + "</costDeptName>");

        for (ExpenseItem expenseItem : expndArray) {
            xmlString.append(convertObjectToXml(expenseItem, "expndArray"));
        }
        for (JourneyInfo journeyInfo : jrnyInfoArray) {
            xmlString.append(convertObjectToXml(journeyInfo, "jrnyInfoArray"));
        }
        for (acsryItem acsryItem : acsryArray) {
            xmlString.append(convertObjectToXml(acsryItem, "acsryArray"));
        }
        return xmlString.toString();
    }


    @Override
    public String toString() {
        return "TravelToEsbBean{" +
                "oaTrvlBnsExpnsAcctNo='" + oaTrvlBnsExpnsAcctNo + '\'' +
                ", operatorNo='" + operatorNo + '\'' +
                ", operatorName='" + operatorName + '\'' +
                ", acsryNums='" + acsryNums + '\'' +
                ", expnsAmt='" + expnsAmt + '\'' +
                ", expnsRsn='" + expnsRsn + '\'' +
                ", remark='" + remark + '\'' +
                ", expndArray=" + expndArray +
                ", jrnyInfoArray=" + jrnyInfoArray +
                ", acsryArray=" + acsryArray +
                '}';
    }

    public List<acsryItem> getAcsryArray() {
        return acsryArray;
    }

    public void setAcsryArray(List<acsryItem> acsryArray) {
        this.acsryArray = acsryArray;
    }


    public String getOaTrvlBnsExpnsAcctNo() {
        return oaTrvlBnsExpnsAcctNo;
    }

    public void setOaTrvlBnsExpnsAcctNo(String oaTrvlBnsExpnsAcctNo) {
        this.oaTrvlBnsExpnsAcctNo = oaTrvlBnsExpnsAcctNo;
    }

    public String getOperatorNo() {
        return operatorNo;
    }

    public void setOperatorNo(String operatorNo) {
        this.operatorNo = operatorNo;
    }

    public String getOperatorName() {
        return operatorName;
    }

    public void setOperatorName(String operatorName) {
        this.operatorName = operatorName;
    }

    public String getAcsryNums() {
        return acsryNums;
    }

    public void setAcsryNums(String acsryNums) {
        this.acsryNums = acsryNums;
    }

    public String getExpnsAmt() {
        return expnsAmt;
    }

    public void setExpnsAmt(String expnsAmt) {
        this.expnsAmt = expnsAmt;
    }

    public String getExpnsRsn() {
        return expnsRsn;
    }

    public void setExpnsRsn(String expnsRsn) {
        this.expnsRsn = expnsRsn;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public List<ExpenseItem> getExpndArray() {
        return expndArray;
    }

    public void setExpndArray(List<ExpenseItem> expndArray) {
        this.expndArray = expndArray;
    }

    public List<JourneyInfo> getJrnyInfoArray() {
        return jrnyInfoArray;
    }

    public void setJrnyInfoArray(List<JourneyInfo> jrnyInfoArray) {
        this.jrnyInfoArray = jrnyInfoArray;
    }

    public static String convertObjectToXml(Object object, String RootName) {


        StringBuilder xmlBuilder = new StringBuilder("<" + (StringUtil.isEmpty(RootName) ? object.getClass().getSimpleName() : RootName) + ">");
        Field[] fields = object.getClass().getDeclaredFields();
        Map<String, Object> attributes = new LinkedHashMap<>();

        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(object);
                if (value != null) {
                    String fieldName = field.getName();
                    xmlBuilder.append("<").append(fieldName).append(">")
                            .append(value)
                            .append("</").append(fieldName).append(">");
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        xmlBuilder.append("</" + (StringUtil.isEmpty(RootName) ? object.getClass().getSimpleName() : RootName) + ">");
        return xmlBuilder.toString();
    }

    public String getCostDeptCode() {
        return costDeptCode;
    }

    public void setCostDeptCode(String costDeptCode) {
        this.costDeptCode = costDeptCode;
    }

    public String getCostDeptName() {
        return costDeptName;
    }

    public void setCostDeptName(String costDeptName) {
        this.costDeptName = costDeptName;
    }

    public static void main(String[] args) throws IOException {
        File tempFile = File.createTempFile("tempfile", ".tmp");
        System.out.println("临时文件路径: " + tempFile.getAbsolutePath());
    }
}
