WPNAMEParasBo.java 5.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
package pwc.taxtech.atms.dto.vatdto;

import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.ValueEval;

import java.util.ArrayList;
import java.util.List;

import static pwc.taxtech.atms.common.util.FormulaUtil.resolverInteger;
import static pwc.taxtech.atms.common.util.FormulaUtil.resolverString;
import static pwc.taxtech.atms.exception.Exceptions.BAD_BBVO_PARAMS;

public class WPNAMEParasBo {
    private String reportCode;
    private Integer columnIndex;
    private Integer rowColumnIndex;
    private String rowName;
    private String formulaExpression;
    private List<PeriodCellDataTemplate> expressionData = new ArrayList<>();

    public static class PeriodCellDataTemplate {
        Integer period;
        Long cellTemplateId;

        @Override
        public String toString() {
            return period + ":" + cellTemplateId;
        }

        public PeriodCellDataTemplate(Integer period, Long cellTemplateId) {
            this.period = period;
            this.cellTemplateId = cellTemplateId;
        }

    }

    public void putPeriodCellTempate(Integer period, Long cellTemplateId) {
        if (period < 1 || period > 12 || cellTemplateId < 0) throw BAD_BBVO_PARAMS;
        expressionData.add(new PeriodCellDataTemplate(period, cellTemplateId));
    }

    public WPNAMEParasBo(ValueEval[] args, OperationEvaluationContext ec) throws EvaluationException {
        StringBuilder expression = new StringBuilder("");
        begin(expression);
        reportCode = resolverString(args, ec, 0);
        concatPara(expression, reportCode);
        try {
            rowColumnIndex = resolverInteger(args, ec, 1);
            split(expression);
            concatPara(expression, rowColumnIndex);
        } catch (Exception e) {
            String columnStr = resolverString(args, ec, 1);
            split(expression);
            concatPara(expression, columnStr);
            int rtn = 0;
            columnStr = columnStr.toUpperCase();
            char[] excelCol = columnStr.toCharArray();
            for (int i = columnStr.length() - 1; i >= 0; i--) {
                if (excelCol[i] >= 'A' && excelCol[i] <= 'Z') {
                    rtn += ((int) Math.pow(26, excelCol.length - 1 - i) * (excelCol[i] - 64));
                } else {
                    rtn = -1;
                }
            }
            rowColumnIndex = rtn;
        }
        rowName = resolverString(args, ec, 2);
        split(expression);
        concatPara(expression, rowName);
        try {
            columnIndex = resolverInteger(args, ec, 3);
            split(expression);
            concatPara(expression, columnIndex);
        } catch (Exception e) {
            String columnStr = resolverString(args, ec, 3);
            split(expression);
            concatPara(expression, columnStr);
            int rtn = 0;
            columnStr = columnStr.toUpperCase();
            char[] excelCol = columnStr.toCharArray();
            for (int i = columnStr.length() - 1; i >= 0; i--) {
                if (excelCol[i] >= 'A' && excelCol[i] <= 'Z') {
                    rtn += ((int) Math.pow(26, excelCol.length - 1 - i) * (excelCol[i] - 64));
                } else {
                    rtn = -1;
                }
            }
            columnIndex = rtn;
        }
        end(expression);
        formulaExpression = expression.toString();
    }

    private StringBuilder begin(StringBuilder expression) {
        return expression.append("WPNAME(");
    }

    private StringBuilder end(StringBuilder expression) {
        return expression.append(")");
    }

    private StringBuilder split(StringBuilder expression) {
        return expression.append(",");
    }

    private StringBuilder concatPara(StringBuilder expression, String para) {
        return expression.append("\"").append(para).append("\"");
    }

    private StringBuilder concatPara(StringBuilder expression, Integer para) {
        return expression.append(para);
    }

    public WPNAMEParasBo(WPNAMEParasBo otherBo) {
        this.reportCode = otherBo.reportCode;
        this.columnIndex = otherBo.getColumnIndex();
        this.rowName = otherBo.rowName;
        this.formulaExpression = otherBo.formulaExpression;
    }

    public String expression() {
        return formulaExpression;
    }

    public String getReportCode() {
        return this.reportCode;
    }

    public void setReportCode(String reportCode) {
        this.reportCode = reportCode;
    }

    public Integer getColumnIndex() {
        return this.columnIndex;
    }

    public void setColumnIndex(Integer columnIndex) {
        this.columnIndex = columnIndex;
    }

    public String getRowName() {
        return rowName;
    }

    public void setRowName(String rowName) {
        this.rowName = rowName;
    }

    public Integer getRowColumnIndex() {
        return rowColumnIndex;
    }

    public void setRowColumnIndex(Integer rowColumnIndex) {
        this.rowColumnIndex = rowColumnIndex;
    }


    public String getFormulaExpression() {
        return this.formulaExpression;
    }

    public void setFormulaExpression(String formulaExpression) {
        this.formulaExpression = formulaExpression;
    }

    public List<PeriodCellDataTemplate> getExpressionData() {
        return this.expressionData;
    }

    public void setExpressionData(List<PeriodCellDataTemplate> expressionData) {
        this.expressionData = expressionData;
    }

    @Override
    public String toString() {
177
        return "WPNAMEParasBo{" +
178 179 180 181 182 183 184
                "reportCode='" + reportCode + '\'' +
                ", columnIndex=" + columnIndex +
                ", rowColumnIndex=" + rowColumnIndex +
                ", rowName=" + rowName +
                '}';
    }
}