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 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 getAccount() { Object o = MasterFtpServiceImpl.RPC(AdminUrlConst.ACCOUNT_SELECTBYDATA,null); return o!=null?JSONTool.toList(JSONTool.toJson(o),Account.class):new ArrayList(); } @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 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 data = new HashMap<>(); //记账所需参数-----------------> //付款信息参数 List> payInfo = getPayInfoSub(this.field.get(PAYINFO), formId, tenantId, Integer.parseInt(account.getDataRowNum()),main); Map 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 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 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 data){ Map res = new HashMap<>(); data.keySet().stream().forEach(k->{ Map map1 = JSONTool.toObj(data.get(k).toString(),Map.class); // System.out.println(JSONTool.toJson(map1)); Map map2 = JSONTool.toObj(JSONTool.toJson(map1.get(TAGATTRIBUTE)),Map.class); if(map2.get(CUSTOMARR) != null && !"".equals(map2.get(CUSTOMARR))) { List 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> getPayInfoSub(String subAlias,String formId,String tenantId,Integer dataRowNum,Map main){ List> payInfos = new ArrayList>(); //首先判断是子表还是子表单 Map map = JSONTool.toObj(JSONTool.toJson(new GmPayInfo()),Map.class); if(subAlias!=null && !"".equals(subAlias)){ //取出子表单集合 Listli = iFromOperate.selectSubList(formId,tenantId,dataRowNum,subAlias); li.stream().forEach(o->{ Map m = new HashMap<>(); map.keySet().stream().forEach(c->{ m.put(c,o.get(this.field.get(c))); }); payInfos.add(m); }); }else{ Map m = new HashMap<>(); getFormMain(main,map,m); payInfos.add(m); } return payInfos; } /** * 筛选主表数据,,参数1-主表数据,参数2需要字段,参数3获取参数填充 */ public void getFormMain(Map data,Map fie,Map 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 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 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())); } }