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
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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package com.changhong.epc.parsing.service.account.impl;
 
import com.changhong.epc.bean.admin.Account;
import com.changhong.epc.bean.form.FormDataEntity;
import com.changhong.epc.bean.tenant.sunCloud.GmAccountData;
import com.changhong.epc.bean.tenant.sunCloud.GmPayInfo;
import com.changhong.epc.bean.tenant.sunCloud.OrderData;
import com.changhong.epc.constter.admin.AdminUrlConst;
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.system.businesscode.BudgetBusinessMeaningCode;
import com.changhong.epc.parsing.service.account.OrderAccountService;
import com.changhong.epc.parsing.service.master.impl.MasterFtpServiceImpl;
import com.changhong.epc.parsing.service.util.IFromOperate;
import com.changhong.epc.rely.api.bean.TenantsAndCompanys;
import com.changhong.epc.rely.api.service.CscApiService;
import com.iemsoft.framework.cloud.core.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * 通用记账
 */
@Service("orderAccountServiceImpl")
@Slf4j
public class OrderAccountServiceImpl implements OrderAccountService,BudgetBusinessMeaningCode {
 
    @Resource(name="cscApiServiceImpl")
    private CscApiService cscApiService;
 
    @Resource(name = "fromOperate")
    private IFromOperate iFromOperate;
    //当前表单自定义属性对应别名
    private Map<String,String> field;
 
    private static final String TAGATTRIBUTE = "tag_Attribute";
 
    private static final String ALIAS = "alias";
 
    private static final String CUSTOMARR = "customArr";
 
    private static final String ITEMTEXT = "itemtext_1";
 
    //付款信息自定义属性
    private static final String PAYINFO = "payInfos";
 
    //当前费用类型
    private String costType;
 
    @Override
    public List<Account> getAccount() {
        Object o = MasterFtpServiceImpl.RPC(AdminUrlConst.ACCOUNT_SELECTBYDATA,null);
        return o!=null?JSONTool.toList(JSONTool.toJson(o),Account.class):new ArrayList<Account>();
    }
 
    @Override
    public int updateAccountState(Integer id,Integer state,String orderCode) {
        Account account = new Account();
        account.setId(id);
        account.setOrderCode(orderCode);
        account.updateIngParam();
        account.setDataStart(state);
//        String url = state == 4 ? AdminUrlConst.ACCOUNT_DELETE : AdminUrlConst.ACCOUNT_UPDATESTATE;
        String url = AdminUrlConst.ACCOUNT_UPDATESTATE;
        Object o = MasterFtpServiceImpl.RPC(url,account);
        return Integer.parseInt(Objects.toString(o,"-1"));
    }
 
    /**
     * 开始记账,记账取参数规则,
     */
    public int buchfuhrung(Account account){
        setTh(account);
        String formId = account.getFormId();
        String tenantId = account.getTenantId();
        Integer dataRowNum = Integer.parseInt(account.getDataRowNum());
 
        //首先请求表单结构
        formatForm(JSONTool.toObj(account.getFieldSet(),Map.class));
        //获取主表单
        Map<String,Object> main = iFromOperate.selectSign(formId,tenantId,dataRowNum);
        Object currOrderCode = main.entrySet().stream().filter(entity->entity.getKey().equalsIgnoreCase(ORDER_CODE)).map(Map.Entry::getValue).findFirst().orElse("");
        //当前费用类型赋值
        this.costType = Objects.toString(main.get(COST_TYPE),"0");
        //主表数据填充
        getFillData(main,account);
        //记账接口参数
        Map<String,Object> data = new HashMap<>();
        //记账所需参数----------------->
        //付款信息参数
        List<Map<String,Object>> payInfo = getPayInfoSub(this.field.get(PAYINFO), formId, tenantId, Integer.parseInt(account.getDataRowNum()),main);
        Map<String,Object> accountData = JSONTool.toObj(JSONTool.toJson(new GmAccountData()),Map.class);
        accountData.put(ORDER_CODE,currOrderCode);
        //填充参数
        getFormMain(main,JSONTool.toObj(JSONTool.toJson(new GmAccountData()),Map.class),accountData);
        Map<String,Object> orderData = JSONTool.toObj(JSONTool.toJson(new OrderData()),Map.class);
        getFormMain(main,JSONTool.toObj(JSONTool.toJson(new OrderData()),Map.class),orderData);
        getFillData(orderData,account);
        //accountData参数
        accountData.put(PAYINFO, payInfo);
 
        //orderData参数
 
        //是否有票据
        data.put("orderCodeFlag",ObjectUtil.notEmpty(currOrderCode)?1:0);
        data.put("tenantId",tenantId);
        data.put("source", 8);
        data.put("orderData",orderData);
        data.put("accountData", accountData);
        data.put("openId",account.getOpenId());
        log.debug("开始记账, formId:{}, dataRowNum:{}, orderCode:{}, data:{}", formId, dataRowNum, currOrderCode, JSONTool.toJson(data));
        Map<String,Object> res =  cscApiService.orderAccount(data);
        Object orderCode = res.get(ORDER_CODE);
        FormDataEntity formDataEntity = new FormDataEntity(formId ,dataRowNum,tenantId);
       if(orderCode !=null && !"".equals(orderCode)){
           log.debug("当前订单号:"+orderCode);
           log.debug("记账成功!");
           this.updateAccountState(account.getId(),4,orderCode.toString());
           //修改表单状态 为记账中
           formDataEntity.setMap(new ModelMap(ORDERSTATUS,35)
                   .addAttribute(ORDER_CODE,orderCode));
           iFromOperate.updateFormData(formDataEntity);
           return 1;
       }else{
           log.debug("记账异常!");
           log.debug("res:"+ JSONTool.toJson(res));
           this.updateAccountState(account.getId(),8,null);
           formDataEntity.setMap(new ModelMap(ORDERSTATUS,49));
           iFromOperate.updateFormData(formDataEntity);
           return -1;
       }
        //解析表单结构
 
 
    }
 
