zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
package com.changhong.epc.count.service.format.impl;
 
import com.changhong.epc.bean.admin.CorresElField;
import com.changhong.epc.bean.admin.CurrencyInfo;
import com.changhong.epc.bean.count.MeInfo;
import com.changhong.epc.bean.count.format.CpnInfo;
import com.changhong.epc.bean.count.format.FormInfo;
import com.changhong.epc.bean.tenant.bill.IOCRBill;
import com.changhong.epc.bean.tenant.master.AutoMasterVal;
import com.changhong.epc.constter.admin.CorresElFieldConst;
import com.changhong.epc.constter.parsing.bill.ConstBill;
import com.changhong.epc.constter.system.businesscode.BudgetBusinessMeaningCode;
import com.changhong.epc.count.service.count.model.CountResult;
import com.changhong.epc.count.service.count.model.PublicCorrencyCountResult;
import com.changhong.epc.rely.api.epc.admin.CorresElFieldApi;
import com.changhong.epc.rely.api.epc.tenant.MasterApi;
import com.changhong.epc.rely.api.tool.CurrencyTool;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.*;
import java.util.function.BiFunction;
 
/**
 * 展示票据详细信息
 * 票据类型|币种缩写|金额|兑换后金额
 * :---:|:---:|:---:|:---:
 * 交通费|USD|100|600
 * 市内交通费|USD|5|30
 */
@Slf4j
@Service("formatBillDetailInfoCount")
public class FormatBillDetailInfoCount extends FormatJourneyErrInfoCount implements BudgetBusinessMeaningCode, CorresElFieldConst, ConstBill {
 
    public static final Map<String, BiFunction<FormInfo, IOCRBill, Object>> TITLE = Collections.unmodifiableMap(
            new LinkedHashMap<String, BiFunction<FormInfo, IOCRBill, Object>>(){
                {
                    put("journey.title.billType", (f, i)->Optional.ofNullable(i.getCostType()).map(IOCRBill.CostType::getName).orElse(""));
                    put("journey.title.money", (f, i)->
                        getCurrencyChar(
                                Optional.ofNullable(i.getBillKeyCode(BILL_CORRENCY))
                                .orElse(
                                        Optional.ofNullable(f.seacheCpnInfoByAlias(CURRENCY)).map(CpnInfo::getValue).map(Object::toString).orElse("")
                                )
                        )
                                +
                                CurrencyTool.format(i.getBillMoney()));
                    put("journey.title.exchangeMoney", (f, i)->
                        getCurrencyChar(Optional.ofNullable(f.seacheCpnInfoByAlias(APPLYCATION_CURRENCY)).map(CpnInfo::getValue).map(Object::toString).orElse(""))
                                +
                                CurrencyTool.valueOf(i.getExchangeRate(), i.getBillMoney()));
                }
            }
    );
 
    public static String getCurrencyChar(String mini){
        if(ObjectUtil.empty(mini)){
            return "*";
        }
        CurrencyInfo currencyInfo =
                Optional.ofNullable(SpringUtil.getBean(CorresElFieldApi.class).getVal(CURRENCY_MASTER_JSON, CorresElField::getMdCode))
                        .map(json->JSONTool.toObj(json, CurrencyInfo.class)).orElse(null);
        if(currencyInfo == null){
            log.error("没有配置币种主数据json");
            return "*";
        }
        AutoMasterVal autoMasterVal = new AutoMasterVal();
        autoMasterVal.setMdCode(currencyInfo.getMasterCode());
        autoMasterVal.setMeCode(currencyInfo.getCurrencyChar());
        autoMasterVal.setParams(
                Arrays.asList(
                        MeInfo.of(currencyInfo.getCurrencyMini(), mini)
                )
        );
        String val = SpringUtil.getBean(MasterApi.class).getMasterVal(autoMasterVal);
        if(ObjectUtil.empty(val)){
            log.debug("缺失币种主数据:{}", mini);
            val = "*";
        }
        return val;
    }
 
    @Override
    protected CpnInfo.CpnMsgInfo getBillCpnMsgInfo(Double billMoney, boolean validate, CountResult countResult) {
        CpnInfo.CpnMsgInfo billCpn = super.getBillCpnMsgInfo(billMoney, validate, countResult);
        if(!(countResult instanceof PublicCorrencyCountResult)){
            return billCpn;
        }
        billCpn.setCpnMsgInfo(billCpn);
        billCpn.addBillDetail(formInfo, ((PublicCorrencyCountResult) countResult).getBills(), TITLE);
        BigDecimal sumBill =
        ((PublicCorrencyCountResult) countResult).getBills().stream()
                .map(bill->new BigDecimal(CurrencyTool.valueOf(bill.getExchangeRate(), bill.getBillMoney())))
                .reduce(new BigDecimal(0), (s, t)->s.add(t));
        billCpn.setValue(sumBill);
        billCpn.setValidateValue(sumBill);
        return billCpn;
    }
 
}