package com.malk.siku.service.impl; import com.alibaba.fastjson.JSONObject; import com.malk.server.aliwork.YDConf; import com.malk.server.aliwork.YDParam; import com.malk.server.common.McR; import com.malk.service.aliwork.YDClient; import com.malk.siku.service.SikuService; import com.malk.siku.utils.MkBxUtil; import com.malk.siku.utils.MkYpUtil; import com.malk.utils.UtilHttp; import com.malk.utils.UtilMap; import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.util.Strings; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.*; @Slf4j @Service public class SikuServiceImpl implements SikuService { @Autowired private YDClient ydClient; @Override public McR saveTradingPartner(Map map) { String formInstId = UtilMap.getString(map, "formInstId"); String type = UtilMap.getString(map, "type"); Map formData = ydClient.queryData(YDParam.builder() .formInstanceId(formInstId) .build(), YDConf.FORM_QUERY.retrieve_id).getFormData(); Map partner = new HashMap(); if ("客户".equals(type)){ //客户 partner.put("partnerName", "客户");//所属分类名称 partner.put("name", UtilMap.getString(formData,"textField_mkddjwy3"));//往来单位中文名 partner.put("partnerType", "客户");//往来关系,可选值为:"供应商"、"客户"、"供应商,客户"(既是供应商又是客户的情况下以中文逗号分隔(供应商在前)) partner.put("partnerProperty", "公司");//往来性质,可选值为:"公司"、"个体工商户" partner.put("businessCode", UtilMap.getString(formData,"serialNumberField_mkddjwy2"));//往来单位编码,不超过50个字符 partner.put("taxNumber", UtilMap.getString(formData,"textField_mkddjwys"));//税务登记号,不超过200个字符 partner.put("readOnly", false);//是否只读,默认false。如果为true,那么在每刻系统中往来单位的所有信息(如权限范围、收款账户等)均无法修改。(接口更新操作不受限制) partner.put("reMark", "");//往来单位备注 partner.put("enabled", true);//是否启用(true:启用;false:禁用)。若该参数为空,新增则默认为true,更新则保持系统内状态。 }else { //供应商、 String hzlx = UtilMap.getString(formData, "radioField_mketkvw7"); partner.put("partnerName", "供应商");//所属分类名称 partner.put("name", "Free".equals(hzlx) ? UtilMap.getString(formData,"textField_mkkixdd6") : UtilMap.getString(formData,"textField_mketkvv0"));//往来单位中文名 partner.put("partnerType", "供应商");//往来关系,可选值为:"供应商"、"客户"、"供应商,客户"(既是供应商又是客户的情况下以中文逗号分隔(供应商在前)) partner.put("partnerProperty", "Free".equals(hzlx) ? "个体工商户" : "公司");//往来性质,可选值为:"公司"、"个体工商户" partner.put("businessCode", UtilMap.getString(formData,"serialNumberField_mkddjwy2"));//往来单位编码,不超过50个字符 partner.put("taxNumber", "Free".equals(hzlx) ? "" : UtilMap.getString(formData,"textField_mketkvxo"));//税务登记号,不超过200个字符 partner.put("readOnly", false);//是否只读,默认false。如果为true,那么在每刻系统中往来单位的所有信息(如权限范围、收款账户等)均无法修改。(接口更新操作不受限制) partner.put("reMark", "");//往来单位备注 partner.put("enabled", UtilMap.getBoolean(formData,"radioField_mketkvw8"));//是否启用(true:启用;false:禁用)。若该参数为空,新增则默认为true,更新则保持系统内状态。 } Map body = new HashMap(); body.put("partnerList", Arrays.asList(partner)); Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://ng.maycur.com/api/openapi/tradingPartner/batch/save", MkBxUtil.initTokenHeader(), null, body)); log.info("Result:",result); return McR.success(); } @Override public McR saveYpApplication(Map map) { String formInstId = UtilMap.getString(map, "formInstId"); Map formData = ydClient.queryData(YDParam.builder() .formInstanceId(formInstId) .build(), YDConf.FORM_QUERY.retrieve_id).getFormData(); Map application = new HashMap(); application.put("formSubTypeBizCode","KPSQD");//单据编码(云票系统内维护的) application.put("bizCode", UtilMap.getString(formData,"serialNumberField_mknspz75"));//外部系统中的开票申请单业务编码。必须系统内唯一,用于判断开票申请单是否已导入。 Map clientLegalEntityOaDto = new HashMap();//客户开票信息对象 clientLegalEntityOaDto.put("invoiceTitle",UtilMap.getString(formData,"selectField_mkxmix7e"));//名称 clientLegalEntityOaDto.put("dutyParagraph",UtilMap.getString(formData,"textField_mm2wj87l"));//税号 clientLegalEntityOaDto.put("account",UtilMap.getString(formData,"textField_mm2wj87u"));//账号 clientLegalEntityOaDto.put("bankBranchName",UtilMap.getString(formData,"textField_mm2wj87t"));//银行 application.put("clientLegalEntityOaDto",clientLegalEntityOaDto); application.put("legalEntityBizCode",UtilMap.getString(formData,"textField_mm2wj87x"));//公司主体code(后续传税号) application.put("estimatedDate",System.currentTimeMillis());//预计开票时间 String fplx = UtilMap.getString(formData, "selectField_mkdnuvem");//发票类型 String estimatedInvoiceType = ""; switch (fplx){ case "应/免税普票":estimatedInvoiceType = "ELECTRONIC_VAT";break;//增值税普通发票(电子) case "专票":estimatedInvoiceType = "ELECTRONIC_SPACIAL_VAT";break;//增值税电子专用发票 case "红冲":break; case "形式发票":break; case "收据":break; default:break; } application.put("estimatedInvoiceType",estimatedInvoiceType);//发票类型 application.put("phoneNumber",UtilMap.getString(formData,"textField_mkdo0azf"));//收票人电话 application.put("email",UtilMap.getString(formData,"textField_mkdo0azn"));//收票邮箱 application.put("applicationComment",UtilMap.getString(formData,"textareaField_mkdo0aze"));//开票备注 Map item = new HashMap(); item.put("productBizCode","1001");//产品bizcode item.put("invoiceName",UtilMap.getString(formData,"textField_mkgarw4d"));//开票名称 item.put("taxPercent",UtilMap.getDouble(formData,"numberField_mkgkboht"));//税率 item.put("quantity",1);//数量 item.put("forexName","CNY");//币种 item.put("exchangeRate",1);//外汇汇率 item.put("priceWithTax",UtilMap.getDouble(formData,"numberField_mkdo0azb"));//含税单价 item.put("taxClassShortCode",UtilMap.getString(formData,"textField_mm49zsli"));//税收分类编码 application.put("items",Arrays.asList(item)); Map result = MkYpUtil.application_oa(Arrays.asList(application)); return McR.success(); } @Override public void invoiceWriteBack(Map map) { //获取发票信息 Map calloutParams = UtilMap.getMap(map, "calloutParams"); Map bizData = UtilMap.getMap(calloutParams, "bizData"); String bizCode = UtilMap.getString(bizData, "bizCode");//发票号码 String invoiceNumber = UtilMap.getString(bizData, "invoiceNumber");//发票号码 String pdfUrl = UtilMap.getString(bizData, "pdfUrl");//pdf文件下载地址 String downloadPageUrl = UtilMap.getString(bizData, "downloadPageUrl");//发票下载页面链接 List detailItems = (List) UtilMap.getList(bizData, "detailItems"); List documentCorrelates = UtilMap.getList(detailItems.get(0), "documentCorrelates"); String lsh = UtilMap.getString(documentCorrelates.get(0), "bizCode");//开票申请单编号 //回写宜搭 Map formData = new HashMap(); formData.put("radioField_mkxripcc","开票成功"); formData.put("textField_mkxripc6",invoiceNumber); formData.put("textField_mm3aixk0",downloadPageUrl); if (Strings.isNotBlank(pdfUrl)){ String fileName = invoiceNumber + ".pdf"; String downloadPath = "d:\\" + fileName; downloadFile(pdfUrl,downloadPath);//下载pdf发票 String downloadUri = "http://localhost:8080/api/siku/files/"+fileName+"?option=download" + fileName; String previewUri = "http://localhost:8080/api/siku/files/"+fileName+"?option=preview" + fileName; Map attachmentField = new HashMap(); attachmentField.put("downloadUrl",downloadUri); attachmentField.put("name",fileName); attachmentField.put("previewUrl",previewUri); attachmentField.put("url",downloadUri); attachmentField.put("ext","pdf"); formData.put("attachmentField_mkxripc5",Arrays.asList(attachmentField)); } ydClient.operateData(YDParam.builder() .formInstanceId(bizCode) .searchCondition(JSONObject.toJSONString(UtilMap.map("serialNumberField_mknspz75",lsh))) .formUuid("FORM-A9A47B0365DB437F8F4C8E01B4468220K7GU") .formDataJson(JSONObject.toJSONString(formData)) .build(), YDConf.FORM_OPERATION.upsert); } //文件下载到本地 private void downloadFile(String downloadUri,String downloadPath){ try { URL url = new URL(downloadUri); HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); int responseCode = httpConn.getResponseCode(); // 检查HTTP响应代码是否为200 if (responseCode == HttpURLConnection.HTTP_OK) { InputStream inputStream = httpConn.getInputStream(); FileOutputStream outputStream = new FileOutputStream(downloadPath); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } outputStream.close(); inputStream.close(); } else { System.out.println("无法下载文件。HTTP响应代码: " + responseCode); } httpConn.disconnect(); }catch (Exception e){ throw new RuntimeException(e); } } }