    /**
     * 获取表单自定义属性
     * @param
     * @return
     */
    public void formatForm(Map<String,Object> data){
        Map<String,String> res = new HashMap<>();
        data.keySet().stream().forEach(k->{
            Map<String,Object> map1 = JSONTool.toObj(data.get(k).toString(),Map.class);
            // System.out.println(JSONTool.toJson(map1));
            Map<String,Object> map2 = JSONTool.toObj(JSONTool.toJson(map1.get(TAGATTRIBUTE)),Map.class);
            if(map2.get(CUSTOMARR) != null && !"".equals(map2.get(CUSTOMARR))) {
                List<Map> custs = JSONTool.toList(JSONTool.toJson(map2.get(CUSTOMARR)),Map.class);
                log.debug("表单自定义属性:"+custs);
                if(custs.size()>0) {
                    custs.stream().forEach(c->{
                        res.put(Objects.toString(c.get(ITEMTEXT)), Objects.toString(map2.get(ALIAS), ""));
                    });
                }
 
            }
        });
        this.field = res;
    }
 
    /**
     * 获取付款信息参数
     * @param
     */
    public List<Map<String,Object>> getPayInfoSub(String subAlias,String formId,String tenantId,Integer dataRowNum,Map<String,Object> main){
        List<Map<String,Object>> payInfos = new ArrayList<Map<String,Object>>();
        //首先判断是子表还是子表单
        Map<String,String> map = JSONTool.toObj(JSONTool.toJson(new GmPayInfo()),Map.class);
        if(subAlias!=null && !"".equals(subAlias)){
            //取出子表单集合
            List<Map>li = iFromOperate.selectSubList(formId,tenantId,dataRowNum,subAlias);
            li.stream().forEach(o->{
                Map<String,Object> m = new HashMap<>();
                map.keySet().stream().forEach(c->{
                    m.put(c,o.get(this.field.get(c)));
                });
                payInfos.add(m);
            });
        }else{
            Map<String,Object> m = new HashMap<>();
            getFormMain(main,map,m);
            payInfos.add(m);
        }
        return payInfos;
    }
 
    /**
     * 筛选主表数据,,参数1-主表数据,参数2需要字段,参数3获取参数填充
     */
    public void getFormMain(Map<String,Object> data,Map<String,String> fie,Map<String,Object> body){
        fie.keySet().stream()
                .filter(f->data.get(this.field.get(f))!=null&&!"".equals(data.get(this.field.get(f))) )
                .forEach(c->
            body.put(c,data.get(this.field.get(c)))
        );
    }
 
    /**
     * 填充数据,组织机构相关参数
     * @param
     */
    public void getFillData(Map<String,Object> main,Account account){
        //填充参数,orgCode,tenantId,companyId,companyCode,orderTypeCode
        log.debug("当前费用类型id:"+this.costType);
        log.debug("开始填充参数:--->");
        getOrgData(account.getTenantId(),account.getCompanyId(),account.getOpenId(),main);
        main.put("tenantId",account.getTenantId());
        //需要云裳行所需id
//        main.put("companyId",account.getCompanyId());
        main.put("orderTypeCode", iFromOperate.getCostCode(Integer.parseInt(this.costType)));
        log.debug("orderData填充后:"+ JSONTool.toJson(main));
    }
 
    //获取组织机构相关参数
    public void getOrgData(String tenantId,String companyId,String openId,Map<String,Object> data){
        ThreadData.set(BaseConst.OPEN_ID,openId);
        TenantsAndCompanys tenants = cscApiService.findTenantsAndCompanys();
        tenants.getTenants()
                .stream()
                    .filter(fi->tenantId.equals(Objects.toString(fi.getId(),"")))
                        .forEach(o->{
                            o.getCompanys()
                                    .stream()
                                        .filter(f->companyId.equals(Objects.toString(f.getId(),"")))
                                            .forEach(c->{
                                                data.put("companyCode",c.getCode());
                                                data.put("orgCode",c.getOrgCode());
                                                data.put("hotInfoCompanyId",c.getHotInfoCompanyId());
                                            });
                    });
    }
    
    /**
     * 当前线程添加参数
     */
    public void setTh(Account account){
        ThreadData.set(Keys.COMPANY_ID,account.getCompanyId());
        ThreadData.set(Keys.TENANT_ID,account.getTenantId());
        ThreadData.set(BaseConst.TENANT_ID,account.getTenantId());
        ThreadData.set(BaseConst.COMPANY_ID,Integer.parseInt(account.getCompanyId()));
    }
 
}