zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1 package com.changhong.epc.parsing.service.account.impl;
Z 2
3 import com.changhong.epc.bean.admin.Account;
4 import com.changhong.epc.bean.form.FormDataEntity;
5 import com.changhong.epc.bean.tenant.sunCloud.GmAccountData;
6 import com.changhong.epc.bean.tenant.sunCloud.GmPayInfo;
7 import com.changhong.epc.bean.tenant.sunCloud.OrderData;
8 import com.changhong.epc.constter.admin.AdminUrlConst;
9 import com.changhong.epc.constter.base.BaseConst;
10 import com.changhong.epc.constter.system.businesscode.BudgetBusinessMeaningCode;
11 import com.changhong.epc.parsing.service.account.OrderAccountService;
12 import com.changhong.epc.parsing.service.master.impl.MasterFtpServiceImpl;
13 import com.changhong.epc.parsing.service.util.IFromOperate;
14 import com.changhong.epc.rely.api.bean.TenantsAndCompanys;
15 import com.changhong.epc.rely.api.service.CscApiService;
16 import com.iemsoft.framework.cloud.core.thread.Keys;
17 import com.iemsoft.framework.cloud.core.thread.ThreadData;
18 import com.iemsoft.framework.cloud.core.tools.JSONTool;
19 import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
20 import lombok.extern.slf4j.Slf4j;
21 import org.springframework.stereotype.Service;
22 import org.springframework.ui.ModelMap;
23
24 import javax.annotation.Resource;
25 import java.util.*;
26
27 /**
28  * 通用记账
29  */
30 @Service("orderAccountServiceImpl")
31 @Slf4j
32 public class OrderAccountServiceImpl implements OrderAccountService,BudgetBusinessMeaningCode {
33
34     @Resource(name="cscApiServiceImpl")
35     private CscApiService cscApiService;
36
37     @Resource(name = "fromOperate")
38     private IFromOperate iFromOperate;
39     //当前表单自定义属性对应别名
40     private Map<String,String> field;
41
42     private static final String TAGATTRIBUTE = "tag_Attribute";
43
44     private static final String ALIAS = "alias";
45
46     private static final String CUSTOMARR = "customArr";
47
48     private static final String ITEMTEXT = "itemtext_1";
49
50     //付款信息自定义属性
51     private static final String PAYINFO = "payInfos";
52
53     //当前费用类型
54     private String costType;
55
56     @Override
57     public List<Account> getAccount() {
58         Object o = MasterFtpServiceImpl.RPC(AdminUrlConst.ACCOUNT_SELECTBYDATA,null);
59         return o!=null?JSONTool.toList(JSONTool.toJson(o),Account.class):new ArrayList<Account>();
60     }
61
62     @Override
63     public int updateAccountState(Integer id,Integer state,String orderCode) {
64         Account account = new Account();
65         account.setId(id);
66         account.setOrderCode(orderCode);
67         account.updateIngParam();
68         account.setDataStart(state);
69 //        String url = state == 4 ? AdminUrlConst.ACCOUNT_DELETE : AdminUrlConst.ACCOUNT_UPDATESTATE;
70         String url = AdminUrlConst.ACCOUNT_UPDATESTATE;
71         Object o = MasterFtpServiceImpl.RPC(url,account);
72         return Integer.parseInt(Objects.toString(o,"-1"));
73     }
74
75     /**
76      * 开始记账,记账取参数规则,
77      */
78     public int buchfuhrung(Account account){
79         setTh(account);
80         String formId = account.getFormId();
81         String tenantId = account.getTenantId();
82         Integer dataRowNum = Integer.parseInt(account.getDataRowNum());
83
84         //首先请求表单结构
85         formatForm(JSONTool.toObj(account.getFieldSet(),Map.class));
86         //获取主表单
87         Map<String,Object> main = iFromOperate.selectSign(formId,tenantId,dataRowNum);
88         Object currOrderCode = main.entrySet().stream().filter(entity->entity.getKey().equalsIgnoreCase(ORDER_CODE)).map(Map.Entry::getValue).findFirst().orElse("");
89         //当前费用类型赋值
90         this.costType = Objects.toString(main.get(COST_TYPE),"0");
91         //主表数据填充
92         getFillData(main,account);
93         //记账接口参数
94         Map<String,Object> data = new HashMap<>();
95         //记账所需参数----------------->
96         //付款信息参数
97         List<Map<String,Object>> payInfo = getPayInfoSub(this.field.get(PAYINFO), formId, tenantId, Integer.parseInt(account.getDataRowNum()),main);
98         Map<String,Object> accountData = JSONTool.toObj(JSONTool.toJson(new GmAccountData()),Map.class);
99         accountData.put(ORDER_CODE,currOrderCode);
100         //填充参数
101         getFormMain(main,JSONTool.toObj(JSONTool.toJson(new GmAccountData()),Map.class),accountData);
102         Map<String,Object> orderData = JSONTool.toObj(JSONTool.toJson(new OrderData()),Map.class);
103         getFormMain(main,JSONTool.toObj(JSONTool.toJson(new OrderData()),Map.class),orderData);
104         getFillData(orderData,account);
105         //accountData参数
106         accountData.put(PAYINFO, payInfo);
107
108         //orderData参数
109
110         //是否有票据
111         data.put("orderCodeFlag",ObjectUtil.notEmpty(currOrderCode)?1:0);
112         data.put("tenantId",tenantId);
113         data.put("source", 8);
114         data.put("orderData",orderData);
115         data.put("accountData", accountData);
116         data.put("openId",account.getOpenId());
117         log.debug("开始记账, formId:{}, dataRowNum:{}, orderCode:{}, data:{}", formId, dataRowNum, currOrderCode, JSONTool.toJson(data));
118         Map<String,Object> res =  cscApiService.orderAccount(data);
119         Object orderCode = res.get(ORDER_CODE);
120         FormDataEntity formDataEntity = new FormDataEntity(formId ,dataRowNum,tenantId);
121        if(orderCode !=null && !"".equals(orderCode)){
122            log.debug("当前订单号:"+orderCode);
123            log.debug("记账成功!");
124            this.updateAccountState(account.getId(),4,orderCode.toString());
125            //修改表单状态 为记账中
126            formDataEntity.setMap(new ModelMap(ORDERSTATUS,35)
127                    .addAttribute(ORDER_CODE,orderCode));
128            iFromOperate.updateFormData(formDataEntity);
129            return 1;
130        }else{
131            log.debug("记账异常!");
132            log.debug("res:"+ JSONTool.toJson(res));
133            this.updateAccountState(account.getId(),8,null);
134            formDataEntity.setMap(new ModelMap(ORDERSTATUS,49));
135            iFromOperate.updateFormData(formDataEntity);
136            return -1;
137        }
138         //解析表单结构
139
140
141     }
142
143     /**
144      * 获取表单自定义属性
145      * @param
146      * @return
147      */
148     public void formatForm(Map<String,Object> data){
149         Map<String,String> res = new HashMap<>();
150         data.keySet().stream().forEach(k->{
151             Map<String,Object> map1 = JSONTool.toObj(data.get(k).toString(),Map.class);
152             // System.out.println(JSONTool.toJson(map1));
153             Map<String,Object> map2 = JSONTool.toObj(JSONTool.toJson(map1.get(TAGATTRIBUTE)),Map.class);
154             if(map2.get(CUSTOMARR) != null && !"".equals(map2.get(CUSTOMARR))) {
155                 List<Map> custs = JSONTool.toList(JSONTool.toJson(map2.get(CUSTOMARR)),Map.class);
156                 log.debug("表单自定义属性:"+custs);
157                 if(custs.size()>0) {
158                     custs.stream().forEach(c->{
159                         res.put(Objects.toString(c.get(ITEMTEXT)), Objects.toString(map2.get(ALIAS), ""));
160                     });
161                 }
162
163             }
164         });
165         this.field = res;
166     }
167
168     /**
169      * 获取付款信息参数
170      * @param
171      */
172     public List<Map<String,Object>> getPayInfoSub(String subAlias,String formId,String tenantId,Integer dataRowNum,Map<String,Object> main){
173         List<Map<String,Object>> payInfos = new ArrayList<Map<String,Object>>();
174         //首先判断是子表还是子表单
175         Map<String,String> map = JSONTool.toObj(JSONTool.toJson(new GmPayInfo()),Map.class);
176         if(subAlias!=null && !"".equals(subAlias)){
177             //取出子表单集合
178             List<Map>li = iFromOperate.selectSubList(formId,tenantId,dataRowNum,subAlias);
179             li.stream().forEach(o->{
180                 Map<String,Object> m = new HashMap<>();
181                 map.keySet().stream().forEach(c->{
182                     m.put(c,o.get(this.field.get(c)));
183                 });
184                 payInfos.add(m);
185             });
186         }else{
187             Map<String,Object> m = new HashMap<>();
188             getFormMain(main,map,m);
189             payInfos.add(m);
190         }
191         return payInfos;
192     }
193
194     /**
195      * 筛选主表数据,,参数1-主表数据,参数2需要字段,参数3获取参数填充
196      */
197     public void getFormMain(Map<String,Object> data,Map<String,String> fie,Map<String,Object> body){
198         fie.keySet().stream()
199                 .filter(f->data.get(this.field.get(f))!=null&&!"".equals(data.get(this.field.get(f))) )
200                 .forEach(c->
201             body.put(c,data.get(this.field.get(c)))
202         );
203     }
204
205     /**
206      * 填充数据,组织机构相关参数
207      * @param
208      */
209     public void getFillData(Map<String,Object> main,Account account){
210         //填充参数,orgCode,tenantId,companyId,companyCode,orderTypeCode
211         log.debug("当前费用类型id:"+this.costType);
212         log.debug("开始填充参数:--->");
213         getOrgData(account.getTenantId(),account.getCompanyId(),account.getOpenId(),main);
214         main.put("tenantId",account.getTenantId());
215         //需要云裳行所需id
216 //        main.put("companyId",account.getCompanyId());
217         main.put("orderTypeCode", iFromOperate.getCostCode(Integer.parseInt(this.costType)));
218         log.debug("orderData填充后:"+ JSONTool.toJson(main));
219     }
220
221     //获取组织机构相关参数
222     public void getOrgData(String tenantId,String companyId,String openId,Map<String,Object> data){
223         ThreadData.set(BaseConst.OPEN_ID,openId);
224         TenantsAndCompanys tenants = cscApiService.findTenantsAndCompanys();
225         tenants.getTenants()
226                 .stream()
227                     .filter(fi->tenantId.equals(Objects.toString(fi.getId(),"")))
228                         .forEach(o->{
229                             o.getCompanys()
230                                     .stream()
231                                         .filter(f->companyId.equals(Objects.toString(f.getId(),"")))
232                                             .forEach(c->{
233                                                 data.put("companyCode",c.getCode());
234                                                 data.put("orgCode",c.getOrgCode());
235                                                 data.put("hotInfoCompanyId",c.getHotInfoCompanyId());
236                                             });
237                     });
238     }
239     
240     /**
241      * 当前线程添加参数
242      */
243     public void setTh(Account account){
244         ThreadData.set(Keys.COMPANY_ID,account.getCompanyId());
245         ThreadData.set(Keys.TENANT_ID,account.getTenantId());
246         ThreadData.set(BaseConst.TENANT_ID,account.getTenantId());
247         ThreadData.set(BaseConst.COMPANY_ID,Integer.parseInt(account.getCompanyId()));
248     }
249
250 }