CellConfigTranslater.java 12.5 KB
Newer Older
1 2 3 4 5 6
package pwc.taxtech.atms.service.impl;

import org.apache.commons.lang3.StringUtils;
import pwc.taxtech.atms.constant.Constant;
import pwc.taxtech.atms.constant.enums.CellDataSourceType;
import pwc.taxtech.atms.dto.CellTemplateConfigDto;
7
import pwc.taxtech.atms.dto.vatdto.CellCalcInfoDto;
8 9
import pwc.taxtech.atms.entity.CellTemplate;
import pwc.taxtech.atms.entity.CellTemplateConfig;
10
import pwc.taxtech.atms.vat.entity.PeriodCellTemplateConfig;
11 12 13 14 15 16 17 18

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public final class CellConfigTranslater {

19
    public static CellTemplateConfigDto getConfigDto(CellTemplate template, List<CellTemplateConfig> configList) {
20 21 22 23
        if (template == null) {
            return null;
        }

24
        return getConfigDto(template.getId(), template.getReportTemplateId(), template.getRowIndex(), template.getRowName(), template.getColumnIndex(),
25 26 27
                template.getColumnName(), template.getDataType(), template.getIsReadOnly(), template.getComment(), configList);
    }

28
    public static CellTemplateConfigDto getConfigDto(CellCalcInfoDto x) {
29
        return CellConfigTranslater.getPeriodConfigDto(Long.parseLong(x.getCellTemplateId()), x.getReportTemplateId(), x.getRowIndex(),
30 31 32 33 34
                x.getRowName(), x.getColumnIndex(), x.getColumnName(),
                x.getDataType(), x.getIsReadOnly(),
                x.getDescription(), x.getConfigList());
    }

35
    private static CellTemplateConfigDto getConfigDto(Long configId, Long templateId, int rowIndex, String rowName, int columnIndex,
36 37
                                                      String columnName, Integer dataType, Boolean isReadOnly, String description,
                                                      List<CellTemplateConfig> configList) {
38 39 40 41 42
        if (configList == null) {
            return null;
        }

        CellTemplateConfigDto cellTemplateConfigDto = new CellTemplateConfigDto();
43 44
        cellTemplateConfigDto.setCellTemplateId(configId.toString());
        cellTemplateConfigDto.setTemplateId(templateId.toString());
45 46 47 48 49 50 51 52
        cellTemplateConfigDto.setRowIndex(rowIndex);
        cellTemplateConfigDto.setRowName(rowName);
        cellTemplateConfigDto.setColumnIndex(columnIndex);
        cellTemplateConfigDto.setColumnName(columnName);
        cellTemplateConfigDto.setDataType(dataType);
        cellTemplateConfigDto.setIsReadOnly(isReadOnly);
        cellTemplateConfigDto.setFormulaDescription(description);

53
        Optional<CellTemplateConfig> formulaItem = configList.stream().filter(a -> a.getDataSourceType().equals(CellDataSourceType.Formula.getCode())).findFirst();
54
        if (formulaItem.isPresent()) {
55 56 57 58 59 60 61
            cellTemplateConfigDto.setHasFormula(true);
            cellTemplateConfigDto.setFormula(formulaItem.get().getFormula());
            if (StringUtils.isEmpty(cellTemplateConfigDto.getFormulaDescription())
                    && !StringUtils.isEmpty(formulaItem.get().getFormulaDescription())) {
                cellTemplateConfigDto.setFormulaDescription(formulaItem.get().getFormulaDescription());
            }
            cellTemplateConfigDto.setFormulaDataSource(formulaItem.get().getFormulaDataSource());
62
            cellTemplateConfigDto.setCellTemplateId(formulaItem.get().getCellTemplateId().toString());
63 64
        }

65
        Optional<CellTemplateConfig> voucherItem = configList.stream().filter(x -> x.getDataSourceType().equals(CellDataSourceType.Voucher.getCode())).findFirst();
66
        if (voucherItem.isPresent()) {
67 68 69
            cellTemplateConfigDto.setHasVoucher(true);
            cellTemplateConfigDto.setVoucherKeyword(voucherItem.get().getVoucherKeyword() == null ? "" : voucherItem.get().getVoucherKeyword());
            if (!StringUtils.isEmpty(voucherItem.get().getAccountCodes())) {
70
                cellTemplateConfigDto.setAccountCodes(getList(voucherItem.get().getAccountCodes()));
71 72 73
            }
        }

74 75 76 77 78 79
        Optional<CellTemplateConfig> invoiceItem = configList
                .stream()
                .filter(x -> x.getDataSourceType().equals(CellDataSourceType.OutputInvoice.getCode())
                        || x.getDataSourceType().equals(CellDataSourceType.InputInvoice.getCode())
                        || x.getDataSourceType().equals(CellDataSourceType.CustomInvoice.getCode()))
                .findFirst();
80
        if (invoiceItem.isPresent()) {
81 82 83 84
            cellTemplateConfigDto.setHasInvoice(true);
            cellTemplateConfigDto.setInvoiceType(invoiceItem.get().getInvoiceType());
            cellTemplateConfigDto.setInvoiceAmountType(invoiceItem.get().getInvoiceAmountType());
            if (!StringUtils.isEmpty(invoiceItem.get().getTaxRate())) {
85
                cellTemplateConfigDto.setTaxRate(getList(invoiceItem.get().getTaxRate()));
86 87 88
            }

            if (!StringUtils.isEmpty(invoiceItem.get().getInvoiceCategory())) {
89 90
                List<String> invoiceCategoryStrs;
                invoiceCategoryStrs = getList(invoiceItem.get().getInvoiceCategory());
91
                List<Integer> ints = new ArrayList<>();
92
                assert invoiceCategoryStrs != null;
93 94 95 96 97 98 99 100 101
                for (String categoryStr : invoiceCategoryStrs) {
                    int categoryVal;
                    categoryVal = Integer.parseInt(categoryStr);
                    ints.add(categoryVal);
                }
                cellTemplateConfigDto.setInvoiceCategory(ints);
            }
        }

102
        Optional<CellTemplateConfig> keyInItem = configList.stream().filter(x -> x.getDataSourceType().equals(CellDataSourceType.KeyIn.getCode())).findFirst();
103
        if (keyInItem.isPresent()) {
104 105 106
            cellTemplateConfigDto.setHasKeyIn(true);
        }

107
        Optional<CellTemplateConfig> modelItem = configList.stream().filter(x -> x.getDataSourceType().equals(CellDataSourceType.RelatedModel.getCode())).findFirst();
108
        if (modelItem.isPresent()) {
109
            cellTemplateConfigDto.setHasModel(true);
110
            cellTemplateConfigDto.setModelIds(getList(modelItem.get().getModelIds()));
111 112
        }

113
        Optional<CellTemplateConfig> validationItem = configList.stream().filter(x -> x.getDataSourceType().equals(CellDataSourceType.Validation.getCode())).findFirst();
114
        if (validationItem.isPresent()) {
115 116 117
            cellTemplateConfigDto.setHasValidation(true);
            cellTemplateConfigDto.setValidation(validationItem.get().getValidation());
            cellTemplateConfigDto.setValidationDescription(validationItem.get().getValidationDescription());
118
            cellTemplateConfigDto.setCellTemplateId(validationItem.get().getCellTemplateId().toString());
119 120 121 122
        }
        return cellTemplateConfigDto;
    }

123

124
    private static CellTemplateConfigDto getPeriodConfigDto(Long cellTemplateId, Long templateId, int rowIndex, String rowName, int columnIndex,
125 126 127 128 129 130 131 132
                                                            String columnName, Integer dataType, Boolean isReadOnly,
                                                            String description, List<PeriodCellTemplateConfig> configList) {
        //already finished by today 20180711
        if (configList == null) {
            return null;
        }

        CellTemplateConfigDto cellTemplateConfigDto = new CellTemplateConfigDto();
133 134
        cellTemplateConfigDto.setCellTemplateId(cellTemplateId.toString());
        cellTemplateConfigDto.setTemplateId(templateId.toString());
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
        cellTemplateConfigDto.setRowIndex(rowIndex);
        cellTemplateConfigDto.setRowName(rowName);
        cellTemplateConfigDto.setColumnIndex(columnIndex);
        cellTemplateConfigDto.setColumnName(columnName);
        cellTemplateConfigDto.setDataType(dataType);
        cellTemplateConfigDto.setIsReadOnly(isReadOnly);
        cellTemplateConfigDto.setFormulaDescription(description);

        Optional<PeriodCellTemplateConfig> formulaItem = configList.stream()
                .filter(x -> x.getDataSourceType().equals(CellDataSourceType.Formula.getCode()))
                .findFirst();

        if (formulaItem.isPresent()) {
            cellTemplateConfigDto.setHasFormula(true);
            cellTemplateConfigDto.setFormula(formulaItem.get().getFormula());
            cellTemplateConfigDto.setParsedFormula(formulaItem.get().getParsedFormula());
            if (StringUtils.isBlank(cellTemplateConfigDto.getFormulaDescription())
                    && StringUtils.isNotBlank(formulaItem.get().getFormulaDescription())) {
                cellTemplateConfigDto.setFormulaDescription(formulaItem.get().getFormulaDescription());
            }
            cellTemplateConfigDto.setFormulaDataSource(formulaItem.get().getFormulaDataSource());
156
            cellTemplateConfigDto.setCellTemplateId(formulaItem.get().getCellTemplateId().toString());
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        }

        Optional<PeriodCellTemplateConfig> voucherItem = configList.stream()
                .filter(x -> x.getDataSourceType().equals(CellDataSourceType.Voucher.getCode())).findFirst();
        if (voucherItem.isPresent()) {
            cellTemplateConfigDto.setHasVoucher(true);
            cellTemplateConfigDto.setVoucherKeyword(voucherItem.get().getVoucherKeyword() == null
                    ? StringUtils.EMPTY : voucherItem.get().getVoucherKeyword());
            if (StringUtils.isNotBlank(voucherItem.get().getAccountCodes())) {
                cellTemplateConfigDto.setAccountCodes(getList(voucherItem.get().getAccountCodes()));
            }
        }

        Optional<PeriodCellTemplateConfig> invoiceItem = configList.stream()
                .filter(x -> (x.getDataSourceType().equals(CellDataSourceType.OutputInvoice.getCode()))
                        || (x.getDataSourceType().equals(CellDataSourceType.InputInvoice.getCode())
                        || (x.getDataSourceType().equals(CellDataSourceType.CustomInvoice.getCode())))).findFirst();
        if (invoiceItem.isPresent()) {
            cellTemplateConfigDto.setHasInvoice(true);
            cellTemplateConfigDto.setInvoiceType(invoiceItem.get().getInvoiceType());
            cellTemplateConfigDto.setInvoiceAmountType(invoiceItem.get().getInvoiceAmountType());
            if (StringUtils.isNotBlank(invoiceItem.get().getTaxRate())) {
                cellTemplateConfigDto.setTaxRate(getList(invoiceItem.get().getTaxRate()));
            }

            if (StringUtils.isNotBlank(invoiceItem.get().getInvoiceCategory())) {
                List<String> invoiceCategoryStrs = getList(invoiceItem.get().getInvoiceCategory());
                List<Integer> invoiceCategoryList = new ArrayList<>();
                invoiceCategoryStrs.forEach(s -> {
                    Integer categoryVal;
                    try {
                        categoryVal = Integer.parseInt(s);
                        invoiceCategoryList.add(categoryVal);
                    } catch (Exception e) {
                        //转换错误就不做添加,直接吃掉这里的异常,不进行阻塞
                    }
                });
                cellTemplateConfigDto.setInvoiceCategory(invoiceCategoryList);
            }
        }

        Optional<PeriodCellTemplateConfig> keyInItem = configList.stream()
                .filter(x -> x.getDataSourceType().equals(CellDataSourceType.KeyIn.getCode()))
                .findFirst();

        if (keyInItem.isPresent()) {
            cellTemplateConfigDto.setHasKeyIn(true);
        }

        Optional<PeriodCellTemplateConfig> modelItem = configList.stream()
                .filter(x -> x.getDataSourceType().equals(CellDataSourceType.RelatedModel.getCode()))
                .findFirst();

        if (modelItem.isPresent()) {
            cellTemplateConfigDto.setHasModel(true);
212
            cellTemplateConfigDto.setModelIds(getList(modelItem.get().getModelIds()));
213 214 215 216 217 218 219 220 221 222 223
        }

        Optional<PeriodCellTemplateConfig> validationItem = configList.stream()
                .filter(x -> x.getDataSourceType().equals(CellDataSourceType.Validation.getCode()))
                .findFirst();

        if (validationItem.isPresent()) {
            cellTemplateConfigDto.setHasValidation(true);
            cellTemplateConfigDto.setValidation(validationItem.get().getValidation());
            cellTemplateConfigDto.setParsedFormula(validationItem.get().getParsedFormula());
            cellTemplateConfigDto.setValidationDescription(validationItem.get().getValidationDescription());
224
            cellTemplateConfigDto.setCellTemplateId(validationItem.get().getCellTemplateId().toString());
225 226 227 228 229 230
        }

        return cellTemplateConfigDto;
    }


231
    private static List<String> getList(String joinString) {
232 233 234 235
        if (StringUtils.isEmpty(joinString)) {
            return null;
        }

236
        return new ArrayList<>(Arrays.asList(joinString.split(Constant.Comma)));
237 238
    }
}