| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409 |
- package com.malk.qiwang.Service.impl;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.malk.qiwang.Service.QiWangService;
- import com.malk.qiwang.Service.TXYInvoice;
- import com.malk.qiwang.entity.CompanyTitle;
- import com.malk.qiwang.entity.InvoiceLibrary;
- import com.malk.qiwang.mapper.CompanyTitleMapper;
- import com.malk.qiwang.mapper.InvoiceLibraryMapper;
- import com.malk.qiwang.Service.IInvoiceLibraryService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.malk.qiwang.model.McInvoiceDto;
- import com.malk.server.common.FilePath;
- import com.malk.server.common.McException;
- import com.malk.server.common.McR;
- import com.malk.server.dingtalk.DDR_New;
- import com.malk.service.aliwork.YDClient;
- import com.malk.service.dingtalk.DDClient;
- import com.malk.service.dingtalk.DDClient_Contacts;
- import com.malk.service.dingtalk.DDClient_Workflow;
- import com.malk.utils.*;
- import com.spire.pdf.PdfCompressionLevel;
- import com.spire.pdf.PdfDocument;
- import com.spire.pdf.PdfPageBase;
- import com.spire.pdf.exporting.PdfImageInfo;
- import com.spire.pdf.graphics.PdfBitmap;
- import com.tencentcloudapi.common.exception.TencentCloudSDKException;
- import lombok.SneakyThrows;
- import lombok.extern.slf4j.Slf4j;
- import net.coobird.thumbnailator.Thumbnails;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.poi.hpsf.Decimal;
- import org.apache.poi.util.IOUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.UrlResource;
- import org.springframework.http.HttpEntity;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.MediaType;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Service;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.client.RestTemplate;
- import java.io.*;
- import java.math.BigDecimal;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.time.LocalDate;
- import java.time.LocalDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.*;
- import java.util.concurrent.CompletableFuture;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 发票库表 服务实现类
- * </p>
- *
- * @author LQY
- * @since 2026-04-27
- */
- @Service
- @Slf4j
- public class InvoiceLibraryServiceImpl extends ServiceImpl<InvoiceLibraryMapper, InvoiceLibrary> implements IInvoiceLibraryService {
- @Autowired
- private QiWangService qiWangService;
- @Autowired
- private DDClient ddClient;
- @Autowired
- private YDClient ydClient;
- @Autowired
- private TXYInvoice txyInvoice;
- @Value("${dingtalk.operator}")
- private String operator;
- @Value("${dingtalk.downloadPath}")
- private String downloadPath;
- @Autowired
- private FilePath filePath;
- @Autowired
- private DDClient_Workflow ddClientWorkflow;
- @Autowired
- private InvoiceLibraryMapper baseMapper;
- @Autowired
- private CompanyTitleMapper companyTitleMapper;
- @Autowired
- private DDClient_Contacts ddClient_contacts;
- private static final String url = "http://47.103.203.2:9092/qiwang/";
- // private static final String url = "http://410a09c5.r39.cpolar.top/qiwang/";
- @Override
- public McR invoiceLibrary(Map map) {
- log.info("接收到的参数: {}", map);
- try {
- String processInstanceId = UtilMap.getString(map, "processInstanceId");
- if (StringUtils.isBlank(processInstanceId)) {
- log.error("processInstanceId为空");
- return McR.error("400", "审批实例ID不能为空");
- }
- // 获取审批实例信息
- String accessToken = ddClient.getAccessToken();
- Map processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, processInstanceId);
- if (processInstance == null) {
- log.error("获取审批实例失败: {}", processInstanceId);
- return McR.error("500", "获取审批实例信息失败");
- }
- String userId = (String) processInstance.get("originatorUserId");
- String businessId = (String) processInstance.get("businessId");
- log.info("审批人ID: {}", userId);
- List<Map> formComponentValues = (List<Map>) processInstance.get("formComponentValues");
- if (formComponentValues == null || formComponentValues.isEmpty()) {
- log.warn("表单数据为空");
- return McR.error("400", "表单数据为空");
- }
- // 存储主表的字段值
- String bm = null;
- String bxlb = null;
- String fkzhxx = null;
- String fkzh = null;
- String yhqc = null;
- String skzh = null;
- BigDecimal mainJe = null;
- // 遍历收集主表字段值
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- Object value = formComponentValue.get("value");
- if ("MoneyField_3QZLY8BD3780".equals(id)) {
- if (value instanceof BigDecimal) {
- mainJe = (BigDecimal) value;
- } else if (value != null) {
- try {
- mainJe = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- mainJe = BigDecimal.ZERO;
- }
- }
- }
- if ("RecipientAccountField_1FGFBO5HL1PC0".equals(id)) {
- skzh = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_1TP75OVCPAAO0".equals(id)) {
- bxlb = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_1RD2SROH579C0".equals(id)) {
- bm = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_11D6S45JKFHS0".equals(id)) {
- fkzhxx = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_1S1VV9PIIQWW0".equals(id)) {
- fkzh = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_ZUVAAH3FIGW0".equals(id)) {
- yhqc = value != null ? String.valueOf(value) : "";
- }
- }
- // 遍历子表数据
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- if ("TableField_1A1CDMEN8DDS0".equals(id)) {
- String tableFieldValue = String.valueOf(formComponentValue.get("value"));
- log.info("子表数据: {}", tableFieldValue);
- try {
- List<Map> tableRows = (List<Map>) JSONObject.parse(tableFieldValue);
- if (tableRows == null || tableRows.isEmpty()) {
- log.warn("子表数据为空");
- return McR.error("400", "发票明细为空");
- }
- // 用于存储无发票记录和发票记录
- List<InvoiceLibrary> noInvoiceList = new ArrayList<>();
- List<InvoiceLibrary> invoiceList = new ArrayList<>();
- List<Object> allResults = new ArrayList<>();
- // 存储有发票("是"和"被共享")的解析数据
- List<Map<String, Object>> parsedInvoiceDataList = new ArrayList<>();
- // 存储共享发票数据
- List<Map<String, Object>> sharedInvoiceDataList = new ArrayList<>();
- // 用于校验的集合
- List<String> invoiceNumberList = new ArrayList<>();
- Map<String, String> invoiceNumberToFileName = new HashMap<>();
- List<String> invalidBuyerTaxIds = new ArrayList<>();
- // 存储税率信息(从"被共享"发票中获取)
- BigDecimal sharedTaxRate = BigDecimal.ZERO;
- String sharedKind = "";
- boolean hasSharedInvoice = false; // 标记是否存在"被共享"发票
- // ========== 第一遍遍历:收集数据并查找"被共享"发票 ==========
- for (Map row : tableRows) {
- List<Map> rowValues = (List<Map>) row.get("rowValue");
- String hasInvoice = null;
- String cdbm = null;
- String lbmx = null;
- String bz = null;
- BigDecimal detailje = null;
- List<Map> attachmentList = null;
- for (Map rowItem : rowValues) {
- String key = String.valueOf(rowItem.get("key"));
- if ("DDSelectField_1ORUK0KIM5D6O".equals(key)) {
- Object value = rowItem.get("value");
- hasInvoice = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_12SQMP8DSUF40".equals(key)) {
- Object value = rowItem.get("value");
- lbmx = value != null ? String.valueOf(value) : "";
- }
- if ("TextareaField_1GRJNUC4BAE80".equals(key)) {
- Object value = rowItem.get("value");
- bz = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_ATSWRJBJ7K00".equals(key)) {
- Object value = rowItem.get("value");
- cdbm = value != null ? String.valueOf(value) : "";
- }
- if ("NumberField_1622XLFLYEWW0".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof BigDecimal) {
- detailje = (BigDecimal) value;
- } else if (value != null) {
- try {
- detailje = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- detailje = BigDecimal.ZERO;
- }
- } else {
- detailje = BigDecimal.ZERO;
- }
- }
- if ("DDAttachment_Z02OGR5QL8U8".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof List) {
- attachmentList = (List<Map>) value;
- }
- }
- }
- // ========== 处理 hasInvoice = "否" 的情况(无发票) ==========
- if ("否".equals(hasInvoice)) {
- // 保存基础报销记录(无发票)
- InvoiceLibrary baseInvoice = new InvoiceLibrary();
- baseInvoice.setOaId(processInstanceId);
- baseInvoice.setSkzh(skzh);
- baseInvoice.setBusinessId(businessId);
- baseInvoice.setOaStatus("0");
- baseInvoice.setInvoiceStatus("0");
- baseInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- baseInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- baseInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- baseInvoice.setDetailAmount(detailje != null ? detailje : BigDecimal.ZERO);
- baseInvoice.setCreatedAt(LocalDateTime.now());
- baseInvoice.setUpdatedAt(LocalDateTime.now());
- baseInvoice.setInvoiceCode("");
- baseInvoice.setInvoiceNumber("");
- baseInvoice.setInvoiceType("");
- baseInvoice.setAmount(null);
- baseInvoice.setTaxAmount(null);
- baseInvoice.setTotalAmount(null);
- baseInvoice.setBuyerName("");
- baseInvoice.setUserId(userId);
- baseInvoice.setBuyerTaxId("");
- baseInvoice.setSellerName("");
- baseInvoice.setSellerTaxId("");
- baseInvoice.setFormName("员工费用报销");
- baseInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- baseInvoice.setPayAccount(fkzh != null ? fkzh : "");
- baseInvoice.setBankName(yhqc != null ? yhqc : "");
- baseInvoice.setHasInvoice("否");
- baseInvoice.setSharedTaxAmount(null);
- baseInvoice.setSharedAmount(null);
- baseInvoice.setSharedRate(null);
- baseInvoice.setNote(bz != null ? bz : "");
- noInvoiceList.add(baseInvoice);
- log.info("收集无发票记录: 成本部门={}, 费用明细={}", cdbm, lbmx);
- continue;
- }
- // ========== 处理 hasInvoice = "被共享"(被共享发票,提供税率) ==========
- if ("被共享".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- // 标记存在被共享发票
- hasSharedInvoice = true;
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- // 解析发票数据
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- // ========== 获取税率(供共享发票使用) ==========
- Object taxRateObj = invoice.get("taxRate");
- Object kind = invoice.getString("kindName");
- if (taxRateObj != null) {
- String taxRateStr = String.valueOf(taxRateObj);
- String taxRateNum = taxRateStr.replace("%", "").trim();
- sharedKind = kind != null ? String.valueOf(kind) : "";
- sharedTaxRate = new BigDecimal(taxRateNum);
- sharedTaxRate = sharedTaxRate.divide(new BigDecimal("100"), 10, BigDecimal.ROUND_HALF_UP);
- log.info("从被共享发票获取税率: 原始={}, 转换后={}", taxRateStr, sharedTaxRate);
- }
- // 保存解析的数据供后续使用
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // ========== 校验1:抬头校验 ==========
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // ========== 校验2:发票号重复校验 ==========
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("被共享发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "被共享发票解析失败: " + e.getMessage());
- return McR.error("400", "被共享发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "是"(有发票,但不共享) ==========
- if ("是".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // 抬头校验
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // 发票号重复校验
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "发票解析失败: " + e.getMessage());
- return McR.error("400", "发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "共享"(共享发票,需要从被共享发票获取税率) ==========
- if ("共享".equals(hasInvoice)) {
- if (detailje != null && detailje.compareTo(BigDecimal.ZERO) > 0) {
- Map<String, Object> sharedData = new HashMap<>();
- sharedData.put("detailje", detailje);
- sharedData.put("lbmx", lbmx);
- sharedData.put("bz", bz);
- sharedData.put("cdbm", cdbm);
- sharedData.put("hasInvoice", hasInvoice);
- sharedInvoiceDataList.add(sharedData);
- log.info("收集共享发票数据: 金额={}, 成本部门={}", detailje, cdbm);
- }
- }
- }
- // ========== 检查:如果存在共享发票但没有被共享发票 ==========
- if (!sharedInvoiceDataList.isEmpty() && !hasSharedInvoice) {
- String errorMsg = "存在'共享'发票,但未找到'被共享'发票,无法获取税率信息,请确保至少有一行选择'被共享'并上传发票附件";
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- // ========== 抬头校验失败处理 ==========
- if (!invalidBuyerTaxIds.isEmpty()) {
- String errorMsg = "发票抬头有误,以下发票的购买方税号不在公司抬头库中:\n" + String.join("\n", invalidBuyerTaxIds);
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- BigDecimal totalAmount = BigDecimal.ZERO;
- // ========== 第二遍遍历:保存发票数据 ==========
- // 1. 保存有发票数据("是"和"被共享")
- for (Map<String, Object> parsedData : parsedInvoiceDataList) {
- JSONObject invoice = (JSONObject) parsedData.get("invoice");
- Object responseData = parsedData.get("responseData");
- String cdbm = (String) parsedData.get("cdbm");
- String lbmx = (String) parsedData.get("lbmx");
- String bz = (String) parsedData.get("bz");
- String hasInvoice = (String) parsedData.get("hasInvoice");
- Object detailjeObj = parsedData.get("detailje");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- allResults.add(responseData);
- BigDecimal amount = invoice.getBigDecimal("amount");
- if (amount != null) {
- totalAmount = totalAmount.add(amount);
- }
- InvoiceLibrary invoiceLibrary = new InvoiceLibrary();
- invoiceLibrary.setOaId(processInstanceId);
- invoiceLibrary.setSkzh(skzh);
- invoiceLibrary.setBusinessId(businessId);
- invoiceLibrary.setInvoiceCode(invoice.getString("code") != null ? invoice.getString("code") : "");
- invoiceLibrary.setInvoiceNumber(invoice.getString("serial") != null ? invoice.getString("serial") : "");
- invoiceLibrary.setInvoiceType(invoice.getString("kindName") != null ? invoice.getString("kindName") : "");
- String date = invoice.getString("date");
- if (StringUtils.isNotBlank(date)) {
- try {
- invoiceLibrary.setInvoiceDate(LocalDate.parse(date.trim()));
- } catch (Exception e) {
- log.warn("日期解析失败: {}", date);
- }
- }
- invoiceLibrary.setAmount(invoice.getBigDecimal("excludingTax") != null ? invoice.getBigDecimal("excludingTax") : BigDecimal.ZERO);
- invoiceLibrary.setTaxAmount(invoice.getBigDecimal("tax") != null ? invoice.getBigDecimal("tax") : BigDecimal.ZERO);
- invoiceLibrary.setTotalAmount(invoice.getBigDecimal("amount") != null ? invoice.getBigDecimal("amount") : BigDecimal.ZERO);
- invoiceLibrary.setBuyerName(invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "");
- invoiceLibrary.setBuyerTaxId(invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "");
- invoiceLibrary.setSellerName(invoice.getString("sellerName") != null ? invoice.getString("sellerName") : "");
- invoiceLibrary.setSellerTaxId(invoice.getString("sellerTaxId") != null ? invoice.getString("sellerTaxId") : "");
- invoiceLibrary.setOaStatus("0");
- invoiceLibrary.setNote(bz != null ? bz : "");
- invoiceLibrary.setFormName("员工费用报销");
- invoiceLibrary.setUserId(userId);
- invoiceLibrary.setPaySubject(fkzhxx != null ? fkzhxx : "");
- invoiceLibrary.setPayAccount(fkzh != null ? fkzh : "");
- invoiceLibrary.setBankName(yhqc != null ? yhqc : "");
- invoiceLibrary.setInvoiceStatus("0");
- invoiceLibrary.setHasInvoice(hasInvoice);
- if (StringUtils.isNotBlank(detailje)) {
- try {
- invoiceLibrary.setDetailAmount(new BigDecimal(detailje));
- } catch (NumberFormatException e) {
- log.error("detailJe格式转换失败: {}", detailje);
- invoiceLibrary.setDetailAmount(null);
- }
- } else {
- invoiceLibrary.setDetailAmount(null);
- }
- invoiceLibrary.setSharedTaxAmount(null);
- invoiceLibrary.setSharedAmount(null);
- invoiceLibrary.setSharedRate(null);
- invoiceLibrary.setAccountTitle(lbmx != null ? lbmx : "");
- invoiceLibrary.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- invoiceLibrary.setCreatedAt(LocalDateTime.now());
- invoiceLibrary.setUpdatedAt(LocalDateTime.now());
- if (cdbm != null && StringUtils.isNotBlank(cdbm)) {
- invoiceLibrary.setDep(cdbm);
- } else {
- invoiceLibrary.setDep(bm != null ? bm : "");
- }
- invoiceList.add(invoiceLibrary);
- }
- // 2. 保存共享发票数据(使用从"被共享"发票获取的税率)
- if (!sharedInvoiceDataList.isEmpty()) {
- // 检查税率是否有效
- if (sharedTaxRate.compareTo(BigDecimal.ZERO) == 0) {
- log.warn("共享发票税率未获取到,使用默认税率0");
- }
- for (Map<String, Object> sharedData : sharedInvoiceDataList) {
- Object detailjeObj = sharedData.get("detailje");
- String cdbm = (String) sharedData.get("cdbm");
- String lbmx = (String) sharedData.get("lbmx");
- String bz = (String) sharedData.get("bz");
- String hasInvoice = (String) sharedData.get("hasInvoice");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- try {
- BigDecimal detailAmount = new BigDecimal(detailje);
- // 使用从"被共享"发票解析到的税率
- BigDecimal ocrTaxRate = sharedTaxRate;
- // 计算金额: 含税金额 / (1 + 税率) = 不含税金额
- BigDecimal divisor = BigDecimal.ONE.add(ocrTaxRate);
- BigDecimal sharedTaxAmount = detailAmount.divide(divisor, 2, BigDecimal.ROUND_HALF_UP);
- BigDecimal sharedAmount = detailAmount.subtract(sharedTaxAmount);
- BigDecimal sharedRate = ocrTaxRate;
- log.info("共享发票计算: 含税金额={}, 税率={}%, 不含税金额={}, 税额={}",
- detailAmount, ocrTaxRate.multiply(new BigDecimal("100")),
- sharedAmount, sharedTaxAmount);
- InvoiceLibrary sharedInvoice = new InvoiceLibrary();
- sharedInvoice.setOaId(processInstanceId);
- sharedInvoice.setSkzh(skzh);
- sharedInvoice.setBusinessId(businessId);
- sharedInvoice.setOaStatus("0");
- sharedInvoice.setInvoiceStatus("0");
- sharedInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- sharedInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- sharedInvoice.setNote(bz != null ? bz : "");
- sharedInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- sharedInvoice.setAmount(null);
- sharedInvoice.setTaxAmount(null);
- sharedInvoice.setTotalAmount(null);
- sharedInvoice.setUserId(userId);
- sharedInvoice.setFormName("员工费用报销");
- sharedInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- sharedInvoice.setPayAccount(fkzh != null ? fkzh : "");
- sharedInvoice.setBankName(yhqc != null ? yhqc : "");
- sharedInvoice.setHasInvoice(hasInvoice);
- sharedInvoice.setDetailAmount(detailAmount);
- sharedInvoice.setSharedTaxAmount(sharedAmount);
- sharedInvoice.setSharedAmount(sharedTaxAmount);
- sharedInvoice.setSharedRate(String.valueOf(sharedRate));
- sharedInvoice.setCreatedAt(LocalDateTime.now());
- sharedInvoice.setUpdatedAt(LocalDateTime.now());
- sharedInvoice.setInvoiceCode("");
- sharedInvoice.setInvoiceNumber("");
- sharedInvoice.setInvoiceType(sharedKind);
- sharedInvoice.setBuyerName("");
- sharedInvoice.setBuyerTaxId("");
- sharedInvoice.setSellerName("");
- sharedInvoice.setSellerTaxId("");
- invoiceList.add(sharedInvoice);
- log.info("成功保存共享发票记录,成本部门: {}", cdbm);
- } catch (NumberFormatException e) {
- log.error("共享发票金额格式转换失败: detailje={}", detailje, e);
- } catch (Exception e) {
- log.error("共享发票处理失败", e);
- }
- }
- }
- // 保存所有记录(无发票 + 有发票)
- if (!noInvoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : noInvoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条无发票记录", noInvoiceList.size());
- addWorkflowComment(processInstanceId, userId, "报销总金额:" + mainJe);
- }
- if (!invoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : invoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条发票记录(有发票: {}, 共享发票: {})",
- invoiceList.size(),
- parsedInvoiceDataList.stream().filter(d -> "是".equals(d.get("hasInvoice")) || "被共享".equals(d.get("hasInvoice"))).count(),
- sharedInvoiceDataList.size());
- addWorkflowComment1(processInstanceId, userId, totalAmount);
- }
- if (!allResults.isEmpty()) {
- return McR.success(allResults);
- } else {
- return McR.success();
- }
- } catch (Exception e) {
- log.error("解析子表数据失败", e);
- return McR.error("300", "解析数据失败: " + e.getMessage());
- }
- }
- }
- return McR.success();
- } catch (Exception e) {
- log.error("处理审批实例失败", e);
- return McR.error("500", "系统处理失败: " + e.getMessage());
- }
- }
- /**
- * 终止钉钉审批流程
- *
- * @param accessToken 钉钉accessToken
- * @param processInstanceId 审批实例ID
- * @param operatingUserId 操作人用户ID(通常是发起人ID)
- * @param remark 终止原因备注
- */
- private void terminateWorkflow(String accessToken, String processInstanceId, String operatingUserId, String remark) {
- // 异步延迟15秒后执行终止
- CompletableFuture.runAsync(() -> {
- try {
- log.info("延迟15秒后开始终止审批流程: processInstanceId={}, remark={}", processInstanceId, remark);
- Thread.sleep(15000);
- // 实际执行终止操作
- doTerminateWorkflow(accessToken, processInstanceId, operatingUserId, remark);
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- log.error("延迟终止流程被中断: processInstanceId={}", processInstanceId, e);
- } catch (Exception e) {
- log.error("异步执行终止流程异常: processInstanceId={}", processInstanceId, e);
- }
- });
- }
- /**
- * 实际执行钉钉审批流程终止
- */
- private void doTerminateWorkflow(String accessToken, String processInstanceId, String operatingUserId, String remark) {
- try {
- log.info("开始调用钉钉API终止审批流程: processInstanceId={}", processInstanceId);
- // 构建请求URL
- String url = "https://api.dingtalk.com/v1.0/workflow/processInstances/terminate";
- // 构建请求头
- HttpHeaders headers = new HttpHeaders();
- headers.setContentType(MediaType.APPLICATION_JSON);
- headers.set("x-acs-dingtalk-access-token", accessToken);
- // 构建请求体
- Map<String, Object> requestBody = new HashMap<>();
- requestBody.put("processInstanceId", processInstanceId);
- requestBody.put("isSystem", true); // 系统调用终止
- requestBody.put("remark", remark);
- requestBody.put("operatingUserId", operatingUserId);
- log.info("钉钉终止请求参数: {}", requestBody);
- // 发送POST请求
- HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
- RestTemplate restTemplate = new RestTemplate();
- ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
- if (response.getStatusCode().is2xxSuccessful()) {
- log.info("审批流程终止成功: processInstanceId={}, response={}", processInstanceId, response.getBody());
- } else {
- log.error("审批流程终止失败: processInstanceId={}, statusCode={}, response={}",
- processInstanceId, response.getStatusCode(), response.getBody());
- }
- } catch (Exception e) {
- log.error("调用钉钉终止流程接口异常: processInstanceId={}, error={}", processInstanceId, e.getMessage(), e);
- }
- }
- @Override
- public void updateOaStatusByOaId(Map map) {
- try {
- String oaId = UtilMap.getString(map, "oaId");
- // 创建更新条件
- LambdaUpdateWrapper<InvoiceLibrary> updateWrapper = new LambdaUpdateWrapper<>();
- updateWrapper.eq(InvoiceLibrary::getOaId, oaId)
- .set(InvoiceLibrary::getOaStatus, "1")
- .set(InvoiceLibrary::getUpdatedAt, LocalDateTime.now());
- int updateCount = baseMapper.update(null, updateWrapper);
- if (updateCount > 0) {
- log.info("更新成功: 共更新{}条记录", updateCount);
- } else {
- log.warn("未找到符合条件的记录: oaId={}", oaId);
- }
- } catch (Exception e) {
- throw new RuntimeException("更新OA状态失败", e);
- }
- }
- @Override
- public McR invoiceLibrarys(Map map) {
- log.info("接收到的参数: {}", map);
- try {
- String processInstanceId = UtilMap.getString(map, "processInstanceId");
- if (StringUtils.isBlank(processInstanceId)) {
- log.error("processInstanceId为空");
- return McR.error("400", "审批实例ID不能为空");
- }
- // 获取审批实例信息
- String accessToken = ddClient.getAccessToken();
- Map processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, processInstanceId);
- if (processInstance == null) {
- log.error("获取审批实例失败: {}", processInstanceId);
- return McR.error("500", "获取审批实例信息失败");
- }
- String userId = (String) processInstance.get("originatorUserId");
- log.info("审批人ID: {}", userId);
- List<Map> formComponentValues = (List<Map>) processInstance.get("formComponentValues");
- if (formComponentValues == null || formComponentValues.isEmpty()) {
- log.warn("表单数据为空");
- return McR.error("400", "表单数据为空");
- }
- // 存储主表的字段值
- String zt = null;
- BigDecimal mainJe = null;
- String bxlb = null;
- String bm = null;
- // 遍历收集主表字段值
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- Object value = formComponentValue.get("value");
- if ("DDSelectField_4SITXLYUEO80".equals(id)) {
- zt = value != null ? String.valueOf(value) : "";
- }
- if ("MoneyField_3QZLY8BD3780".equals(id)) {
- if (value instanceof BigDecimal) {
- mainJe = (BigDecimal) value;
- } else if (value != null) {
- try {
- mainJe = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- mainJe = BigDecimal.ZERO;
- }
- }
- }
- if ("DDSelectField_1TP75OVCPAAO0".equals(id)) {
- bxlb = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_OKBSJN0MD6O0".equals(id)) {
- bm = value != null ? String.valueOf(value) : "";
- }
- }
- // 判断主表状态 - 无发票
- if ("否".equals(zt)) {
- Map<String, Object> result = new HashMap<>();
- result.put("je", mainJe);
- result.put("status", "否");
- result.put("message", "无发票报销");
- // 保存基础报销记录
- InvoiceLibrary baseInvoice = new InvoiceLibrary();
- baseInvoice.setOaId(processInstanceId);
- baseInvoice.setOaStatus("0");
- baseInvoice.setInvoiceStatus("0");
- baseInvoice.setDep(bm != null ? bm : "");
- baseInvoice.setAccountTitle(bxlb != null ? bxlb : "");
- baseInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- baseInvoice.setCreatedAt(LocalDateTime.now());
- baseInvoice.setUpdatedAt(LocalDateTime.now());
- baseInvoice.setInvoiceCode("");
- baseInvoice.setInvoiceNumber("");
- baseInvoice.setInvoiceType("");
- baseInvoice.setAmount(BigDecimal.ZERO);
- baseInvoice.setTaxAmount(BigDecimal.ZERO);
- baseInvoice.setTotalAmount(BigDecimal.ZERO);
- baseInvoice.setBuyerName("");
- baseInvoice.setBuyerTaxId("");
- baseInvoice.setSellerName("");
- baseInvoice.setSellerTaxId("");
- baseMapper.insert(baseInvoice);
- addWorkflowComment(processInstanceId, userId, result);
- return McR.success(result);
- }
- // 判断主表状态 - 有发票
- if ("是".equals(zt)) {
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- if ("TableField_1A1CDMEN8DDS0".equals(id)) {
- String tableFieldValue = String.valueOf(formComponentValue.get("value"));
- log.info("子表数据: {}", tableFieldValue);
- try {
- List<Map> tableRows = (List<Map>) JSONObject.parse(tableFieldValue);
- if (tableRows == null || tableRows.isEmpty()) {
- log.warn("子表数据为空");
- return McR.error("400", "发票明细为空");
- }
- List<Object> allResults = new ArrayList<>();
- List<InvoiceLibrary> invoiceList = new ArrayList<>();
- for (Map row : tableRows) {
- List<Map> rowValues = (List<Map>) row.get("rowValue");
- String hasInvoice = null;
- List<Map> attachmentList = null;
- for (Map rowItem : rowValues) {
- String key = String.valueOf(rowItem.get("key"));
- if ("DDSelectField_1ORUK0KIM5D6O".equals(key)) {
- Object value = rowItem.get("value");
- hasInvoice = value != null ? String.valueOf(value) : "";
- }
- if ("DDAttachment_Z02OGR5QL8U8".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof List) {
- attachmentList = (List<Map>) value;
- }
- }
- }
- if ("是".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- log.info("处理发票: fileName={}, fileId={}", fileName, fileId);
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- allResults.add(responseData);
- // 解析发票数据 - responseData 是对象,不是数组
- if (responseData != null) {
- try {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- // 获取 result 数组
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- JSONObject invoice = resultArray.getJSONObject(0);
- InvoiceLibrary invoiceLibrary = new InvoiceLibrary();
- invoiceLibrary.setOaId(processInstanceId);
- invoiceLibrary.setInvoiceCode(invoice.getString("code") != null ? invoice.getString("code") : "");
- invoiceLibrary.setInvoiceNumber(invoice.getString("serial") != null ? invoice.getString("serial") : "");
- invoiceLibrary.setInvoiceType(invoice.getString("kindName") != null ? invoice.getString("kindName") : "");
- String date = invoice.getString("date");
- if (StringUtils.isNotBlank(date)) {
- try {
- invoiceLibrary.setInvoiceDate(LocalDate.parse(date.trim()));
- } catch (Exception e) {
- log.warn("日期解析失败: {}", date);
- }
- }
- invoiceLibrary.setAmount(invoice.getBigDecimal("excludingTax") != null ? invoice.getBigDecimal("excludingTax") : BigDecimal.ZERO);
- invoiceLibrary.setTaxAmount(invoice.getBigDecimal("tax") != null ? invoice.getBigDecimal("tax") : BigDecimal.ZERO);
- invoiceLibrary.setTotalAmount(invoice.getBigDecimal("amount") != null ? invoice.getBigDecimal("amount") : BigDecimal.ZERO);
- invoiceLibrary.setBuyerName(invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "");
- invoiceLibrary.setBuyerTaxId(invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "");
- invoiceLibrary.setSellerName(invoice.getString("sellerName") != null ? invoice.getString("sellerName") : "");
- invoiceLibrary.setSellerTaxId(invoice.getString("sellerTaxId") != null ? invoice.getString("sellerTaxId") : "");
- invoiceLibrary.setOaStatus("0");
- invoiceLibrary.setInvoiceStatus("0");
- invoiceLibrary.setDep(bm != null ? bm : "");
- invoiceLibrary.setAccountTitle(bxlb != null ? bxlb : "");
- invoiceLibrary.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- invoiceLibrary.setCreatedAt(LocalDateTime.now());
- invoiceLibrary.setUpdatedAt(LocalDateTime.now());
- invoiceList.add(invoiceLibrary);
- }
- } catch (Exception e) {
- log.error("解析发票数据失败", e);
- }
- }
- } catch (Exception e) {
- log.error("处理发票失败: fileId={}, fileName={}", fileId, fileName, e);
- }
- }
- }
- }
- if (!invoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : invoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条发票记录", invoiceList.size());
- }
- addWorkflowComment(processInstanceId, userId, allResults);
- return McR.success(allResults);
- } catch (Exception e) {
- log.error("解析子表数据失败", e);
- return McR.error("300", "解析数据失败: " + e.getMessage());
- }
- }
- }
- }
- return McR.success();
- } catch (Exception e) {
- log.error("处理审批实例失败", e);
- return McR.error("500", "系统处理失败: " + e.getMessage());
- }
- }
- @Override
- public McR deleteAllByOaId(String oaId) {
- try {
- // 创建删除条件
- LambdaQueryWrapper<InvoiceLibrary> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(InvoiceLibrary::getOaId, oaId);
- // 执行删除
- int deleteCount = baseMapper.delete(queryWrapper);
- if (deleteCount > 0) {
- return McR.success("删除成功,共删除 " + deleteCount + " 条记录");
- } else {
- return McR.error("404", "未找到 oa_id 为 " + oaId + " 的记录");
- }
- } catch (Exception e) {
- log.error("删除失败", e);
- return McR.error("500", "删除失败: " + e.getMessage());
- }
- }
- @Override
- public McR invoiceLibrary1(Map map) {
- log.info("接收到的参数: {}", map);
- try {
- String processInstanceId = UtilMap.getString(map, "processInstanceId");
- if (StringUtils.isBlank(processInstanceId)) {
- log.error("processInstanceId为空");
- return McR.error("400", "审批实例ID不能为空");
- }
- // 获取审批实例信息
- String accessToken = ddClient.getAccessToken();
- Map processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, processInstanceId);
- if (processInstance == null) {
- log.error("获取审批实例失败: {}", processInstanceId);
- return McR.error("500", "获取审批实例信息失败");
- }
- String userId = (String) processInstance.get("originatorUserId");
- String businessId = (String) processInstance.get("businessId");
- log.info("审批人ID: {}", userId);
- List<Map> formComponentValues = (List<Map>) processInstance.get("formComponentValues");
- if (formComponentValues == null || formComponentValues.isEmpty()) {
- log.warn("表单数据为空");
- return McR.error("400", "表单数据为空");
- }
- // 存储主表的字段值
- String bm = null;
- String bxlb = null;
- String fkzhxx = null;
- String fkzh = null;
- String yhqc = null;
- String skzh = null;
- BigDecimal mainJe = null;
- // 遍历收集主表字段值
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- Object value = formComponentValue.get("value");
- if ("MoneyField_1FVEBDQBSQ680".equals(id)) {
- if (value instanceof BigDecimal) {
- mainJe = (BigDecimal) value;
- } else if (value != null) {
- try {
- mainJe = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- mainJe = BigDecimal.ZERO;
- }
- }
- }
- if ("RecipientAccountField_1U3QZJI8H6RK0".equals(id)) {
- skzh = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_YLLB7AD1ATS0".equals(id)) {
- bxlb = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_RF93A6VHA9C0".equals(id)) {
- bm = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_23RVAEKUM5UO0".equals(id)) {
- fkzhxx = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_1TE3AOHRMD4W0".equals(id)) {
- fkzh = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_EN2MDZIMUTK0".equals(id)) {
- yhqc = value != null ? String.valueOf(value) : "";
- }
- }
- // 遍历子表数据
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- if ("TableField_1VKP90DW7WKG0".equals(id)) {
- String tableFieldValue = String.valueOf(formComponentValue.get("value"));
- log.info("子表数据: {}", tableFieldValue);
- try {
- List<Map> tableRows = (List<Map>) JSONObject.parse(tableFieldValue);
- if (tableRows == null || tableRows.isEmpty()) {
- log.warn("子表数据为空");
- return McR.error("400", "发票明细为空");
- }
- // 用于存储无发票记录和发票记录
- List<InvoiceLibrary> noInvoiceList = new ArrayList<>();
- List<InvoiceLibrary> invoiceList = new ArrayList<>();
- List<Object> allResults = new ArrayList<>();
- // 存储有发票("是"和"被共享")的解析数据
- List<Map<String, Object>> parsedInvoiceDataList = new ArrayList<>();
- // 存储共享发票数据
- List<Map<String, Object>> sharedInvoiceDataList = new ArrayList<>();
- // 用于校验的集合
- List<String> invoiceNumberList = new ArrayList<>();
- Map<String, String> invoiceNumberToFileName = new HashMap<>();
- List<String> invalidBuyerTaxIds = new ArrayList<>();
- // 存储税率信息(从"被共享"发票中获取)
- BigDecimal sharedTaxRate = BigDecimal.ZERO;
- String sharedKind = "";
- boolean hasSharedInvoice = false; // 标记是否存在"被共享"发票
- // ========== 第一遍遍历:收集数据并查找"被共享"发票 ==========
- for (Map row : tableRows) {
- List<Map> rowValues = (List<Map>) row.get("rowValue");
- String hasInvoice = null;
- String cdbm = null;
- String lbmx = null;
- String bz = null;
- BigDecimal detailje = null;
- List<Map> attachmentList = null;
- for (Map rowItem : rowValues) {
- String key = String.valueOf(rowItem.get("key"));
- if ("DDSelectField_20ZGDOIRHB400".equals(key)) {
- Object value = rowItem.get("value");
- hasInvoice = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_BVQPDAKTMO74".equals(key)) {
- Object value = rowItem.get("value");
- lbmx = value != null ? String.valueOf(value) : "";
- }
- if ("TextareaField_MO34ZZVGJHC".equals(key)) {
- Object value = rowItem.get("value");
- bz = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_1VQ3M98PC7SW0".equals(key)) {
- Object value = rowItem.get("value");
- cdbm = value != null ? String.valueOf(value) : "";
- }
- if ("NumberField_BLX4BRW8GEW0".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof BigDecimal) {
- detailje = (BigDecimal) value;
- } else if (value != null) {
- try {
- detailje = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- detailje = BigDecimal.ZERO;
- }
- } else {
- detailje = BigDecimal.ZERO;
- }
- }
- if ("DDAttachment_1YQ3DD7BH4BK0".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof List) {
- attachmentList = (List<Map>) value;
- }
- }
- }
- // ========== 处理 hasInvoice = "否" 的情况(无发票) ==========
- if ("否".equals(hasInvoice)) {
- // 保存基础报销记录(无发票)
- InvoiceLibrary baseInvoice = new InvoiceLibrary();
- baseInvoice.setOaId(processInstanceId);
- baseInvoice.setSkzh(skzh);
- baseInvoice.setBusinessId(businessId);
- baseInvoice.setOaStatus("0");
- baseInvoice.setInvoiceStatus("0");
- baseInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- baseInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- baseInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- baseInvoice.setDetailAmount(detailje != null ? detailje : BigDecimal.ZERO);
- baseInvoice.setCreatedAt(LocalDateTime.now());
- baseInvoice.setUpdatedAt(LocalDateTime.now());
- baseInvoice.setInvoiceCode("");
- baseInvoice.setInvoiceNumber("");
- baseInvoice.setInvoiceType("");
- baseInvoice.setAmount(null);
- baseInvoice.setTaxAmount(null);
- baseInvoice.setTotalAmount(null);
- baseInvoice.setBuyerName("");
- baseInvoice.setBuyerTaxId("");
- baseInvoice.setSellerName("");
- baseInvoice.setSellerTaxId("");
- baseInvoice.setUserId(userId);
- baseInvoice.setFormName("付款申请");
- baseInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- baseInvoice.setPayAccount(fkzh != null ? fkzh : "");
- baseInvoice.setBankName(yhqc != null ? yhqc : "");
- baseInvoice.setHasInvoice("否");
- baseInvoice.setSharedTaxAmount(null);
- baseInvoice.setSharedAmount(null);
- baseInvoice.setSharedRate(null);
- baseInvoice.setNote(bz != null ? bz : "");
- noInvoiceList.add(baseInvoice);
- log.info("收集无发票记录: 成本部门={}, 费用明细={}", cdbm, lbmx);
- continue;
- }
- // ========== 处理 hasInvoice = "被共享"(被共享发票,提供税率) ==========
- if ("被共享".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- // 标记存在被共享发票
- hasSharedInvoice = true;
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- // 解析发票数据
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- // ========== 获取税率(供共享发票使用) ==========
- Object taxRateObj = invoice.get("taxRate");
- Object kind = invoice.getString("kindName");
- if (taxRateObj != null) {
- String taxRateStr = String.valueOf(taxRateObj);
- String taxRateNum = taxRateStr.replace("%", "").trim();
- sharedKind = kind != null ? String.valueOf(kind) : "";
- sharedTaxRate = new BigDecimal(taxRateNum);
- sharedTaxRate = sharedTaxRate.divide(new BigDecimal("100"), 10, BigDecimal.ROUND_HALF_UP);
- log.info("从被共享发票获取税率: 原始={}, 转换后={}", taxRateStr, sharedTaxRate);
- }
- // 保存解析的数据供后续使用
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // ========== 校验1:抬头校验 ==========
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // ========== 校验2:发票号重复校验 ==========
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("被共享发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "被共享发票解析失败: " + e.getMessage());
- return McR.error("400", "被共享发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "是"(有发票,但不共享) ==========
- if ("是".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // 抬头校验
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // 发票号重复校验
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "发票解析失败: " + e.getMessage());
- return McR.error("400", "发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "共享"(共享发票,需要从被共享发票获取税率) ==========
- if ("共享".equals(hasInvoice)) {
- if (detailje != null && detailje.compareTo(BigDecimal.ZERO) > 0) {
- Map<String, Object> sharedData = new HashMap<>();
- sharedData.put("detailje", detailje);
- sharedData.put("lbmx", lbmx);
- sharedData.put("bz", bz);
- sharedData.put("cdbm", cdbm);
- sharedData.put("hasInvoice", hasInvoice);
- sharedInvoiceDataList.add(sharedData);
- log.info("收集共享发票数据: 金额={}, 成本部门={}", detailje, cdbm);
- }
- }
- }
- // ========== 检查:如果存在共享发票但没有被共享发票 ==========
- if (!sharedInvoiceDataList.isEmpty() && !hasSharedInvoice) {
- String errorMsg = "存在'共享'发票,但未找到'被共享'发票,无法获取税率信息,请确保至少有一行选择'被共享'并上传发票附件";
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- // ========== 抬头校验失败处理 ==========
- if (!invalidBuyerTaxIds.isEmpty()) {
- String errorMsg = "发票抬头有误,以下发票的购买方税号不在公司抬头库中:\n" + String.join("\n", invalidBuyerTaxIds);
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- BigDecimal totalAmount = BigDecimal.ZERO;
- // ========== 第二遍遍历:保存发票数据 ==========
- // 1. 保存有发票数据("是"和"被共享")
- for (Map<String, Object> parsedData : parsedInvoiceDataList) {
- JSONObject invoice = (JSONObject) parsedData.get("invoice");
- Object responseData = parsedData.get("responseData");
- String cdbm = (String) parsedData.get("cdbm");
- String lbmx = (String) parsedData.get("lbmx");
- String bz = (String) parsedData.get("bz");
- String hasInvoice = (String) parsedData.get("hasInvoice");
- Object detailjeObj = parsedData.get("detailje");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- allResults.add(responseData);
- BigDecimal amount = invoice.getBigDecimal("amount");
- if (amount != null) {
- totalAmount = totalAmount.add(amount);
- }
- InvoiceLibrary invoiceLibrary = new InvoiceLibrary();
- invoiceLibrary.setOaId(processInstanceId);
- invoiceLibrary.setSkzh(skzh);
- invoiceLibrary.setBusinessId(businessId);
- invoiceLibrary.setInvoiceCode(invoice.getString("code") != null ? invoice.getString("code") : "");
- invoiceLibrary.setInvoiceNumber(invoice.getString("serial") != null ? invoice.getString("serial") : "");
- invoiceLibrary.setInvoiceType(invoice.getString("kindName") != null ? invoice.getString("kindName") : "");
- String date = invoice.getString("date");
- if (StringUtils.isNotBlank(date)) {
- try {
- invoiceLibrary.setInvoiceDate(LocalDate.parse(date.trim()));
- } catch (Exception e) {
- log.warn("日期解析失败: {}", date);
- }
- }
- invoiceLibrary.setAmount(invoice.getBigDecimal("excludingTax") != null ? invoice.getBigDecimal("excludingTax") : BigDecimal.ZERO);
- invoiceLibrary.setTaxAmount(invoice.getBigDecimal("tax") != null ? invoice.getBigDecimal("tax") : BigDecimal.ZERO);
- invoiceLibrary.setTotalAmount(invoice.getBigDecimal("amount") != null ? invoice.getBigDecimal("amount") : BigDecimal.ZERO);
- invoiceLibrary.setBuyerName(invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "");
- invoiceLibrary.setBuyerTaxId(invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "");
- invoiceLibrary.setSellerName(invoice.getString("sellerName") != null ? invoice.getString("sellerName") : "");
- invoiceLibrary.setSellerTaxId(invoice.getString("sellerTaxId") != null ? invoice.getString("sellerTaxId") : "");
- invoiceLibrary.setOaStatus("0");
- invoiceLibrary.setUserId(userId);
- invoiceLibrary.setNote(bz != null ? bz : "");
- invoiceLibrary.setFormName("付款申请");
- invoiceLibrary.setPaySubject(fkzhxx != null ? fkzhxx : "");
- invoiceLibrary.setPayAccount(fkzh != null ? fkzh : "");
- invoiceLibrary.setBankName(yhqc != null ? yhqc : "");
- invoiceLibrary.setInvoiceStatus("0");
- invoiceLibrary.setHasInvoice(hasInvoice);
- if (StringUtils.isNotBlank(detailje)) {
- try {
- invoiceLibrary.setDetailAmount(new BigDecimal(detailje));
- } catch (NumberFormatException e) {
- log.error("detailJe格式转换失败: {}", detailje);
- invoiceLibrary.setDetailAmount(null);
- }
- } else {
- invoiceLibrary.setDetailAmount(null);
- }
- invoiceLibrary.setSharedTaxAmount(null);
- invoiceLibrary.setSharedAmount(null);
- invoiceLibrary.setSharedRate(null);
- invoiceLibrary.setAccountTitle(lbmx != null ? lbmx : "");
- invoiceLibrary.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- invoiceLibrary.setCreatedAt(LocalDateTime.now());
- invoiceLibrary.setUpdatedAt(LocalDateTime.now());
- if (cdbm != null && StringUtils.isNotBlank(cdbm)) {
- invoiceLibrary.setDep(cdbm);
- } else {
- invoiceLibrary.setDep(bm != null ? bm : "");
- }
- invoiceList.add(invoiceLibrary);
- }
- // 2. 保存共享发票数据(使用从"被共享"发票获取的税率)
- if (!sharedInvoiceDataList.isEmpty()) {
- // 检查税率是否有效
- if (sharedTaxRate.compareTo(BigDecimal.ZERO) == 0) {
- log.warn("共享发票税率未获取到,使用默认税率0");
- }
- for (Map<String, Object> sharedData : sharedInvoiceDataList) {
- Object detailjeObj = sharedData.get("detailje");
- String cdbm = (String) sharedData.get("cdbm");
- String lbmx = (String) sharedData.get("lbmx");
- String bz = (String) sharedData.get("bz");
- String hasInvoice = (String) sharedData.get("hasInvoice");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- try {
- BigDecimal detailAmount = new BigDecimal(detailje);
- // 使用从"被共享"发票解析到的税率
- BigDecimal ocrTaxRate = sharedTaxRate;
- // 计算金额: 含税金额 / (1 + 税率) = 不含税金额
- BigDecimal divisor = BigDecimal.ONE.add(ocrTaxRate);
- BigDecimal sharedTaxAmount = detailAmount.divide(divisor, 2, BigDecimal.ROUND_HALF_UP);
- BigDecimal sharedAmount = detailAmount.subtract(sharedTaxAmount);
- BigDecimal sharedRate = ocrTaxRate;
- log.info("共享发票计算: 含税金额={}, 税率={}%, 不含税金额={}, 税额={}",
- detailAmount, ocrTaxRate.multiply(new BigDecimal("100")),
- sharedAmount, sharedTaxAmount);
- InvoiceLibrary sharedInvoice = new InvoiceLibrary();
- sharedInvoice.setOaId(processInstanceId);
- sharedInvoice.setSkzh(skzh);
- sharedInvoice.setBusinessId(businessId);
- sharedInvoice.setOaStatus("0");
- sharedInvoice.setInvoiceStatus("0");
- sharedInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- sharedInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- sharedInvoice.setNote(bz != null ? bz : "");
- sharedInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- sharedInvoice.setAmount(null);
- sharedInvoice.setTaxAmount(null);
- sharedInvoice.setTotalAmount(null);
- sharedInvoice.setUserId(userId);
- sharedInvoice.setFormName("付款申请");
- sharedInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- sharedInvoice.setPayAccount(fkzh != null ? fkzh : "");
- sharedInvoice.setBankName(yhqc != null ? yhqc : "");
- sharedInvoice.setHasInvoice(hasInvoice);
- sharedInvoice.setDetailAmount(detailAmount);
- sharedInvoice.setSharedTaxAmount(sharedAmount);
- sharedInvoice.setSharedAmount(sharedTaxAmount);
- sharedInvoice.setSharedRate(String.valueOf(sharedRate));
- sharedInvoice.setCreatedAt(LocalDateTime.now());
- sharedInvoice.setUpdatedAt(LocalDateTime.now());
- sharedInvoice.setInvoiceCode("");
- sharedInvoice.setInvoiceNumber("");
- sharedInvoice.setInvoiceType(sharedKind);
- sharedInvoice.setBuyerName("");
- sharedInvoice.setBuyerTaxId("");
- sharedInvoice.setSellerName("");
- sharedInvoice.setSellerTaxId("");
- invoiceList.add(sharedInvoice);
- log.info("成功保存共享发票记录,成本部门: {}", cdbm);
- } catch (NumberFormatException e) {
- log.error("共享发票金额格式转换失败: detailje={}", detailje, e);
- } catch (Exception e) {
- log.error("共享发票处理失败", e);
- }
- }
- }
- // 保存所有记录(无发票 + 有发票)
- if (!noInvoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : noInvoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条无发票记录", noInvoiceList.size());
- addWorkflowComment(processInstanceId, userId, "报销总金额:" + mainJe);
- }
- if (!invoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : invoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条发票记录(有发票: {}, 共享发票: {})",
- invoiceList.size(),
- parsedInvoiceDataList.stream().filter(d -> "是".equals(d.get("hasInvoice")) || "被共享".equals(d.get("hasInvoice"))).count(),
- sharedInvoiceDataList.size());
- addWorkflowComment1(processInstanceId, userId, totalAmount);
- }
- if (!allResults.isEmpty()) {
- return McR.success(allResults);
- } else {
- return McR.success();
- }
- } catch (Exception e) {
- log.error("解析子表数据失败", e);
- return McR.error("300", "解析数据失败: " + e.getMessage());
- }
- }
- }
- return McR.success();
- } catch (Exception e) {
- log.error("处理审批实例失败", e);
- return McR.error("500", "系统处理失败: " + e.getMessage());
- }
- }
- @Override
- public McR invoiceLibrary2(Map map) {
- log.info("接收到的参数: {}", map);
- try {
- String processInstanceId = UtilMap.getString(map, "processInstanceId");
- if (StringUtils.isBlank(processInstanceId)) {
- log.error("processInstanceId为空");
- return McR.error("400", "审批实例ID不能为空");
- }
- // 获取审批实例信息
- String accessToken = ddClient.getAccessToken();
- Map processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, processInstanceId);
- if (processInstance == null) {
- log.error("获取审批实例失败: {}", processInstanceId);
- return McR.error("500", "获取审批实例信息失败");
- }
- String userId = (String) processInstance.get("originatorUserId");
- String businessId = (String) processInstance.get("businessId");
- log.info("审批人ID: {}", userId);
- List<Map> formComponentValues = (List<Map>) processInstance.get("formComponentValues");
- if (formComponentValues == null || formComponentValues.isEmpty()) {
- log.warn("表单数据为空");
- return McR.error("400", "表单数据为空");
- }
- // 存储主表的字段值
- String bm = null;
- String bxlb = null;
- String fkzhxx = null;
- String fkzh = null;
- String yhqc = null;
- String skzh = null;
- BigDecimal mainJe = null;
- // 遍历收集主表字段值
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- Object value = formComponentValue.get("value");
- if ("MoneyField_1V4A9P72K6TC0".equals(id)) {
- if (value instanceof BigDecimal) {
- mainJe = (BigDecimal) value;
- } else if (value != null) {
- try {
- mainJe = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- mainJe = BigDecimal.ZERO;
- }
- }
- }
- if ("DDSelectField_YLLB7AD1ATS0".equals(id)) {
- bxlb = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_ZI9KALEYOTC0".equals(id)) {
- bm = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_1WEX2WWCJ3PC0".equals(id)) {
- fkzhxx = value != null ? String.valueOf(value) : "";
- }
- if ("RecipientAccountField_13CHMZZOTK4G0".equals(id)) {
- skzh = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_2W39FPCZH1A0".equals(id)) {
- fkzh = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_QW7HJIR7T800".equals(id)) {
- yhqc = value != null ? String.valueOf(value) : "";
- }
- }
- // 遍历子表数据
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- if ("TableField_182UNZEX02U80".equals(id)) {
- String tableFieldValue = String.valueOf(formComponentValue.get("value"));
- log.info("子表数据: {}", tableFieldValue);
- try {
- List<Map> tableRows = (List<Map>) JSONObject.parse(tableFieldValue);
- if (tableRows == null || tableRows.isEmpty()) {
- log.warn("子表数据为空");
- return McR.error("400", "发票明细为空");
- }
- // 用于存储无发票记录和发票记录
- List<InvoiceLibrary> noInvoiceList = new ArrayList<>();
- List<InvoiceLibrary> invoiceList = new ArrayList<>();
- List<Object> allResults = new ArrayList<>();
- // 存储有发票("是"和"被共享")的解析数据
- List<Map<String, Object>> parsedInvoiceDataList = new ArrayList<>();
- // 存储共享发票数据
- List<Map<String, Object>> sharedInvoiceDataList = new ArrayList<>();
- // 用于校验的集合
- List<String> invoiceNumberList = new ArrayList<>();
- Map<String, String> invoiceNumberToFileName = new HashMap<>();
- List<String> invalidBuyerTaxIds = new ArrayList<>();
- // 存储税率信息(从"被共享"发票中获取)
- BigDecimal sharedTaxRate = BigDecimal.ZERO;
- String sharedKind = "";
- boolean hasSharedInvoice = false; // 标记是否存在"被共享"发票
- // ========== 第一遍遍历:收集数据并查找"被共享"发票 ==========
- for (Map row : tableRows) {
- List<Map> rowValues = (List<Map>) row.get("rowValue");
- String hasInvoice = null;
- String cdbm = null;
- String lbmx = null;
- String bz = null;
- BigDecimal detailje = null;
- List<Map> attachmentList = null;
- for (Map rowItem : rowValues) {
- String key = String.valueOf(rowItem.get("key"));
- if ("DDSelectField_44I0SP6M1I00".equals(key)) {
- Object value = rowItem.get("value");
- hasInvoice = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_20HL7OBLLB4W0".equals(key)) {
- Object value = rowItem.get("value");
- lbmx = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_1YUNETYYW9XC0".equals(key)) {
- Object value = rowItem.get("value");
- bz = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_RC2YZRJQAQ80".equals(key)) {
- Object value = rowItem.get("value");
- cdbm = value != null ? String.valueOf(value) : "";
- }
- if ("NumberField_1UC3HZ8OEIPS0".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof BigDecimal) {
- detailje = (BigDecimal) value;
- } else if (value != null) {
- try {
- detailje = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- detailje = BigDecimal.ZERO;
- }
- } else {
- detailje = BigDecimal.ZERO;
- }
- }
- if ("DDAttachment_LEUWYIC68TC0".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof List) {
- attachmentList = (List<Map>) value;
- }
- }
- }
- // ========== 处理 hasInvoice = "否" 的情况(无发票) ==========
- if ("否".equals(hasInvoice)) {
- // 保存基础报销记录(无发票)
- InvoiceLibrary baseInvoice = new InvoiceLibrary();
- baseInvoice.setOaId(processInstanceId);
- baseInvoice.setSkzh(skzh);
- baseInvoice.setBusinessId(businessId);
- baseInvoice.setOaStatus("0");
- baseInvoice.setInvoiceStatus("0");
- baseInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- baseInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- baseInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- baseInvoice.setDetailAmount(detailje != null ? detailje : BigDecimal.ZERO);
- baseInvoice.setCreatedAt(LocalDateTime.now());
- baseInvoice.setUpdatedAt(LocalDateTime.now());
- baseInvoice.setInvoiceCode("");
- baseInvoice.setInvoiceNumber("");
- baseInvoice.setInvoiceType("");
- baseInvoice.setAmount(null);
- baseInvoice.setTaxAmount(null);
- baseInvoice.setTotalAmount(null);
- baseInvoice.setBuyerName("");
- baseInvoice.setUserId(userId);
- baseInvoice.setBuyerTaxId("");
- baseInvoice.setSellerName("");
- baseInvoice.setSellerTaxId("");
- baseInvoice.setFormName("合作商付款申请");
- baseInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- baseInvoice.setPayAccount(fkzh != null ? fkzh : "");
- baseInvoice.setBankName(yhqc != null ? yhqc : "");
- baseInvoice.setHasInvoice("否");
- baseInvoice.setSharedTaxAmount(null);
- baseInvoice.setSharedAmount(null);
- baseInvoice.setSharedRate(null);
- baseInvoice.setNote(bz != null ? bz : "");
- noInvoiceList.add(baseInvoice);
- log.info("收集无发票记录: 成本部门={}, 费用明细={}", cdbm, lbmx);
- continue;
- }
- // ========== 处理 hasInvoice = "被共享"(被共享发票,提供税率) ==========
- if ("被共享".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- // 标记存在被共享发票
- hasSharedInvoice = true;
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- // 解析发票数据
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- // ========== 获取税率(供共享发票使用) ==========
- Object taxRateObj = invoice.get("taxRate");
- Object kind = invoice.getString("kindName");
- if (taxRateObj != null) {
- String taxRateStr = String.valueOf(taxRateObj);
- String taxRateNum = taxRateStr.replace("%", "").trim();
- sharedKind = kind != null ? String.valueOf(kind) : "";
- sharedTaxRate = new BigDecimal(taxRateNum);
- sharedTaxRate = sharedTaxRate.divide(new BigDecimal("100"), 10, BigDecimal.ROUND_HALF_UP);
- log.info("从被共享发票获取税率: 原始={}, 转换后={}", taxRateStr, sharedTaxRate);
- }
- // 保存解析的数据供后续使用
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // ========== 校验1:抬头校验 ==========
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // ========== 校验2:发票号重复校验 ==========
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("被共享发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "被共享发票解析失败: " + e.getMessage());
- return McR.error("400", "被共享发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "是"(有发票,但不共享) ==========
- if ("是".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // 抬头校验
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // 发票号重复校验
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "发票解析失败: " + e.getMessage());
- return McR.error("400", "发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "共享"(共享发票,需要从被共享发票获取税率) ==========
- if ("共享".equals(hasInvoice)) {
- if (detailje != null && detailje.compareTo(BigDecimal.ZERO) > 0) {
- Map<String, Object> sharedData = new HashMap<>();
- sharedData.put("detailje", detailje);
- sharedData.put("lbmx", lbmx);
- sharedData.put("bz", bz);
- sharedData.put("cdbm", cdbm);
- sharedData.put("hasInvoice", hasInvoice);
- sharedInvoiceDataList.add(sharedData);
- log.info("收集共享发票数据: 金额={}, 成本部门={}", detailje, cdbm);
- }
- }
- }
- // ========== 检查:如果存在共享发票但没有被共享发票 ==========
- if (!sharedInvoiceDataList.isEmpty() && !hasSharedInvoice) {
- String errorMsg = "存在'共享'发票,但未找到'被共享'发票,无法获取税率信息,请确保至少有一行选择'被共享'并上传发票附件";
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- // ========== 抬头校验失败处理 ==========
- if (!invalidBuyerTaxIds.isEmpty()) {
- String errorMsg = "发票抬头有误,以下发票的购买方税号不在公司抬头库中:\n" + String.join("\n", invalidBuyerTaxIds);
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- BigDecimal totalAmount = BigDecimal.ZERO;
- // ========== 第二遍遍历:保存发票数据 ==========
- // 1. 保存有发票数据("是"和"被共享")
- for (Map<String, Object> parsedData : parsedInvoiceDataList) {
- JSONObject invoice = (JSONObject) parsedData.get("invoice");
- Object responseData = parsedData.get("responseData");
- String cdbm = (String) parsedData.get("cdbm");
- String lbmx = (String) parsedData.get("lbmx");
- String bz = (String) parsedData.get("bz");
- String hasInvoice = (String) parsedData.get("hasInvoice");
- Object detailjeObj = parsedData.get("detailje");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- allResults.add(responseData);
- BigDecimal amount = invoice.getBigDecimal("amount");
- if (amount != null) {
- totalAmount = totalAmount.add(amount);
- }
- InvoiceLibrary invoiceLibrary = new InvoiceLibrary();
- invoiceLibrary.setOaId(processInstanceId);
- invoiceLibrary.setSkzh(skzh);
- invoiceLibrary.setBusinessId(businessId);
- invoiceLibrary.setInvoiceCode(invoice.getString("code") != null ? invoice.getString("code") : "");
- invoiceLibrary.setInvoiceNumber(invoice.getString("serial") != null ? invoice.getString("serial") : "");
- invoiceLibrary.setInvoiceType(invoice.getString("kindName") != null ? invoice.getString("kindName") : "");
- String date = invoice.getString("date");
- if (StringUtils.isNotBlank(date)) {
- try {
- invoiceLibrary.setInvoiceDate(LocalDate.parse(date.trim()));
- } catch (Exception e) {
- log.warn("日期解析失败: {}", date);
- }
- }
- invoiceLibrary.setAmount(invoice.getBigDecimal("excludingTax") != null ? invoice.getBigDecimal("excludingTax") : BigDecimal.ZERO);
- invoiceLibrary.setTaxAmount(invoice.getBigDecimal("tax") != null ? invoice.getBigDecimal("tax") : BigDecimal.ZERO);
- invoiceLibrary.setTotalAmount(invoice.getBigDecimal("amount") != null ? invoice.getBigDecimal("amount") : BigDecimal.ZERO);
- invoiceLibrary.setBuyerName(invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "");
- invoiceLibrary.setBuyerTaxId(invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "");
- invoiceLibrary.setSellerName(invoice.getString("sellerName") != null ? invoice.getString("sellerName") : "");
- invoiceLibrary.setSellerTaxId(invoice.getString("sellerTaxId") != null ? invoice.getString("sellerTaxId") : "");
- invoiceLibrary.setOaStatus("0");
- invoiceLibrary.setUserId(userId);
- invoiceLibrary.setNote(bz != null ? bz : "");
- invoiceLibrary.setFormName("合作商付款申请");
- invoiceLibrary.setPaySubject(fkzhxx != null ? fkzhxx : "");
- invoiceLibrary.setPayAccount(fkzh != null ? fkzh : "");
- invoiceLibrary.setBankName(yhqc != null ? yhqc : "");
- invoiceLibrary.setInvoiceStatus("0");
- invoiceLibrary.setHasInvoice(hasInvoice);
- if (StringUtils.isNotBlank(detailje)) {
- try {
- invoiceLibrary.setDetailAmount(new BigDecimal(detailje));
- } catch (NumberFormatException e) {
- log.error("detailJe格式转换失败: {}", detailje);
- invoiceLibrary.setDetailAmount(null);
- }
- } else {
- invoiceLibrary.setDetailAmount(null);
- }
- invoiceLibrary.setSharedTaxAmount(null);
- invoiceLibrary.setSharedAmount(null);
- invoiceLibrary.setSharedRate(null);
- invoiceLibrary.setAccountTitle(lbmx != null ? lbmx : "");
- invoiceLibrary.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- invoiceLibrary.setCreatedAt(LocalDateTime.now());
- invoiceLibrary.setUpdatedAt(LocalDateTime.now());
- if (cdbm != null && StringUtils.isNotBlank(cdbm)) {
- invoiceLibrary.setDep(cdbm);
- } else {
- invoiceLibrary.setDep(bm != null ? bm : "");
- }
- invoiceList.add(invoiceLibrary);
- }
- // 2. 保存共享发票数据(使用从"被共享"发票获取的税率)
- if (!sharedInvoiceDataList.isEmpty()) {
- // 检查税率是否有效
- if (sharedTaxRate.compareTo(BigDecimal.ZERO) == 0) {
- log.warn("共享发票税率未获取到,使用默认税率0");
- }
- for (Map<String, Object> sharedData : sharedInvoiceDataList) {
- Object detailjeObj = sharedData.get("detailje");
- String cdbm = (String) sharedData.get("cdbm");
- String lbmx = (String) sharedData.get("lbmx");
- String bz = (String) sharedData.get("bz");
- String hasInvoice = (String) sharedData.get("hasInvoice");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- try {
- BigDecimal detailAmount = new BigDecimal(detailje);
- // 使用从"被共享"发票解析到的税率
- BigDecimal ocrTaxRate = sharedTaxRate;
- // 计算金额: 含税金额 / (1 + 税率) = 不含税金额
- BigDecimal divisor = BigDecimal.ONE.add(ocrTaxRate);
- BigDecimal sharedTaxAmount = detailAmount.divide(divisor, 2, BigDecimal.ROUND_HALF_UP);
- BigDecimal sharedAmount = detailAmount.subtract(sharedTaxAmount);
- BigDecimal sharedRate = ocrTaxRate;
- log.info("共享发票计算: 含税金额={}, 税率={}%, 不含税金额={}, 税额={}",
- detailAmount, ocrTaxRate.multiply(new BigDecimal("100")),
- sharedAmount, sharedTaxAmount);
- InvoiceLibrary sharedInvoice = new InvoiceLibrary();
- sharedInvoice.setOaId(processInstanceId);
- sharedInvoice.setSkzh(skzh);
- sharedInvoice.setBusinessId(businessId);
- sharedInvoice.setOaStatus("0");
- sharedInvoice.setInvoiceStatus("0");
- sharedInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- sharedInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- sharedInvoice.setNote(bz != null ? bz : "");
- sharedInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- sharedInvoice.setAmount(null);
- sharedInvoice.setTaxAmount(null);
- sharedInvoice.setTotalAmount(null);
- sharedInvoice.setUserId(userId);
- sharedInvoice.setFormName("合作商付款申请");
- sharedInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- sharedInvoice.setPayAccount(fkzh != null ? fkzh : "");
- sharedInvoice.setBankName(yhqc != null ? yhqc : "");
- sharedInvoice.setHasInvoice(hasInvoice);
- sharedInvoice.setDetailAmount(detailAmount);
- sharedInvoice.setSharedTaxAmount(sharedAmount);
- sharedInvoice.setSharedAmount(sharedTaxAmount);
- sharedInvoice.setSharedRate(String.valueOf(sharedRate));
- sharedInvoice.setCreatedAt(LocalDateTime.now());
- sharedInvoice.setUpdatedAt(LocalDateTime.now());
- sharedInvoice.setInvoiceCode("");
- sharedInvoice.setInvoiceNumber("");
- sharedInvoice.setInvoiceType(sharedKind);
- sharedInvoice.setBuyerName("");
- sharedInvoice.setBuyerTaxId("");
- sharedInvoice.setSellerName("");
- sharedInvoice.setSellerTaxId("");
- invoiceList.add(sharedInvoice);
- log.info("成功保存共享发票记录,成本部门: {}", cdbm);
- } catch (NumberFormatException e) {
- log.error("共享发票金额格式转换失败: detailje={}", detailje, e);
- } catch (Exception e) {
- log.error("共享发票处理失败", e);
- }
- }
- }
- // 保存所有记录(无发票 + 有发票)
- if (!noInvoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : noInvoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条无发票记录", noInvoiceList.size());
- addWorkflowComment(processInstanceId, userId, "报销总金额:" + mainJe);
- }
- if (!invoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : invoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条发票记录(有发票: {}, 共享发票: {})",
- invoiceList.size(),
- parsedInvoiceDataList.stream().filter(d -> "是".equals(d.get("hasInvoice")) || "被共享".equals(d.get("hasInvoice"))).count(),
- sharedInvoiceDataList.size());
- addWorkflowComment1(processInstanceId, userId, totalAmount);
- }
- if (!allResults.isEmpty()) {
- return McR.success(allResults);
- } else {
- return McR.success();
- }
- } catch (Exception e) {
- log.error("解析子表数据失败", e);
- return McR.error("300", "解析数据失败: " + e.getMessage());
- }
- }
- }
- return McR.success();
- } catch (Exception e) {
- log.error("处理审批实例失败", e);
- return McR.error("500", "系统处理失败: " + e.getMessage());
- }
- }
- @Override
- public McR invoiceLibrary3(Map map) {
- log.info("接收到的参数: {}", map);
- try {
- String processInstanceId = UtilMap.getString(map, "processInstanceId");
- if (StringUtils.isBlank(processInstanceId)) {
- log.error("processInstanceId为空");
- return McR.error("400", "审批实例ID不能为空");
- }
- // 获取审批实例信息
- String accessToken = ddClient.getAccessToken();
- Map processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, processInstanceId);
- if (processInstance == null) {
- log.error("获取审批实例失败: {}", processInstanceId);
- return McR.error("500", "获取审批实例信息失败");
- }
- String userId = (String) processInstance.get("originatorUserId");
- String businessId = (String) processInstance.get("businessId");
- log.info("审批人ID: {}", userId);
- List<Map> formComponentValues = (List<Map>) processInstance.get("formComponentValues");
- if (formComponentValues == null || formComponentValues.isEmpty()) {
- log.warn("表单数据为空");
- return McR.error("400", "表单数据为空");
- }
- // 存储主表的字段值
- String bm = null;
- String bxlb = null;
- String fkzhxx = null;
- String fkzh = null;
- String yhqc = null;
- String skzh = null;
- BigDecimal mainJe = null;
- // 遍历收集主表字段值
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- Object value = formComponentValue.get("value");
- if ("MoneyField_5U5SLLA3CS40".equals(id)) {
- if (value instanceof BigDecimal) {
- mainJe = (BigDecimal) value;
- } else if (value != null) {
- try {
- mainJe = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- mainJe = BigDecimal.ZERO;
- }
- }
- }
- if ("RecipientAccountField_1J46MPCCTYZK0".equals(id)) {
- skzh = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_BS1HCMO7YTK0".equals(id)) {
- bxlb = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_S9DH58V8SNK0".equals(id)) {
- bm = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_GLW8YHBHSLS0".equals(id)) {
- fkzhxx = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_1PR1HQ4H28SG0".equals(id)) {
- fkzh = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_1UPR1XZTI3VK".equals(id)) {
- yhqc = value != null ? String.valueOf(value) : "";
- }
- }
- // 遍历子表数据
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- if ("TableField_9LJ65U3CQ980".equals(id)) {
- String tableFieldValue = String.valueOf(formComponentValue.get("value"));
- log.info("子表数据: {}", tableFieldValue);
- try {
- List<Map> tableRows = (List<Map>) JSONObject.parse(tableFieldValue);
- if (tableRows == null || tableRows.isEmpty()) {
- log.warn("子表数据为空");
- return McR.error("400", "发票明细为空");
- }
- // 用于存储无发票记录和发票记录
- List<InvoiceLibrary> noInvoiceList = new ArrayList<>();
- List<InvoiceLibrary> invoiceList = new ArrayList<>();
- List<Object> allResults = new ArrayList<>();
- // 存储有发票("是"和"被共享")的解析数据
- List<Map<String, Object>> parsedInvoiceDataList = new ArrayList<>();
- // 存储共享发票数据
- List<Map<String, Object>> sharedInvoiceDataList = new ArrayList<>();
- // 用于校验的集合
- List<String> invoiceNumberList = new ArrayList<>();
- Map<String, String> invoiceNumberToFileName = new HashMap<>();
- List<String> invalidBuyerTaxIds = new ArrayList<>();
- // 存储税率信息(从"被共享"发票中获取)
- BigDecimal sharedTaxRate = BigDecimal.ZERO;
- String sharedKind = "";
- boolean hasSharedInvoice = false; // 标记是否存在"被共享"发票
- // ========== 第一遍遍历:收集数据并查找"被共享"发票 ==========
- for (Map row : tableRows) {
- List<Map> rowValues = (List<Map>) row.get("rowValue");
- String hasInvoice = null;
- String cdbm = null;
- String lbmx = null;
- String bz = null;
- BigDecimal detailje = null;
- List<Map> attachmentList = null;
- for (Map rowItem : rowValues) {
- String key = String.valueOf(rowItem.get("key"));
- if ("DDSelectField_TTTOAI0FDXJ4".equals(key)) {
- Object value = rowItem.get("value");
- hasInvoice = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_17FH3ES8HWHS0".equals(key)) {
- Object value = rowItem.get("value");
- lbmx = value != null ? String.valueOf(value) : "";
- }
- if ("TextareaField_1XIFOSSQO5A80".equals(key)) {
- Object value = rowItem.get("value");
- bz = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_15QGD1PV69XC0".equals(key)) {
- Object value = rowItem.get("value");
- cdbm = value != null ? String.valueOf(value) : "";
- }
- if ("NumberField_FYHZK218TTDS".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof BigDecimal) {
- detailje = (BigDecimal) value;
- } else if (value != null) {
- try {
- detailje = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- detailje = BigDecimal.ZERO;
- }
- } else {
- detailje = BigDecimal.ZERO;
- }
- }
- if ("DDAttachment_XVU9V89QOEM8".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof List) {
- attachmentList = (List<Map>) value;
- }
- }
- }
- // ========== 处理 hasInvoice = "否" 的情况(无发票) ==========
- if ("否".equals(hasInvoice)) {
- // 保存基础报销记录(无发票)
- InvoiceLibrary baseInvoice = new InvoiceLibrary();
- baseInvoice.setOaId(processInstanceId);
- baseInvoice.setSkzh(skzh);
- baseInvoice.setBusinessId(businessId);
- baseInvoice.setOaStatus("0");
- baseInvoice.setInvoiceStatus("0");
- baseInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- baseInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- baseInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- baseInvoice.setDetailAmount(detailje != null ? detailje : BigDecimal.ZERO);
- baseInvoice.setCreatedAt(LocalDateTime.now());
- baseInvoice.setUpdatedAt(LocalDateTime.now());
- baseInvoice.setInvoiceCode("");
- baseInvoice.setInvoiceNumber("");
- baseInvoice.setInvoiceType("");
- baseInvoice.setAmount(null);
- baseInvoice.setUserId(userId);
- baseInvoice.setTaxAmount(null);
- baseInvoice.setTotalAmount(null);
- baseInvoice.setBuyerName("");
- baseInvoice.setBuyerTaxId("");
- baseInvoice.setSellerName("");
- baseInvoice.setSellerTaxId("");
- baseInvoice.setFormName("合作商报销申请");
- baseInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- baseInvoice.setPayAccount(fkzh != null ? fkzh : "");
- baseInvoice.setBankName(yhqc != null ? yhqc : "");
- baseInvoice.setHasInvoice("否");
- baseInvoice.setSharedTaxAmount(null);
- baseInvoice.setSharedAmount(null);
- baseInvoice.setSharedRate(null);
- baseInvoice.setNote(bz != null ? bz : "");
- noInvoiceList.add(baseInvoice);
- log.info("收集无发票记录: 成本部门={}, 费用明细={}", cdbm, lbmx);
- continue;
- }
- // ========== 处理 hasInvoice = "被共享"(被共享发票,提供税率) ==========
- if ("被共享".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- // 标记存在被共享发票
- hasSharedInvoice = true;
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- // 解析发票数据
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- // ========== 获取税率(供共享发票使用) ==========
- Object taxRateObj = invoice.get("taxRate");
- Object kind = invoice.getString("kindName");
- if (taxRateObj != null) {
- String taxRateStr = String.valueOf(taxRateObj);
- String taxRateNum = taxRateStr.replace("%", "").trim();
- sharedKind = kind != null ? String.valueOf(kind) : "";
- sharedTaxRate = new BigDecimal(taxRateNum);
- sharedTaxRate = sharedTaxRate.divide(new BigDecimal("100"), 10, BigDecimal.ROUND_HALF_UP);
- log.info("从被共享发票获取税率: 原始={}, 转换后={}", taxRateStr, sharedTaxRate);
- }
- // 保存解析的数据供后续使用
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // ========== 校验1:抬头校验 ==========
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // ========== 校验2:发票号重复校验 ==========
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("被共享发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "被共享发票解析失败: " + e.getMessage());
- return McR.error("400", "被共享发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "是"(有发票,但不共享) ==========
- if ("是".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // 抬头校验
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // 发票号重复校验
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "发票解析失败: " + e.getMessage());
- return McR.error("400", "发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "共享"(共享发票,需要从被共享发票获取税率) ==========
- if ("共享".equals(hasInvoice)) {
- if (detailje != null && detailje.compareTo(BigDecimal.ZERO) > 0) {
- Map<String, Object> sharedData = new HashMap<>();
- sharedData.put("detailje", detailje);
- sharedData.put("lbmx", lbmx);
- sharedData.put("bz", bz);
- sharedData.put("cdbm", cdbm);
- sharedData.put("hasInvoice", hasInvoice);
- sharedInvoiceDataList.add(sharedData);
- log.info("收集共享发票数据: 金额={}, 成本部门={}", detailje, cdbm);
- }
- }
- }
- // ========== 检查:如果存在共享发票但没有被共享发票 ==========
- if (!sharedInvoiceDataList.isEmpty() && !hasSharedInvoice) {
- String errorMsg = "存在'共享'发票,但未找到'被共享'发票,无法获取税率信息,请确保至少有一行选择'被共享'并上传发票附件";
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- // ========== 抬头校验失败处理 ==========
- if (!invalidBuyerTaxIds.isEmpty()) {
- String errorMsg = "发票抬头有误,以下发票的购买方税号不在公司抬头库中:\n" + String.join("\n", invalidBuyerTaxIds);
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- BigDecimal totalAmount = BigDecimal.ZERO;
- // ========== 第二遍遍历:保存发票数据 ==========
- // 1. 保存有发票数据("是"和"被共享")
- for (Map<String, Object> parsedData : parsedInvoiceDataList) {
- JSONObject invoice = (JSONObject) parsedData.get("invoice");
- Object responseData = parsedData.get("responseData");
- String cdbm = (String) parsedData.get("cdbm");
- String lbmx = (String) parsedData.get("lbmx");
- String bz = (String) parsedData.get("bz");
- String hasInvoice = (String) parsedData.get("hasInvoice");
- Object detailjeObj = parsedData.get("detailje");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- allResults.add(responseData);
- BigDecimal amount = invoice.getBigDecimal("amount");
- if (amount != null) {
- totalAmount = totalAmount.add(amount);
- }
- InvoiceLibrary invoiceLibrary = new InvoiceLibrary();
- invoiceLibrary.setOaId(processInstanceId);
- invoiceLibrary.setSkzh(skzh);
- invoiceLibrary.setBusinessId(businessId);
- invoiceLibrary.setInvoiceCode(invoice.getString("code") != null ? invoice.getString("code") : "");
- invoiceLibrary.setInvoiceNumber(invoice.getString("serial") != null ? invoice.getString("serial") : "");
- invoiceLibrary.setInvoiceType(invoice.getString("kindName") != null ? invoice.getString("kindName") : "");
- String date = invoice.getString("date");
- if (StringUtils.isNotBlank(date)) {
- try {
- invoiceLibrary.setInvoiceDate(LocalDate.parse(date.trim()));
- } catch (Exception e) {
- log.warn("日期解析失败: {}", date);
- }
- }
- invoiceLibrary.setAmount(invoice.getBigDecimal("excludingTax") != null ? invoice.getBigDecimal("excludingTax") : BigDecimal.ZERO);
- invoiceLibrary.setTaxAmount(invoice.getBigDecimal("tax") != null ? invoice.getBigDecimal("tax") : BigDecimal.ZERO);
- invoiceLibrary.setTotalAmount(invoice.getBigDecimal("amount") != null ? invoice.getBigDecimal("amount") : BigDecimal.ZERO);
- invoiceLibrary.setBuyerName(invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "");
- invoiceLibrary.setBuyerTaxId(invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "");
- invoiceLibrary.setSellerName(invoice.getString("sellerName") != null ? invoice.getString("sellerName") : "");
- invoiceLibrary.setSellerTaxId(invoice.getString("sellerTaxId") != null ? invoice.getString("sellerTaxId") : "");
- invoiceLibrary.setOaStatus("0");
- invoiceLibrary.setNote(bz != null ? bz : "");
- invoiceLibrary.setUserId(userId);
- invoiceLibrary.setFormName("合作商报销申请");
- invoiceLibrary.setPaySubject(fkzhxx != null ? fkzhxx : "");
- invoiceLibrary.setPayAccount(fkzh != null ? fkzh : "");
- invoiceLibrary.setBankName(yhqc != null ? yhqc : "");
- invoiceLibrary.setInvoiceStatus("0");
- invoiceLibrary.setHasInvoice(hasInvoice);
- if (StringUtils.isNotBlank(detailje)) {
- try {
- invoiceLibrary.setDetailAmount(new BigDecimal(detailje));
- } catch (NumberFormatException e) {
- log.error("detailJe格式转换失败: {}", detailje);
- invoiceLibrary.setDetailAmount(null);
- }
- } else {
- invoiceLibrary.setDetailAmount(null);
- }
- invoiceLibrary.setSharedTaxAmount(null);
- invoiceLibrary.setSharedAmount(null);
- invoiceLibrary.setSharedRate(null);
- invoiceLibrary.setAccountTitle(lbmx != null ? lbmx : "");
- invoiceLibrary.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- invoiceLibrary.setCreatedAt(LocalDateTime.now());
- invoiceLibrary.setUpdatedAt(LocalDateTime.now());
- if (cdbm != null && StringUtils.isNotBlank(cdbm)) {
- invoiceLibrary.setDep(cdbm);
- } else {
- invoiceLibrary.setDep(bm != null ? bm : "");
- }
- invoiceList.add(invoiceLibrary);
- }
- // 2. 保存共享发票数据(使用从"被共享"发票获取的税率)
- if (!sharedInvoiceDataList.isEmpty()) {
- // 检查税率是否有效
- if (sharedTaxRate.compareTo(BigDecimal.ZERO) == 0) {
- log.warn("共享发票税率未获取到,使用默认税率0");
- }
- for (Map<String, Object> sharedData : sharedInvoiceDataList) {
- Object detailjeObj = sharedData.get("detailje");
- String cdbm = (String) sharedData.get("cdbm");
- String lbmx = (String) sharedData.get("lbmx");
- String bz = (String) sharedData.get("bz");
- String hasInvoice = (String) sharedData.get("hasInvoice");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- try {
- BigDecimal detailAmount = new BigDecimal(detailje);
- // 使用从"被共享"发票解析到的税率
- BigDecimal ocrTaxRate = sharedTaxRate;
- // 计算金额: 含税金额 / (1 + 税率) = 不含税金额
- BigDecimal divisor = BigDecimal.ONE.add(ocrTaxRate);
- BigDecimal sharedTaxAmount = detailAmount.divide(divisor, 2, BigDecimal.ROUND_HALF_UP);
- BigDecimal sharedAmount = detailAmount.subtract(sharedTaxAmount);
- BigDecimal sharedRate = ocrTaxRate;
- log.info("共享发票计算: 含税金额={}, 税率={}%, 不含税金额={}, 税额={}",
- detailAmount, ocrTaxRate.multiply(new BigDecimal("100")),
- sharedAmount, sharedTaxAmount);
- InvoiceLibrary sharedInvoice = new InvoiceLibrary();
- sharedInvoice.setOaId(processInstanceId);
- sharedInvoice.setSkzh(skzh);
- sharedInvoice.setBusinessId(businessId);
- sharedInvoice.setOaStatus("0");
- sharedInvoice.setInvoiceStatus("0");
- sharedInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- sharedInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- sharedInvoice.setNote(bz != null ? bz : "");
- sharedInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- sharedInvoice.setAmount(null);
- sharedInvoice.setTaxAmount(null);
- sharedInvoice.setTotalAmount(null);
- sharedInvoice.setUserId(userId);
- sharedInvoice.setFormName("合作商报销申请");
- sharedInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- sharedInvoice.setPayAccount(fkzh != null ? fkzh : "");
- sharedInvoice.setBankName(yhqc != null ? yhqc : "");
- sharedInvoice.setHasInvoice(hasInvoice);
- sharedInvoice.setDetailAmount(detailAmount);
- sharedInvoice.setSharedTaxAmount(sharedAmount);
- sharedInvoice.setSharedAmount(sharedTaxAmount);
- sharedInvoice.setSharedRate(String.valueOf(sharedRate));
- sharedInvoice.setCreatedAt(LocalDateTime.now());
- sharedInvoice.setUpdatedAt(LocalDateTime.now());
- sharedInvoice.setInvoiceCode("");
- sharedInvoice.setInvoiceNumber("");
- sharedInvoice.setInvoiceType(sharedKind);
- sharedInvoice.setBuyerName("");
- sharedInvoice.setBuyerTaxId("");
- sharedInvoice.setSellerName("");
- sharedInvoice.setSellerTaxId("");
- invoiceList.add(sharedInvoice);
- log.info("成功保存共享发票记录,成本部门: {}", cdbm);
- } catch (NumberFormatException e) {
- log.error("共享发票金额格式转换失败: detailje={}", detailje, e);
- } catch (Exception e) {
- log.error("共享发票处理失败", e);
- }
- }
- }
- // 保存所有记录(无发票 + 有发票)
- if (!noInvoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : noInvoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条无发票记录", noInvoiceList.size());
- addWorkflowComment(processInstanceId, userId, "报销总金额:" + mainJe);
- }
- if (!invoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : invoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条发票记录(有发票: {}, 共享发票: {})",
- invoiceList.size(),
- parsedInvoiceDataList.stream().filter(d -> "是".equals(d.get("hasInvoice")) || "被共享".equals(d.get("hasInvoice"))).count(),
- sharedInvoiceDataList.size());
- addWorkflowComment1(processInstanceId, userId, totalAmount);
- }
- if (!allResults.isEmpty()) {
- return McR.success(allResults);
- } else {
- return McR.success();
- }
- } catch (Exception e) {
- log.error("解析子表数据失败", e);
- return McR.error("300", "解析数据失败: " + e.getMessage());
- }
- }
- }
- return McR.success();
- } catch (Exception e) {
- log.error("处理审批实例失败", e);
- return McR.error("500", "系统处理失败: " + e.getMessage());
- }
- }
- @Override
- public McR invoiceLibrary5(Map map) {
- log.info("接收到的参数: {}", map);
- try {
- String processInstanceId = UtilMap.getString(map, "processInstanceId");
- if (StringUtils.isBlank(processInstanceId)) {
- log.error("processInstanceId为空");
- return McR.error("400", "审批实例ID不能为空");
- }
- // 获取审批实例信息
- String accessToken = ddClient.getAccessToken();
- Map processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, processInstanceId);
- if (processInstance == null) {
- log.error("获取审批实例失败: {}", processInstanceId);
- return McR.error("500", "获取审批实例信息失败");
- }
- String userId = (String) processInstance.get("originatorUserId");
- String businessId = (String) processInstance.get("businessId");
- log.info("审批人ID: {}", userId);
- List<Map> formComponentValues = (List<Map>) processInstance.get("formComponentValues");
- if (formComponentValues == null || formComponentValues.isEmpty()) {
- log.warn("表单数据为空");
- return McR.error("400", "表单数据为空");
- }
- // 存储主表的字段值
- String bm = null;
- String bxlb = null;
- String fkzhxx = null;
- String fkzh = null;
- String yhqc = null;
- String skzh = null;
- BigDecimal mainJe = null;
- // 遍历收集主表字段值
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- Object value = formComponentValue.get("value");
- if ("MoneyField_1435GRXGYPA80".equals(id)) {
- if (value instanceof BigDecimal) {
- mainJe = (BigDecimal) value;
- } else if (value != null) {
- try {
- mainJe = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- mainJe = BigDecimal.ZERO;
- }
- }
- }
- if ("RecipientAccountField_1PNXMXLXIW000".equals(id)) {
- skzh = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_1UX0JVU89Q000".equals(id)) {
- bxlb = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_1BJT6UY9Y5B40".equals(id)) {
- bm = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_1D77TV92WXEO0".equals(id)) {
- fkzhxx = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_3CQPLF9BVBO0".equals(id)) {
- fkzh = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_YSFYQQ305SG0".equals(id)) {
- yhqc = value != null ? String.valueOf(value) : "";
- }
- }
- // 遍历子表数据
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- if ("TableField_169TONKJ3KW00".equals(id)) {
- String tableFieldValue = String.valueOf(formComponentValue.get("value"));
- log.info("子表数据: {}", tableFieldValue);
- try {
- List<Map> tableRows = (List<Map>) JSONObject.parse(tableFieldValue);
- if (tableRows == null || tableRows.isEmpty()) {
- log.warn("子表数据为空");
- return McR.error("400", "发票明细为空");
- }
- // 用于存储无发票记录和发票记录
- List<InvoiceLibrary> noInvoiceList = new ArrayList<>();
- List<InvoiceLibrary> invoiceList = new ArrayList<>();
- List<Object> allResults = new ArrayList<>();
- // 存储有发票("是"和"被共享")的解析数据
- List<Map<String, Object>> parsedInvoiceDataList = new ArrayList<>();
- // 存储共享发票数据
- List<Map<String, Object>> sharedInvoiceDataList = new ArrayList<>();
- // 用于校验的集合
- List<String> invoiceNumberList = new ArrayList<>();
- Map<String, String> invoiceNumberToFileName = new HashMap<>();
- List<String> invalidBuyerTaxIds = new ArrayList<>();
- // 存储税率信息(从"被共享"发票中获取)
- BigDecimal sharedTaxRate = BigDecimal.ZERO;
- String sharedKind = "";
- boolean hasSharedInvoice = false; // 标记是否存在"被共享"发票
- // ========== 第一遍遍历:收集数据并查找"被共享"发票 ==========
- for (Map row : tableRows) {
- List<Map> rowValues = (List<Map>) row.get("rowValue");
- String hasInvoice = null;
- String cdbm = null;
- String lbmx = null;
- String bz = null;
- BigDecimal detailje = null;
- List<Map> attachmentList = null;
- for (Map rowItem : rowValues) {
- String key = String.valueOf(rowItem.get("key"));
- if ("DDSelectField_1G6XMYA3FMCG0".equals(key)) {
- Object value = rowItem.get("value");
- hasInvoice = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_17K1Q5M73YKG0".equals(key)) {
- Object value = rowItem.get("value");
- lbmx = value != null ? String.valueOf(value) : "";
- }
- if ("TextareaField_207VNZ39WOCG0".equals(key)) {
- Object value = rowItem.get("value");
- bz = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_KJIWTTZLE800".equals(key)) {
- Object value = rowItem.get("value");
- cdbm = value != null ? String.valueOf(value) : "";
- }
- if ("NumberField_L0ALER5S6180".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof BigDecimal) {
- detailje = (BigDecimal) value;
- } else if (value != null) {
- try {
- detailje = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value);
- detailje = BigDecimal.ZERO;
- }
- } else {
- detailje = BigDecimal.ZERO;
- }
- }
- if ("DDAttachment_EPL7ILUTD0G0".equals(key)) {
- Object value = rowItem.get("value");
- if (value instanceof List) {
- attachmentList = (List<Map>) value;
- }
- }
- }
- // ========== 处理 hasInvoice = "否" 的情况(无发票) ==========
- if ("否".equals(hasInvoice)) {
- // 保存基础报销记录(无发票)
- InvoiceLibrary baseInvoice = new InvoiceLibrary();
- baseInvoice.setOaId(processInstanceId);
- baseInvoice.setSkzh(skzh);
- baseInvoice.setBusinessId(businessId);
- baseInvoice.setOaStatus("0");
- baseInvoice.setInvoiceStatus("0");
- baseInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- baseInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- baseInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- baseInvoice.setDetailAmount(detailje != null ? detailje : BigDecimal.ZERO);
- baseInvoice.setCreatedAt(LocalDateTime.now());
- baseInvoice.setUpdatedAt(LocalDateTime.now());
- baseInvoice.setInvoiceCode("");
- baseInvoice.setInvoiceNumber("");
- baseInvoice.setInvoiceType("");
- baseInvoice.setAmount(null);
- baseInvoice.setTaxAmount(null);
- baseInvoice.setTotalAmount(null);
- baseInvoice.setBuyerName("");
- baseInvoice.setBuyerTaxId("");
- baseInvoice.setSellerName("");
- baseInvoice.setSellerTaxId("");
- baseInvoice.setUserId(userId);
- baseInvoice.setFormName("出差费用报销");
- baseInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- baseInvoice.setPayAccount(fkzh != null ? fkzh : "");
- baseInvoice.setBankName(yhqc != null ? yhqc : "");
- baseInvoice.setHasInvoice("否");
- baseInvoice.setSharedTaxAmount(null);
- baseInvoice.setSharedAmount(null);
- baseInvoice.setSharedRate(null);
- baseInvoice.setNote(bz != null ? bz : "");
- noInvoiceList.add(baseInvoice);
- log.info("收集无发票记录: 成本部门={}, 费用明细={}", cdbm, lbmx);
- continue;
- }
- // ========== 处理 hasInvoice = "被共享"(被共享发票,提供税率) ==========
- if ("被共享".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- // 标记存在被共享发票
- hasSharedInvoice = true;
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- // 解析发票数据
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- // ========== 获取税率(供共享发票使用) ==========
- Object taxRateObj = invoice.get("taxRate");
- Object kind = invoice.getString("kindName");
- if (taxRateObj != null) {
- String taxRateStr = String.valueOf(taxRateObj);
- String taxRateNum = taxRateStr.replace("%", "").trim();
- sharedKind = kind != null ? String.valueOf(kind) : "";
- sharedTaxRate = new BigDecimal(taxRateNum);
- sharedTaxRate = sharedTaxRate.divide(new BigDecimal("100"), 10, BigDecimal.ROUND_HALF_UP);
- log.info("从被共享发票获取税率: 原始={}, 转换后={}", taxRateStr, sharedTaxRate);
- }
- // 保存解析的数据供后续使用
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // ========== 校验1:抬头校验 ==========
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方名称为空,无法校验公司抬头", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方名称为空, 文件名={}", fileName);
- }
- // ========== 校验2:发票号重复校验 ==========
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("被共享发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "被共享发票解析失败: " + e.getMessage());
- return McR.error("400", "被共享发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "是"(有发票,但不共享) ==========
- if ("是".equals(hasInvoice) && attachmentList != null && !attachmentList.isEmpty()) {
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- for (int invoiceIndex = 0; invoiceIndex < resultArray.size(); invoiceIndex++) {
- JSONObject invoice = resultArray.getJSONObject(invoiceIndex);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("bz", bz);
- parsedData.put("lbmx", lbmx);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("cdbm", cdbm);
- parsedData.put("hasInvoice", hasInvoice);
- parsedData.put("detailje", detailje);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // 抬头校验
- if (!isTitleValidationExempt(invoice.getString("name")) && StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else if (!isTitleValidationExempt(invoice.getString("name"))) {
- String errorInfo = String.format("发票: %s, 购买方税号为空", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方税号为空, 文件名={}", fileName);
- }
- // 发票号重复校验
- if (StringUtils.isNotBlank(invoiceNumber)) {
- InvoiceLibrary existingInvoice = baseMapper.selectByInvoiceNumber(invoiceNumber);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- }
- }
- }
- } catch (Exception e) {
- log.error("发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- terminateWorkflow(accessToken, processInstanceId, userId, "发票解析失败: " + e.getMessage());
- return McR.error("400", "发票解析失败: " + e.getMessage());
- }
- }
- continue;
- }
- // ========== 处理 hasInvoice = "共享"(共享发票,需要从被共享发票获取税率) ==========
- if ("共享".equals(hasInvoice)) {
- if (detailje != null && detailje.compareTo(BigDecimal.ZERO) > 0) {
- Map<String, Object> sharedData = new HashMap<>();
- sharedData.put("detailje", detailje);
- sharedData.put("lbmx", lbmx);
- sharedData.put("bz", bz);
- sharedData.put("cdbm", cdbm);
- sharedData.put("hasInvoice", hasInvoice);
- sharedInvoiceDataList.add(sharedData);
- log.info("收集共享发票数据: 金额={}, 成本部门={}", detailje, cdbm);
- }
- }
- }
- // ========== 检查:如果存在共享发票但没有被共享发票 ==========
- if (!sharedInvoiceDataList.isEmpty() && !hasSharedInvoice) {
- String errorMsg = "存在'共享'发票,但未找到'被共享'发票,无法获取税率信息,请确保至少有一行选择'被共享'并上传发票附件";
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- // ========== 抬头校验失败处理 ==========
- if (!invalidBuyerTaxIds.isEmpty()) {
- String errorMsg = "发票抬头有误,以下发票的购买方税号不在公司抬头库中:\n" + String.join("\n", invalidBuyerTaxIds);
- log.error(errorMsg);
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- BigDecimal totalAmount = BigDecimal.ZERO;
- // ========== 第二遍遍历:保存发票数据 ==========
- // 1. 保存有发票数据("是"和"被共享")
- for (Map<String, Object> parsedData : parsedInvoiceDataList) {
- JSONObject invoice = (JSONObject) parsedData.get("invoice");
- Object responseData = parsedData.get("responseData");
- String cdbm = (String) parsedData.get("cdbm");
- String lbmx = (String) parsedData.get("lbmx");
- String bz = (String) parsedData.get("bz");
- String hasInvoice = (String) parsedData.get("hasInvoice");
- Object detailjeObj = parsedData.get("detailje");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- allResults.add(responseData);
- BigDecimal amount = invoice.getBigDecimal("amount");
- if (amount != null) {
- totalAmount = totalAmount.add(amount);
- }
- InvoiceLibrary invoiceLibrary = new InvoiceLibrary();
- invoiceLibrary.setOaId(processInstanceId);
- invoiceLibrary.setSkzh(skzh);
- invoiceLibrary.setBusinessId(businessId);
- invoiceLibrary.setUserId(userId);
- invoiceLibrary.setInvoiceCode(invoice.getString("code") != null ? invoice.getString("code") : "");
- invoiceLibrary.setInvoiceNumber(invoice.getString("serial") != null ? invoice.getString("serial") : "");
- invoiceLibrary.setInvoiceType(invoice.getString("kindName") != null ? invoice.getString("kindName") : "");
- String date = invoice.getString("date");
- if (StringUtils.isNotBlank(date)) {
- try {
- invoiceLibrary.setInvoiceDate(LocalDate.parse(date.trim()));
- } catch (Exception e) {
- log.warn("日期解析失败: {}", date);
- }
- }
- invoiceLibrary.setAmount(invoice.getBigDecimal("excludingTax") != null ? invoice.getBigDecimal("excludingTax") : BigDecimal.ZERO);
- invoiceLibrary.setTaxAmount(invoice.getBigDecimal("tax") != null ? invoice.getBigDecimal("tax") : BigDecimal.ZERO);
- invoiceLibrary.setTotalAmount(invoice.getBigDecimal("amount") != null ? invoice.getBigDecimal("amount") : BigDecimal.ZERO);
- invoiceLibrary.setBuyerName(invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "");
- invoiceLibrary.setBuyerTaxId(invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "");
- invoiceLibrary.setSellerName(invoice.getString("sellerName") != null ? invoice.getString("sellerName") : "");
- invoiceLibrary.setSellerTaxId(invoice.getString("sellerTaxId") != null ? invoice.getString("sellerTaxId") : "");
- invoiceLibrary.setOaStatus("0");
- invoiceLibrary.setNote(bz != null ? bz : "");
- invoiceLibrary.setFormName("出差费用报销");
- invoiceLibrary.setPaySubject(fkzhxx != null ? fkzhxx : "");
- invoiceLibrary.setPayAccount(fkzh != null ? fkzh : "");
- invoiceLibrary.setBankName(yhqc != null ? yhqc : "");
- invoiceLibrary.setInvoiceStatus("0");
- invoiceLibrary.setHasInvoice(hasInvoice);
- if (StringUtils.isNotBlank(detailje)) {
- try {
- invoiceLibrary.setDetailAmount(new BigDecimal(detailje));
- } catch (NumberFormatException e) {
- log.error("detailJe格式转换失败: {}", detailje);
- invoiceLibrary.setDetailAmount(null);
- }
- } else {
- invoiceLibrary.setDetailAmount(null);
- }
- invoiceLibrary.setSharedTaxAmount(null);
- invoiceLibrary.setSharedAmount(null);
- invoiceLibrary.setSharedRate(null);
- invoiceLibrary.setAccountTitle(lbmx != null ? lbmx : "");
- invoiceLibrary.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- invoiceLibrary.setCreatedAt(LocalDateTime.now());
- invoiceLibrary.setUpdatedAt(LocalDateTime.now());
- if (cdbm != null && StringUtils.isNotBlank(cdbm)) {
- invoiceLibrary.setDep(cdbm);
- } else {
- invoiceLibrary.setDep(bm != null ? bm : "");
- }
- invoiceList.add(invoiceLibrary);
- }
- // 2. 保存共享发票数据(使用从"被共享"发票获取的税率)
- if (!sharedInvoiceDataList.isEmpty()) {
- // 检查税率是否有效
- if (sharedTaxRate.compareTo(BigDecimal.ZERO) == 0) {
- log.warn("共享发票税率未获取到,使用默认税率0");
- }
- for (Map<String, Object> sharedData : sharedInvoiceDataList) {
- Object detailjeObj = sharedData.get("detailje");
- String cdbm = (String) sharedData.get("cdbm");
- String lbmx = (String) sharedData.get("lbmx");
- String bz = (String) sharedData.get("bz");
- String hasInvoice = (String) sharedData.get("hasInvoice");
- String detailje = detailjeObj != null ? String.valueOf(detailjeObj) : "";
- try {
- BigDecimal detailAmount = new BigDecimal(detailje);
- // 使用从"被共享"发票解析到的税率
- BigDecimal ocrTaxRate = sharedTaxRate;
- // 计算金额: 含税金额 / (1 + 税率) = 不含税金额
- BigDecimal divisor = BigDecimal.ONE.add(ocrTaxRate);
- BigDecimal sharedTaxAmount = detailAmount.divide(divisor, 2, BigDecimal.ROUND_HALF_UP);
- BigDecimal sharedAmount = detailAmount.subtract(sharedTaxAmount);
- BigDecimal sharedRate = ocrTaxRate;
- log.info("共享发票计算: 含税金额={}, 税率={}%, 不含税金额={}, 税额={}",
- detailAmount, ocrTaxRate.multiply(new BigDecimal("100")),
- sharedAmount, sharedTaxAmount);
- InvoiceLibrary sharedInvoice = new InvoiceLibrary();
- sharedInvoice.setOaId(processInstanceId);
- sharedInvoice.setSkzh(skzh);
- sharedInvoice.setBusinessId(businessId);
- sharedInvoice.setOaStatus("0");
- sharedInvoice.setInvoiceStatus("0");
- sharedInvoice.setDep(cdbm != null && StringUtils.isNotBlank(cdbm) ? cdbm : (bm != null ? bm : ""));
- sharedInvoice.setAccountTitle(lbmx != null ? lbmx : "");
- sharedInvoice.setNote(bz != null ? bz : "");
- sharedInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- sharedInvoice.setAmount(null);
- sharedInvoice.setTaxAmount(null);
- sharedInvoice.setTotalAmount(null);
- sharedInvoice.setUserId(userId);
- sharedInvoice.setFormName("出差费用报销");
- sharedInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- sharedInvoice.setPayAccount(fkzh != null ? fkzh : "");
- sharedInvoice.setBankName(yhqc != null ? yhqc : "");
- sharedInvoice.setHasInvoice(hasInvoice);
- sharedInvoice.setDetailAmount(detailAmount);
- sharedInvoice.setSharedTaxAmount(sharedAmount);
- sharedInvoice.setSharedAmount(sharedTaxAmount);
- sharedInvoice.setSharedRate(String.valueOf(sharedRate));
- sharedInvoice.setCreatedAt(LocalDateTime.now());
- sharedInvoice.setUpdatedAt(LocalDateTime.now());
- sharedInvoice.setInvoiceCode("");
- sharedInvoice.setInvoiceNumber("");
- sharedInvoice.setInvoiceType(sharedKind);
- sharedInvoice.setBuyerName("");
- sharedInvoice.setBuyerTaxId("");
- sharedInvoice.setSellerName("");
- sharedInvoice.setSellerTaxId("");
- invoiceList.add(sharedInvoice);
- log.info("成功保存共享发票记录,成本部门: {}", cdbm);
- } catch (NumberFormatException e) {
- log.error("共享发票金额格式转换失败: detailje={}", detailje, e);
- } catch (Exception e) {
- log.error("共享发票处理失败", e);
- }
- }
- }
- // 保存所有记录(无发票 + 有发票)
- if (!noInvoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : noInvoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条无发票记录", noInvoiceList.size());
- addWorkflowComment(processInstanceId, userId, "报销总金额:" + mainJe);
- }
- if (!invoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : invoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条发票记录(有发票: {}, 共享发票: {})",
- invoiceList.size(),
- parsedInvoiceDataList.stream().filter(d -> "是".equals(d.get("hasInvoice")) || "被共享".equals(d.get("hasInvoice"))).count(),
- sharedInvoiceDataList.size());
- addWorkflowComment1(processInstanceId, userId, totalAmount);
- }
- if (!allResults.isEmpty()) {
- return McR.success(allResults);
- } else {
- return McR.success();
- }
- } catch (Exception e) {
- log.error("解析子表数据失败", e);
- return McR.error("300", "解析数据失败: " + e.getMessage());
- }
- }
- }
- return McR.success();
- } catch (Exception e) {
- log.error("处理审批实例失败", e);
- return McR.error("500", "系统处理失败: " + e.getMessage());
- }
- }
- @Override
- public McR invoiceLibrary6(Map map) {
- log.info("接收到的参数: {}", map);
- try {
- String processInstanceId = UtilMap.getString(map, "processInstanceId");
- if (StringUtils.isBlank(processInstanceId)) {
- log.error("processInstanceId为空");
- return McR.error("400", "审批实例ID不能为空");
- }
- // 获取审批实例信息
- String accessToken = ddClient.getAccessToken();
- Map processInstance = ddClientWorkflow.getProcessInstanceId(accessToken, processInstanceId);
- if (processInstance == null) {
- log.error("获取审批实例失败: {}", processInstanceId);
- return McR.error("500", "获取审批实例信息失败");
- }
- String userId = (String) processInstance.get("originatorUserId");
- log.info("审批人ID: {}", userId);
- List<Map> formComponentValues = (List<Map>) processInstance.get("formComponentValues");
- if (formComponentValues == null || formComponentValues.isEmpty()) {
- log.warn("表单数据为空");
- return McR.error("400", "表单数据为空");
- }
- // 存储主表的字段值
- String zt = null;
- BigDecimal mainJe = null;
- String bxlb = null;
- String bm = null;
- String fkzhxx = null;
- String fkzh = null;
- String yhqc = null;
- String sfct = null;
- // 遍历收集主表字段值
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- Object value = formComponentValue.get("value");
- if ("DDSelectField_VWIFJ1JZK0W0".equals(id)) {
- zt = value != null ? String.valueOf(value) : "";
- }
- if ("MoneyField_3QS10ALLABU0".equals(id)) {
- if (value instanceof BigDecimal) {
- mainJe = (BigDecimal) value;
- } else if (value != null) {
- try {
- mainJe = new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- log.error("金额格式转换失败: {}", value, e);
- mainJe = BigDecimal.ZERO;
- }
- }
- }
- // 如果需要这两个字段,请取消注释并填写正确的字段ID
- if ("DDSelectField_YLLB7AD1ATS0".equals(id)) {
- bxlb = value != null ? String.valueOf(value) : "";
- }
- if ("DepartmentField_RF93A6VHA9C0".equals(id)) {
- bm = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_IQXAGL7Z8XC0".equals(id)) {
- fkzhxx = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_3W9CLQOZZ540".equals(id)) {
- fkzh = value != null ? String.valueOf(value) : "";
- }
- if ("TextField_9PJ4JW8KTCS0".equals(id)) {
- yhqc = value != null ? String.valueOf(value) : "";
- }
- if ("DDSelectField_21L6KT7DT70G0".equals(id)) {
- sfct = value != null ? String.valueOf(value) : "否";
- }
- }
- // 判断主表状态 - 无发票
- if ("否".equals(zt)) {
- // Map<String, Object> result = new HashMap<>();
- // result.put("je", mainJe);
- // result.put("status", "否");
- // result.put("message", "无发票报销");
- // 保存基础报销记录
- InvoiceLibrary baseInvoice = new InvoiceLibrary();
- baseInvoice.setOaId(processInstanceId);
- baseInvoice.setOaStatus("0");
- baseInvoice.setInvoiceStatus("0");
- baseInvoice.setDep(bm != null ? bm : "");
- baseInvoice.setAccountTitle(bxlb != null ? bxlb : "");
- baseInvoice.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- baseInvoice.setCreatedAt(LocalDateTime.now());
- baseInvoice.setUpdatedAt(LocalDateTime.now());
- baseInvoice.setInvoiceCode("");
- baseInvoice.setInvoiceNumber("");
- baseInvoice.setInvoiceType("");
- baseInvoice.setAmount(BigDecimal.ZERO);
- baseInvoice.setTaxAmount(BigDecimal.ZERO);
- baseInvoice.setTotalAmount(BigDecimal.ZERO);
- baseInvoice.setBuyerName("");
- baseInvoice.setFormName("批量付款申请(一对多)");
- baseInvoice.setPaySubject(fkzhxx != null ? fkzhxx : "");
- baseInvoice.setPayAccount(fkzh != null ? fkzh : "");
- baseInvoice.setBankName(yhqc != null ? yhqc : "");
- baseInvoice.setIsLongTerm(sfct != null ? sfct : "");
- baseInvoice.setBuyerTaxId("");
- baseInvoice.setSellerName("");
- baseInvoice.setSellerTaxId("");
- baseMapper.insert(baseInvoice);
- addWorkflowComment(processInstanceId, userId, "报销总金额:"+mainJe);
- return McR.success();
- }
- // 判断主表状态 - 有发票
- if ("是".equals(zt)) {
- for (Map formComponentValue : formComponentValues) {
- String id = String.valueOf(formComponentValue.get("id"));
- if ("DDAttachment_1C1GRYIRFJOG0".equals(id)) {
- // 获取附件value(它是一个JSON字符串)
- Object attachmentValue = formComponentValue.get("value");
- List<Map> attachmentList = null;
- // 解析附件JSON字符串
- if (attachmentValue != null) {
- String attachmentJsonStr = String.valueOf(attachmentValue);
- if (StringUtils.isNotBlank(attachmentJsonStr)) {
- try {
- attachmentList = JSONObject.parseArray(attachmentJsonStr, Map.class);
- log.info("解析附件列表成功,共{}个附件", attachmentList != null ? attachmentList.size() : 0);
- } catch (Exception e) {
- log.error("解析附件JSON失败: {}", attachmentJsonStr, e);
- return McR.error("400", "附件格式解析失败");
- }
- }
- }
- try {
- List<Object> allResults = new ArrayList<>();
- List<InvoiceLibrary> invoiceList = new ArrayList<>();
- // 用于校验的集合
- List<String> invoiceNumberList = new ArrayList<>();
- Map<String, String> invoiceNumberToFileName = new HashMap<>();
- List<String> invalidBuyerTaxIds = new ArrayList<>();
- // 存储第一遍解析的发票数据,避免重复解析
- List<Map<String, Object>> parsedInvoiceDataList = new ArrayList<>();
- if (attachmentList != null && !attachmentList.isEmpty()) {
- for (Map attachment : attachmentList) {
- String fileId = UtilMap.getString(attachment, "fileId");
- String fileType = UtilMap.getString(attachment, "fileType");
- String fileName = UtilMap.getString(attachment, "fileName");
- // 参数校验
- if (StringUtils.isBlank(fileId) || StringUtils.isBlank(fileType)) {
- log.warn("文件信息不完整: fileId={}, fileType={}, fileName={}", fileId, fileType, fileName);
- continue;
- }
- try {
- String filePath = downloadPath + fileId + "." + fileType;
- downloadDdFile(processInstanceId, fileId, filePath);
- String hz = "qw/files/" + fileId + "." + fileType;
- Map fileMap = new HashMap();
- fileMap.put("url", url + hz);
- fileMap.put("size", new File(filePath).length() / 1024f / 1024f);
- fileMap.put("isPdf", "pdf".equalsIgnoreCase(fileType));
- McR<Object> result = processMixedInvoice(fileMap);
- Object responseData = result.getData();
- // 解析发票数据
- if (responseData != null) {
- String jsonStr = JSONObject.toJSONString(responseData);
- JSONObject jsonObject = JSONObject.parseObject(jsonStr);
- JSONArray resultArray = jsonObject.getJSONArray("result");
- if (resultArray != null && !resultArray.isEmpty()) {
- JSONObject invoice = resultArray.getJSONObject(0);
- String invoiceNumber = invoice.getString("serial") != null ? invoice.getString("serial") : "";
- String buyerTaxId = invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "";
- String buyerName = invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "";
- // 保存解析的数据供后续使用
- Map<String, Object> parsedData = new HashMap<>();
- parsedData.put("invoice", invoice);
- parsedData.put("fileId", fileId);
- parsedData.put("fileName", fileName);
- parsedData.put("fileType", fileType);
- parsedData.put("responseData", responseData);
- parsedInvoiceDataList.add(parsedData);
- // ========== 校验1:抬头校验 ==========
- if (StringUtils.isNotBlank(buyerName)) {
- CompanyTitle existingTitle = companyTitleMapper.selectByTaxId(buyerName);
- if (existingTitle == null) {
- String errorInfo = String.format("发票: %s, 购买方名称: %s, 税号: %s",
- fileName, buyerName, buyerTaxId);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 税号={} 不存在于抬头库, 文件名={}", buyerTaxId, fileName);
- }
- } else {
- String errorInfo = String.format("发票: %s, 购买方税号为空", fileName);
- invalidBuyerTaxIds.add(errorInfo);
- log.warn("抬头校验失败: 购买方税号为空, 文件名={}", fileName);
- }
- // ========== 校验2:发票号重复校验(与数据库中的发票号比较) ==========
- if (StringUtils.isNotBlank(invoiceNumber)) {
- // 检查数据库中是否已存在相同的发票号
- LambdaQueryWrapper<InvoiceLibrary> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.eq(InvoiceLibrary::getInvoiceNumber, invoiceNumber);
- InvoiceLibrary existingInvoice = baseMapper.selectOne(queryWrapper);
- if (existingInvoice != null) {
- String errorMsg = String.format("发票号重复: %s (当前文件: %s, 已存在于OA审批单: %s)",
- invoiceNumber, fileName, existingInvoice.getOaId());
- log.error(errorMsg);
- // 终止流程并回写原因
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- // 同时检查同一审批单内是否有重复(防止同一单据内上传重复发票)
- if (invoiceNumberList.contains(invoiceNumber)) {
- String errorMsg = String.format("当前审批单内发票号重复: %s (当前文件: %s, 已存在文件: %s)",
- invoiceNumber, fileName, invoiceNumberToFileName.get(invoiceNumber));
- log.error(errorMsg);
- // 终止流程并回写原因
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- invoiceNumberList.add(invoiceNumber);
- invoiceNumberToFileName.put(invoiceNumber, fileName);
- }
- } else {
- log.warn("发票解析结果为空: fileName={}", fileName);
- }
- } else {
- log.warn("发票OCR识别结果为空: fileName={}", fileName);
- }
- } catch (Exception e) {
- log.error("发票解析异常: fileId={}, fileName={}", fileId, fileName, e);
- String errorMsg = "发票解析失败: " + e.getMessage();
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- }
- } else {
- log.warn("附件列表为空");
- return McR.error("400", "请上传发票附件");
- }
- // ========== 抬头校验失败处理 ==========
- if (!invalidBuyerTaxIds.isEmpty()) {
- String errorMsg = "发票抬头有误,以下发票的购买方税号不在公司抬头库中:\n" + String.join("\n", invalidBuyerTaxIds);
- log.error(errorMsg);
- // 终止流程
- terminateWorkflow(accessToken, processInstanceId, userId, errorMsg);
- return McR.error("400", errorMsg);
- }
- BigDecimal totalAmount = BigDecimal.ZERO;
- // ========== 第二遍遍历:保存发票数据(校验通过后) ==========
- for (Map<String, Object> parsedData : parsedInvoiceDataList) {
- JSONObject invoice = (JSONObject) parsedData.get("invoice");
- Object responseData = parsedData.get("responseData");
- allResults.add(responseData);
- BigDecimal amount = invoice.getBigDecimal("amount");
- if (amount != null) {
- totalAmount = totalAmount.add(amount);
- }
- // 构建InvoiceLibrary对象
- InvoiceLibrary invoiceLibrary = new InvoiceLibrary();
- invoiceLibrary.setOaId(processInstanceId);
- invoiceLibrary.setInvoiceCode(invoice.getString("code") != null ? invoice.getString("code") : "");
- invoiceLibrary.setInvoiceNumber(invoice.getString("serial") != null ? invoice.getString("serial") : "");
- invoiceLibrary.setInvoiceType(invoice.getString("kindName") != null ? invoice.getString("kindName") : "");
- String date = invoice.getString("date");
- if (StringUtils.isNotBlank(date)) {
- try {
- invoiceLibrary.setInvoiceDate(LocalDate.parse(date.trim()));
- } catch (Exception e) {
- log.warn("日期解析失败: {}", date, e);
- }
- }
- invoiceLibrary.setAmount(invoice.getBigDecimal("excludingTax") != null ? invoice.getBigDecimal("excludingTax") : BigDecimal.ZERO);
- invoiceLibrary.setTaxAmount(invoice.getBigDecimal("tax") != null ? invoice.getBigDecimal("tax") : BigDecimal.ZERO);
- invoiceLibrary.setTotalAmount(invoice.getBigDecimal("amount") != null ? invoice.getBigDecimal("amount") : BigDecimal.ZERO);
- invoiceLibrary.setBuyerName(invoice.getString("buyerName") != null ? invoice.getString("buyerName") : "");
- invoiceLibrary.setBuyerTaxId(invoice.getString("buyerTaxId") != null ? invoice.getString("buyerTaxId") : "");
- invoiceLibrary.setSellerName(invoice.getString("sellerName") != null ? invoice.getString("sellerName") : "");
- invoiceLibrary.setSellerTaxId(invoice.getString("sellerTaxId") != null ? invoice.getString("sellerTaxId") : "");
- invoiceLibrary.setOaStatus("0");
- invoiceLibrary.setInvoiceStatus("0");
- invoiceLibrary.setDep(bm != null ? bm : "");
- invoiceLibrary.setAccountTitle(bxlb != null ? bxlb : "");
- invoiceLibrary.setPayAmount(mainJe != null ? mainJe : BigDecimal.ZERO);
- invoiceLibrary.setCreatedAt(LocalDateTime.now());
- invoiceLibrary.setUpdatedAt(LocalDateTime.now());
- invoiceLibrary.setFormName("批量付款申请(一对多)");
- invoiceLibrary.setPaySubject(fkzhxx != null ? fkzhxx : "");
- invoiceLibrary.setPayAccount(fkzh != null ? fkzh : "");
- invoiceLibrary.setBankName(yhqc != null ? yhqc : "");
- invoiceLibrary.setIsLongTerm(sfct != null ? sfct : "");
- invoiceList.add(invoiceLibrary);
- }
- // 保存发票记录
- if (!invoiceList.isEmpty()) {
- for (InvoiceLibrary invoice : invoiceList) {
- baseMapper.insert(invoice);
- }
- log.info("成功保存 {} 条发票记录", invoiceList.size());
- } else {
- log.warn("没有有效的发票数据可保存");
- }
- addWorkflowComment1(processInstanceId, userId, totalAmount);
- return McR.success(allResults);
- } catch (Exception e) {
- log.error("解析子表数据失败", e);
- return McR.error("300", "解析数据失败: " + e.getMessage());
- }
- }
- }
- }
- return McR.success();
- } catch (Exception e) {
- log.error("处理审批实例失败", e);
- return McR.error("500", "系统处理失败: " + e.getMessage());
- }
- }
- @Override
- public void updateSfctByOaId(Map map) {
- try {
- String oaId = UtilMap.getString(map, "oaId");
- // String sfct = UtilMap.getString(map, "sfct");
- String fkzhxx = UtilMap.getString(map, "fkzhxx");
- String fkzh = UtilMap.getString(map, "fkzh");
- String yhqc = UtilMap.getString(map, "yhqc");
- // sfct = (sfct == null || sfct.trim().isEmpty()) ? "否" : sfct;
- // 创建更新条件
- LambdaUpdateWrapper<InvoiceLibrary> updateWrapper = new LambdaUpdateWrapper<>();
- updateWrapper.eq(InvoiceLibrary::getOaId, oaId)
- // .set(InvoiceLibrary::getIsLongTerm, sfct)
- .set(InvoiceLibrary::getPayAccount, fkzh)
- .set(InvoiceLibrary::getBankName, yhqc)
- .set(InvoiceLibrary::getPaySubject, fkzhxx)
- .set(InvoiceLibrary::getUpdatedAt, LocalDateTime.now());
- int updateCount = baseMapper.update(null, updateWrapper);
- if (updateCount > 0) {
- log.info("更新成功: 共更新{}条记录", updateCount);
- } else {
- log.warn("未找到符合条件的记录: oaId={}", oaId);
- }
- } catch (Exception e) {
- throw new RuntimeException("更新OA状态失败", e);
- }
- }
- /**
- * 添加钉钉审批评论
- */
- private void addWorkflowComment(String processInstanceId, String userId, Object totalAmount) {
- try {
- Map<String, Object> body = new HashMap<>();
- body.put("processInstanceId", processInstanceId);
- body.put("text", totalAmount);
- body.put("commentUserId", userId);
- String result = UtilHttp.doPost(
- "https://api.dingtalk.com/v1.0/workflow/processInstances/comments",
- ddClient.initTokenHeader(),
- null,
- body
- );
- log.info("添加审批评论成功: {}", result);
- } catch (Exception e) {
- log.error("添加审批评论失败", e);
- }
- }
- private void addWorkflowComment1(String processInstanceId, String userId, BigDecimal totalAmount) {
- try {
- Map<String, Object> body = new HashMap<>();
- body.put("processInstanceId", processInstanceId);
- body.put("text", "发票总金额:"+totalAmount);
- body.put("commentUserId", userId);
- String result = UtilHttp.doPost(
- "https://api.dingtalk.com/v1.0/workflow/processInstances/comments",
- ddClient.initTokenHeader(),
- null,
- body
- );
- log.info("添加审批评论成功: {}", result);
- } catch (Exception e) {
- log.error("添加审批评论失败", e);
- }
- }
- /**
- * 从 Map 中获取字符串值
- */
- private String getStringValue(Map<String, Object> map, String key) {
- Object value = map.get(key);
- return value != null ? String.valueOf(value) : "";
- }
- /**
- * 从 Map 中获取 BigDecimal 值
- */
- private BigDecimal getBigDecimalValue(Map<String, Object> map, String key) {
- Object value = map.get(key);
- if (value == null) {
- return BigDecimal.ZERO;
- }
- if (value instanceof BigDecimal) {
- return (BigDecimal) value;
- }
- try {
- return new BigDecimal(String.valueOf(value));
- } catch (NumberFormatException e) {
- return BigDecimal.ZERO;
- }
- }
- /**
- * 空字符串转换
- */
- private String nullToEmpty(String str) {
- return str != null ? str : "";
- }
- private void downloadDdFile(String processInstanceId, String fileId, String downloadPath) {
- try {
- Map body = new HashMap();
- body.put("processInstanceId", processInstanceId);
- body.put("fileId", fileId);
- DDR_New ddrNew = (DDR_New) UtilHttp.doPost("https://api.dingtalk.com/v1.0/workflow/processInstances/spaces/files/urls/download", ddClient.initTokenHeader(), null, body, DDR_New.class);
- //2、执行下载
- Map result = (Map) ddrNew.getResult();
- String downloadUri = UtilMap.getString(result, "downloadUri");
- downloadFile(downloadUri, downloadPath);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- //文件下载到本地
- 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);
- }
- }
- /**
- * 私有化方法:混票识别处理
- *
- * @param data 包含url、size、isPdf的Map参数
- * @return 识别结果
- */
- private McR<Object> processMixedInvoice(Map<String, Object> data) throws TencentCloudSDKException {
- McException.assertParamException_Null(data, "url");
- String image = String.valueOf(data.get("url"));
- log.info("混票识别, 免登地址, {}", image);
- // 非PDF, 且内存大于3M, 压缩后上传
- if (UtilMap.getFloat(data, "size") > 3.0f && !UtilMap.getBoolean(data, "isPdf")) {
- image = imageUrlConvertBase64(image);
- }
- if (UtilMap.getFloat(data, "size") > 6.0f && UtilMap.getBoolean(data, "isPdf")) {
- image = pdfUrlConvertBase64(image);
- }
- List<Map> invoices = (List<Map>) txyInvoice.doRecognizeGeneralInvoice(image).get("MixedInvoiceItems");
- System.out.println("invoices=====" + invoices);
- List<String> nos = new ArrayList<>();
- List<McInvoiceDto> result = invoices.stream().map(item -> {
- Map prop = UtilMap.getMap(UtilMap.getMap(item, "SingleInvoiceInfos"), UtilMap.getString(item, "SubType"));
- String no = UtilMap.getString(prop, "Number");
- // if ("OtherInvoice".equals(UtilMap.getString(item, "SubType")) && StringUtils.isBlank(no)) {
- // log.info("忽略无发票号码的 OCR 非票据内容: subType={}, description={}",
- // UtilMap.getString(item, "SubType"), UtilMap.getString(item, "SubTypeDescription"));
- // return null;
- // }
- String taxRate = null;
- // List<Map<String, Object>> vatInvoiceItemInfos = UtilMap.getList(prop, "VatInvoiceItemInfos");
- // 优先使用 VatInvoiceItemInfos,如果为空则使用 VatElectronicItems
- List<Map<String, Object>> vatInvoiceItemInfos =
- UtilMap.getList(prop, "VatInvoiceItemInfos") != null && !UtilMap.getList(prop, "VatInvoiceItemInfos").isEmpty()
- ? UtilMap.getList(prop, "VatInvoiceItemInfos")
- : UtilMap.getList(prop, "VatElectronicItems");
- // if (!vatInvoiceItemInfos.isEmpty()) {
- // taxRate = UtilMap.getString(vatInvoiceItemInfos.get(0), "TaxRate");
- // }
- if (!vatInvoiceItemInfos.isEmpty()) {
- String originalTaxRate = UtilMap.getString(vatInvoiceItemInfos.get(0), "TaxRate");
- // 判断是否为有效数字(包括带%号的格式,如"9%")
- if (StringUtils.isNotBlank(originalTaxRate)) {
- // 移除百分号并尝试解析为数字
- String cleanedTaxRate = originalTaxRate.replace("%", "").trim();
- try {
- // 尝试转换为数字
- new BigDecimal(cleanedTaxRate);
- taxRate = originalTaxRate; // 保持原值(可能带%)
- } catch (NumberFormatException e) {
- // 如果不是数字(如"不征税"、"免税"等),赋值为"0"
- taxRate = "0";
- log.debug("税率格式异常,原始值: {}, 已设置为0", originalTaxRate);
- }
- } else {
- taxRate = "0";
- }
- } else {
- taxRate = "0";
- }
- if (nos.contains(no)) {
- return null;
- }
- nos.add(no);
- String kind = UtilMap.getString(item, "TypeDescription");
- String invoiceName = UtilMap.getString(item, "SubTypeDescription");
- if (kind.equals("全电发票")) {
- if (invoiceName.contains("铁路电子客票")) {
- kind = "火车票";
- } else if (invoiceName.contains("机票行程单")) {
- kind = "机票行程单";
- } else if (invoiceName.contains("专用发票")) {
- kind = "全电专用发票";
- } else {
- kind = "全电普通发票";
- }
- }
- if (kind.equals("增值税发票")) {
- if (invoiceName.contains("区块链电子发票")) {
- kind = "区块链电子发票";
- } else if (invoiceName.contains("增值税专用发票")) {
- kind = "增值税专用发票";
- } else if (invoiceName.contains("增值税普通发票")) {
- kind = "增值税普通发票";
- } else if (invoiceName.contains("增值税电子专用发票")) {
- kind = "增值税电子专用发票";
- } else if (invoiceName.contains("增值税电子普通发票")) {
- kind = "增值税电子普通发票";
- } else {
- kind = "增值税普通发票";
- }
- }
- if (isTitleValidationExempt(invoiceName)) {
- kind = "普通发票";
- }
- McInvoiceDto invoiceDto = McInvoiceDto.builder()
- .name(UtilMap.getString(item, "SubTypeDescription"))
- .kindName(kind)
- .kind(UtilMap.getInt(item, "Type"))
- .taxRate(taxRate)
- .code(UtilMap.getString(prop, "Code"))
- .serial(UtilMap.getString(prop, "Number"))
- .date(UtilString.replaceDateZH_cn(UtilMap.getString(prop, "Date")))
- .checkCode(UtilMap.getString(prop, "CheckCode"))
- .amount(UtilNumber.setBigDecimal(UtilMap.getString_first(prop, "Total", "Fare")))
- .tax(UtilNumber.setBigDecimal(UtilMap.getString_first(prop, "Tax")))
- .excludingTax(UtilNumber.setBigDecimal(UtilMap.getString(prop, "PretaxAmount")))
- .buyerName(StringUtils.defaultString(guyuanNameRepalce(UtilMap.getString(prop, "Buyer"))))
- .buyerTaxId(StringUtils.isBlank(UtilMap.getString(prop, "BuyerTaxID"))
- ? "" : UtilMap.getString(prop, "BuyerTaxID"))
- .sellerName(guyuanNameRepalce(UtilMap.getString_first(prop, "Seller", "Issuer")))
- .sellerTaxId(UtilMap.getString_first(prop, "SellerTaxID", "AgentCode"))
- .passengerName(UtilMap.getString_first(prop, "Name", "UserName"))
- .seatType(UtilMap.getString(prop, "Seat"))
- .departureTime(UtilString.replaceDateZH_cn(UtilMap.getString(prop, "DateGetOn"))
- + " " + UtilMap.getString(prop, "TimeGetOn"))
- .departurePort(UtilMap.getString_first(prop, "StationGetOn", "Entrance", "Place"))
- .arrivePort(UtilMap.getString_first(prop, "StationGetOff", "Exit"))
- .trainNo(UtilMap.getString_first(prop, "TrainNumber", "LicensePlate"))
- .insuranceCosts(UtilNumber.setBigDecimal(UtilMap.getString(prop, "Insurance")))
- .fuelCosts(UtilNumber.setBigDecimal(UtilMap.getString(prop, "FuelSurcharge")))
- .constructionCosts(UtilNumber.setBigDecimal(UtilMap.getString(prop, "AirDevelopmentFund")))
- .build();
- if ("机票行程单".equals(kind)) {
- invoiceDto.setSellerName(UtilMap.getString_first(prop, "Issuer"));
- invoiceDto.setSellerTaxId(UtilMap.getString_first(prop, "Seller"));
- Map flight = (Map) UtilMap.getList(prop, "FlightItems").get(0);
- invoiceDto.setDepartureTime(UtilString.replaceDateZH_cn(UtilMap.getString(item, "DateGetOn"))
- + " " + UtilMap.getString(prop, "TimeGetOn"));
- invoiceDto.setDeparturePort(UtilMap.getString(flight, "StationGetOn"));
- invoiceDto.setArrivePort(UtilMap.getString(flight, "StationGetOff"));
- invoiceDto.setSeatType(UtilMap.getString(flight, "Seat"));
- }
- if ("出租车发票".equals(item.get("TypeDescription"))) {
- invoiceDto.setDepartureTime(UtilMap.getString(prop, "TimeGetOn")
- + " ~ " + UtilMap.getString(prop, "TimeGetOff"));
- }
- return invoiceDto;
- }).filter(Objects::nonNull).collect(Collectors.toList());
- return McR.success(McInvoiceDto.formatResponse(result));
- }
- private boolean isTitleValidationExempt(String invoiceName) {
- return Arrays.asList(
- "出租车发票",
- "汽车票",
- "通用机打发票",
- "定额发票",
- "轮船票",
- "医疗门诊收费票据(电子)",
- "医疗住院收费票据(电子)",
- "过路过桥费发票"
- ).contains(invoiceName);
- }
- private static String guyuanNameRepalce(String name) {
- name = name.replaceAll(" ", "");
- if (name.contains("谷元")) {
- return UtilString.replaceBracketIsWhole(name);
- } else {
- return UtilString.replaceBracketIsSemiangle(name);
- }
- }
- /// url压缩转base64
- @SneakyThrows
- private String imageUrlConvertBase64(String imageUrl) {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- // scale(比例), outputQuality(质量)
- Thumbnails.fromURLs(Arrays.asList(new URL(imageUrl))).scale(0.5f).outputQuality(0.25f).toOutputStream(out);
- InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
- //转换为base64
- byte[] bytes = IOUtils.toByteArray(inputStream);
- return Base64.getEncoder().encodeToString(bytes);
- }
- @SneakyThrows
- private String pdfUrlConvertBase64(String pdfUrl) {
- String fileName = "tmp_" + new Date().getTime() + ".pdf";
- // 下载文件
- File file = UtilFile.mkdirIfNot(fileName, filePath.getPath().getTmp());
- UtilHttp.doDownload(pdfUrl, file);
- // PDF压缩
- PdfDocument doc = new PdfDocument(); // 创建PdfDocument类的对象
- doc.loadFromFile(file.getAbsolutePath()); // 加载PDF文档
- doc.getFileInfo().setIncrementalUpdate(false); // 禁用增量更新
- doc.setCompressionLevel(PdfCompressionLevel.Normal); // 将压缩级别设置为最佳
- // 遍历文档页面
- for (int i = 0; i < doc.getPages().getCount(); i++) {
- PdfPageBase page = doc.getPages().get(i); // 获取指定页面
- PdfImageInfo[] images = page.getImagesInfo(); // 获取每个页面的图像信息集合
- // 遍历集合中的所有项目
- if (images != null && images.length > 0)
- for (int j = 0; j < images.length; j++) {
- PdfImageInfo image = images[j]; // 获取指定图片
- PdfBitmap bp = new PdfBitmap(image.getImage());
- bp.setQuality(30); // 设置压缩质量
- page.replaceImage(j, bp); // 将原始图像替换为压缩图像
- }
- }
- // 将结果文档保存至另一个PDF文档中: 覆盖
- doc.saveToFile(file.getAbsolutePath());
- doc.close();
- // PDF转base64, 无需透出本地文件地址
- String base64 = UtilFile.fileToBase64(file.getAbsolutePath());
- // 删除临时PDF文件
- UtilFile.deleteFile(file.getAbsolutePath());
- return base64;
- }
- @GetMapping("/files/{fileId}")
- public ResponseEntity<Resource> getFileResource(
- @PathVariable String fileId,
- @RequestParam(defaultValue = "download") String option) throws IOException {
- // 根据fileId获取实际文件路径,这里简化处理,实际可能需要从数据库查询
- Path filePath = Paths.get(downloadPath).resolve(fileId).normalize();
- // Path filePath = Paths.get("D://qiwang//files//").resolve(fileId).normalize();
- // 检查文件是否存在
- if (!Files.exists(filePath)) {
- return ResponseEntity.notFound().build();
- }
- // 创建Resource对象
- Resource resource = new UrlResource(filePath.toUri());
- // 根据选项设置响应头
- HttpHeaders headers = new HttpHeaders();
- if ("preview".equalsIgnoreCase(option)) {
- // 预览模式 - 尝试确定内容类型
- String contentType = Files.probeContentType(filePath);
- // 强制修正 PDF 的 Content-Type
- if (filePath.toString().toLowerCase().endsWith(".pdf")) {
- contentType = "application/pdf";
- } else if (contentType == null) {
- contentType = "application/octet-stream";
- }
- headers.setContentType(MediaType.parseMediaType(contentType));
- headers.add("Content-Disposition", "inline; filename=\"" + resource.getFilename() + "\"");
- headers.add("X-Content-Type-Options", "nosniff"); // 防止浏览器忽略 Content-Type
- } else {
- // 下载模式 - 默认处理
- headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
- headers.add("Content-Disposition", "attachment; filename=\"" + resource.getFilename() + "\"");
- }
- return ResponseEntity.ok()
- .headers(headers)
- .contentLength(resource.contentLength())
- .body(resource);
- }
- }
|