CostDepartmentProfitServiceImpl.java 232 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336
  1. package com.kcim.service.impl;
  2. import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
  3. import cn.afterturn.easypoi.util.PoiCellUtil;
  4. import cn.afterturn.easypoi.util.PoiMergeCellUtil;
  5. import cn.hutool.core.collection.CollUtil;
  6. import cn.hutool.core.date.DateTime;
  7. import cn.hutool.core.date.DateUtil;
  8. import cn.hutool.core.util.ReUtil;
  9. import cn.hutool.core.util.StrUtil;
  10. import cn.hutool.poi.excel.ExcelWriter;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  13. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.google.common.collect.ImmutableMap;
  16. import com.kcim.common.constants.NumberConstant;
  17. import com.kcim.common.constants.ParameterConstant;
  18. import com.kcim.common.constants.SQLParameter;
  19. import com.kcim.common.constants.SplitConstant;
  20. import com.kcim.common.enums.CustomSqlTypeEnum;
  21. import com.kcim.common.enums.DateStyleEnum;
  22. import com.kcim.common.enums.ErrorCodeEnum;
  23. import com.kcim.common.exception.CostException;
  24. import com.kcim.common.file.MinioConfig;
  25. import com.kcim.common.file.MinioFileUtil;
  26. import com.kcim.common.util.*;
  27. import com.kcim.common.util.excel.ExcelPoiUtil;
  28. import com.kcim.common.util.excel.entity.ColEntity;
  29. import com.kcim.common.util.excel.entity.TitleEntity;
  30. import com.kcim.dao.mapper.CostDepartmentProfitMapper;
  31. import com.kcim.dao.model.*;
  32. import com.kcim.dao.repository.ComputeLastProfitDateRepository;
  33. import com.kcim.dao.repository.CostDepartmentProfitAccountRepository;
  34. import com.kcim.dao.repository.CostDepartmentProfitRepository;
  35. import com.kcim.dao.repository.ResponsibilityRepository;
  36. import com.kcim.service.*;
  37. import com.kcim.vo.*;
  38. import com.kcim.web.reponse.BatchCostProfitResponse;
  39. import com.kcim.web.reponse.ComputeProfitCollectResponse;
  40. import com.kcim.web.reponse.CostProfitRedirectResponse;
  41. import com.kcim.web.reponse.ProfitReportResponse;
  42. import lombok.AllArgsConstructor;
  43. import lombok.extern.slf4j.Slf4j;
  44. import org.apache.commons.fileupload.FileItem;
  45. import org.apache.commons.fileupload.FileItemFactory;
  46. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  47. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  48. import org.apache.poi.ss.usermodel.Sheet;
  49. import org.apache.poi.ss.util.CellRangeAddress;
  50. import org.apache.poi.xssf.usermodel.XSSFRow;
  51. import org.apache.poi.xssf.usermodel.XSSFSheet;
  52. import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  53. import org.jetbrains.annotations.NotNull;
  54. import org.springframework.stereotype.Service;
  55. import org.springframework.transaction.annotation.Propagation;
  56. import org.springframework.transaction.annotation.Transactional;
  57. import org.springframework.util.CollectionUtils;
  58. import org.springframework.util.StringUtils;
  59. import org.springframework.web.multipart.MultipartFile;
  60. import org.springframework.web.multipart.commons.CommonsMultipartFile;
  61. import java.io.IOException;
  62. import java.io.InputStream;
  63. import java.io.OutputStream;
  64. import java.math.BigDecimal;
  65. import java.math.RoundingMode;
  66. import java.text.DecimalFormat;
  67. import java.util.*;
  68. import java.util.concurrent.ConcurrentHashMap;
  69. import java.util.concurrent.atomic.AtomicReference;
  70. import java.util.stream.Collectors;
  71. import java.util.stream.IntStream;
  72. import static com.kcim.common.constants.Constant.PROFIT_REPORT_TYPE;
  73. import static com.kcim.common.constants.ParameterConstant.MEDICAL_TECHNIQUES_SHARE_LEVEL_ID;
  74. @Service("costDepartmentProfitService")
  75. @Slf4j
  76. @AllArgsConstructor
  77. public class CostDepartmentProfitServiceImpl extends ServiceImpl<CostDepartmentProfitMapper, CostDepartmentProfit> implements CostDepartmentProfitService {
  78. // @Value("${file.filelocal}")
  79. // private String hospProfitReportUrl;
  80. //
  81. // @Value("${file.serverUrl}")
  82. // private String serverUrl;
  83. private final ReportFormService reportFormService;
  84. private final IncomeCollectionService incomeCollectionService;
  85. private final CostShareLevelService costShareLevelService;
  86. private final ResponsibilityService responsibilityService;
  87. private final ReportRelationService reportRelationService;
  88. private final AllocationService allocationService;
  89. private final AllocationQueryService allocationQueryService;
  90. private final FileRecordService fileRecordService;
  91. private final MinioConfig minioConfig;
  92. private final MinioFileUtil minioFileUtil;
  93. private final CenterService centerService;
  94. private final CostDepartmentProfitRepository costDepartmentProfitRepository;
  95. private final ComputeLastProfitDateRepository computeLastProfitDateRepository;
  96. private final AccountingService accountingService;
  97. private final CostAccountShareService costAccountShareService;
  98. private final UserResponsibilityRightService userResponsibilityRightService;
  99. ResponsibilityRepository responsibilityRepository;
  100. CostDepartmentProfitAccountRepository costDepartmentProfitAccountRepository;
  101. private final SqlService sqlService;
  102. private final String AMOUNT = "金额";
  103. private final String PERCENT = "占比";
  104. private final String AMOUNT_FIELD = "amount";
  105. private final String PERCENT_FIELD = "percent";
  106. //
  107. // public CostDepartmentProfitServiceImpl(ReportFormService reportFormService, IncomeCollectionService incomeCollectionService, CostShareLevelService costShareLevelService, ResponsibilityService responsibilityService, ReportRelationService reportRelationService, AllocationService allocationService, AllocationQueryService allocationQueryService, FileRecordService fileRecordService, MinioConfig minioConfig, MinioFileUtil minioFileUtil, CenterService centerService, CostDepartmentProfitRepository costDepartmentProfitRepository, ComputeLastProfitDateRepository computeLastProfitDateRepository, AccountingService accountingService, CostAccountShareService costAccountShareService, SqlService sqlService, UserResponsibilityRightService userResponsibilityRightService) {
  108. // this.reportFormService = reportFormService;
  109. // this.incomeCollectionService = incomeCollectionService;
  110. // this.costShareLevelService = costShareLevelService;
  111. // this.responsibilityService = responsibilityService;
  112. // this.reportRelationService = reportRelationService;
  113. // this.allocationService = allocationService;
  114. // this.allocationQueryService = allocationQueryService;
  115. // this.fileRecordService = fileRecordService;
  116. // this.minioConfig = minioConfig;
  117. // this.minioFileUtil = minioFileUtil;
  118. // this.centerService = centerService;
  119. // this.costDepartmentProfitRepository = costDepartmentProfitRepository;
  120. // this.computeLastProfitDateRepository = computeLastProfitDateRepository;
  121. // this.accountingService = accountingService;
  122. // this.costAccountShareService = costAccountShareService;
  123. // this.sqlService = sqlService;
  124. // this.userResponsibilityRightService = userResponsibilityRightService;
  125. // }
  126. /**
  127. * 查询科室损益数据
  128. *
  129. * @param current
  130. * @param pageSize
  131. * @param responsibilityCode
  132. * @param date
  133. * @param hospId
  134. * @return
  135. */
  136. @Override
  137. public PageUtils queryList(Integer current, Integer pageSize, String responsibilityCode, String date, Long hospId) {
  138. int year = 0;
  139. int month = 0;
  140. if (StrUtil.isNotBlank(date)) {
  141. Date dateTime = DateUtils.StringToDate(date, DateStyleEnum.YYYY_MM_DD);
  142. year = DateUtil.year(dateTime);
  143. month = DateUtil.month(dateTime) + 1;
  144. }
  145. Page<CostDepartmentProfit> departmentProfitPage = new Page<>(current, pageSize);
  146. // 查询的时候过过滤那些计算方式是不设置的数据
  147. Page<CostDepartmentProfit> pages = this.page(departmentProfitPage, new QueryWrapper<CostDepartmentProfit>().lambda()
  148. .eq(CostDepartmentProfit::getHospId, hospId)
  149. .eq(StrUtil.isNotBlank(responsibilityCode), CostDepartmentProfit::getResponsibilityCode, responsibilityCode)
  150. .eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getYear, year)
  151. .eq(StrUtil.isNotBlank(date), CostDepartmentProfit::getMonth, month)
  152. .ne(CostDepartmentProfit::getCalcType, NumberConstant.ZERO).orderByDesc(CostDepartmentProfit::getAmount));
  153. List<CostDepartmentProfit> records = pages.getRecords();
  154. List<CostDepartmentProfitVO> costDepartmentProfitVOList = BeanUtil.convertList(records, CostDepartmentProfitVO.class);
  155. DecimalFormat df = new DecimalFormat("0.00%");
  156. for (CostDepartmentProfitVO costDepartmentProfitVO : costDepartmentProfitVOList) {
  157. if (costDepartmentProfitVO.getPercent() != null) {
  158. costDepartmentProfitVO.setPercentName(df.format(costDepartmentProfitVO.getPercent()));
  159. }
  160. }
  161. PageUtils pageUtils = new PageUtils(pages);
  162. pageUtils.setList(costDepartmentProfitVOList);
  163. return pageUtils;
  164. }
  165. /**
  166. * 科室损益计算
  167. *
  168. * @param date
  169. * @param hospId
  170. */
  171. @Override
  172. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
  173. public void setDepartmentProfit(String date, Long hospId) {
  174. DateTime parse = DateUtil.parse(date);
  175. int year = DateUtil.year(parse);
  176. int month = DateUtil.month(parse) + 1;
  177. // 先查询指定条件的报表数据 查询损益表的数据
  178. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  179. .eq(ReportForm::getHospId, hospId)
  180. .eq(ReportForm::getReportType, NumberConstant.ZERO));
  181. if (CollUtil.isEmpty(reportFormList)) {
  182. throw new CostException(500, "损益表未找到");
  183. }
  184. // 遍历报表数据根据报表数据计算方式进行计算
  185. //这里拆分成两个 原有的最后一层 以及 参数医技
  186. // 查询最后一个层级的责任中心
  187. List<CostShareLevel> costShareLevelList = costShareLevelService.list(new QueryWrapper<CostShareLevel>().lambda()
  188. .eq(CostShareLevel::getHospId, hospId).orderByDesc(CostShareLevel::getLeverSort));
  189. if (CollUtil.isEmpty(costShareLevelList)) {
  190. throw new CostException(500, "分摊层级未设置");
  191. }
  192. // TODO 可能多个
  193. Long id = costShareLevelList.get(0).getId();
  194. // 查询责任中心里面是这个层级的所有的收益中心
  195. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  196. .eq(Responsibility::getHospId, hospId).eq(Responsibility::getResponsibilityType, NumberConstant.ONE).eq(Responsibility::getShareId, id));
  197. //通过参数获取医技成分摊层级id
  198. CommonParameterVo parameter = centerService.getParameter(MEDICAL_TECHNIQUES_SHARE_LEVEL_ID);
  199. // 归集后
  200. List<IncomeCollection> incomeList = incomeCollectionService.list(new QueryWrapper<IncomeCollection>().lambda()
  201. .eq(IncomeCollection::getHospId, hospId)
  202. .eq(year > 0, IncomeCollection::getYear, year).eq(month > 0, IncomeCollection::getMonth, month));
  203. if (CollUtil.isEmpty(incomeList)) {
  204. throw new CostException(500, "归集后数据不存在");
  205. }
  206. Map<Long, List<ReportRelation>> reportRelationMap = reportRelationService.list(new QueryWrapper<ReportRelation>().lambda().eq(ReportRelation::getHospId, hospId)).stream().collect(Collectors.groupingBy(ReportRelation::getReportId));
  207. // 分摊后的数据说明是分摊后的数据 这里原有为收益中心数据 同步添加医技分摊后数据
  208. List<AllocationQuery> allocationQueryList = allocationQueryService.list(new QueryWrapper<AllocationQuery>().lambda()
  209. .eq(AllocationQuery::getHospId, hospId)
  210. .eq(year > 0, AllocationQuery::getDateYear, year)
  211. .eq(month > 0, AllocationQuery::getDateMonth, month)
  212. .eq(AllocationQuery::getShareType, NumberConstant.ONE)
  213. );
  214. if (CollUtil.isEmpty(allocationQueryList)) {
  215. throw new CostException(500, "分摊后数据不存在");
  216. }
  217. //获取医技分摊后数据
  218. List<AllocationQuery> medicalTechniquesAllocationQueryList = allocationQueryService.list(new QueryWrapper<AllocationQuery>().lambda()
  219. .eq(AllocationQuery::getHospId, hospId)
  220. .eq(year > 0, AllocationQuery::getDateYear, year)
  221. .eq(month > 0, AllocationQuery::getDateMonth, month)
  222. .eq(AllocationQuery::getShareType, NumberConstant.TWO)
  223. );
  224. // 封装数据
  225. List<AllocationQueryReportVO> allocationQueryReportVOList = BeanUtil.convertList(allocationQueryList, AllocationQueryReportVO.class);
  226. allocationQueryReportVOList.forEach(i -> {
  227. i.setAccountingCodes(Arrays.asList(i.getAccountingCode().split(StrUtil.COMMA)));
  228. i.setAccountingNames(Arrays.asList(i.getAccountingName().split(StrUtil.COMMA)));
  229. });
  230. List<AllocationQueryReportVO> medicalTechniquesAllocationQueryReportVOList = BeanUtil.convertList(medicalTechniquesAllocationQueryList, AllocationQueryReportVO.class);
  231. medicalTechniquesAllocationQueryReportVOList.forEach(i -> {
  232. i.setAccountingCodes(Arrays.asList(i.getAccountingCode().split(StrUtil.COMMA)));
  233. i.setAccountingNames(Arrays.asList(i.getAccountingName().split(StrUtil.COMMA)));
  234. });
  235. // 查询分摊的报表数据 后面的计算方式需要使用 小计等需要使用
  236. List<Allocation> allocationList = allocationService.list(new QueryWrapper<Allocation>().lambda().eq(Allocation::getHospId, hospId)
  237. .eq(year > 0, Allocation::getDateYear, year).eq(month > 0, Allocation::getDateMonth, month));
  238. if (CollUtil.isEmpty(allocationList)) {
  239. throw new CostException(500, "分摊报表数据不存在");
  240. }
  241. // 查询所有指定类型的损益表
  242. // 每个责任中心对应报表都要生成记录
  243. // 封装需要设置的数据
  244. List<CostDepartmentProfitVO> list = getCostDepartmentProfitVOS(hospId, year, month, reportFormList, costShareLevelList, responsibilityList, incomeList, reportRelationMap, allocationQueryReportVOList, allocationList, NumberConstant.ONE);
  245. // 删除这个年月的数据
  246. this.remove(new QueryWrapper<CostDepartmentProfit>().lambda().eq(CostDepartmentProfit::getHospId, hospId)
  247. .eq(CostDepartmentProfit::getYear, year).eq(CostDepartmentProfit::getMonth, month));
  248. // 添加数据
  249. List<CostDepartmentProfit> costDepartmentProfits = BeanUtil.convertList(list, CostDepartmentProfit.class);
  250. if (Objects.nonNull(parameter)) {
  251. String value = parameter.getValue();
  252. if (StringUtils.isEmpty(value)) {
  253. throw new CostException("医技成分摊层级id参数未设置");
  254. }
  255. // 查询责任中心里面是医技分摊层级的
  256. List<Responsibility> medicalTechniquesResponsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  257. .eq(Responsibility::getHospId, hospId).eq(Responsibility::getShareId, value));
  258. List<CostDepartmentProfitVO> medicalTechniquesList = getCostDepartmentProfitVOS(hospId, year, month, reportFormList, costShareLevelList, medicalTechniquesResponsibilityList, incomeList, reportRelationMap, medicalTechniquesAllocationQueryReportVOList, allocationList, NumberConstant.TWO);
  259. List<CostDepartmentProfit> medicalTechniquesCostDepartmentProfits = BeanUtil.convertList(medicalTechniquesList, CostDepartmentProfit.class);
  260. if (!CollectionUtils.isEmpty(medicalTechniquesCostDepartmentProfits)) {
  261. costDepartmentProfits.addAll(medicalTechniquesCostDepartmentProfits);
  262. }
  263. }
  264. long l = System.currentTimeMillis();
  265. costDepartmentProfits.forEach(i -> {
  266. i.setCreateTime(l);
  267. });
  268. this.saveBatch(costDepartmentProfits);
  269. }
  270. private List<CostDepartmentProfitVO> getCostDepartmentProfitVOS(Long hospId, int year, int month, List<ReportForm> reportFormList,
  271. List<CostShareLevel> costShareLevelList, List<Responsibility> responsibilityList,
  272. List<IncomeCollection> incomeList, Map<Long, List<ReportRelation>> reportRelationMap,
  273. List<AllocationQueryReportVO> allocationQueryReportVOList, List<Allocation> allocationList, Integer shareType) {
  274. List<CostDepartmentProfitVO> list = new ArrayList<>();
  275. int finalYear = year;
  276. int finalMonth = month;
  277. responsibilityList.forEach(i -> {
  278. reportFormList.forEach(j -> {
  279. CostDepartmentProfitVO costDepartmentProfitVO = new CostDepartmentProfitVO();
  280. costDepartmentProfitVO.setYear(finalYear);
  281. costDepartmentProfitVO.setMonth(finalMonth);
  282. costDepartmentProfitVO.setReportId(j.getId());
  283. costDepartmentProfitVO.setReportNum(j.getNum());
  284. costDepartmentProfitVO.setCalcType(j.getCalcType());
  285. costDepartmentProfitVO.setReportName(j.getReportName());
  286. costDepartmentProfitVO.setCalcFormula(j.getCalcFormula());
  287. costDepartmentProfitVO.setReportParentId(j.getParentId());
  288. costDepartmentProfitVO.setResponsibilityCode(i.getResponsibilityCode());
  289. costDepartmentProfitVO.setResponsibilityName(i.getResponsibilityName());
  290. costDepartmentProfitVO.setCostType(NumberConstant.ONE);
  291. costDepartmentProfitVO.setIncomeType(NumberConstant.ONE);
  292. costDepartmentProfitVO.setHospId(hospId);
  293. costDepartmentProfitVO.setShareType(shareType);
  294. costDepartmentProfitVO.setType(j.getCostType());
  295. costDepartmentProfitVO.setFraction(j.getFraction());
  296. list.add(costDepartmentProfitVO);
  297. });
  298. });
  299. Map<Long, List<CostDepartmentProfitVO>> listMap = list.stream().collect(Collectors.groupingBy(CostDepartmentProfitVO::getReportId));
  300. List<CostDepartmentProfitVO> allList = BeanUtil.convertList(list, CostDepartmentProfitVO.class);
  301. // 记录每一次计算的钱
  302. list.forEach(i -> {
  303. Integer calcType = i.getCalcType();
  304. if (NumberConstant.ONE.equals(calcType)) {
  305. // TODO 按照会计科目进行计算
  306. i.setAmount(setAccountReportData(i, incomeList, allocationQueryReportVOList, reportRelationMap, allList));
  307. } else if (NumberConstant.TWO.equals(calcType)) {
  308. // TODO 按照分摊层级进行计算
  309. i.setAmount(setShareLevelReportData(i, costShareLevelList, reportRelationMap, allocationList, allList));
  310. } else if (NumberConstant.THREE.equals(calcType)) {
  311. // TODO 按照小计进行计算
  312. i.setAmount(setSubtotal(i, costShareLevelList, listMap, list, incomeList, allocationQueryReportVOList, reportRelationMap, allocationList, allList));
  313. } else if (NumberConstant.FOUR.equals(calcType)) {
  314. // TODO 按照计算公式进行计算
  315. i.setAmount(setCalculation(i, list, costShareLevelList, listMap, incomeList, allocationQueryReportVOList, reportRelationMap, allocationList, allList));
  316. } else if (NumberConstant.FIVE.equals(calcType)) {
  317. // TODO 按照责任中心进行计算
  318. i.setAmount(setResponsibilityCode(i, reportRelationMap, allocationList, allList));
  319. } else {
  320. i.setAmount(new BigDecimal("0.000000"));
  321. }
  322. });
  323. //计算占比
  324. if (CollectionUtils.isEmpty(list)) {
  325. return list;
  326. }
  327. //单独计算计算公式数据
  328. setCalculationAmount(list);
  329. //收入
  330. List<CostDepartmentProfitVO> profitIncomeList = list.stream().filter(f -> f.getType().equals(NumberConstant.ONE)).collect(Collectors.toList());
  331. List<CostDepartmentProfitVO> profitIncome =
  332. costPercent(profitIncomeList, "收入");
  333. //成本
  334. List<CostDepartmentProfitVO> profitCostList = list.stream().filter(f -> f.getType().equals(NumberConstant.TWO)).collect(Collectors.toList());
  335. List<CostDepartmentProfitVO> profitCost = costPercent(profitCostList, "成本");
  336. //不计算项目
  337. List<CostDepartmentProfitVO> noCompute = list.stream().filter(f -> f.getFraction().equals(NumberConstant.THREE)).collect(Collectors.toList());
  338. noCompute.forEach(profitVO -> profitVO.setPercent(null));
  339. if (!CollectionUtils.isEmpty(profitIncome) && !CollectionUtils.isEmpty(profitCost) && !CollectionUtils.isEmpty(noCompute)) {
  340. profitIncome.addAll(profitCost);
  341. profitIncome.addAll(noCompute);
  342. return profitIncome;
  343. } else if (!CollectionUtils.isEmpty(profitIncome) && !CollectionUtils.isEmpty(noCompute)) {
  344. profitIncome.addAll(noCompute);
  345. return profitIncome;
  346. } else if (!CollectionUtils.isEmpty(profitCost) && !CollectionUtils.isEmpty(noCompute)) {
  347. profitCost.addAll(noCompute);
  348. return profitCost;
  349. } else if (!CollectionUtils.isEmpty(noCompute)) {
  350. return noCompute;
  351. } else {
  352. return list;
  353. }
  354. }
  355. private static void setCalculationAmount(List<CostDepartmentProfitVO> list) {
  356. Map<String, List<CostDepartmentProfitVO>> collect = list.stream().collect(Collectors.groupingBy(CostDepartmentProfitVO::getResponsibilityCode));
  357. collect.forEach((responsibilityCode, v) -> {
  358. // Map<Integer, BigDecimal> dataMap = new HashMap<>();
  359. // for (CostDepartmentProfitVO costDepartmentProfitVO : v) {
  360. // if (costDepartmentProfitVO.getAmount() != null) {
  361. // dataMap.put(costDepartmentProfitVO.getReportNum(), costDepartmentProfitVO.getAmount());
  362. // } else {
  363. // dataMap.put(costDepartmentProfitVO.getReportNum(), new BigDecimal("0.000000"));
  364. // }
  365. // }
  366. Map<Integer, List<CostDepartmentProfitVO>> collect1 = v.stream().collect(Collectors.groupingBy(CostDepartmentProfitVO::getCalcType));
  367. //取出所有计算公式数据
  368. List<CostDepartmentProfitVO> costDepartmentProfitVOS = collect1.get(NumberConstant.FOUR);
  369. costDepartmentProfitVOS.sort(Comparator.comparing(CostDepartmentProfitVO::getReportNum, Comparator.nullsLast(Integer::compareTo)));
  370. for (CostDepartmentProfitVO profitVO : costDepartmentProfitVOS) {
  371. // 获取当前报表的计算方式 [1]+[2]类型/ [1]-[2] [1]*[2] [1]/[2]
  372. String formula = profitVO.getCalcFormula();
  373. //找出公式当中所有代码
  374. String replace = formula.replace("[", "")
  375. .replace("]", "")
  376. .replace("-", ",")
  377. .replace("+", ",")
  378. .replace("*", ",")
  379. .replace("/", ",");
  380. ArrayList<String> codeList = CollUtil.newArrayList(replace.split(","));
  381. Map<Integer, String> codeMap = new ConcurrentHashMap<>();
  382. for (int j = 0; j < codeList.size(); j++) {
  383. codeMap.put(j, codeList.get(j));
  384. }
  385. List<String> expressions = ReUtil.findAll("[^0-9]", "+" + formula.replace("[", "")
  386. .replace("]", "").trim(), 0)
  387. .stream().filter(StrUtil::isNotBlank).collect(Collectors.toList());
  388. // 得到预算表达式 得到所有表达式的Map + - 相关的
  389. Map<Integer, String> expressionMap = new ConcurrentHashMap<>();
  390. for (int k = 0; k < expressions.size(); k++) {
  391. expressionMap.put(k, expressions.get(k));
  392. }
  393. // 数字的索引和表达式的索引累加计算
  394. Set<Integer> codeSet = codeMap.keySet();
  395. List<Integer> codes = new ArrayList<>(codeSet);
  396. AtomicReference<BigDecimal> totalAmount = new AtomicReference<>(new BigDecimal("0.000000"));
  397. for (int i = 0; i < codes.size(); i++) {
  398. // 编号
  399. String code = codeMap.get(i);
  400. BigDecimal amount = null;
  401. for (CostDepartmentProfitVO costDepartmentProfitVO : v) {
  402. if (costDepartmentProfitVO.getReportNum().equals(Integer.valueOf(code))) {
  403. amount = costDepartmentProfitVO.getAmount();
  404. }
  405. }
  406. // BigDecimal amount = dataMap.get(Integer.valueOf(code));
  407. if (amount == null) {
  408. continue;
  409. }
  410. // BigDecimal amount = new BigDecimal(o.toString());
  411. String str = expressionMap.get(i);
  412. if (str.equals("+")) {
  413. totalAmount.set(totalAmount.get().add(amount));
  414. } else if (str.contains("-")) {
  415. totalAmount.set(totalAmount.get().subtract(amount));
  416. } else if (str.contains("*")) {
  417. totalAmount.set(totalAmount.get().multiply(amount));
  418. } else if (str.contains("/")) {
  419. if (amount.compareTo(BigDecimal.ZERO.setScale(6, RoundingMode.HALF_UP)) == 0) {
  420. totalAmount.set(BigDecimal.ZERO);
  421. break;
  422. } else {
  423. totalAmount.set(totalAmount.get().divide(amount, 6, RoundingMode.HALF_UP));
  424. }
  425. }
  426. }
  427. profitVO.setAmount(totalAmount.get());
  428. }
  429. });
  430. }
  431. private List<CostDepartmentProfitVO> costPercent(List<CostDepartmentProfitVO> profitIncomeList, String type) {
  432. List<CostDepartmentProfitVO> list = new ArrayList<>();
  433. Map<String, List<CostDepartmentProfitVO>> responsibilityGroup = profitIncomeList.stream().collect(Collectors.groupingBy(CostDepartmentProfitVO::getResponsibilityCode));
  434. responsibilityGroup.forEach((k, v) -> {
  435. Map<Integer, List<CostDepartmentProfitVO>> collect = v.stream().collect(Collectors.groupingBy(CostDepartmentProfitVO::getFraction));
  436. //分母只有一个
  437. List<CostDepartmentProfitVO> denominatorList = collect.get(NumberConstant.TWO);
  438. if (CollectionUtils.isEmpty(denominatorList)) {
  439. throw new CostException(type + "项目未配置分母,请查证后再计算科室损益");
  440. }
  441. if (denominatorList.size() > 1) {
  442. throw new CostException(type + "收入项目分母大于一个,请查证后再计算科室损益");
  443. }
  444. BigDecimal denominator = denominatorList.get(0).getAmount();
  445. //收入分子
  446. List<CostDepartmentProfitVO> numeratorList = collect.get(NumberConstant.ONE);
  447. if (denominator.compareTo(BigDecimal.ZERO) == 0) {
  448. for (CostDepartmentProfitVO profitVO : numeratorList) {
  449. profitVO.setPercent(BigDecimal.ZERO.setScale(4, RoundingMode.HALF_UP));
  450. }
  451. denominatorList.get(0).setPercent(BigDecimal.ZERO.setScale(4, RoundingMode.HALF_UP));
  452. } else {
  453. for (CostDepartmentProfitVO profitVO : numeratorList) {
  454. profitVO.setPercent(profitVO.getAmount().divide(denominator, 4, RoundingMode.HALF_UP));
  455. }
  456. denominatorList.get(0).setPercent(BigDecimal.ONE.setScale(4, RoundingMode.HALF_UP));
  457. }
  458. list.add(denominatorList.get(0));
  459. list.addAll(numeratorList);
  460. });
  461. return list;
  462. }
  463. /**
  464. * 科室损益计算导出
  465. *
  466. * @param writer
  467. * @param sheet
  468. * @param date
  469. */
  470. @Override
  471. public void getDepartmentProfit(ExcelWriter writer, Sheet sheet, String date) {
  472. Long hospId = UserContext.getHospId();
  473. //先展示所有责任中心 非汇总中心 收益中心的责任中心
  474. // 查询所有的节点
  475. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId));
  476. if (CollUtil.isEmpty(responsibilityList)) {
  477. throw new CostException(500, "责任中心不存在");
  478. }
  479. // 设置表头
  480. Map<Long, List<Responsibility>> responsibilityMap = responsibilityList.stream().collect(Collectors.groupingBy(Responsibility::getId));
  481. List<Responsibility> responsibilities = responsibilityList.stream().filter(i -> NumberConstant.TWO.equals(i.getIsGatherCenter()) && NumberConstant.ONE.equals(i.getResponsibilityType())).collect(Collectors.toList());
  482. Map<Long, List<Responsibility>> responsibilityParentMap = responsibilities.stream().collect(Collectors.groupingBy(Responsibility::getParentId));
  483. Set<Long> keySet = responsibilityParentMap.keySet();
  484. int column = 2;
  485. for (Long parentId : keySet) {
  486. // 同一父节点的叶子节点数据
  487. List<Responsibility> list = responsibilityParentMap.get(parentId);
  488. for (int i = 0; i < list.size(); i++) {
  489. String responsibilityName1 = list.get(i).getResponsibilityName();
  490. // 父层级责任中心名称
  491. List<Responsibility> responsibilityList1 = responsibilityMap.get(list.get(i).getParentId());
  492. if (CollUtil.isEmpty(responsibilityList1)) {
  493. writer.writeCellValue(column, 0, null);
  494. } else {
  495. // 父层级责任中心名称
  496. String responsibilityName = responsibilityList1.get(0).getResponsibilityName();
  497. // 子层级责任中心名称
  498. writer.writeCellValue(column, 0, responsibilityName);
  499. }
  500. writer.writeCellValue(column, 1, responsibilityName1);
  501. column++;
  502. }
  503. }
  504. writer.merge(1, 1, 0, 1, "项目", false);
  505. DateTime dateTime = DateUtil.parse(date);
  506. int year = DateUtil.year(dateTime);
  507. int month = DateUtil.month(dateTime) + 1;
  508. // 设置列
  509. List<CostDepartmentProfit> departmentProfits = this.list(new QueryWrapper<CostDepartmentProfit>().lambda()
  510. .eq(CostDepartmentProfit::getHospId, hospId)
  511. .eq(CostDepartmentProfit::getYear, year).eq(CostDepartmentProfit::getMonth, month).eq(CostDepartmentProfit::getShareType, 0));
  512. // 用来筛选金额
  513. Map<String, CostDepartmentProfit> profitMap = departmentProfits.stream().filter(i -> i.getReportParentId() != 0L).collect(Collectors.toMap(k -> k.getReportName() + "cost" + k.getResponsibilityName(), synOne -> synOne));
  514. // 报表的父层级报表
  515. Map<Long, List<CostDepartmentProfit>> departmentParentReportMap = departmentProfits.stream().filter(i -> i.getReportParentId() == 0L)
  516. .collect(Collectors.groupingBy(CostDepartmentProfit::getReportId)).entrySet().stream()
  517. .sorted(Map.Entry.comparingByKey()).collect(
  518. Collectors.toMap(
  519. Map.Entry::getKey,
  520. Map.Entry::getValue,
  521. (oldVal, newVal) -> oldVal,
  522. LinkedHashMap::new
  523. )
  524. );
  525. // 不是父节点的所有叶子节点
  526. Map<Long, List<CostDepartmentProfit>> listParentMap = departmentProfits.stream().filter(i -> i.getReportParentId() != 0L).collect(Collectors.groupingBy(CostDepartmentProfit::getReportParentId));
  527. Set<Long> ids = departmentParentReportMap.keySet();
  528. // 默认是在第二行开始的
  529. int row = 2;
  530. int columnNum = 0;
  531. for (Long id : ids) {
  532. String reportParentName = departmentParentReportMap.get(id).get(0).getReportName();
  533. List<CostDepartmentProfit> profitList = listParentMap.get(id);
  534. Map<Integer, List<CostDepartmentProfit>> linkedHashMap = profitList.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getReportNum)).entrySet().stream()
  535. .sorted(Map.Entry.comparingByKey()).collect(
  536. Collectors.toMap(
  537. Map.Entry::getKey,
  538. Map.Entry::getValue,
  539. (oldVal, newVal) -> oldVal,
  540. LinkedHashMap::new
  541. ));
  542. Set<Integer> reportNums = linkedHashMap.keySet();
  543. for (Integer reportNum : reportNums) {
  544. // 这一行要显示的记录
  545. List<CostDepartmentProfit> costDepartmentProfits = linkedHashMap.get(reportNum);
  546. // 子层级扥报表名称
  547. String reportName = costDepartmentProfits.get(0).getReportName();
  548. for (int i = 0; i < costDepartmentProfits.size(); i++) {
  549. // 添加项目名称
  550. columnNum++;
  551. writer.writeCellValue(0, row, reportParentName);
  552. writer.writeCellValue(1, row, reportName);
  553. for (int j = 0; j < column - 2; j++) {
  554. // TODO 设置添加具体金额
  555. // 获取表格对应的字段的值
  556. String responsibilityTopName = sheet.getRow(1).getCell(j + 2).getStringCellValue();
  557. String reportTopName = sheet.getRow(row).getCell(1).getStringCellValue();
  558. BigDecimal amount = profitMap.get(reportTopName + "cost" + responsibilityTopName).getAmount();
  559. writer.writeCellValue(j + 2, row, amount);
  560. }
  561. }
  562. row++;
  563. }
  564. }
  565. // TODO 合并留在最后
  566. int cc = 2;
  567. // 合并行
  568. for (int m = 2; m < column - 1; m++) {
  569. String cellValue1 = sheet.getRow(0).getCell(m).getStringCellValue();
  570. String cellValue2 = sheet.getRow(0).getCell(m + 1).getStringCellValue();
  571. if (!cellValue1.equals(cellValue2)) {
  572. if (cc != m) {
  573. writer.merge(0, 0, cc, m, cellValue1, false);
  574. } else {
  575. writer.writeCellValue(cc, 0, cellValue1);
  576. }
  577. cc = m + 1;
  578. } else if (m == column - 2) {
  579. writer.merge(0, 0, cc, m + 1, sheet.getRow(0).getCell(m + 1).getStringCellValue(), false);
  580. }
  581. }
  582. // 合并列
  583. int jj = 2;
  584. for (int i = 2; i < row - 1; i++) {
  585. String cellValue1 = sheet.getRow(i).getCell(0).getStringCellValue();
  586. String cellValue2 = sheet.getRow(i + 1).getCell(0).getStringCellValue();
  587. if (!cellValue1.equals(cellValue2)) {
  588. if (jj != i) {
  589. writer.merge(jj, i, 0, 0, cellValue1, false);
  590. } else {
  591. writer.writeCellValue(0, jj, cellValue1);
  592. }
  593. jj = i + 1;
  594. } else if (i == row - 2) {
  595. writer.merge(jj, i + 1, 0, 0, cellValue1, false);
  596. }
  597. }
  598. // 设置列宽
  599. for (int i = 0; i < 50; i++) {
  600. // 调整每一列宽度
  601. sheet.autoSizeColumn((short) i);
  602. // 解决自动设置列宽中文失效的问题
  603. sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 14 / 10);
  604. }
  605. }
  606. private void getDepartmentProfit(String date, OutputStream outputStream, Integer reportType, String name) throws Exception {
  607. Long hospId = UserContext.getHospId();
  608. DateTime dateTime = DateUtil.parse(date);
  609. int year = DateUtil.year(dateTime);
  610. int month = DateUtil.month(dateTime) + 1;
  611. // 查询所有的节点
  612. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId));
  613. if (CollUtil.isEmpty(responsibilityList)) {
  614. throw new CostException(500, "责任中心不存在");
  615. }
  616. Map<String, Long> responseCodeParentIdMap = responsibilityList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getParentId, (a, b) -> b));
  617. // Map<String,Long> responseCodeIdMap = responsibilityList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getId, (a, b) -> b));
  618. Map<Long, Responsibility> responsibilityIdMap = responsibilityList.stream().collect(Collectors.toMap(Responsibility::getId, responsibility -> responsibility, (a, b) -> b));
  619. // Map<Long, List<Responsibility>> responsibilityMap = responsibilityList.stream().collect(Collectors.groupingBy(Responsibility::getId));
  620. // List<Responsibility> responsibilities = responsibilityList.stream().filter(i -> NumberConstant.TWO.equals(i.getIsGatherCenter()) && NumberConstant.ONE.equals(i.getResponsibilityType())).collect(Collectors.toList());
  621. // Map<Long, List<Responsibility>> responsibilityParentMap = responsibilities.stream().collect(Collectors.groupingBy(Responsibility::getParentId));
  622. //
  623. // 表头
  624. List<CostDepartmentProfit> departmentProfits = this.list(new QueryWrapper<CostDepartmentProfit>().lambda()
  625. .eq(CostDepartmentProfit::getHospId, hospId)
  626. .eq(CostDepartmentProfit::getYear, year).eq(CostDepartmentProfit::getMonth, month)
  627. .eq(CostDepartmentProfit::getShareType, reportType));
  628. if (CollectionUtils.isEmpty(departmentProfits)) {
  629. throw new CostException(500, name + "未进行科室损益计算");
  630. }
  631. for (CostDepartmentProfit departmentProfit : departmentProfits) {
  632. Long aLong = responseCodeParentIdMap.get(departmentProfit.getResponsibilityCode());
  633. if (aLong != null) {
  634. if (aLong.equals(NumberConstant.ZERO_L)) {
  635. // Long aLong1 = responseCodeIdMap.get(departmentProfit.getResponsibilityCode());
  636. departmentProfit.setParentResponsibilityCode(departmentProfit.getResponsibilityCode());
  637. departmentProfit.setParentResponsibilityName(departmentProfit.getResponsibilityName());
  638. } else {
  639. Responsibility responsibility = responsibilityIdMap.get(aLong);
  640. if (Objects.nonNull(responsibility)) {
  641. departmentProfit.setParentResponsibilityCode(responsibility.getResponsibilityCode());
  642. departmentProfit.setParentResponsibilityName(responsibility.getResponsibilityName());
  643. }
  644. }
  645. }
  646. }
  647. //损益表设置
  648. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  649. .eq(ReportForm::getHospId, hospId)
  650. .eq(ReportForm::getReportType, reportType));
  651. if (CollUtil.isEmpty(reportFormList)) {
  652. throw new CostException(500, "损益表未找到");
  653. }
  654. Map<Long, List<ReportForm>> collect = reportFormList.stream().collect(Collectors.groupingBy(ReportForm::getParentId));
  655. Map<String, List<ColEntity>> heads = new HashMap<>();// 最终导出的多个sheet的表头
  656. Map<String, List<Map<String, String>>> datas = new HashMap<>();// 最终导出的多个sheet的内容
  657. Map<String, Integer> types = new HashMap<>();
  658. Map<String, List<Integer>> mergeindexs = new HashMap<>();// 最终导出的每个sheet的需要纵向合并的单元格列号
  659. //第一层
  660. List<ReportForm> firstTitle = collect.get(NumberConstant.ZERO_L);
  661. List<TitleEntity> titleList = new ArrayList<>();
  662. titleList.add(new TitleEntity("0", null, name, false, NumberConstant.ZERO));
  663. //添加默认三列责任中心
  664. setDefaultColumn(titleList);
  665. for (ReportForm reportForm : firstTitle) {
  666. TitleEntity entity = new TitleEntity();
  667. entity.setId(String.valueOf(reportForm.getId()));
  668. entity.setPid(String.valueOf(reportForm.getParentId()));
  669. entity.setContent(reportForm.getReportName());
  670. entity.setFieldName(String.valueOf(reportForm.getNum()));
  671. entity.setWidth(20);
  672. entity.setSort(reportForm.getSort());
  673. boolean child = getChild(titleList, reportForm, collect);
  674. entity.setLast(child);
  675. titleList.add(entity);
  676. }
  677. titleList.sort(Comparator.comparing(TitleEntity::getSort));
  678. // List<CostDepartmentProfit> costDepartmentProfitsDoctor = departmentProfits.stream().filter(f -> f.getShareType().equals(NumberConstant.ONE)).collect(Collectors.toList());
  679. // List<CostDepartmentProfit> costDepartmentProfits = departmentProfits.stream().filter(f -> f.getShareType().equals(NumberConstant.TWO)).collect(Collectors.toList());
  680. //行
  681. Map<String, List<CostDepartmentProfit>> doctorParentMap = departmentProfits.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getParentResponsibilityCode));
  682. // Map<String, List<CostDepartmentProfit>> yiParentMap = costDepartmentProfits.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getParentResponsibilityCode));
  683. //列
  684. List<TitleEntity> titleEntities = titleList.stream().filter(TitleEntity::isLast).collect(Collectors.toList());
  685. List<Map<String, String>> rowList = new ArrayList<>();
  686. // List<Map<String, String>> rowList1 = new ArrayList<>();
  687. setRowList(doctorParentMap, titleEntities, rowList);
  688. // setRowList(yiParentMap, titleEntities, rowList1);
  689. ExcelPoiUtil excelTool = new ExcelPoiUtil(name);
  690. Map<String, String> param = ImmutableMap.<String, String>builder().put("id", "id").put("pid", "pid")
  691. .put("content", "content").put("fieldName", "fieldName").put("width", "width").build();
  692. List<ColEntity> titleData = excelTool.colEntityTransformer(titleList, param, "0");
  693. Map<String, List<Integer>> autoRowHeights = new HashMap<>();
  694. heads.put(name, titleData);// 每个sheet的表头,sheet名称为key
  695. datas.put(name, rowList);// 每个sheet的内容,sheet名称为key
  696. types.put(name, 0);// 每个sheet的样式类型,sheet名称为key
  697. mergeindexs.put(name, Arrays.asList(0, 1));// 每个sheet的默认行高,sheet名称为key
  698. autoRowHeights.put(name, Arrays.asList(1, 2, 3, 4, 5));
  699. // heads.put("科室损益-医技", titleData);// 每个sheet的表头,sheet名称为key
  700. // datas.put("科室损益-医技", rowList1);// 每个sheet的内容,sheet名称为key
  701. // types.put("科室损益-医技", 0);// 每个sheet的样式类型,sheet名称为key
  702. // mergeindexs.put("科室损益-医技", Arrays.asList(0, 1));// 每个sheet的默认行高,sheet名称为key
  703. // autoRowHeights.put("科室损益-医技", Arrays.asList(1, 2, 3, 4, 5));
  704. // 多个sheet导出
  705. HSSFWorkbook workbook = excelTool.exportWorkbook(heads, datas, types, autoRowHeights, mergeindexs);
  706. // excelTool.save(workbook, "D:\\科室损益.xls");
  707. workbook.write(outputStream);
  708. workbook.close();
  709. }
  710. private static void setRowList(Map<String, List<CostDepartmentProfit>> doctorParentMap, List<TitleEntity> titleEntities,
  711. List<Map<String, String>> rowList) {
  712. DecimalFormat df = new DecimalFormat("0.00%");
  713. doctorParentMap.forEach((k, v) -> {
  714. Map<String, List<CostDepartmentProfit>> listMap = v.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getResponsibilityCode));
  715. listMap.forEach((k1, v1) -> {
  716. Map<String, CostDepartmentProfit> map = v1.stream().collect(Collectors.toMap(costDepartmentProfit -> String.valueOf(costDepartmentProfit.getReportNum()), costDepartmentProfit -> costDepartmentProfit, (a, b) -> b));
  717. for (int i = 0; i < 2; i++) {
  718. Map<String, String> m = new HashMap<String, String>();
  719. for (TitleEntity titleEntity : titleEntities) {
  720. if (titleEntity.getId().equals("parentResponsibility")) {
  721. m.put("parent", v1.get(0).getParentResponsibilityName());
  722. } else if (titleEntity.getId().equals("childResponsibility")) {
  723. m.put("child", v1.get(0).getResponsibilityName());
  724. } else if (titleEntity.getId().equals("item")) {
  725. if (i == 0) {
  726. m.put("itemType", "金额");
  727. } else {
  728. m.put("itemType", "占比");
  729. }
  730. } else {
  731. CostDepartmentProfit profit = map.get(titleEntity.getFieldName());
  732. if (Objects.isNull(profit)) {
  733. m.put(titleEntity.getFieldName(), "");
  734. } else {
  735. if (i == 0) {
  736. m.put(titleEntity.getFieldName(), profit.getAmount().setScale(2, RoundingMode.HALF_UP).toString());
  737. } else {
  738. if (profit.getPercent() != null) {
  739. m.put(titleEntity.getFieldName(), df.format(profit.getPercent()));
  740. } else {
  741. m.put(titleEntity.getFieldName(), "");
  742. }
  743. }
  744. }
  745. }
  746. }
  747. rowList.add(m);
  748. }
  749. });
  750. });
  751. }
  752. private void setDefaultColumn(List<TitleEntity> titleList) {
  753. TitleEntity parentResponsibility = new TitleEntity();
  754. parentResponsibility.setId("parentResponsibility");
  755. parentResponsibility.setPid("0");
  756. parentResponsibility.setContent("");
  757. parentResponsibility.setFieldName("parent");
  758. parentResponsibility.setWidth(15);
  759. parentResponsibility.setSort(NumberConstant.ZERO);
  760. parentResponsibility.setLast(true);
  761. titleList.add(parentResponsibility);
  762. TitleEntity childResponsibility = new TitleEntity();
  763. childResponsibility.setId("childResponsibility");
  764. childResponsibility.setPid("0");
  765. childResponsibility.setContent("");
  766. childResponsibility.setFieldName("child");
  767. childResponsibility.setWidth(15);
  768. childResponsibility.setSort(NumberConstant.ZERO);
  769. childResponsibility.setLast(true);
  770. titleList.add(childResponsibility);
  771. TitleEntity item = new TitleEntity();
  772. item.setId("item");
  773. item.setPid("0");
  774. item.setContent("");
  775. item.setFieldName("itemType");
  776. item.setWidth(10);
  777. item.setSort(NumberConstant.ZERO);
  778. item.setLast(true);
  779. titleList.add(item);
  780. }
  781. private boolean getChild(List<TitleEntity> titleList, ReportForm reportForm, Map<Long, List<ReportForm>> collect) {
  782. List<ReportForm> forms = collect.get(reportForm.getId());
  783. if (!CollectionUtils.isEmpty(forms)) {
  784. for (ReportForm form : forms) {
  785. TitleEntity entity = new TitleEntity();
  786. entity.setId(String.valueOf(form.getId()));
  787. entity.setPid(String.valueOf(form.getParentId()));
  788. entity.setContent(form.getReportName());
  789. entity.setFieldName(String.valueOf(form.getNum()));
  790. entity.setWidth(15);
  791. entity.setSort(form.getSort());
  792. boolean last = getChild(titleList, form, collect);
  793. entity.setLast(last);
  794. titleList.add(entity);
  795. }
  796. return false;
  797. } else {
  798. return true;
  799. }
  800. }
  801. private boolean getChild(List<TitleEntity> titleList, String parentResponsibilityCode,
  802. Map<String, List<ResponsibilityVo>> collect, Map<String, Long> responseCodeIdMap, String pid) {
  803. List<ResponsibilityVo> forms = collect.get(parentResponsibilityCode);
  804. if (!CollectionUtils.isEmpty(forms)) {
  805. for (ResponsibilityVo form : forms) {
  806. TitleEntity entity = new TitleEntity();
  807. entity.setPid(pid);
  808. entity.setContent(form.getResponsibilityName());
  809. entity.setFieldName(form.getResponsibilityName());
  810. entity.setWidth(10);
  811. entity.setSort(form.getSort());
  812. if (form.getResponsibilityCode().equals(parentResponsibilityCode)) {
  813. entity.setId(responseCodeIdMap.get(form.getResponsibilityCode()) + "child");
  814. entity.setLast(false);
  815. titleList.add(entity);
  816. //添加固定两列金额 占比
  817. TitleEntity amountEntity = new TitleEntity();
  818. amountEntity.setId(parentResponsibilityCode + "|" + AMOUNT_FIELD);
  819. amountEntity.setPid(responseCodeIdMap.get(form.getResponsibilityCode()) + "child");
  820. amountEntity.setContent("金额");
  821. amountEntity.setFieldName(parentResponsibilityCode + "|" + AMOUNT_FIELD);
  822. amountEntity.setWidth(15);
  823. amountEntity.setSort(2);
  824. amountEntity.setLast(true);
  825. titleList.add(amountEntity);
  826. TitleEntity percentEntity = new TitleEntity();
  827. percentEntity.setId(parentResponsibilityCode + "|" + PERCENT_FIELD);
  828. percentEntity.setPid(responseCodeIdMap.get(form.getResponsibilityCode()) + "child");
  829. percentEntity.setContent("占比");
  830. percentEntity.setFieldName(parentResponsibilityCode + "|" + PERCENT_FIELD);
  831. percentEntity.setWidth(10);
  832. percentEntity.setSort(2);
  833. percentEntity.setLast(true);
  834. titleList.add(percentEntity);
  835. } else {
  836. entity.setId(String.valueOf(responseCodeIdMap.get(form.getResponsibilityCode())));
  837. boolean last = getChild(titleList, form.getResponsibilityCode(), collect, responseCodeIdMap, String.valueOf(responseCodeIdMap.get(form.getResponsibilityCode())));
  838. entity.setLast(last);
  839. titleList.add(entity);
  840. }
  841. }
  842. } else {
  843. //添加固定两列金额 占比
  844. TitleEntity amountEntity = new TitleEntity();
  845. amountEntity.setId(parentResponsibilityCode + "|" + AMOUNT_FIELD);
  846. amountEntity.setPid(pid);
  847. amountEntity.setContent("金额");
  848. amountEntity.setFieldName(parentResponsibilityCode + "|" + AMOUNT_FIELD);
  849. amountEntity.setWidth(15);
  850. amountEntity.setSort(2);
  851. amountEntity.setLast(true);
  852. titleList.add(amountEntity);
  853. TitleEntity percentEntity = new TitleEntity();
  854. percentEntity.setId(parentResponsibilityCode + "|" + PERCENT_FIELD);
  855. percentEntity.setPid(pid);
  856. percentEntity.setContent("占比");
  857. percentEntity.setFieldName(parentResponsibilityCode + "|" + PERCENT_FIELD);
  858. percentEntity.setWidth(10);
  859. percentEntity.setSort(2);
  860. percentEntity.setLast(true);
  861. titleList.add(percentEntity);
  862. }
  863. return false;
  864. }
  865. private List<ExcelExportEntity> getEntityList(ReportForm reportForm, Map<Long, List<ReportForm>> collect) {
  866. List<ExcelExportEntity> list = new ArrayList<>();
  867. List<ReportForm> forms = collect.get(reportForm.getId());
  868. if (!CollectionUtils.isEmpty(forms)) {
  869. for (ReportForm form : forms) {
  870. ExcelExportEntity entity = new ExcelExportEntity();
  871. entity.setName(form.getReportName());
  872. entity.setKey(form.getNum());
  873. entity.setOrderNum(form.getSort());
  874. entity.setList(getEntityList(form, collect));
  875. list.add(entity);
  876. }
  877. }
  878. return list;
  879. }
  880. /**
  881. * 科室损益计算导出
  882. *
  883. * @param date
  884. * @param hospId
  885. * @param reportType
  886. */
  887. @Override
  888. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  889. public void getDepartProfitReport(String date, Long hospId, Integer reportType) throws Exception {
  890. DateTime dateTime = DateUtil.parse(date);
  891. int year = DateUtil.year(dateTime);
  892. int month = DateUtil.month(dateTime) + 1;
  893. int day = DateUtil.dayOfMonth(dateTime);
  894. DictDataVo dict = centerService.getDict(PROFIT_REPORT_TYPE);
  895. if (Objects.isNull(dict)) {
  896. throw new CostException("未找到损益报表类型字典");
  897. }
  898. List<DictDataVo> dataVoList = dict.getDataVoList();
  899. if (CollectionUtils.isEmpty(dataVoList)) {
  900. throw new CostException("未找到损益报表类型字典");
  901. }
  902. // Map<String, DictDataVo> dictMap = dataVoList.stream().collect(Collectors.toMap(DictDataVo::getCode, vo -> vo, (a, b) -> b));
  903. //去除全院损益字典
  904. List<DictDataVo> collect = dataVoList.stream().filter(f -> f.getValue().equals(NumberConstant.ONE_S)).collect(Collectors.toList());
  905. if (CollectionUtils.isEmpty(collect)) {
  906. throw new CostException("未找到科室损益报表类型字典");
  907. }
  908. for (DictDataVo vo : collect) {
  909. Integer reportDictType = Integer.valueOf(vo.getCode());
  910. //
  911. String time = DateUtil.format(DateUtil.date(), "yyyy年MM月dd日HH时mm分ss秒");
  912. //
  913. FileItemFactory factory = new DiskFileItemFactory(5242880, null);
  914. //
  915. FileItem fileItem = factory.createItem("file", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8", true, vo.getName() + time + ".xlsx");
  916. //// try {
  917. OutputStream outputStream = fileItem.getOutputStream();
  918. // getDepartmentProfit(date, outputStream, reportDictType,vo.getName());
  919. exportDepartmentProfit(date, outputStream, reportDictType, vo.getName());
  920. // } catch (Exception e) {
  921. // throw new RuntimeException(e);
  922. // }
  923. MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
  924. String file = uploadFile(multipartFile);
  925. FileRecord fileRecord = new FileRecord();
  926. fileRecord.setFileName(vo.getName() + time + ".xlsx")
  927. .setFileSource(reportType)
  928. .setFileType(vo.getName())
  929. .setFileUrl(file)
  930. .setHospId(hospId)
  931. .setDateYear(year).setDateMonth(month)
  932. .setCreateTime(System.currentTimeMillis());
  933. fileRecordService.save(fileRecord);
  934. fileItem.delete();
  935. }
  936. }
  937. // public void getDepartProfitReport(String date, Long hospId, Integer reportType) throws Exception {
  938. // DateTime dateTime = DateUtil.parse(date);
  939. // int year = DateUtil.year(dateTime);
  940. // int month = DateUtil.month(dateTime) + 1;
  941. // int day = DateUtil.dayOfMonth(dateTime);
  942. //
  943. //
  944. // String time = DateUtil.format(DateUtil.date(), "yyyy年MM月dd日HH时mm分ss秒");
  945. // FileItemFactory factory = new DiskFileItemFactory(5242880, null);
  946. // FileItem fileItem = factory.createItem("file", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8", true, "测试临床" + time + ".xls");
  947. // OutputStream outputStream = fileItem.getOutputStream();
  948. //
  949. // handleDepartmentProfit(date, outputStream,8,"测试临床");
  950. //
  951. // }
  952. private void getLastDefaultHeader(List<TitleEntity> titleList, String responsibilityId) {
  953. TitleEntity amountEntity = new TitleEntity();
  954. amountEntity.setId(NumberConstant.ONE_S);
  955. amountEntity.setPid(responsibilityId);
  956. amountEntity.setContent(AMOUNT);
  957. amountEntity.setFieldName(AMOUNT);
  958. amountEntity.setWidth(20);
  959. amountEntity.setLast(true);
  960. titleList.add(amountEntity);
  961. TitleEntity percentEntity = new TitleEntity();
  962. percentEntity.setId(NumberConstant.TWO_S);
  963. percentEntity.setPid(responsibilityId);
  964. percentEntity.setContent(PERCENT);
  965. percentEntity.setFieldName(PERCENT);
  966. percentEntity.setWidth(20);
  967. percentEntity.setLast(true);
  968. titleList.add(percentEntity);
  969. }
  970. public String uploadFile(MultipartFile file) {
  971. Long hospId = UserContext.getCurrentLoginHospId();
  972. String fileName = file.getOriginalFilename().substring(0, file.getOriginalFilename().lastIndexOf(".")) + System.currentTimeMillis() + ".xls";
  973. DateTime date = DateUtil.date();
  974. int month = DateUtil.month(date) + 1;
  975. int year = DateUtil.year(date);
  976. int day = DateUtil.dayOfMonth(date);
  977. String fileUrl = "";
  978. try {
  979. // file.transferTo(new File(localFilePath));
  980. // String format = DateUtil.format(date, DatePattern.PURE_DATETIME_PATTERN);
  981. String dataDirectory = minioConfig.getBucketName();
  982. String uploadFileName = "departReport" + "/" + hospId + "/" + year + "/" + month + "/" + day + "/" + fileName;
  983. InputStream inputStream = file.getInputStream();
  984. minioFileUtil.putObject(dataDirectory, uploadFileName, inputStream);
  985. fileUrl = minioFileUtil.getObjectUrl(dataDirectory, uploadFileName);
  986. } catch (IOException e) {
  987. log.error("【文件上传至服务器】失败,绝对路径:{}", e.getMessage());
  988. throw new CostException(ErrorCodeEnum.FILE_UPLOAD_ERROR);
  989. }
  990. return fileUrl;
  991. }
  992. /**
  993. * 删除科室损益数据
  994. *
  995. * @param idList 科室损益的Id的集合
  996. */
  997. @Override
  998. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  999. public void deleteByIds(List<Long> idList) {
  1000. this.removeByIds(idList);
  1001. }
  1002. @Override
  1003. public void computeProfit(String computeDate, Long hospId, String reportType) {
  1004. //需要用代码计算时
  1005. if(IsNeedCalc(reportType)){
  1006. String parameterValue = centerService.getParameterValue(ParameterConstant.ALLOCATION_TYPE);
  1007. if (NumberConstant.ONE_S.equals(parameterValue)) {
  1008. //按责任中心分摊的科室损益计算
  1009. computeProfitAction(computeDate, hospId, reportType);
  1010. }else if (NumberConstant.TWO_S.equals(parameterValue)) {
  1011. //按会计科目分摊的科室损益计算
  1012. calcDeptProfit(computeDate, hospId, reportType);
  1013. }
  1014. //计算同环比
  1015. handleSpecificMonthsCalculation(hospId, computeDate, reportType);
  1016. }
  1017. execDeptProfitSql(computeDate, reportType);
  1018. }
  1019. @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  1020. public void computeProfitAction(String computeDate, Long hospId, String reportType) {
  1021. // DateTime parse = DateUtil.parse(computeDate);
  1022. // int year = DateUtil.year(parse);
  1023. // int month = DateUtil.month(parse) + 1;
  1024. Integer year = ComputeDateUtils.getComputeYear(computeDate);
  1025. Integer month = ComputeDateUtils.getComputeMonth(computeDate);
  1026. // 先查询指定条件的报表数据 查询损益表的数据
  1027. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  1028. .eq(ReportForm::getHospId, hospId)
  1029. .eq(ReportForm::getReportType, reportType));
  1030. if (CollUtil.isEmpty(reportFormList)) {
  1031. throw new CostException(500, "损益表未找到");
  1032. }
  1033. // 遍历报表数据根据报表数据计算方式进行计算
  1034. //这里拆分成两个 原有的最后一层 以及 参数医技
  1035. DictDataVo dataVo = getDictDataVo(reportType);
  1036. Long id = Long.valueOf(dataVo.getExpandOne());
  1037. // 查询最后一个层级的责任中心
  1038. List<CostShareLevel> costShareLevelList = costShareLevelService.list(new QueryWrapper<CostShareLevel>().lambda()
  1039. .eq(CostShareLevel::getHospId, hospId).orderByDesc(CostShareLevel::getLeverSort));
  1040. if (CollUtil.isEmpty(costShareLevelList)) {
  1041. throw new CostException(500, "分摊层级未设置");
  1042. }
  1043. // // TODO 可能多个
  1044. // Long id = costShareLevelList.get(0).getId();
  1045. // 查询责任中心里面是这个层级的所有的收益中心
  1046. // List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  1047. // .eq(Responsibility::getHospId, hospId).eq(Responsibility::getResponsibilityType, NumberConstant.ONE).eq(Responsibility::getShareId, id));
  1048. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  1049. .eq(Responsibility::getHospId, hospId).eq(Responsibility::getShareId, id));
  1050. // 查询责任中心里面是医技分摊层级的
  1051. // List<Responsibility> medicalTechniquesResponsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  1052. // .eq(Responsibility::getHospId, hospId).eq(Responsibility::getShareId, value));
  1053. // 归集后
  1054. List<IncomeCollection> incomeList = incomeCollectionService.list(new QueryWrapper<IncomeCollection>().lambda()
  1055. .eq(IncomeCollection::getHospId, hospId)
  1056. .eq(year > 0, IncomeCollection::getYear, year).eq(month > 0, IncomeCollection::getMonth, month));
  1057. if (CollUtil.isEmpty(incomeList)) {
  1058. throw new CostException(500, "归集后数据不存在");
  1059. }
  1060. Map<Long, List<ReportRelation>> reportRelationMap = reportRelationService.list(new QueryWrapper<ReportRelation>().lambda().eq(ReportRelation::getHospId, hospId)).stream().collect(Collectors.groupingBy(ReportRelation::getReportId));
  1061. // 分摊后的数据说明是分摊后的数据 这里原有为收益中心数据 同步添加医技分摊后数据
  1062. List<AllocationQuery> allocationQueryList = allocationQueryService.list(new QueryWrapper<AllocationQuery>().lambda().eq(AllocationQuery::getHospId, hospId)
  1063. .eq(year > 0, AllocationQuery::getDateYear, year)
  1064. .eq(month > 0, AllocationQuery::getDateMonth, month));
  1065. if (CollUtil.isEmpty(allocationQueryList)) {
  1066. throw new CostException(500, "分摊后数据不存在");
  1067. }
  1068. // 封装数据
  1069. List<AllocationQueryReportVO> allocationQueryReportVOList = BeanUtil.convertList(allocationQueryList, AllocationQueryReportVO.class);
  1070. allocationQueryReportVOList.forEach(i -> {
  1071. i.setAccountingCodes(Arrays.asList(i.getAccountingCode().split(StrUtil.COMMA)));
  1072. i.setAccountingNames(Arrays.asList(i.getAccountingName().split(StrUtil.COMMA)));
  1073. });
  1074. // 查询分摊的报表数据 后面的计算方式需要使用 小计等需要使用
  1075. List<Allocation> allocationList = allocationService.list(new QueryWrapper<Allocation>().lambda().eq(Allocation::getHospId, hospId)
  1076. .eq(year > 0, Allocation::getDateYear, year).eq(month > 0, Allocation::getDateMonth, month));
  1077. if (CollUtil.isEmpty(allocationList)) {
  1078. throw new CostException(500, "分摊报表数据不存在");
  1079. }
  1080. // 查询所有指定类型的损益表
  1081. // 每个责任中心对应报表都要生成记录
  1082. // 封装需要设置的数据
  1083. List<CostDepartmentProfitVO> list = getCostDepartmentProfitVOS(hospId, year, month, reportFormList, costShareLevelList, responsibilityList, incomeList, reportRelationMap, allocationQueryReportVOList, allocationList, Integer.valueOf(reportType));
  1084. // 删除这个年月的数据
  1085. this.remove(new QueryWrapper<CostDepartmentProfit>().lambda().eq(CostDepartmentProfit::getHospId, hospId)
  1086. .eq(CostDepartmentProfit::getYear, year).eq(CostDepartmentProfit::getMonth, month).eq(CostDepartmentProfit::getShareType, Integer.valueOf(reportType)));
  1087. // 添加数据
  1088. List<CostDepartmentProfit> costDepartmentProfits = BeanUtil.convertList(list, CostDepartmentProfit.class);
  1089. long l = System.currentTimeMillis();
  1090. costDepartmentProfits.forEach(i -> {
  1091. i.setCreateTime(l);
  1092. });
  1093. this.saveBatch(costDepartmentProfits);
  1094. //记录最后一次 损益计算日期
  1095. computeLastProfitDateRepository.saveLastComputeDate(hospId, computeDate);
  1096. }
  1097. /**
  1098. * 处理指定月份及关联月份的计算(当前月、下个月、明年同月)
  1099. *
  1100. * @param hospId 院区ID
  1101. * @param computeDate 日期字符串,格式:YYYY-MM
  1102. * @param shareType 报表类型
  1103. */
  1104. public void handleSpecificMonthsCalculation(Long hospId, String computeDate, String shareType) {
  1105. // 解析输入的年月
  1106. Integer currentYear = ComputeDateUtils.getComputeYear(computeDate);
  1107. Integer currentMonth = ComputeDateUtils.getComputeMonth(computeDate);
  1108. // 计算需要处理的期间信息
  1109. List<PeriodInfoVO> periods = calculateAllRelatedPeriods(currentYear, currentMonth);
  1110. // 获取所有相关的期间数据
  1111. Map<String, List<CostDepartmentProfit>> allDataMap = loadAllPeriodData(hospId, shareType, periods);
  1112. // 对每个期间执行计算
  1113. for (PeriodInfoVO period : periods) {
  1114. String currentKey = buildPeriodKey(period.getYear(), period.getMonth());
  1115. if (allDataMap.containsKey(currentKey) && !CollectionUtils.isEmpty(allDataMap.get(currentKey))) {
  1116. List<CostDepartmentProfit> currentRecords = allDataMap.get(currentKey);
  1117. List<CostDepartmentProfit> prevRecords = getPeriodData(allDataMap, period.getPrevYear(), period.getPrevMonth());
  1118. List<CostDepartmentProfit> lastYearRecords = getPeriodData(allDataMap, period.getLastYear(), period.getLastMonth());
  1119. // 执行批量计算
  1120. calculatePeriodComparisonWithPreloadedData(currentRecords, prevRecords, lastYearRecords);
  1121. }
  1122. }
  1123. }
  1124. /**
  1125. * 构建所有需要处理的期间信息
  1126. */
  1127. private List<PeriodInfoVO> calculateAllRelatedPeriods(int baseYear, int baseMonth) {
  1128. List<PeriodInfoVO> result = new ArrayList<>();
  1129. // 添加当前期间
  1130. result.add(calculatePeriodInfo(baseYear, baseMonth));
  1131. // 下个月
  1132. int nextMonth = baseMonth + 1;
  1133. int nextMonthYear = baseYear;
  1134. if (baseMonth == 12) {
  1135. nextMonth = 1;
  1136. nextMonthYear += 1;
  1137. }
  1138. result.add(calculatePeriodInfo(nextMonthYear, nextMonth));
  1139. // 明年同月
  1140. result.add(calculatePeriodInfo(baseYear + 1, baseMonth));
  1141. return result;
  1142. }
  1143. /**
  1144. * 构建单个期间的信息(当前月、上月、去年同期)
  1145. */
  1146. private PeriodInfoVO calculatePeriodInfo(int year, int month) {
  1147. PeriodInfoVO info = new PeriodInfoVO();
  1148. info.setYear( year);
  1149. info.setMonth( month);
  1150. // 上期
  1151. if (month == 1) {
  1152. info.setPrevYear(year - 1);
  1153. info.setPrevMonth(12);
  1154. } else {
  1155. info.setPrevYear( year);
  1156. info.setPrevMonth(month - 1);
  1157. }
  1158. // 同期
  1159. info.setLastYear(year - 1);
  1160. info.setLastMonth( month);
  1161. return info;
  1162. }
  1163. /**
  1164. * 加载所有相关期间的数据(去重处理)
  1165. */
  1166. private Map<String, List<CostDepartmentProfit>> loadAllPeriodData(Long hospId, String shareType, List<PeriodInfoVO> periods) {
  1167. Set<String> allPeriodKeys = new HashSet<>();
  1168. // 收集所有需要查询的年月组合
  1169. for (PeriodInfoVO period : periods) {
  1170. allPeriodKeys.add(buildPeriodKey(period.getYear(), period.getMonth()));
  1171. allPeriodKeys.add(buildPeriodKey(period.getPrevYear(), period.getPrevMonth()));
  1172. allPeriodKeys.add(buildPeriodKey(period.getLastYear(), period.getLastMonth()));
  1173. }
  1174. // 去重后查询
  1175. Map<String, List<CostDepartmentProfit>> result = new HashMap<>();
  1176. Set<String> processedKeys = new HashSet<>(); // 已处理的键集合
  1177. for (String key : allPeriodKeys) {
  1178. if (!processedKeys.contains(key)) {
  1179. int[] ym = parsePeriodKey(key);
  1180. List<CostDepartmentProfit> records = getPeriodRecords(hospId, shareType, ym[0], ym[1]);
  1181. result.put(key, records);
  1182. processedKeys.add(key); // 标记为已处理
  1183. }
  1184. }
  1185. return result;
  1186. }
  1187. /**
  1188. * 获取指定期间的数据
  1189. *
  1190. * @param hospId 院区ID
  1191. * @param shareType 报表类型
  1192. * @param year 年份
  1193. * @param month 月份
  1194. * @return 符合条件的数据列表
  1195. */
  1196. private List<CostDepartmentProfit> getPeriodRecords(Long hospId, String shareType, int year, int month) {
  1197. return this.list(new QueryWrapper<CostDepartmentProfit>().lambda()
  1198. .eq(CostDepartmentProfit::getHospId, hospId)
  1199. .eq(CostDepartmentProfit::getShareType, shareType)
  1200. .eq(CostDepartmentProfit::getYear, year)
  1201. .eq(CostDepartmentProfit::getMonth, month)
  1202. .eq(CostDepartmentProfit::getDeleteTime, 0));
  1203. }
  1204. /**
  1205. * 根据年月获取对应的数据(从内存中查找)
  1206. */
  1207. private List<CostDepartmentProfit> getPeriodData(Map<String, List<CostDepartmentProfit>> dataMap, int year, int month) {
  1208. String key = buildPeriodKey(year, month);
  1209. return dataMap.getOrDefault(key, Collections.emptyList());
  1210. }
  1211. /**
  1212. * 批量计算同环比及同期金额(使用预加载数据)
  1213. */
  1214. public void calculatePeriodComparisonWithPreloadedData(
  1215. List<CostDepartmentProfit> currentRecords,
  1216. List<CostDepartmentProfit> prevRecords,
  1217. List<CostDepartmentProfit> lastYearRecords) {
  1218. if (CollectionUtils.isEmpty(currentRecords)) {
  1219. return;
  1220. }
  1221. // 创建责任码+报表编号到记录的映射
  1222. Map<String, CostDepartmentProfit> prevMap = new HashMap<>();
  1223. for (CostDepartmentProfit record : prevRecords) {
  1224. String key = buildRecordKey(record);
  1225. prevMap.put(key, record);
  1226. }
  1227. Map<String, CostDepartmentProfit> lastYearMap = new HashMap<>();
  1228. for (CostDepartmentProfit record : lastYearRecords) {
  1229. String key = buildRecordKey(record);
  1230. lastYearMap.put(key, record);
  1231. }
  1232. // 批量更新数据
  1233. for (CostDepartmentProfit record : currentRecords) {
  1234. String key = buildRecordKey(record);
  1235. CostDepartmentProfit prevRecord = prevMap.get(key);
  1236. CostDepartmentProfit lastYearRecord = lastYearMap.get(key);
  1237. // 设置上期和同期金额
  1238. record.setPrevPeriodAmount(prevRecord != null ? prevRecord.getAmount() : BigDecimal.ZERO);
  1239. record.setSamePeriodAmount(lastYearRecord != null ? lastYearRecord.getAmount() : BigDecimal.ZERO);
  1240. // 计算环比和同比
  1241. record.setMomRate(calculateProfitRate(record.getAmount().subtract(record.getSamePeriodAmount()), record.getPrevPeriodAmount()));
  1242. record.setYoyRate(calculateProfitRate(record.getAmount().subtract(record.getSamePeriodAmount()), record.getSamePeriodAmount()));
  1243. }
  1244. // 批量更新数据库
  1245. this.updateBatchById(currentRecords);
  1246. }
  1247. /**
  1248. * 构建期间键(用于存储和检索数据)
  1249. */
  1250. private String buildPeriodKey(int year, int month) {
  1251. return year + "_" + month;
  1252. }
  1253. /**
  1254. * 解析期间键为年月数组
  1255. */
  1256. private int[] parsePeriodKey(String key) {
  1257. String[] parts = key.split("_");
  1258. return new int[]{Integer.parseInt(parts[0]), Integer.parseInt(parts[1])};
  1259. }
  1260. /**
  1261. * 构建记录的唯一标识键(责任码 + 报表编号)
  1262. */
  1263. private String buildRecordKey(CostDepartmentProfit record) {
  1264. return record.getResponsibilityCode() + "_" + record.getReportNum();
  1265. }
  1266. /**
  1267. * 安全地计算百分比:避免除零错误
  1268. */
  1269. private BigDecimal calculateProfitRate(BigDecimal current, BigDecimal base) {
  1270. if (base == null || base.compareTo(BigDecimal.ZERO) == 0) {
  1271. return BigDecimal.ZERO;
  1272. }
  1273. BigDecimal bigDecimal = current.divide(base, 4, RoundingMode.HALF_UP).setScale(4, RoundingMode.HALF_UP);
  1274. return bigDecimal;
  1275. }
  1276. /**
  1277. * 执行科室损益后续脚本
  1278. *
  1279. * @param computeDate
  1280. * @param reportType
  1281. */
  1282. public void execDeptProfitSql(String computeDate, String reportType) {
  1283. Map<String, String> sqlParameter = new HashMap<>();
  1284. sqlParameter.put(SQLParameter.COMPUTE_DATE_CODE, computeDate);
  1285. sqlParameter.put(SQLParameter.REPORT_TYPE_CODE, reportType);
  1286. sqlService.autoExecuteSql(CustomSqlTypeEnum.DEPT_PROFIT_CALC.getCode(), sqlParameter);
  1287. }
  1288. private DictDataVo getDictDataVo(String reportType) {
  1289. DictDataVo dict = centerService.getDict(PROFIT_REPORT_TYPE);
  1290. if (Objects.isNull(dict)) {
  1291. throw new CostException("未找到损益报表类型字典");
  1292. }
  1293. List<DictDataVo> dataVoList = dict.getDataVoList();
  1294. if (CollectionUtils.isEmpty(dataVoList)) {
  1295. throw new CostException("未找到损益报表类型字典");
  1296. }
  1297. Map<String, DictDataVo> dictMap = dataVoList.stream().collect(Collectors.toMap(DictDataVo::getCode, vo -> vo, (a, b) -> b));
  1298. DictDataVo dataVo = dictMap.get(reportType);
  1299. if (Objects.isNull(dataVo)) {
  1300. throw new CostException("未找到损益报表类型为【" + reportType + "】的字典");
  1301. }
  1302. return dataVo;
  1303. }
  1304. /**
  1305. * 计算界面报表样式展示
  1306. *
  1307. * @param computeDate 核算年月
  1308. * @param hospId 医院id
  1309. * @param reportType 报表类型
  1310. * @return
  1311. */
  1312. @Override
  1313. public Object computeProfitReport(String computeDate, Long hospId, String reportType) {
  1314. Integer year = ComputeDateUtils.getComputeYear(computeDate);
  1315. Integer month = ComputeDateUtils.getComputeMonth(computeDate);
  1316. Integer integerReportType = Integer.valueOf(reportType);
  1317. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  1318. .eq(ReportForm::getHospId, hospId)
  1319. .eq(ReportForm::getReportType, integerReportType).eq(ReportForm::getHide, NumberConstant.ONE));
  1320. if (CollUtil.isEmpty(reportFormList)) {
  1321. throw new CostException(500, "损益表未找到");
  1322. }
  1323. List<CommonTitleReportVo> commonTitleReportVos = reportFormList.stream().map(reportForm -> {
  1324. CommonTitleReportVo commonTitleReportVo = new CommonTitleReportVo(reportForm.getId(), reportForm.getParentId(), reportForm.getReportName(), reportForm.getSort());
  1325. commonTitleReportVo.setDataType(reportForm.getDataType());
  1326. commonTitleReportVo.setDecimalPlace(reportForm.getDecimalPlace());
  1327. commonTitleReportVo.setPermil(reportForm.getPermil());
  1328. return commonTitleReportVo;
  1329. }).collect(Collectors.toList());
  1330. // List<CommonTitleReportVo> commonTitleReportVos = BeanUtil.convertList(reportFormList,CommonTitleReportVo.class);
  1331. Map<Long, List<CommonTitleReportVo>> collect = commonTitleReportVos.stream().collect(Collectors.groupingBy(CommonTitleReportVo::getParentReportId));
  1332. List<CommonTitleReportVo> titleReportVoList = collect.get(NumberConstant.ZERO_L);
  1333. for (CommonTitleReportVo reportVo : titleReportVoList) {
  1334. List<CommonTitleReportVo> childList = collect.get(reportVo.getReportId());
  1335. if (!CollectionUtils.isEmpty(childList)) {
  1336. childList.sort(Comparator.comparing(CommonTitleReportVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  1337. reportVo.setChildTitle(setReportChildren(childList, collect));
  1338. }
  1339. }
  1340. ProfitReportResponse response = new ProfitReportResponse();
  1341. if (!CollectionUtils.isEmpty(titleReportVoList)) {
  1342. titleReportVoList.sort(Comparator.comparing(CommonTitleReportVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  1343. }
  1344. response.setTitle(titleReportVoList);
  1345. List<CostDepartmentProfit> departmentProfits = this.list(new QueryWrapper<CostDepartmentProfit>().lambda()
  1346. .eq(CostDepartmentProfit::getHospId, hospId)
  1347. .eq(CostDepartmentProfit::getYear, year)
  1348. .eq(CostDepartmentProfit::getMonth, month)
  1349. .eq(CostDepartmentProfit::getShareType, integerReportType));
  1350. if (CollectionUtils.isEmpty(departmentProfits)) {
  1351. response.setData(new ArrayList<>());
  1352. return response;
  1353. }
  1354. List<CostDepartmentProfit> filterProfit = new ArrayList<>();
  1355. Map<Long, List<CostDepartmentProfit>> collectGroup = departmentProfits.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getReportId));
  1356. for (ReportForm reportForm : reportFormList) {
  1357. Integer hide = reportForm.getHide();
  1358. if (hide.equals(NumberConstant.ONE)) {
  1359. List<CostDepartmentProfit> costDepartmentProfits = collectGroup.get(reportForm.getId());
  1360. if (!CollectionUtils.isEmpty(costDepartmentProfits)) {
  1361. filterProfit.addAll(costDepartmentProfits);
  1362. }
  1363. }
  1364. }
  1365. List<CostProfitVo> costProfitVos = BeanUtil.convertList(filterProfit, CostProfitVo.class);
  1366. Map<String, List<CostProfitVo>> responseGroup = costProfitVos.stream().collect(Collectors.groupingBy(CostProfitVo::getResponsibilityCode));
  1367. //取一行数据 作为父类模版
  1368. Set<Map.Entry<String, List<CostProfitVo>>> entrySet = responseGroup.entrySet();
  1369. Map.Entry<String, List<CostProfitVo>> firstEntry = entrySet.iterator().next();
  1370. List<CostProfitVo> parentModel = firstEntry.getValue();
  1371. //备注:所有记算过的数据都是最后一层责任中心,所以按最后一层责任 中心 找出所有上级责任中心后 给模版添加到上层责任中心
  1372. //按报表类型 取字典
  1373. DictDataVo dataVo = getDictDataVo(reportType);
  1374. Long id = Long.valueOf(dataVo.getExpandOne());
  1375. //按字典给护 的 分摊层级id 查找对应报表 的责任中心 找出的是没有父类的
  1376. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  1377. .eq(Responsibility::getHospId, hospId)
  1378. .eq(Responsibility::getShareId, id));
  1379. if (CollectionUtils.isEmpty(responsibilityList)) {
  1380. throw new CostException("未找到对应分摊层级责任中心");
  1381. }
  1382. //获取授权的责任中心
  1383. responsibilityList = userResponsibilityRightService.getAuthorizedResponsibility(responsibilityList);
  1384. //获取当前院区所有的责任中心
  1385. List<Responsibility> responsibilityAllList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  1386. .eq(Responsibility::getHospId, hospId));
  1387. Map<Long, Responsibility> map = responsibilityAllList.stream().collect(Collectors.toMap(Responsibility::getId, responsibility -> responsibility, (a, b) -> b));
  1388. Map<String, Long> responseCodeParentIdMap = responsibilityAllList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getParentId, (a, b) -> b));
  1389. Map<String, String> responseNameMap = responsibilityAllList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getResponsibilityName, (a, b) -> b));
  1390. //通过查询的最下层责任中心 找到其所有父类责任中心
  1391. List<Responsibility> parentResponseList = new ArrayList<>();
  1392. for (Responsibility responsibility : responsibilityList) {
  1393. if (responsibility.getParentId().equals(NumberConstant.ZERO_L)) {
  1394. parentResponseList.add(responsibility);
  1395. } else {
  1396. Responsibility responsibility1 = map.get(responsibility.getParentId());
  1397. if (Objects.nonNull(responsibility1)) {
  1398. parentResponseList.add(responsibility1);
  1399. if (!responsibility1.getParentId().equals(NumberConstant.ZERO_L)) {
  1400. getResponsibilityParent(responsibility1, map, parentResponseList);
  1401. }
  1402. }
  1403. }
  1404. }
  1405. //去除重复组装数据
  1406. List<Responsibility> parentResponsibility = parentResponseList.stream().distinct().collect(Collectors.toList());
  1407. //找出最外层用于出参循环
  1408. List<Responsibility> responseParent = parentResponsibility.stream().filter(f -> f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  1409. //只留下中间责任中心
  1410. parentResponsibility.removeAll(responseParent);
  1411. //最上级添加模版
  1412. List<CostProfitVo> parentReportVos = new ArrayList<>();
  1413. for (Responsibility responsibility : responseParent) {
  1414. List<CostProfitVo> profitVos = BeanUtil.convertList(parentModel, CostProfitVo.class);
  1415. for (CostProfitVo costProfitVo : profitVos) {
  1416. costProfitVo.setResponsibilityCode(responsibility.getResponsibilityCode());
  1417. costProfitVo.setResponsibilityName(responsibility.getResponsibilityName());
  1418. costProfitVo.setSort(responsibility.getSort());
  1419. costProfitVo.setAmount(BigDecimal.ZERO);
  1420. costProfitVo.setPercent(BigDecimal.ZERO);
  1421. parentReportVos.add(costProfitVo);
  1422. }
  1423. }
  1424. //中间责任中心 添加模版 添加父类责任中心
  1425. List<CostProfitVo> middleReportVos = new ArrayList<>();
  1426. for (Responsibility responsibility : parentResponsibility) {
  1427. List<CostProfitVo> profitVos = BeanUtil.convertList(parentModel, CostProfitVo.class);
  1428. Long aLong = responseCodeParentIdMap.get(responsibility.getResponsibilityCode());
  1429. for (CostProfitVo costProfitVo : profitVos) {
  1430. costProfitVo.setResponsibilityCode(responsibility.getResponsibilityCode());
  1431. costProfitVo.setResponsibilityName(responsibility.getResponsibilityName());
  1432. costProfitVo.setSort(responsibility.getSort());
  1433. costProfitVo.setAmount(BigDecimal.ZERO);
  1434. costProfitVo.setPercent(BigDecimal.ZERO);
  1435. if (aLong.equals(NumberConstant.ZERO_L)) {
  1436. costProfitVo.setParentResponsibilityCode(responsibility.getResponsibilityCode());
  1437. costProfitVo.setParentResponsibilityName(responsibility.getResponsibilityName());
  1438. } else {
  1439. Responsibility responsibility1 = map.get(aLong);
  1440. if (Objects.nonNull(responsibility1)) {
  1441. costProfitVo.setParentResponsibilityCode(responsibility1.getResponsibilityCode());
  1442. costProfitVo.setParentResponsibilityName(responsibility1.getResponsibilityName());
  1443. }
  1444. }
  1445. middleReportVos.add(costProfitVo);
  1446. }
  1447. }
  1448. // 添加父类责任中心
  1449. //已计算过的数据添加上级责任中心
  1450. for (CostProfitVo departmentProfit : costProfitVos) {
  1451. Long aLong = responseCodeParentIdMap.get(departmentProfit.getResponsibilityCode());
  1452. if (aLong != null) {
  1453. if (aLong.equals(NumberConstant.ZERO_L)) {
  1454. // Long aLong1 = responseCodeIdMap.get(departmentProfit.getResponsibilityCode());
  1455. departmentProfit.setParentResponsibilityCode(departmentProfit.getResponsibilityCode());
  1456. departmentProfit.setParentResponsibilityName(departmentProfit.getResponsibilityName());
  1457. } else {
  1458. Responsibility responsibility = map.get(aLong);
  1459. if (Objects.nonNull(responsibility)) {
  1460. departmentProfit.setParentResponsibilityCode(responsibility.getResponsibilityCode());
  1461. departmentProfit.setParentResponsibilityName(responsibility.getResponsibilityName());
  1462. }
  1463. }
  1464. }
  1465. }
  1466. //已计算过的 按责任中心-报表id 分组
  1467. Map<String, Map<Long, List<CostProfitVo>>> sumGroup = new HashMap<>();
  1468. //已计算过的 按父类责任中心分组
  1469. Map<String, List<CostProfitVo>> collect1 = costProfitVos.stream().collect(Collectors.groupingBy(CostProfitVo::getParentResponsibilityCode));
  1470. if (!CollectionUtils.isEmpty(collect1)) {
  1471. collect1.forEach((k, v) -> {
  1472. Map<Long, List<CostProfitVo>> collect2 = v.stream().collect(Collectors.groupingBy(CostProfitVo::getReportId));
  1473. sumGroup.put(k, collect2);
  1474. });
  1475. }
  1476. //计算所有中间层
  1477. List<CostProfitVo> middleFilterReportVos = new ArrayList<>();
  1478. for (CostProfitVo middleReportVo : middleReportVos) {
  1479. String responsibilityCode = middleReportVo.getResponsibilityCode();
  1480. Map<Long, List<CostProfitVo>> longListMap = sumGroup.get(responsibilityCode);
  1481. if (!CollectionUtils.isEmpty(longListMap)) {
  1482. List<CostProfitVo> profitVos = longListMap.get(middleReportVo.getReportId());
  1483. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  1484. if (!CollectionUtils.isEmpty(profitVos)) {
  1485. profitVos.forEach(m -> {
  1486. sum.updateAndGet(v -> v.add(m.getAmount()));
  1487. });
  1488. middleReportVo.setAmount(sum.get());
  1489. }
  1490. } else {
  1491. //未在最下层找到数据的可能还是一个分类 提取出来
  1492. middleFilterReportVos.add(middleReportVo);
  1493. }
  1494. }
  1495. //用于处理多层级中 有两层中间层的数据
  1496. if (!CollectionUtils.isEmpty(middleFilterReportVos)) {
  1497. handleMiddleFilterReport(middleReportVos, middleFilterReportVos);
  1498. }
  1499. Map<String, Map<Long, List<CostProfitVo>>> sumMiddleGroup = new HashMap<>();
  1500. Map<String, List<CostProfitVo>> collect2 = middleReportVos.stream().collect(Collectors.groupingBy(CostProfitVo::getParentResponsibilityCode));
  1501. if (!CollectionUtils.isEmpty(collect2)) {
  1502. collect2.forEach((k, v) -> {
  1503. Map<Long, List<CostProfitVo>> collect3 = v.stream().collect(Collectors.groupingBy(CostProfitVo::getReportId));
  1504. sumMiddleGroup.put(k, collect3);
  1505. });
  1506. }
  1507. // 最上层的需要执行两次分组 两层时取最后一层计算 多层时取 中间层计算合计
  1508. for (CostProfitVo parentReportVo : parentReportVos) {
  1509. String responsibilityCode = parentReportVo.getResponsibilityCode();
  1510. Map<Long, List<CostProfitVo>> longListMap = sumGroup.get(responsibilityCode);
  1511. //在最下层找到的 说明是两层
  1512. if (!CollectionUtils.isEmpty(longListMap)) {
  1513. List<CostProfitVo> profitVos = longListMap.get(parentReportVo.getReportId());
  1514. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  1515. if (!CollectionUtils.isEmpty(profitVos)) {
  1516. profitVos.forEach(m -> {
  1517. sum.updateAndGet(v -> v.add(m.getAmount()));
  1518. });
  1519. }
  1520. //再找中间层是否存在子类
  1521. Map<Long, List<CostProfitVo>> longListMap1 = sumMiddleGroup.get(responsibilityCode);
  1522. if (!CollectionUtils.isEmpty(longListMap1)) {
  1523. List<CostProfitVo> profitVos1 = longListMap1.get(parentReportVo.getReportId());
  1524. if (!CollectionUtils.isEmpty(profitVos1)) {
  1525. profitVos1.forEach(m -> {
  1526. sum.updateAndGet(v -> v.add(m.getAmount()));
  1527. });
  1528. }
  1529. }
  1530. parentReportVo.setAmount(sum.get());
  1531. } else {
  1532. //未在最下层找到从中间层找进行计算
  1533. Map<Long, List<CostProfitVo>> longListMap1 = sumMiddleGroup.get(responsibilityCode);
  1534. if (!CollectionUtils.isEmpty(longListMap1)) {
  1535. List<CostProfitVo> profitVos = longListMap1.get(parentReportVo.getReportId());
  1536. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  1537. if (!CollectionUtils.isEmpty(profitVos)) {
  1538. profitVos.forEach(m -> {
  1539. sum.updateAndGet(v -> v.add(m.getAmount()));
  1540. });
  1541. }
  1542. parentReportVo.setAmount(sum.get());
  1543. }
  1544. }
  1545. }
  1546. //把最上层 中间层 最下层组装成一个list 进行分组
  1547. //组装数据
  1548. Map<String, List<CostProfitVo>> collect3 = parentReportVos.stream().collect(Collectors.groupingBy(CostProfitVo::getResponsibilityCode));
  1549. List<CostResponsibilityProfitVo> list = new ArrayList<>();
  1550. collect3.forEach((k, v) -> {
  1551. CostResponsibilityProfitVo vo = new CostResponsibilityProfitVo();
  1552. // List<CostProfitVo> costProfitVos1 = collect3.get(s);
  1553. vo.setResponsibilityCode(k);
  1554. vo.setResponsibilityName(responseNameMap.get(k));
  1555. if (!CollectionUtils.isEmpty(v)) {
  1556. vo.setSort(v.get(0).getSort());
  1557. List<CommonDataReportVo> dataReportVos = new ArrayList<>();
  1558. for (CostProfitVo costProfitVo : v) {
  1559. CommonDataReportVo vo1 = new CommonDataReportVo();
  1560. vo1.setReportId(costProfitVo.getReportId());
  1561. vo1.setValue(costProfitVo.getAmount());
  1562. dataReportVos.add(vo1);
  1563. }
  1564. vo.setProfitList(dataReportVos);
  1565. } else {
  1566. vo.setSort(NumberConstant.ZERO);
  1567. vo.setProfitList(new ArrayList<>());
  1568. }
  1569. list.add(vo);
  1570. });
  1571. middleReportVos.addAll(costProfitVos);
  1572. Map<String, List<CostProfitVo>> collect5 = middleReportVos.stream().collect(Collectors.groupingBy(CostProfitVo::getResponsibilityCode));
  1573. List<CostResponsibilityProfitVo> list1 = new ArrayList<>();
  1574. collect5.forEach((k, v) -> {
  1575. CostResponsibilityProfitVo vo = new CostResponsibilityProfitVo();
  1576. vo.setResponsibilityCode(k);
  1577. vo.setResponsibilityName(responseNameMap.get(k));
  1578. if (!CollectionUtils.isEmpty(v)) {
  1579. vo.setParentResponsibilityCode(v.get(0).getParentResponsibilityCode());
  1580. vo.setSort(v.get(0).getSort());
  1581. List<CommonDataReportVo> dataReportVos = new ArrayList<>();
  1582. for (CostProfitVo costProfitVo : v) {
  1583. CommonDataReportVo vo1 = new CommonDataReportVo();
  1584. vo1.setReportId(costProfitVo.getReportId());
  1585. vo1.setValue(costProfitVo.getAmount());
  1586. dataReportVos.add(vo1);
  1587. }
  1588. vo.setProfitList(dataReportVos);
  1589. } else {
  1590. vo.setSort(NumberConstant.ZERO);
  1591. vo.setProfitList(new ArrayList<>());
  1592. }
  1593. list1.add(vo);
  1594. });
  1595. Map<String, List<CostResponsibilityProfitVo>> collect4 = list1.stream().collect(Collectors.groupingBy(CostResponsibilityProfitVo::getParentResponsibilityCode));
  1596. for (CostResponsibilityProfitVo vo : list) {
  1597. List<CostResponsibilityProfitVo> list2 = collect4.get(vo.getResponsibilityCode());
  1598. if (!CollectionUtils.isEmpty(list2)) {
  1599. if (!list2.get(0).getResponsibilityCode().equals(vo.getResponsibilityCode())) {
  1600. vo.setChild(setProfitChildren(list2, collect4));
  1601. }
  1602. }
  1603. }
  1604. //按责任中心序号排序
  1605. costResponsibilityProfitVoSort(list);
  1606. response.setData(list);
  1607. //提取出最上层
  1608. return response;
  1609. }
  1610. private static void handleMiddleFilterReport(List<CostProfitVo> middleReportVos, List<CostProfitVo> middleFilterReportVos) {
  1611. //去除没计算完的
  1612. middleReportVos.removeAll(middleFilterReportVos);
  1613. //说明有没计算完的
  1614. Map<String, Map<Long, List<CostProfitVo>>> sumFilterGroup = new HashMap<>();
  1615. //已计算过的 按父类责任中心分组
  1616. Map<String, List<CostProfitVo>> collect2 = middleReportVos.stream().collect(Collectors.groupingBy(CostProfitVo::getParentResponsibilityCode));
  1617. if (!CollectionUtils.isEmpty(collect2)) {
  1618. collect2.forEach((k, v) -> {
  1619. Map<Long, List<CostProfitVo>> collect3 = v.stream().collect(Collectors.groupingBy(CostProfitVo::getReportId));
  1620. sumFilterGroup.put(k, collect3);
  1621. });
  1622. }
  1623. for (CostProfitVo middleReportVo : middleFilterReportVos) {
  1624. String responsibilityCode = middleReportVo.getResponsibilityCode();
  1625. Map<Long, List<CostProfitVo>> longListMap = sumFilterGroup.get(responsibilityCode);
  1626. if (!CollectionUtils.isEmpty(longListMap)) {
  1627. List<CostProfitVo> profitVos = longListMap.get(middleReportVo.getReportId());
  1628. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  1629. if (!CollectionUtils.isEmpty(profitVos)) {
  1630. profitVos.forEach(m -> {
  1631. sum.updateAndGet(v -> v.add(m.getAmount()));
  1632. });
  1633. middleReportVo.setAmount(sum.get());
  1634. }
  1635. }
  1636. //todo:先处理到四层
  1637. }
  1638. //添加回去
  1639. middleReportVos.addAll(middleFilterReportVos);
  1640. }
  1641. private List<CostResponsibilityProfitVo> setProfitChildren(List<CostResponsibilityProfitVo> child, Map<String, List<CostResponsibilityProfitVo>> collect) {
  1642. for (CostResponsibilityProfitVo reportVo : child) {
  1643. if (!CollectionUtils.isEmpty(collect.get(reportVo.getResponsibilityCode()))) {
  1644. reportVo.setChild(setProfitChildren(collect.get(reportVo.getResponsibilityCode()), collect));
  1645. }
  1646. }
  1647. return child;
  1648. }
  1649. private List<CommonTitleReportVo> setReportChildren(List<CommonTitleReportVo> child, Map<Long, List<CommonTitleReportVo>> collect) {
  1650. for (CommonTitleReportVo reportVo : child) {
  1651. if (!CollectionUtils.isEmpty(collect.get(reportVo.getReportId()))) {
  1652. reportVo.setChildTitle(setReportChildren(collect.get(reportVo.getReportId()), collect));
  1653. }
  1654. }
  1655. if (!CollectionUtils.isEmpty(child)) {
  1656. child.sort(Comparator.comparing(CommonTitleReportVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  1657. }
  1658. return child;
  1659. }
  1660. /**
  1661. * 损益计算获取责任中心列表
  1662. *
  1663. * @param reportType 报表类型
  1664. * @param responsibilityName 责任中心名称
  1665. * @return 责任中心列表
  1666. */
  1667. @Override
  1668. public Object getProfitResponsibility(String reportType, String responsibilityName) {
  1669. Long hospId = UserContext.getHospId();
  1670. DictDataVo dataVo = getDictDataVo(reportType);
  1671. Long id = Long.valueOf(dataVo.getExpandOne());
  1672. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  1673. .eq(Responsibility::getHospId, hospId)
  1674. .eq(Responsibility::getShareId, id)
  1675. .and(q -> q.eq(Responsibility::getStatus, NumberConstant.ONE).or().eq(Responsibility::getStatus, null))
  1676. .like(StrUtil.isNotBlank(responsibilityName), Responsibility::getResponsibilityName, responsibilityName));
  1677. if (CollectionUtils.isEmpty(responsibilityList)) {
  1678. throw new CostException("未找到对应分摊层级责任中心");
  1679. }
  1680. //获取授权的责任中心
  1681. responsibilityList = userResponsibilityRightService.getAuthorizedResponsibility(responsibilityList);
  1682. List<Responsibility> responsibilityAllList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  1683. .eq(Responsibility::getHospId, hospId));
  1684. Map<Long, Responsibility> map = responsibilityAllList.stream().collect(Collectors.toMap(Responsibility::getId, responsibility -> responsibility, (a, b) -> b));
  1685. List<Responsibility> addList = new ArrayList<>();
  1686. for (Responsibility responsibility : responsibilityList) {
  1687. Responsibility responsibility1 = map.get(responsibility.getParentId());
  1688. if (Objects.nonNull(responsibility1)) {
  1689. addList.add(responsibility1);
  1690. if (!responsibility1.getParentId().equals(NumberConstant.ZERO_L)) {
  1691. getResponsibilityParent(responsibility1, map, addList);
  1692. }
  1693. }
  1694. }
  1695. if (!CollectionUtils.isEmpty(addList)) {
  1696. //去重
  1697. List<Responsibility> collect = addList.stream().distinct().collect(Collectors.toList());
  1698. responsibilityList.addAll(collect);
  1699. }
  1700. Map<Long, List<Responsibility>> collect = responsibilityList.stream().collect(Collectors.groupingBy(Responsibility::getParentId));
  1701. List<Responsibility> responsibilities = collect.get(NumberConstant.ZERO_L);
  1702. collect.remove(NumberConstant.ZERO_L);
  1703. for (Responsibility responsibility : responsibilities) {
  1704. List<Responsibility> responsibilities1 = collect.get(responsibility.getId());
  1705. if (!CollectionUtils.isEmpty(responsibilities1)) {
  1706. responsibility.setChildren(setResponsibilityChildren(responsibilities1, collect));
  1707. }
  1708. }
  1709. responsibilitySort(responsibilities);
  1710. return responsibilities;
  1711. }
  1712. private void getResponsibilityParent(Responsibility responsibility, Map<Long, Responsibility> collect, List<Responsibility> addList) {
  1713. Responsibility responsibility1 = collect.get(responsibility.getParentId());
  1714. if (Objects.nonNull(responsibility1)) {
  1715. addList.add(responsibility1);
  1716. if (!responsibility1.getParentId().equals(NumberConstant.ZERO_L)) {
  1717. getResponsibilityParent(responsibility1, collect, addList);
  1718. }
  1719. }
  1720. }
  1721. private List<Responsibility> setResponsibilityChildren(List<Responsibility> child, Map<Long, List<Responsibility>> collect) {
  1722. for (Responsibility responsibility : child) {
  1723. if (!CollectionUtils.isEmpty(collect.get(responsibility.getId()))) {
  1724. responsibility.setChildren(setResponsibilityChildren(collect.get(responsibility.getId()), collect));
  1725. }
  1726. }
  1727. return child;
  1728. }
  1729. /**
  1730. * @param computeDate
  1731. * @param hospId
  1732. * @param reportType
  1733. * @param responsibilityCode
  1734. * @return
  1735. */
  1736. @Override
  1737. public Object getComputeProfitList(String computeDate, Long hospId, String reportType, String responsibilityCode) {
  1738. Integer year = ComputeDateUtils.getComputeYear(computeDate);
  1739. Integer month = ComputeDateUtils.getComputeMonth(computeDate);
  1740. //取出计算当前月计算过的科室损益
  1741. List<CostDepartmentProfit> departmentProfits = costDepartmentProfitRepository.getCurrentByReportType(year, month, hospId, reportType, responsibilityCode);
  1742. if (CollectionUtils.isEmpty(departmentProfits)) {
  1743. throw new CostException(500, "未进行科室损益计算");
  1744. }
  1745. Integer integerReportType = Integer.valueOf(reportType);
  1746. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  1747. .eq(ReportForm::getHospId, hospId)
  1748. .eq(ReportForm::getReportType, integerReportType));
  1749. if (CollUtil.isEmpty(reportFormList)) {
  1750. throw new CostException(500, "损益表未找到");
  1751. }
  1752. List<CostDepartmentProfit> filterProfit = new ArrayList<>();
  1753. Map<Long, List<CostDepartmentProfit>> collect1 = departmentProfits.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getReportId));
  1754. for (ReportForm reportForm : reportFormList) {
  1755. Integer hide = reportForm.getHide();
  1756. if (hide.equals(NumberConstant.ONE)) {
  1757. // filterProfit.addAll(collect1.get(reportForm.getId()));
  1758. List<CostDepartmentProfit> costDepartmentProfits = collect1.get(reportForm.getId());
  1759. if (!CollectionUtils.isEmpty(costDepartmentProfits)) {
  1760. filterProfit.addAll(costDepartmentProfits);
  1761. }
  1762. }
  1763. }
  1764. Map<Long, Integer> sortMap = reportFormList.stream().collect(Collectors.toMap(ReportForm::getId, ReportForm::getSort, (a, b) -> b));
  1765. Map<Long, String> descriptionMap = reportFormList.stream().collect(Collectors.toMap(ReportForm::getId, reportForm -> !StringUtils.isEmpty(reportForm.getDescription()) ? reportForm.getDescription() : "", (a, b) -> b));
  1766. Map<Long, List<ReportForm>> reportIdMap = reportFormList.stream().collect(Collectors.groupingBy(ReportForm::getId));
  1767. List<CostProfitVo> costProfitVos = BeanUtil.convertList(filterProfit, CostProfitVo.class);
  1768. costProfitVos.forEach(profitVo -> {
  1769. profitVo.setDescription(descriptionMap.get(profitVo.getReportId()));
  1770. profitVo.setSort(sortMap.get(profitVo.getReportId()));
  1771. List<ReportForm> profitReportFormList = reportIdMap.get(profitVo.getReportId());
  1772. if (!CollectionUtils.isEmpty(profitReportFormList)) {
  1773. profitVo.setDataType(profitReportFormList.get(NumberConstant.ZERO).getDataType());
  1774. profitVo.setDecimalPlace(profitReportFormList.get(NumberConstant.ZERO).getDecimalPlace());
  1775. profitVo.setPermil(profitReportFormList.get(NumberConstant.ZERO).getPermil());
  1776. } else {
  1777. profitVo.setDataType(NumberConstant.ONE);
  1778. profitVo.setDecimalPlace(NumberConstant.TWO);
  1779. profitVo.setPermil(NumberConstant.ONE);
  1780. }
  1781. });
  1782. Map<Long, List<CostProfitVo>> collect = costProfitVos.stream().collect(Collectors.groupingBy(CostProfitVo::getReportParentId));
  1783. List<CostProfitVo> costProfitParentVos = collect.get(NumberConstant.ZERO_L);
  1784. collect.remove(NumberConstant.ZERO_L);
  1785. for (CostProfitVo costProfitVo : costProfitParentVos) {
  1786. List<CostProfitVo> costProfitVo1 = collect.get(costProfitVo.getReportId());
  1787. if (!CollectionUtils.isEmpty(costProfitVo1)) {
  1788. costProfitVo.setChildren(setChildren(costProfitVo1, collect));
  1789. }
  1790. }
  1791. if (!CollectionUtils.isEmpty(costProfitParentVos)) {
  1792. costProfitParentVos.sort(Comparator.comparing(CostProfitVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  1793. }
  1794. return costProfitParentVos;
  1795. }
  1796. @Override
  1797. public Object getLastComputeDate() {
  1798. Long hospId = UserContext.getHospId();
  1799. ComputeLastProfitDate lastComputeDate = computeLastProfitDateRepository.getLastComputeDate(hospId);
  1800. if (Objects.nonNull(lastComputeDate)) {
  1801. return lastComputeDate.getComputeDate();
  1802. }
  1803. int year = DateUtils.getYear(new Date());
  1804. int month = DateUtils.getMonth(new Date()) + 1;
  1805. if (month < 10) {
  1806. return year + "-0" + month;
  1807. } else {
  1808. return year + "-" + month;
  1809. }
  1810. }
  1811. @Override
  1812. public Object getBatchComputeProfitList(String beginComputeDate, String endComputeDate, Long hospId, String reportType, String responsibilityCode) {
  1813. Map<String, List<CostProfitVo>> map = new HashMap<>();
  1814. List<BatchCostProfitResponse> responses = new ArrayList<>();
  1815. if (beginComputeDate.equals(endComputeDate)) {
  1816. Integer year = ComputeDateUtils.getComputeYear(beginComputeDate);
  1817. Integer month = ComputeDateUtils.getComputeMonth(beginComputeDate);
  1818. //取出计算当前月计算过的科室损益
  1819. List<CostDepartmentProfit> departmentProfits = costDepartmentProfitRepository.getCurrentByReportType(year, month, hospId, reportType, responsibilityCode);
  1820. if (CollectionUtils.isEmpty(departmentProfits)) {
  1821. throw new CostException(500, "未进行科室损益计算");
  1822. }
  1823. Integer integerReportType = Integer.valueOf(reportType);
  1824. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  1825. .eq(ReportForm::getHospId, hospId)
  1826. .eq(ReportForm::getReportType, integerReportType));
  1827. if (CollUtil.isEmpty(reportFormList)) {
  1828. throw new CostException(500, "损益表未找到");
  1829. }
  1830. Map<Long, List<ReportForm>> reportIdMap = reportFormList.stream().collect(Collectors.groupingBy(ReportForm::getId));
  1831. List<CostDepartmentProfit> filterProfit = new ArrayList<>();
  1832. Map<Long, List<CostDepartmentProfit>> collect1 = departmentProfits.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getReportId));
  1833. for (ReportForm reportForm : reportFormList) {
  1834. Integer hide = reportForm.getHide();
  1835. if (hide.equals(NumberConstant.ONE)) {
  1836. // filterProfit.addAll(collect1.get(reportForm.getId()));
  1837. List<CostDepartmentProfit> costDepartmentProfits = collect1.get(reportForm.getId());
  1838. if (!CollectionUtils.isEmpty(costDepartmentProfits)) {
  1839. filterProfit.addAll(costDepartmentProfits);
  1840. }
  1841. }
  1842. }
  1843. Map<Long, Integer> sortMap = reportFormList.stream().collect(Collectors.toMap(ReportForm::getId, ReportForm::getSort, (a, b) -> b));
  1844. Map<Long, String> descriptionMap = reportFormList.stream().collect(Collectors.toMap(ReportForm::getId, reportForm -> !StringUtils.isEmpty(reportForm.getDescription()) ? reportForm.getDescription() : "", (a, b) -> b));
  1845. List<CostProfitVo> costProfitVos = BeanUtil.convertList(filterProfit, CostProfitVo.class);
  1846. costProfitVos.forEach(profitVo -> {
  1847. profitVo.setDescription(descriptionMap.get(profitVo.getReportId()));
  1848. profitVo.setSort(sortMap.get(profitVo.getReportId()));
  1849. List<ReportForm> profitReportFormList = reportIdMap.get(profitVo.getReportId());
  1850. if (!CollectionUtils.isEmpty(profitReportFormList)) {
  1851. profitVo.setDataType(profitReportFormList.get(NumberConstant.ZERO).getDataType());
  1852. profitVo.setDecimalPlace(profitReportFormList.get(NumberConstant.ZERO).getDecimalPlace());
  1853. profitVo.setPermil(profitReportFormList.get(NumberConstant.ZERO).getPermil());
  1854. } else {
  1855. profitVo.setDataType(NumberConstant.ONE);
  1856. profitVo.setDecimalPlace(NumberConstant.TWO);
  1857. profitVo.setPermil(NumberConstant.ONE);
  1858. }
  1859. });
  1860. Map<Long, List<CostProfitVo>> collect = costProfitVos.stream().collect(Collectors.groupingBy(CostProfitVo::getReportParentId));
  1861. List<CostProfitVo> costProfitParentVos = collect.get(NumberConstant.ZERO_L);
  1862. collect.remove(NumberConstant.ZERO_L);
  1863. for (CostProfitVo costProfitVo : costProfitParentVos) {
  1864. List<CostProfitVo> costProfitVo1 = collect.get(costProfitVo.getReportId());
  1865. if (!CollectionUtils.isEmpty(costProfitVo1)) {
  1866. costProfitVo.setChildren(setChildren(costProfitVo1, collect));
  1867. }
  1868. }
  1869. if (!CollectionUtils.isEmpty(costProfitParentVos)) {
  1870. costProfitParentVos.sort(Comparator.comparing(CostProfitVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  1871. }
  1872. BatchCostProfitResponse response = new BatchCostProfitResponse();
  1873. response.setComputeDate(beginComputeDate);
  1874. response.setProfitVoList(costProfitParentVos);
  1875. response.setAllowDrillDown(true);
  1876. responses.add(response);
  1877. return responses;
  1878. } else {
  1879. Date date = DateUtils.StringToDate(beginComputeDate, DateStyleEnum.YYYY_MM);
  1880. Date endDate = DateUtils.StringToDate(endComputeDate, DateStyleEnum.YYYY_MM);
  1881. if (DateUtils.compareDate(date, endDate)) {
  1882. throw new CostException("开始日期不能大于结束日期");
  1883. }
  1884. List<String> intervalMonthList = DateUtils.getIntervalMonthList(date, endDate);
  1885. List<CostProfitVo> allCostProfitVo=new ArrayList<>();
  1886. if (!CollectionUtils.isEmpty(intervalMonthList)) {
  1887. for (String computeDates : intervalMonthList) {
  1888. Integer year = ComputeDateUtils.getComputeYear(computeDates);
  1889. Integer month = ComputeDateUtils.getComputeMonth(computeDates);
  1890. //取出计算当前月计算过的科室损益
  1891. List<CostDepartmentProfit> departmentProfits = costDepartmentProfitRepository.getCurrentByReportType(year, month, hospId, reportType, responsibilityCode);
  1892. if (CollectionUtils.isEmpty(departmentProfits)) {
  1893. throw new CostException(500, "未进行科室损益计算");
  1894. }
  1895. Integer integerReportType = Integer.valueOf(reportType);
  1896. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  1897. .eq(ReportForm::getHospId, hospId)
  1898. .eq(ReportForm::getReportType, integerReportType));
  1899. if (CollUtil.isEmpty(reportFormList)) {
  1900. throw new CostException(500, "损益表未找到");
  1901. }
  1902. Map<Long, List<ReportForm>> reportIdMap = reportFormList.stream().collect(Collectors.groupingBy(ReportForm::getId));
  1903. List<CostDepartmentProfit> filterProfit = new ArrayList<>();
  1904. Map<Long, List<CostDepartmentProfit>> collect1 = departmentProfits.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getReportId));
  1905. for (ReportForm reportForm : reportFormList) {
  1906. Integer hide = reportForm.getHide();
  1907. if (hide.equals(NumberConstant.ONE)) {
  1908. // filterProfit.addAll(collect1.get(reportForm.getId()));
  1909. List<CostDepartmentProfit> costDepartmentProfits = collect1.get(reportForm.getId());
  1910. if (!CollectionUtils.isEmpty(costDepartmentProfits)) {
  1911. filterProfit.addAll(costDepartmentProfits);
  1912. }
  1913. }
  1914. }
  1915. Map<Long, Integer> sortMap = reportFormList.stream().collect(Collectors.toMap(ReportForm::getId, ReportForm::getSort, (a, b) -> b));
  1916. Map<Long, String> descriptionMap = reportFormList.stream().collect(Collectors.toMap(ReportForm::getId, reportForm -> !StringUtils.isEmpty(reportForm.getDescription()) ? reportForm.getDescription() : "", (a, b) -> b));
  1917. List<CostProfitVo> costProfitVos = BeanUtil.convertList(filterProfit, CostProfitVo.class);
  1918. costProfitVos.forEach(profitVo -> {
  1919. profitVo.setDescription(descriptionMap.get(profitVo.getReportId()));
  1920. profitVo.setSort(sortMap.get(profitVo.getReportId()));
  1921. List<ReportForm> profitReportFormList = reportIdMap.get(profitVo.getReportId());
  1922. if (!CollectionUtils.isEmpty(profitReportFormList)) {
  1923. profitVo.setDataType(profitReportFormList.get(NumberConstant.ZERO).getDataType());
  1924. profitVo.setDecimalPlace(profitReportFormList.get(NumberConstant.ZERO).getDecimalPlace());
  1925. profitVo.setPermil(profitReportFormList.get(NumberConstant.ZERO).getPermil());
  1926. } else {
  1927. profitVo.setDataType(NumberConstant.ONE);
  1928. profitVo.setDecimalPlace(NumberConstant.TWO);
  1929. profitVo.setPermil(NumberConstant.ONE);
  1930. }
  1931. });
  1932. //记录所有月份的科室损益数据
  1933. allCostProfitVo.addAll(costProfitVos);
  1934. Map<Long, List<CostProfitVo>> collect = costProfitVos.stream().collect(Collectors.groupingBy(CostProfitVo::getReportParentId));
  1935. List<CostProfitVo> costProfitParentVos = collect.get(NumberConstant.ZERO_L);
  1936. collect.remove(NumberConstant.ZERO_L);
  1937. for (CostProfitVo costProfitVo : costProfitParentVos) {
  1938. List<CostProfitVo> costProfitVo1 = collect.get(costProfitVo.getReportId());
  1939. if (!CollectionUtils.isEmpty(costProfitVo1)) {
  1940. costProfitVo.setChildren(setChildren(costProfitVo1, collect));
  1941. }
  1942. }
  1943. if (!CollectionUtils.isEmpty(costProfitParentVos)) {
  1944. costProfitParentVos.sort(Comparator.comparing(CostProfitVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  1945. }
  1946. BatchCostProfitResponse response = new BatchCostProfitResponse();
  1947. response.setComputeDate(computeDates);
  1948. response.setProfitVoList(costProfitParentVos);
  1949. response.setAllowDrillDown(true);
  1950. responses.add(response);
  1951. }
  1952. //获取月度汇总数据
  1953. List<CostProfitVo> costProfitVos = generateSummaryForMultipleMonths(allCostProfitVo);
  1954. if(!CollectionUtils.isEmpty(costProfitVos)){
  1955. BatchCostProfitResponse response = new BatchCostProfitResponse();
  1956. response.setComputeDate(String.format("%s至%s", beginComputeDate, endComputeDate));
  1957. response.setProfitVoList(costProfitVos);
  1958. response.setAllowDrillDown(false);
  1959. response.setMonthlyAccumulation(true);
  1960. responses.add(NumberConstant.ZERO,response);
  1961. }
  1962. return responses;
  1963. } else {
  1964. return responses;
  1965. }
  1966. }
  1967. }
  1968. /**
  1969. * 获取树状结构的科室损益数据
  1970. * @param costProfitVos
  1971. * @return
  1972. */
  1973. public List<CostProfitVo> getCostProfitVoTree(List<CostProfitVo> costProfitVos){
  1974. // 参数校验
  1975. if (CollectionUtils.isEmpty(costProfitVos)) {
  1976. return Collections.emptyList();
  1977. }
  1978. Map<Long, List<CostProfitVo>> collect = costProfitVos.stream().collect(Collectors.groupingBy(CostProfitVo::getReportParentId));
  1979. List<CostProfitVo> costProfitParentVos = collect.get(NumberConstant.ZERO_L);
  1980. collect.remove(NumberConstant.ZERO_L);
  1981. for (CostProfitVo costProfitVo : costProfitParentVos) {
  1982. List<CostProfitVo> costProfitVo1 = collect.get(costProfitVo.getReportId());
  1983. if (!CollectionUtils.isEmpty(costProfitVo1)) {
  1984. costProfitVo.setChildren(setChildren(costProfitVo1, collect));
  1985. }
  1986. }
  1987. if (!CollectionUtils.isEmpty(costProfitParentVos)) {
  1988. costProfitParentVos.sort(Comparator.comparing(CostProfitVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  1989. }
  1990. return costProfitParentVos;
  1991. }
  1992. /**
  1993. * 生成多月份汇总的科室损益数据
  1994. *
  1995. * @param allCostProfitVo 原始数据列表
  1996. * @return 汇总后的科室损益数据
  1997. */
  1998. private List<CostProfitVo> generateSummaryForMultipleMonths(List<CostProfitVo> allCostProfitVo){
  1999. // 参数校验
  2000. if (CollectionUtils.isEmpty(allCostProfitVo)) {
  2001. return Collections.emptyList();
  2002. }
  2003. List<CostProfitVo> profitVoList=new ArrayList<>() ;
  2004. Map<Long, List<CostProfitVo>> reportGroup = allCostProfitVo.stream().collect(Collectors.groupingBy(CostProfitVo::getReportId));
  2005. reportGroup.forEach((reportId, profitVos) -> {
  2006. CostProfitVo costProfitVo = BeanUtil.convertObj(profitVos.get(NumberConstant.ZERO), CostProfitVo.class);
  2007. BigDecimal totalAmount = profitVos.stream().map(vo -> Optional.ofNullable(vo.getAmount()).orElse(BigDecimal.ZERO))
  2008. .reduce(BigDecimal.ZERO, BigDecimal::add);
  2009. BigDecimal totalBudgetAmount = profitVos.stream().map(vo -> Optional.ofNullable(vo.getBudgetAmount()).orElse(BigDecimal.ZERO))
  2010. .reduce(BigDecimal.ZERO, BigDecimal::add);
  2011. BigDecimal totalPrevPeriodAmount = profitVos.stream().map(vo -> Optional.ofNullable(vo.getPrevPeriodAmount()).orElse(BigDecimal.ZERO))
  2012. .reduce(BigDecimal.ZERO, BigDecimal::add);
  2013. BigDecimal totalSamePeriodAmount = profitVos.stream().map(vo -> Optional.ofNullable(vo.getSamePeriodAmount()).orElse(BigDecimal.ZERO))
  2014. .reduce(BigDecimal.ZERO, BigDecimal::add);
  2015. costProfitVo.setAmount(totalAmount);
  2016. costProfitVo.setBudgetAmount(totalBudgetAmount);
  2017. costProfitVo.setPrevPeriodAmount(totalPrevPeriodAmount);
  2018. costProfitVo.setSamePeriodAmount(totalSamePeriodAmount);
  2019. costProfitVo.setCompletionRate(calculateRate(costProfitVo.getAmount(), costProfitVo.getBudgetAmount()));
  2020. costProfitVo.setMomRate(calculateRate(costProfitVo.getAmount().subtract(costProfitVo.getPrevPeriodAmount()), costProfitVo.getPrevPeriodAmount()));
  2021. costProfitVo.setYoyRate(calculateRate(costProfitVo.getAmount().subtract(costProfitVo.getSamePeriodAmount()), costProfitVo.getSamePeriodAmount()));
  2022. profitVoList.add(costProfitVo);
  2023. });
  2024. //转成树状结构
  2025. List<CostProfitVo> costProfitVoTree = getCostProfitVoTree(profitVoList);
  2026. return costProfitVoTree;
  2027. }
  2028. @Override
  2029. public Object getRedirectData(String computeDate, Long reportId, Long hospId, String reportType, String responsibilityCode) {
  2030. //查询报表项目
  2031. Integer year = ComputeDateUtils.getComputeYear(computeDate);
  2032. Integer month = ComputeDateUtils.getComputeMonth(computeDate);
  2033. LambdaQueryWrapper<ReportForm> reportFormQueryWrapper = new LambdaQueryWrapper<>();
  2034. reportFormQueryWrapper.eq(ReportForm::getHospId, hospId);
  2035. reportFormQueryWrapper.eq(ReportForm::getReportType, reportType);
  2036. reportFormQueryWrapper.eq(ReportForm::getId, reportId);
  2037. //这里所有配置都需要做特殊处理,为防程序报错及项目调整,已计算过的数据可能需要查询已作废的报表项目设置
  2038. ReportForm one = reportFormService.getByReportId(hospId, reportType, reportId);
  2039. if (Objects.isNull(one)) {
  2040. throw new CostException("未找到报表id为【" + reportId + "】报表配置,请确认数据是否有调整");
  2041. }
  2042. Integer calcType = one.getCalcType();
  2043. //查询关联关系
  2044. Map<Long, List<ReportRelation>> reportRelationMap = reportRelationService.list(new QueryWrapper<ReportRelation>().lambda().eq(ReportRelation::getHospId, hospId)).stream().collect(Collectors.groupingBy(ReportRelation::getReportId));
  2045. List<ReportRelation> reportRelations = reportRelationMap.get(reportId);
  2046. if (CollectionUtils.isEmpty(reportRelations)) {
  2047. throw new CostException("未找到报表id为【" + reportId + "】报表关系对应配置,请确认数据是否有调整");
  2048. }
  2049. //会计科目代码
  2050. List<String> relations = reportRelations.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  2051. DecimalFormat df = new DecimalFormat("0.00%");
  2052. if (calcType.equals(NumberConstant.ONE)) {
  2053. // 归集后
  2054. List<IncomeCollection> incomeList = incomeCollectionService.list(new QueryWrapper<IncomeCollection>().lambda()
  2055. .eq(IncomeCollection::getHospId, hospId)
  2056. .eq(year > 0, IncomeCollection::getYear, year).eq(month > 0, IncomeCollection::getMonth, month)
  2057. .eq(IncomeCollection::getResponsibilityCode, responsibilityCode));
  2058. // 分摊后的数据说明是分摊后的数据 这里原有为收益中心数据 同步添加医技分摊后数据
  2059. List<AllocationQuery> allocationQueryList = allocationQueryService.list(new QueryWrapper<AllocationQuery>().lambda().eq(AllocationQuery::getHospId, hospId)
  2060. .eq(year > 0, AllocationQuery::getDateYear, year)
  2061. .eq(month > 0, AllocationQuery::getDateMonth, month)
  2062. .eq(AllocationQuery::getTargetResponsibilityCode, responsibilityCode));
  2063. // 封装数据
  2064. List<AllocationQueryReportVO> allocationQueryReportVOList = BeanUtil.convertList(allocationQueryList, AllocationQueryReportVO.class);
  2065. allocationQueryReportVOList.forEach(i -> {
  2066. i.setAccountingCodes(Arrays.asList(i.getAccountingCode().split(StrUtil.COMMA)));
  2067. i.setAccountingNames(Arrays.asList(i.getAccountingName().split(StrUtil.COMMA)));
  2068. });
  2069. //取会计科目字典
  2070. List<Accounting> accountList1 = accountingService.getAccountList(hospId, relations);
  2071. if (CollectionUtils.isEmpty(accountList1)) {
  2072. throw new CostException("未找到会计科目字典");
  2073. }
  2074. // 查找在归集数据里面当前责任中心对应的这些会计科目的金额
  2075. Map<String, List<IncomeCollection>> incomeCollect = new HashMap<>();
  2076. if (!CollectionUtils.isEmpty(incomeList)) {
  2077. List<IncomeCollection> incomeCollectionList = incomeList.stream().filter(income -> relations.contains(income.getAccountingCode())).collect(Collectors.toList());
  2078. incomeCollect = incomeCollectionList.stream().collect(Collectors.groupingBy(IncomeCollection::getAccountingCode));
  2079. }
  2080. // 需要查询分摊后的表
  2081. Map<String, List<AllocationQueryReportVO>> costCollect = new HashMap<>();
  2082. if (!CollectionUtils.isEmpty(allocationQueryReportVOList)) {
  2083. List<AllocationQueryReportVO> reportVOList = allocationQueryReportVOList.stream().filter(m -> relations.contains(m.getAccountingCode())).collect(Collectors.toList());
  2084. costCollect = reportVOList.stream().collect(Collectors.groupingBy(AllocationQueryReportVO::getAccountingCode));
  2085. }
  2086. List<AccountAmountVo> list = new ArrayList<>();
  2087. for (Accounting accounting : accountList1) {
  2088. AccountAmountVo vo = new AccountAmountVo();
  2089. vo.setAccountCode(accounting.getAccountingCode());
  2090. vo.setAccountName(accounting.getAccountingName());
  2091. List<IncomeCollection> incomeCollections = incomeCollect.get(accounting.getAccountingCode());
  2092. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.000000"));
  2093. if (!CollectionUtils.isEmpty(incomeCollections)) {
  2094. incomeCollections.forEach(m -> {
  2095. sum.updateAndGet(v -> v.add(m.getAmount()));
  2096. });
  2097. }
  2098. List<AllocationQueryReportVO> costCollections = costCollect.get(accounting.getAccountingCode());
  2099. if (!CollectionUtils.isEmpty(costCollections)) {
  2100. costCollections.forEach(m -> {
  2101. sum.updateAndGet(v -> v.add(m.getAmount()));
  2102. });
  2103. }
  2104. vo.setAmount(sum.get().setScale(2, RoundingMode.HALF_UP));
  2105. list.add(vo);
  2106. }
  2107. return list;
  2108. } else if (calcType.equals(NumberConstant.TWO)) {
  2109. //分摊层级
  2110. // List<Responsibility> list = responsibilityService.list(new QueryWrapper<Responsibility>()
  2111. // .lambda()
  2112. // .select(Responsibility::getResponsibilityCode, Responsibility::getResponsibilityName)
  2113. // .eq(Responsibility::getHospId, hospId).in(Responsibility::getShareId, relations));
  2114. // if(CollectionUtils.isEmpty(list)){
  2115. // throw new CostException(reportId+"下报表下,关联的分摊层级【"+relations+"】未找到责任中心数据");
  2116. // }
  2117. List<CostAccountShare> accountShares = costAccountShareService.getAll();
  2118. if (CollectionUtils.isEmpty(accountShares)) {
  2119. throw new CostException("成本分摊参数对应不存在");
  2120. }
  2121. Map<Long, CostAccountShare> accountShareMap = accountShares.stream().collect(Collectors.toMap(CostAccountShare::getId, accountShare -> accountShare, (a, b) -> b));
  2122. // List<String> collect = list.stream().map(Responsibility::getResponsibilityCode).collect(Collectors.toList());
  2123. List<Allocation> allocationList = allocationService.list(new QueryWrapper<Allocation>()
  2124. .lambda()
  2125. .eq(Allocation::getHospId, hospId)
  2126. .eq(year > 0, Allocation::getDateYear, year)
  2127. .eq(month > 0, Allocation::getDateMonth, month)
  2128. .eq(Allocation::getTargetResponsibilityCode, responsibilityCode)
  2129. .in(Allocation::getShareLevelId, relations));
  2130. List<AllocationReportVO> allocationReportVOS = BeanUtil.convertList(allocationList, AllocationReportVO.class);
  2131. for (AllocationReportVO allocationReportVO : allocationReportVOS) {
  2132. CostAccountShare costAccountShare = accountShareMap.get(allocationReportVO.getAccountShareId());
  2133. if (Objects.nonNull(costAccountShare)) {
  2134. String paramList = costAccountShare.getParamList();
  2135. if (!StringUtils.isEmpty(paramList)) {
  2136. List<AccountShareVO> accountShareVOs = JacksonUtil.str2ObjList(paramList, List.class, AccountShareVO.class);
  2137. if (!CollectionUtils.isEmpty(accountShareVOs)) {
  2138. Map<String, String> shareParamPercentMap = accountShareVOs.stream().collect(Collectors.toMap(AccountShareVO::getShareParamCode, AccountShareVO::getShareParamPopout, (a, b) -> b));
  2139. String s = shareParamPercentMap.get(allocationReportVO.getShareParamCode());
  2140. if (!StringUtils.isEmpty(s)) {
  2141. allocationReportVO.setShareParamPercent(new BigDecimal(s).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP));
  2142. }
  2143. }
  2144. }
  2145. String alias = costAccountShare.getAlias();
  2146. if (StringUtils.isEmpty(alias)) {
  2147. allocationReportVO.setAlias(costAccountShare.getAccountingNames());
  2148. } else {
  2149. allocationReportVO.setAlias(alias);
  2150. }
  2151. }
  2152. }
  2153. Map<String, List<AllocationReportVO>> collect1 = allocationReportVOS.stream().collect(Collectors.groupingBy(AllocationReportVO::getResponsibilityCode));
  2154. List<CostProfitRedirectResponse> responses = new ArrayList<>();
  2155. collect1.forEach((k, v) -> {
  2156. CostProfitRedirectResponse response = new CostProfitRedirectResponse();
  2157. Map<String, List<AllocationReportVO>> aliasCollect = v.stream().collect(Collectors.groupingBy(AllocationReportVO::getAlias));
  2158. List<CostProfitRedirectAliasVo> aliasVos = new ArrayList<>();
  2159. aliasCollect.forEach((k1, v1) -> {
  2160. CostProfitRedirectAliasVo redirectAliasVo = new CostProfitRedirectAliasVo();
  2161. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  2162. List<CostProfitRedirectDataVo> dataVoList = new ArrayList<>();
  2163. v1.forEach(f -> {
  2164. CostProfitRedirectDataVo dataVo = new CostProfitRedirectDataVo();
  2165. BigDecimal shareParamPercent = f.getShareParamPercent();
  2166. if (shareParamPercent != null) {
  2167. dataVo.setShareParamAmount(f.getTotalAmount().multiply(shareParamPercent).setScale(2, RoundingMode.HALF_UP));
  2168. dataVo.setShareParamName(f.getShareParamName() + "(" + df.format(shareParamPercent) + ")");
  2169. }
  2170. dataVo.setShareParamCode(f.getShareParamCode());
  2171. dataVo.setShareParamNum(f.getShareParamValueNum());
  2172. dataVo.setShareParamRate(df.format(f.getShareParamRate()));
  2173. dataVo.setShareParamValue(f.getAmount());
  2174. sum.updateAndGet(i -> i.add(f.getTotalAmount()));
  2175. redirectAliasVo.setAlias(k1 + "(" + f.getTotalAmount().setScale(2, RoundingMode.HALF_UP) + ")");
  2176. dataVoList.add(dataVo);
  2177. });
  2178. // if(!CollectionUtils.isEmpty(dataVoList)){
  2179. // for (CostProfitRedirectDataVo dataVo : dataVoList) {
  2180. // if(sum.get().compareTo(BigDecimal.ZERO.setScale(4,RoundingMode.HALF_UP))==0){
  2181. // dataVo.setShareParamName(dataVo.getShareParamName()+"0.0000");
  2182. // }else {
  2183. // BigDecimal divide = dataVo.getShareParamAmount().divide(sum.get(), 2, RoundingMode.HALF_UP);
  2184. // dataVo.setShareParamName(dataVo.getShareParamName());
  2185. // }
  2186. // }
  2187. // }
  2188. // redirectAliasVo.setAlias(k1+"("+sum.get()+")");
  2189. redirectAliasVo.setShareParamData(dataVoList);
  2190. aliasVos.add(redirectAliasVo);
  2191. });
  2192. response.setResponseCode(k);
  2193. response.setResponseName(v.get(0).getResponsibilityName());
  2194. response.setDataList(aliasVos);
  2195. responses.add(response);
  2196. });
  2197. return responses;
  2198. } else if (calcType.equals(NumberConstant.FIVE)) {
  2199. //责任中心
  2200. List<CostAccountShare> accountShares = costAccountShareService.getAll();
  2201. if (CollectionUtils.isEmpty(accountShares)) {
  2202. throw new CostException("成本分摊参数对应不存在");
  2203. }
  2204. Map<Long, CostAccountShare> accountShareMap = accountShares.stream().collect(Collectors.toMap(CostAccountShare::getId, accountShare -> accountShare, (a, b) -> b));
  2205. List<Allocation> allocationList = allocationService.list(new QueryWrapper<Allocation>()
  2206. .lambda()
  2207. .eq(Allocation::getHospId, hospId)
  2208. .eq(year > 0, Allocation::getDateYear, year)
  2209. .eq(month > 0, Allocation::getDateMonth, month)
  2210. .eq(Allocation::getTargetResponsibilityCode, responsibilityCode)
  2211. .in(Allocation::getResponsibilityCode, relations));
  2212. List<AllocationReportVO> allocationReportVOS = BeanUtil.convertList(allocationList, AllocationReportVO.class);
  2213. for (AllocationReportVO allocationReportVO : allocationReportVOS) {
  2214. CostAccountShare costAccountShare = accountShareMap.get(allocationReportVO.getAccountShareId());
  2215. if (Objects.nonNull(costAccountShare)) {
  2216. String paramList = costAccountShare.getParamList();
  2217. if (!StringUtils.isEmpty(paramList)) {
  2218. List<AccountShareVO> accountShareVOs = JacksonUtil.str2ObjList(paramList, List.class, AccountShareVO.class);
  2219. if (!CollectionUtils.isEmpty(accountShareVOs)) {
  2220. Map<String, String> shareParamPercentMap = accountShareVOs.stream().collect(Collectors.toMap(AccountShareVO::getShareParamCode, AccountShareVO::getShareParamPopout, (a, b) -> b));
  2221. String s = shareParamPercentMap.get(allocationReportVO.getShareParamCode());
  2222. if (!StringUtils.isEmpty(s)) {
  2223. allocationReportVO.setShareParamPercent(new BigDecimal(s).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP));
  2224. }
  2225. }
  2226. }
  2227. String alias = costAccountShare.getAlias();
  2228. if (StringUtils.isEmpty(alias)) {
  2229. allocationReportVO.setAlias(costAccountShare.getAccountingNames());
  2230. } else {
  2231. allocationReportVO.setAlias(alias);
  2232. }
  2233. }
  2234. }
  2235. Map<String, List<AllocationReportVO>> collect1 = allocationReportVOS.stream().collect(Collectors.groupingBy(AllocationReportVO::getResponsibilityCode));
  2236. List<CostProfitRedirectResponse> responses = new ArrayList<>();
  2237. collect1.forEach((k, v) -> {
  2238. CostProfitRedirectResponse response = new CostProfitRedirectResponse();
  2239. Map<String, List<AllocationReportVO>> aliasCollect = v.stream().collect(Collectors.groupingBy(AllocationReportVO::getAlias));
  2240. List<CostProfitRedirectAliasVo> aliasVos = new ArrayList<>();
  2241. aliasCollect.forEach((k1, v1) -> {
  2242. CostProfitRedirectAliasVo redirectAliasVo = new CostProfitRedirectAliasVo();
  2243. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  2244. List<CostProfitRedirectDataVo> dataVoList = new ArrayList<>();
  2245. v1.forEach(f -> {
  2246. CostProfitRedirectDataVo dataVo = new CostProfitRedirectDataVo();
  2247. BigDecimal shareParamPercent = f.getShareParamPercent();
  2248. if (shareParamPercent != null) {
  2249. dataVo.setShareParamAmount(f.getTotalAmount().multiply(shareParamPercent));
  2250. dataVo.setShareParamName(f.getShareParamName() + "(" + df.format(shareParamPercent) + ")");
  2251. }
  2252. dataVo.setShareParamCode(f.getShareParamCode());
  2253. // dataVo.setShareParamName(f.getShareParamName());
  2254. dataVo.setShareParamNum(f.getShareParamValueNum());
  2255. dataVo.setShareParamRate(df.format(f.getShareParamRate()));
  2256. dataVo.setShareParamValue(f.getAmount());
  2257. sum.updateAndGet(i -> i.add(f.getTotalAmount()));
  2258. redirectAliasVo.setAlias(k1 + "(" + f.getTotalAmount().setScale(2, RoundingMode.HALF_UP) + ")");
  2259. dataVoList.add(dataVo);
  2260. });
  2261. // if (!CollectionUtils.isEmpty(dataVoList)) {
  2262. // for (CostProfitRedirectDataVo dataVo : dataVoList) {
  2263. // if (sum.get().compareTo(BigDecimal.ZERO.setScale(4, RoundingMode.HALF_UP)) == 0) {
  2264. // dataVo.setShareParamName(dataVo.getShareParamName() + "0.0000");
  2265. // } else {
  2266. // BigDecimal divide = dataVo.getShareParamAmount().divide(sum.get(), 2, RoundingMode.HALF_UP);
  2267. //// dataVo.setShareParamName(dataVo.getShareParamName()+"("+divide+")");
  2268. // dataVo.setShareParamName(dataVo.getShareParamName());
  2269. //
  2270. // }
  2271. // }
  2272. // }
  2273. redirectAliasVo.setShareParamData(dataVoList);
  2274. aliasVos.add(redirectAliasVo);
  2275. });
  2276. response.setResponseCode(k);
  2277. response.setResponseName(v.get(0).getResponsibilityName());
  2278. response.setDataList(aliasVos);
  2279. responses.add(response);
  2280. });
  2281. return responses;
  2282. }
  2283. return new ArrayList<>();
  2284. }
  2285. @Override
  2286. public Object getComputeProfitCollect(String computeDate, Long hospId, String reportType) {
  2287. Integer year = ComputeDateUtils.getComputeYear(computeDate);
  2288. Integer month = ComputeDateUtils.getComputeMonth(computeDate);
  2289. Integer integerReportType = Integer.valueOf(reportType);
  2290. DictDataVo dataVo = getDictDataVo(reportType);
  2291. Long id = Long.valueOf(dataVo.getExpandOne());
  2292. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  2293. .eq(Responsibility::getHospId, hospId)
  2294. .eq(Responsibility::getShareId, id)
  2295. .and(q -> q.eq(Responsibility::getStatus, NumberConstant.ONE).or().eq(Responsibility::getStatus, null)));
  2296. if (CollectionUtils.isEmpty(responsibilityList)) {
  2297. throw new CostException("未找到对应分摊层级责任中心");
  2298. }
  2299. //获取授权的责任中心
  2300. responsibilityList = userResponsibilityRightService.getAuthorizedResponsibility(responsibilityList);
  2301. List<Responsibility> responsibilityAllList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  2302. .eq(Responsibility::getHospId, hospId));
  2303. Map<Long, Responsibility> map = responsibilityAllList.stream().collect(Collectors.toMap(Responsibility::getId, responsibility -> responsibility, (a, b) -> b));
  2304. List<Responsibility> addList = new ArrayList<>();
  2305. for (Responsibility responsibility : responsibilityList) {
  2306. Responsibility responsibility1 = map.get(responsibility.getParentId());
  2307. if (Objects.nonNull(responsibility1)) {
  2308. addList.add(responsibility1);
  2309. if (!responsibility1.getParentId().equals(NumberConstant.ZERO_L)) {
  2310. getResponsibilityParent(responsibility1, map, addList);
  2311. }
  2312. }
  2313. }
  2314. if (!CollectionUtils.isEmpty(addList)) {
  2315. //去重
  2316. List<Responsibility> collect = addList.stream().distinct().collect(Collectors.toList());
  2317. responsibilityList.addAll(collect);
  2318. }
  2319. //找出最外层用于出参循环
  2320. Map<Long, List<Responsibility>> responsibilityGroup = responsibilityList.stream().collect(Collectors.groupingBy(Responsibility::getParentId));
  2321. List<Responsibility> responseParent = responsibilityList.stream().filter(f -> f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  2322. //只留下中间责任中心
  2323. responsibilityList.removeAll(responseParent);
  2324. List<CommonResponsibilityReportVo> list = new ArrayList<>();
  2325. for (Responsibility responsibility : responseParent) {
  2326. CommonResponsibilityReportVo commonResponsibilityReportVo = new CommonResponsibilityReportVo();
  2327. commonResponsibilityReportVo.setResponsibilityCode(responsibility.getResponsibilityCode());
  2328. commonResponsibilityReportVo.setResponsibilityName(responsibility.getResponsibilityName());
  2329. commonResponsibilityReportVo.setSort(responsibility.getSort());
  2330. List<Responsibility> responsibilities = responsibilityGroup.get(responsibility.getId());
  2331. if (!CollectionUtils.isEmpty(responsibilities)) {
  2332. commonResponsibilityReportVo.setChild(getChildTitle(responsibilities, responsibilityGroup));
  2333. } else {
  2334. //要显示占比时自动生成默认金额占比层
  2335. if (IsShowPercent()) {
  2336. commonResponsibilityReportVo.setChild(setDefalutChildList(responsibility));
  2337. } else {
  2338. //不显示占比时,直接责任中心作为金额层
  2339. commonResponsibilityReportVo.setResponsibilityCode(commonResponsibilityReportVo.getResponsibilityCode() + AMOUNT_FIELD);
  2340. }
  2341. }
  2342. list.add(commonResponsibilityReportVo);
  2343. }
  2344. ComputeProfitCollectResponse response = new ComputeProfitCollectResponse();
  2345. commonResponsibilityReportVoSort(list);
  2346. response.setTitle(list);
  2347. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  2348. .eq(ReportForm::getHospId, hospId)
  2349. .eq(ReportForm::getReportType, integerReportType).eq(ReportForm::getHide, NumberConstant.ONE));
  2350. if (CollUtil.isEmpty(reportFormList)) {
  2351. throw new CostException(500, "【" + integerReportType + "】损益表配置未找到");
  2352. }
  2353. List<ReportFormCustomVo> reportFormCustomVos = BeanUtil.convertList(reportFormList, ReportFormCustomVo.class);
  2354. List<CostDepartmentProfit> departmentProfits = this.list(new QueryWrapper<CostDepartmentProfit>().lambda()
  2355. .eq(CostDepartmentProfit::getHospId, hospId)
  2356. .eq(CostDepartmentProfit::getYear, year)
  2357. .eq(CostDepartmentProfit::getMonth, month)
  2358. .eq(CostDepartmentProfit::getShareType, integerReportType));
  2359. List<CostProfitVo> costProfitVos = BeanUtil.convertList(departmentProfits, CostProfitVo.class);
  2360. //按责任中心-reportId进行分组
  2361. Map<Long, List<CostProfitVo>> collect2 = costProfitVos.stream().collect(Collectors.groupingBy(CostProfitVo::getReportId));
  2362. DecimalFormat df = new DecimalFormat("0.00%");
  2363. Map<Long, List<ReportVo>> reportDataMap = new HashMap<>();
  2364. collect2.forEach((k, v) -> {
  2365. List<ReportVo> reportVos = new ArrayList<>();
  2366. v.forEach(f -> {
  2367. ReportVo amount = new ReportVo();
  2368. amount.setCode(f.getResponsibilityCode() + AMOUNT_FIELD);
  2369. amount.setValue(f.getAmount());
  2370. reportVos.add(amount);
  2371. ReportVo percent = new ReportVo();
  2372. percent.setCode(f.getResponsibilityCode() + PERCENT_FIELD);
  2373. if (f.getPercent() != null) {
  2374. percent.setValue(df.format(f.getPercent()));
  2375. } else {
  2376. percent.setValue("");
  2377. }
  2378. reportVos.add(percent);
  2379. });
  2380. reportDataMap.put(k, reportVos);
  2381. });
  2382. for (ReportFormCustomVo reportFormCustomVo : reportFormCustomVos) {
  2383. List<ReportVo> reportVos = reportDataMap.get(reportFormCustomVo.getId());
  2384. if (!CollectionUtils.isEmpty(responsibilityGroup)) {
  2385. reportFormCustomVo.setData(reportVos);
  2386. }
  2387. }
  2388. Map<Long, List<ReportFormCustomVo>> collect = reportFormCustomVos.stream().collect(Collectors.groupingBy(ReportFormCustomVo::getParentId));
  2389. List<ReportFormCustomVo> costProfitVos1 = collect.get(0L);
  2390. collect.remove(NumberConstant.ZERO_L);
  2391. //最外层排序
  2392. if (!CollectionUtils.isEmpty(costProfitVos1)) {
  2393. costProfitVos1.sort(Comparator.comparing(ReportFormCustomVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  2394. }
  2395. for (ReportFormCustomVo costProfitVo : costProfitVos1) {
  2396. List<ReportFormCustomVo> costProfitVo1 = collect.get(costProfitVo.getId());
  2397. if (!CollectionUtils.isEmpty(costProfitVo1)) {
  2398. //子项排序
  2399. costProfitVo1.sort(Comparator.comparing(ReportFormCustomVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  2400. costProfitVo.setChildren(setCustomChildren(costProfitVo1, collect));
  2401. }
  2402. }
  2403. response.setData(costProfitVos1);
  2404. return response;
  2405. //
  2406. //
  2407. //
  2408. // //取一行数据 作为父类模版
  2409. // Set<Map.Entry<String, List<CostProfitVo>>> entrySet = responseGroup.entrySet();
  2410. // Map.Entry<String, List<CostProfitVo>> firstEntry = entrySet.iterator().next();
  2411. // List<CostProfitVo> parentModel = firstEntry.getValue();
  2412. // //备注:所有记算过的数据都是最后一层责任中心,所以按最后一层责任 中心 找出所有上级责任中心后 给模版添加到上层责任中心
  2413. // //按报表类型 取字典
  2414. // DictDataVo dataVo = getDictDataVo(reportType);
  2415. // Long id = Long.valueOf(dataVo.getExpandOne());
  2416. //
  2417. // //按字典给护 的 分摊层级id 查找对应报表 的责任中心 找出的是没有父类的
  2418. // List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  2419. // .eq(Responsibility::getHospId, hospId)
  2420. // .eq(Responsibility::getShareId, id));
  2421. // if (CollectionUtils.isEmpty(responsibilityList)) {
  2422. // throw new CostException("未找到对应分摊层级责任中心");
  2423. // }
  2424. // //获取当前院区所有的责任中心
  2425. // List<Responsibility> responsibilityAllList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  2426. // .eq(Responsibility::getHospId, hospId));
  2427. // Map<Long, Responsibility> map = responsibilityAllList.stream().collect(Collectors.toMap(Responsibility::getId, responsibility -> responsibility, (a, b) -> b));
  2428. // Map<String, Long> responseCodeParentIdMap = responsibilityAllList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getParentId, (a, b) -> b));
  2429. // Map<String, String> responseNameMap = responsibilityAllList.stream().collect(Collectors.toMap(Responsibility::getResponsibilityCode, Responsibility::getResponsibilityName, (a, b) -> b));
  2430. //
  2431. //
  2432. // //通过查询的最下层责任中心 找到其所有父类责任中心
  2433. // List<Responsibility> parentResponseList = new ArrayList<>();
  2434. // for (Responsibility responsibility : responsibilityList) {
  2435. // if (responsibility.getParentId().equals(NumberConstant.ZERO_L)) {
  2436. // parentResponseList.add(responsibility);
  2437. // } else {
  2438. // Responsibility responsibility1 = map.get(responsibility.getParentId());
  2439. // if (Objects.nonNull(responsibility1)) {
  2440. // parentResponseList.add(responsibility1);
  2441. // if (!responsibility1.getParentId().equals(NumberConstant.ZERO_L)) {
  2442. // getResponsibilityParent(responsibility1, map, parentResponseList);
  2443. // }
  2444. // }
  2445. // }
  2446. //
  2447. // }
  2448. // //去除重复组装数据
  2449. // List<Responsibility> parentResponsibility = parentResponseList.stream().distinct().collect(Collectors.toList());
  2450. // //找出最外层用于出参循环
  2451. // List<Responsibility> responseParent = parentResponsibility.stream().filter(f -> f.getParentId().equals(NumberConstant.ZERO_L)).collect(Collectors.toList());
  2452. // //只留下中间责任中心
  2453. // parentResponsibility.removeAll(responseParent);
  2454. //
  2455. // //最上级添加模版
  2456. // List<CostProfitVo> parentReportVos = new ArrayList<>();
  2457. // for (Responsibility responsibility : responseParent) {
  2458. // List<CostProfitVo> profitVos = BeanUtil.convertList(parentModel, CostProfitVo.class);
  2459. // for (CostProfitVo costProfitVo : profitVos) {
  2460. // costProfitVo.setResponsibilityCode(responsibility.getResponsibilityCode());
  2461. // costProfitVo.setResponsibilityName(responsibility.getResponsibilityName());
  2462. // costProfitVo.setAmount(BigDecimal.ZERO);
  2463. // costProfitVo.setPercent(BigDecimal.ZERO);
  2464. // parentReportVos.add(costProfitVo);
  2465. // }
  2466. // }
  2467. //
  2468. // //中间责任中心 添加模版 添加父类责任中心
  2469. // List<CostProfitVo> middleReportVos = new ArrayList<>();
  2470. // for (Responsibility responsibility : parentResponsibility) {
  2471. // List<CostProfitVo> profitVos = BeanUtil.convertList(parentModel, CostProfitVo.class);
  2472. // Long aLong = responseCodeParentIdMap.get(responsibility.getResponsibilityCode());
  2473. // for (CostProfitVo costProfitVo : profitVos) {
  2474. // costProfitVo.setResponsibilityCode(responsibility.getResponsibilityCode());
  2475. // costProfitVo.setResponsibilityName(responsibility.getResponsibilityName());
  2476. // costProfitVo.setAmount(BigDecimal.ZERO);
  2477. // costProfitVo.setPercent(BigDecimal.ZERO);
  2478. // if (aLong.equals(NumberConstant.ZERO_L)) {
  2479. // costProfitVo.setParentResponsibilityCode(responsibility.getResponsibilityCode());
  2480. // costProfitVo.setParentResponsibilityName(responsibility.getResponsibilityName());
  2481. // } else {
  2482. // Responsibility responsibility1 = map.get(aLong);
  2483. // if (Objects.nonNull(responsibility1)) {
  2484. // costProfitVo.setParentResponsibilityCode(responsibility1.getResponsibilityCode());
  2485. // costProfitVo.setParentResponsibilityName(responsibility1.getResponsibilityName());
  2486. // }
  2487. // }
  2488. // middleReportVos.add(costProfitVo);
  2489. // }
  2490. // }
  2491. // //添加父类责任中心
  2492. // //已计算过的数据添加上级责任中心
  2493. // for (CostProfitVo departmentProfit : costProfitVos) {
  2494. // Long aLong = responseCodeParentIdMap.get(departmentProfit.getResponsibilityCode());
  2495. // if (aLong != null) {
  2496. // if (aLong.equals(NumberConstant.ZERO_L)) {
  2497. //// Long aLong1 = responseCodeIdMap.get(departmentProfit.getResponsibilityCode());
  2498. // departmentProfit.setParentResponsibilityCode(departmentProfit.getResponsibilityCode());
  2499. // departmentProfit.setParentResponsibilityName(departmentProfit.getResponsibilityName());
  2500. // } else {
  2501. // Responsibility responsibility = map.get(aLong);
  2502. // if (Objects.nonNull(responsibility)) {
  2503. // departmentProfit.setParentResponsibilityCode(responsibility.getResponsibilityCode());
  2504. // departmentProfit.setParentResponsibilityName(responsibility.getResponsibilityName());
  2505. // }
  2506. // }
  2507. // }
  2508. // }
  2509. // //已计算过的 按责任中心-报表id 分组
  2510. // Map<String, Map<Long, List<CostProfitVo>>> sumGroup = new HashMap<>();
  2511. // //已计算过的 按父类责任中心分组
  2512. // Map<String, List<CostProfitVo>> collect1 = costProfitVos.stream().collect(Collectors.groupingBy(CostProfitVo::getParentResponsibilityCode));
  2513. // if (!CollectionUtils.isEmpty(collect1)) {
  2514. // collect1.forEach((k, v) -> {
  2515. // Map<Long, List<CostProfitVo>> collect2 = v.stream().collect(Collectors.groupingBy(CostProfitVo::getReportId));
  2516. // sumGroup.put(k, collect2);
  2517. // });
  2518. // }
  2519. //
  2520. // //计算所有中间层
  2521. // List<CostProfitVo> middleFilterReportVos = new ArrayList<>();
  2522. // for (CostProfitVo middleReportVo : middleReportVos) {
  2523. // String responsibilityCode = middleReportVo.getResponsibilityCode();
  2524. // Map<Long, List<CostProfitVo>> longListMap = sumGroup.get(responsibilityCode);
  2525. // if (!CollectionUtils.isEmpty(longListMap)) {
  2526. // List<CostProfitVo> profitVos = longListMap.get(middleReportVo.getReportId());
  2527. // AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  2528. // if (!CollectionUtils.isEmpty(profitVos)) {
  2529. // profitVos.forEach(m -> {
  2530. // sum.updateAndGet(v -> v.add(m.getAmount()));
  2531. // });
  2532. //
  2533. // middleReportVo.setAmount(sum.get());
  2534. // }
  2535. // } else {
  2536. // //未在最下层找到数据的可能还是一个分类 提取出来
  2537. // middleFilterReportVos.add(middleReportVo);
  2538. // }
  2539. // }
  2540. // //用于处理多层级中 有两层中间层的数据
  2541. // if (!CollectionUtils.isEmpty(middleFilterReportVos)) {
  2542. // handleMiddleFilterReport(middleReportVos, middleFilterReportVos);
  2543. // }
  2544. // Map<String, Map<Long, List<CostProfitVo>>> sumMiddleGroup = new HashMap<>();
  2545. // Map<String, List<CostProfitVo>> collect2 = middleReportVos.stream().collect(Collectors.groupingBy(CostProfitVo::getParentResponsibilityCode));
  2546. // if (!CollectionUtils.isEmpty(collect2)) {
  2547. // collect2.forEach((k, v) -> {
  2548. // Map<Long, List<CostProfitVo>> collect3 = v.stream().collect(Collectors.groupingBy(CostProfitVo::getReportId));
  2549. // sumMiddleGroup.put(k, collect3);
  2550. // });
  2551. // }
  2552. //
  2553. // // 最上层的需要执行两次分组 两层时取最后一层计算 多层时取 中间层计算合计
  2554. // for (CostProfitVo parentReportVo : parentReportVos) {
  2555. // String responsibilityCode = parentReportVo.getResponsibilityCode();
  2556. // Map<Long, List<CostProfitVo>> longListMap = sumGroup.get(responsibilityCode);
  2557. // //在最下层找到的 说明是两层
  2558. // if (!CollectionUtils.isEmpty(longListMap)) {
  2559. // List<CostProfitVo> profitVos = longListMap.get(parentReportVo.getReportId());
  2560. // AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  2561. // if (!CollectionUtils.isEmpty(profitVos)) {
  2562. // profitVos.forEach(m -> {
  2563. // sum.updateAndGet(v -> v.add(m.getAmount()));
  2564. // });
  2565. // }
  2566. // //再找中间层是否存在子类
  2567. // Map<Long, List<CostProfitVo>> longListMap1 = sumMiddleGroup.get(responsibilityCode);
  2568. // if (!CollectionUtils.isEmpty(longListMap1)) {
  2569. // List<CostProfitVo> profitVos1 = longListMap1.get(parentReportVo.getReportId());
  2570. // if (!CollectionUtils.isEmpty(profitVos1)) {
  2571. // profitVos1.forEach(m -> {
  2572. // sum.updateAndGet(v -> v.add(m.getAmount()));
  2573. // });
  2574. // }
  2575. // }
  2576. // parentReportVo.setAmount(sum.get());
  2577. // } else {
  2578. // //未在最下层找到从中间层找进行计算
  2579. // Map<Long, List<CostProfitVo>> longListMap1 = sumMiddleGroup.get(responsibilityCode);
  2580. // if (!CollectionUtils.isEmpty(longListMap1)) {
  2581. // List<CostProfitVo> profitVos = longListMap1.get(parentReportVo.getReportId());
  2582. // AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  2583. // if (!CollectionUtils.isEmpty(profitVos)) {
  2584. // profitVos.forEach(m -> {
  2585. // sum.updateAndGet(v -> v.add(m.getAmount()));
  2586. // });
  2587. // }
  2588. // parentReportVo.setAmount(sum.get());
  2589. // }
  2590. // }
  2591. // }
  2592. // //把最上层 中间层 最下层组装成一个list 进行分组
  2593. //
  2594. // //组装数据
  2595. // Map<String, List<CostProfitVo>> collect3 = parentReportVos.stream().collect(Collectors.groupingBy(CostProfitVo::getResponsibilityCode));
  2596. // List<CostResponsibilityProfitVo> list = new ArrayList<>();
  2597. // collect3.forEach((k, v) -> {
  2598. // CostResponsibilityProfitVo vo = new CostResponsibilityProfitVo();
  2599. //// List<CostProfitVo> costProfitVos1 = collect3.get(s);
  2600. // vo.setResponsibilityCode(k);
  2601. // vo.setResponsibilityName(responseNameMap.get(k));
  2602. // if (!CollectionUtils.isEmpty(v)) {
  2603. // List<CommonDataReportVo> dataReportVos = new ArrayList<>();
  2604. // for (CostProfitVo costProfitVo : v) {
  2605. // CommonDataReportVo vo1 = new CommonDataReportVo();
  2606. // vo1.setReportId(costProfitVo.getReportId());
  2607. // vo1.setValue(costProfitVo.getAmount());
  2608. // dataReportVos.add(vo1);
  2609. // }
  2610. // vo.setProfitList(dataReportVos);
  2611. // } else {
  2612. // vo.setProfitList(new ArrayList<>());
  2613. // }
  2614. // list.add(vo);
  2615. // });
  2616. // middleReportVos.addAll(costProfitVos);
  2617. // Map<String, List<CostProfitVo>> collect5 = middleReportVos.stream().collect(Collectors.groupingBy(CostProfitVo::getResponsibilityCode));
  2618. // List<CostResponsibilityProfitVo> list1 = new ArrayList<>();
  2619. // collect5.forEach((k, v) -> {
  2620. // CostResponsibilityProfitVo vo = new CostResponsibilityProfitVo();
  2621. // vo.setResponsibilityCode(k);
  2622. // vo.setResponsibilityName(responseNameMap.get(k));
  2623. // if (!CollectionUtils.isEmpty(v)) {
  2624. // vo.setParentResponsibilityCode(v.get(0).getParentResponsibilityCode());
  2625. // List<CommonDataReportVo> dataReportVos = new ArrayList<>();
  2626. // for (CostProfitVo costProfitVo : v) {
  2627. // CommonDataReportVo vo1 = new CommonDataReportVo();
  2628. // vo1.setReportId(costProfitVo.getReportId());
  2629. // vo1.setValue(costProfitVo.getAmount());
  2630. // dataReportVos.add(vo1);
  2631. // }
  2632. // vo.setProfitList(dataReportVos);
  2633. // } else {
  2634. // vo.setProfitList(new ArrayList<>());
  2635. // }
  2636. // list1.add(vo);
  2637. // });
  2638. // Map<String, List<CostResponsibilityProfitVo>> collect4 = list1.stream().collect(Collectors.groupingBy(CostResponsibilityProfitVo::getParentResponsibilityCode));
  2639. // for (CostResponsibilityProfitVo vo : list) {
  2640. // List<CostResponsibilityProfitVo> list2 = collect4.get(vo.getResponsibilityCode());
  2641. // if (!CollectionUtils.isEmpty(list2)) {
  2642. // if (!list2.get(0).getResponsibilityCode().equals(vo.getResponsibilityCode())) {
  2643. // vo.setChild(setProfitChildren(list2, collect4));
  2644. // }
  2645. // }
  2646. // }
  2647. // response.setData(list);
  2648. //
  2649. // //提取出最上层
  2650. //
  2651. //
  2652. // return response;
  2653. }
  2654. /**
  2655. * 是否显示占比
  2656. *
  2657. * @return
  2658. */
  2659. public boolean IsShowPercent() {
  2660. String parameterValue = centerService.getParameterValue(ParameterConstant.SHOW_PERCENT);
  2661. return NumberConstant.ONE_S.equals(parameterValue);
  2662. }
  2663. /**
  2664. * 是否需要代码计算
  2665. * @return
  2666. */
  2667. public boolean IsNeedCalc(String reportType) {
  2668. String parameterValue = centerService.getParameterValue(ParameterConstant.SQL_CALC_REPORT_TYPE);
  2669. if(StringUtils.isEmpty(parameterValue)){
  2670. return true;
  2671. }
  2672. //不需计算的报表类型列表
  2673. List<String> nonCalcReportTypeList = Arrays.stream(parameterValue.split(SplitConstant.SEPARATOR_VERTICALLINE)).collect(Collectors.toList());
  2674. return !nonCalcReportTypeList.contains(reportType);
  2675. }
  2676. private List<CommonResponsibilityReportVo> getChildTitle(List<Responsibility> responsibilities, Map<Long, List<Responsibility>> responsibilityGroup) {
  2677. List<CommonResponsibilityReportVo> list = new ArrayList<>();
  2678. for (Responsibility responsibility : responsibilities) {
  2679. CommonResponsibilityReportVo commonResponsibilityReportVo = new CommonResponsibilityReportVo();
  2680. commonResponsibilityReportVo.setResponsibilityCode(responsibility.getResponsibilityCode());
  2681. commonResponsibilityReportVo.setResponsibilityName(responsibility.getResponsibilityName());
  2682. commonResponsibilityReportVo.setSort(responsibility.getSort());
  2683. List<Responsibility> responsibilityChild = responsibilityGroup.get(responsibility.getId());
  2684. if (!CollectionUtils.isEmpty(responsibilityChild)) {
  2685. commonResponsibilityReportVo.setChild(getChildTitle(responsibilityChild, responsibilityGroup));
  2686. } else {
  2687. // //添加金额占比
  2688. // commonResponsibilityReportVo.setChild(setDefalutChildList(responsibility));
  2689. //要显示占比时自动生成默认金额占比层
  2690. if (IsShowPercent()) {
  2691. commonResponsibilityReportVo.setChild(setDefalutChildList(responsibility));
  2692. } else {
  2693. //不显示占比时,直接责任中心作为金额层
  2694. commonResponsibilityReportVo.setResponsibilityCode(commonResponsibilityReportVo.getResponsibilityCode() + AMOUNT_FIELD);
  2695. }
  2696. }
  2697. list.add(commonResponsibilityReportVo);
  2698. }
  2699. return list;
  2700. }
  2701. private @NotNull List<CommonResponsibilityReportVo> setDefalutChildList(Responsibility responsibility) {
  2702. List<CommonResponsibilityReportVo> childList = new ArrayList<>();
  2703. CommonResponsibilityReportVo amount = new CommonResponsibilityReportVo();
  2704. amount.setResponsibilityCode(responsibility.getResponsibilityCode() + AMOUNT_FIELD);
  2705. amount.setResponsibilityName(AMOUNT);
  2706. amount.setSort(1);
  2707. childList.add(amount);
  2708. CommonResponsibilityReportVo percent = new CommonResponsibilityReportVo();
  2709. percent.setResponsibilityCode(responsibility.getResponsibilityCode() + PERCENT_FIELD);
  2710. percent.setResponsibilityName(PERCENT);
  2711. percent.setSort(2);
  2712. childList.add(percent);
  2713. return childList;
  2714. }
  2715. private List<CostProfitVo> setChildren(List<CostProfitVo> child, Map<Long, List<CostProfitVo>> collect) {
  2716. for (CostProfitVo costProfitVo : child) {
  2717. if (!CollectionUtils.isEmpty(collect.get(costProfitVo.getReportId()))) {
  2718. costProfitVo.setChildren(setChildren(collect.get(costProfitVo.getReportId()), collect));
  2719. }
  2720. }
  2721. if (!CollectionUtils.isEmpty(child)) {
  2722. child.sort(Comparator.comparing(CostProfitVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  2723. }
  2724. return child;
  2725. }
  2726. private List<ReportFormCustomVo> setCustomChildren(List<ReportFormCustomVo> child, Map<Long, List<ReportFormCustomVo>> collect) {
  2727. for (ReportFormCustomVo costProfitVo : child) {
  2728. if (!CollectionUtils.isEmpty(collect.get(costProfitVo.getId()))) {
  2729. costProfitVo.setChildren(setCustomChildren(collect.get(costProfitVo.getId()), collect));
  2730. }
  2731. }
  2732. if (!CollectionUtils.isEmpty(child)) {
  2733. child.sort(Comparator.comparing(ReportFormCustomVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  2734. }
  2735. return child;
  2736. }
  2737. /**
  2738. * 按照会计科目进行计算
  2739. *
  2740. * @param i
  2741. */
  2742. private BigDecimal setAccountReportData(CostDepartmentProfitVO i, List<IncomeCollection> list, List<AllocationQueryReportVO> allocationQueryReportVOList, Map<Long, List<ReportRelation>> reportRelationMap, List<CostDepartmentProfitVO> allList) {
  2743. // 在报表关联里面查询当前报表关联的
  2744. Long reportId = i.getReportId();
  2745. List<ReportRelation> reportRelationList = reportRelationMap.get(reportId);
  2746. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.000"));
  2747. if (CollUtil.isNotEmpty(reportRelationList)) {
  2748. // 获取对应的会计科目信息 筛选会计科目的Code
  2749. List<String> accountList = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  2750. // 查找在归集数据里面当前责任中心对应的这些会计科目的金额
  2751. List<IncomeCollection> incomeCollectionList = list.stream().filter(income -> income.getResponsibilityCode().equals(i.getResponsibilityCode()) && accountList.contains(income.getAccountingCode())).collect(Collectors.toList());
  2752. // 需要查询分摊后的表
  2753. List<AllocationQueryReportVO> reportVOList = allocationQueryReportVOList.stream().filter(m -> m.getTargetResponsibilityCode().equals(i.getResponsibilityCode()) && accountList.contains(m.getAccountingCode())).collect(Collectors.toList());
  2754. if (CollUtil.isNotEmpty(incomeCollectionList)) {
  2755. incomeCollectionList.forEach(m -> {
  2756. sum.updateAndGet(v -> v.add(m.getAmount()));
  2757. });
  2758. }
  2759. // 成本减
  2760. if (CollUtil.isNotEmpty(reportVOList)) {
  2761. reportVOList.forEach(m -> {
  2762. sum.updateAndGet(v -> v.add(m.getAmount()));
  2763. });
  2764. }
  2765. }
  2766. // i.setAmount(new BigDecimal(sum.toString()));
  2767. // numMap.put(i.getReportNum()+i.getResponsibilityCode(),new BigDecimal(sum.toString()));
  2768. return sum.get();
  2769. }
  2770. /**
  2771. * 按照分摊层级进行计算
  2772. * 按照分摊层级计算 报表分摊层级是当前的层级 并且目标责任中心失败当前责任中心
  2773. */
  2774. private BigDecimal setShareLevelReportData(CostDepartmentProfitVO i, List<CostShareLevel> costShareLevelList,
  2775. Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList, List<CostDepartmentProfitVO> allList) {
  2776. List<ReportRelation> reportRelationList = reportRelationMap.get(i.getReportId());
  2777. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.000"));
  2778. if (CollUtil.isNotEmpty(reportRelationList)) {
  2779. // 找到对应的分摊层级的Id 但是分摊报表里面存的是分摊层级的序号
  2780. List<Long> shareLevelIds = reportRelationList.stream().map(ReportRelation::getRelationCode).map(Long::valueOf).collect(Collectors.toList());
  2781. // List<Integer> levelShortList = costShareLevelList.stream()
  2782. // .filter(m -> shareLevelIds.contains(m.getId()))
  2783. // .map(CostShareLevel::getLeverSort).collect(Collectors.toList());
  2784. if (CollUtil.isNotEmpty(shareLevelIds)) {
  2785. // 查询报表里面是当前分摊层级的数据
  2786. List<Allocation> allocations = allocationList.stream().filter(m -> shareLevelIds.contains(m.getShareLevelId()) && m.getTargetResponsibilityCode().equals(i.getResponsibilityCode())).collect(Collectors.toList());
  2787. if (CollUtil.isNotEmpty(allocations)) {
  2788. // allocations.forEach(m -> {
  2789. // sum.updateAndGet(v -> v.add(m.getAmount()));
  2790. // });
  2791. BigDecimal reduce = allocations.stream().map(Allocation::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
  2792. sum.set(reduce);
  2793. }
  2794. }
  2795. }
  2796. // i.setAmount(new BigDecimal(sum.toString()));
  2797. return sum.get();
  2798. }
  2799. /**
  2800. * 按照责任中心进行计算
  2801. * 原始责任中心是设置的责任中心 目标责任中心是报表的责任中心
  2802. * 查询分摊报表里面目标责任中心是当前责任中心 报表责任中心是当前设置的责任中心
  2803. */
  2804. public BigDecimal setResponsibilityCode(CostDepartmentProfitVO costDepartmentProfitVO, Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList, List<CostDepartmentProfitVO> allList) {
  2805. // 获取当前报表对应的责任中心
  2806. List<ReportRelation> reportRelationList = reportRelationMap.get(costDepartmentProfitVO.getReportId());
  2807. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.000"));
  2808. if (CollUtil.isNotEmpty(reportRelationList)) {
  2809. // 获取对应的责任中心的Code集合 这个是设置的责任中心
  2810. List<String> responsibilityCodes = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  2811. if (CollUtil.isNotEmpty(responsibilityCodes)) {
  2812. // 查询报表里面是当前分摊层级的数据
  2813. List<Allocation> allocations = allocationList.stream().filter(i -> i.getTargetResponsibilityCode().equals(costDepartmentProfitVO.getResponsibilityCode()) && responsibilityCodes.contains(i.getResponsibilityCode())).collect(Collectors.toList());
  2814. if (CollUtil.isNotEmpty(allocations)) {
  2815. BigDecimal reduce = allocations.stream().map(Allocation::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
  2816. sum.set(reduce);
  2817. // allocations.forEach(m -> {
  2818. // sum.updateAndGet(v -> v.add(m.getAmount()));
  2819. // });
  2820. }
  2821. }
  2822. }
  2823. // costDepartmentProfitVO.setAmount(new BigDecimal(sum.toString()));
  2824. return sum.get();
  2825. }
  2826. /**
  2827. * 按照小计的计算方式
  2828. * 同一个目录下相同的其他金额的和
  2829. */
  2830. public BigDecimal setSubtotal(CostDepartmentProfitVO costDepartmentProfitVO,
  2831. List<CostShareLevel> costShareLevelList,
  2832. Map<Long, List<CostDepartmentProfitVO>> listMap,
  2833. List<CostDepartmentProfitVO> profitVOS,
  2834. List<IncomeCollection> list,
  2835. List<AllocationQueryReportVO> allocationQueryReportVOList,
  2836. Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList, List<CostDepartmentProfitVO> allList) {
  2837. // 因为报表是按照报表项目的升序进行排序的 前面的报表是已经进行计算了的
  2838. // 查询当前报表的父层级id
  2839. Long reportParentId = costDepartmentProfitVO.getReportParentId();
  2840. String responsibilityCode = costDepartmentProfitVO.getResponsibilityCode();
  2841. // 查询报表里面在当前父层级下的并且不是自己的报表项目
  2842. List<CostDepartmentProfitVO> costDepartmentProfitVOS = allList.stream().filter(i -> i.getReportParentId().equals(reportParentId) && i.getResponsibilityCode().equals(responsibilityCode) && !NumberConstant.THREE.equals(i.getCalcType())).collect(Collectors.toList());
  2843. AtomicReference<BigDecimal> sum = new AtomicReference<>(new BigDecimal("0.0000"));
  2844. // 遍历数据 之前的已经经过了计算
  2845. costDepartmentProfitVOS.forEach(i -> {
  2846. Long reportId = i.getReportId();
  2847. List<CostDepartmentProfitVO> costDepartmentProfitVOS1 = listMap.get(reportId);
  2848. if (CollUtil.isEmpty(costDepartmentProfitVOS1)) {
  2849. throw new CostException(500, "报表未找到");
  2850. }
  2851. BigDecimal amount = getAmount(costDepartmentProfitVOS1, costShareLevelList, responsibilityCode, list, allocationQueryReportVOList, reportRelationMap, allocationList, listMap, allList);
  2852. sum.set(amount.add(sum.get()));
  2853. });
  2854. // costDepartmentProfitVO.setAmount(new BigDecimal(sum.toString()));
  2855. return sum.get();
  2856. }
  2857. /***
  2858. * 按照计算方式进行计算
  2859. */
  2860. public BigDecimal setCalculation(CostDepartmentProfitVO costDepartmentProfitVO, List<CostDepartmentProfitVO> costDepartmentProfitVOList,
  2861. List<CostShareLevel> costShareLevelList, Map<Long, List<CostDepartmentProfitVO>> listMap,
  2862. List<IncomeCollection> list, List<AllocationQueryReportVO> allocationQueryReportVOList,
  2863. Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList, List<CostDepartmentProfitVO> allList) {
  2864. // 获取当前报表的计算方式 [1]+[2]类型/ [1]-[2]
  2865. String calcFormula = costDepartmentProfitVO.getCalcFormula();
  2866. String responsibilityCode = costDepartmentProfitVO.getResponsibilityCode();
  2867. String replace = calcFormula.replace("[", "").replace("]", "").replace("+", ",").replace("-", ",-");
  2868. List<Integer> calcFormulaList = Arrays.stream(replace.split(StrUtil.COMMA)).map(Integer::valueOf).collect(Collectors.toList());
  2869. // 查询这个编号集合的报表
  2870. AtomicReference<BigDecimal> bigDecimal = new AtomicReference<>(new BigDecimal("0.0000"));
  2871. calcFormulaList.forEach(calc -> {
  2872. Integer calcNum = Math.abs(calc);
  2873. List<Long> reportIdList = allList.stream().filter(i -> i.getResponsibilityCode().equals(responsibilityCode) && calcNum.equals(i.getReportNum())).map(CostDepartmentProfitVO::getReportId).collect(Collectors.toList());
  2874. if (CollUtil.isNotEmpty(reportIdList)) {
  2875. reportIdList.forEach(i -> {
  2876. List<CostDepartmentProfitVO> profitVOS = listMap.get(i);
  2877. if (CollUtil.isEmpty(profitVOS)) {
  2878. throw new CostException(500, "报表未找到");
  2879. }
  2880. BigDecimal amount = getAmount(profitVOS, costShareLevelList, responsibilityCode, list, allocationQueryReportVOList, reportRelationMap, allocationList, listMap, allList);
  2881. if (calc > 0) {
  2882. bigDecimal.updateAndGet(v -> v.add(amount));
  2883. } else {
  2884. bigDecimal.updateAndGet(v -> v.subtract(amount));
  2885. }
  2886. });
  2887. }
  2888. });
  2889. return bigDecimal.get();
  2890. }
  2891. /**
  2892. * 判断是那种计算方式 调用方法进行计算
  2893. */
  2894. public BigDecimal getAmount(List<CostDepartmentProfitVO> profitVOS,
  2895. List<CostShareLevel> costShareLevelList,
  2896. String responsibilityCode, List<IncomeCollection> list,
  2897. List<AllocationQueryReportVO> allocationQueryReportVOList,
  2898. Map<Long, List<ReportRelation>> reportRelationMap, List<Allocation> allocationList,
  2899. Map<Long, List<CostDepartmentProfitVO>> listMap, List<CostDepartmentProfitVO> allList) {
  2900. BigDecimal bigDecimal = new BigDecimal("0.0000");
  2901. // 在对这个报表进行过滤
  2902. List<CostDepartmentProfitVO> costDepartmentProfitVOS = profitVOS.stream().filter(i -> i.getResponsibilityCode().equals(responsibilityCode)).collect(Collectors.toList());
  2903. // 都一个就是
  2904. CostDepartmentProfitVO costDepartmentProfitVO = costDepartmentProfitVOS.get(0);
  2905. Integer calcType = costDepartmentProfitVO.getCalcType();
  2906. if (NumberConstant.ONE.equals(calcType)) {
  2907. // 调用计算的方法 按照会计科目
  2908. bigDecimal = setAccountReportData(costDepartmentProfitVO, list, allocationQueryReportVOList, reportRelationMap, allList);
  2909. } else if (NumberConstant.TWO.equals(calcType)) {
  2910. // 按照分摊层级
  2911. bigDecimal = setShareLevelReportData(costDepartmentProfitVO, costShareLevelList, reportRelationMap, allocationList, allList);
  2912. } else if (NumberConstant.THREE.equals(calcType)) {
  2913. // 小计
  2914. bigDecimal = setSubtotal(costDepartmentProfitVO, costShareLevelList, listMap, profitVOS, list, allocationQueryReportVOList, reportRelationMap, allocationList, allList);
  2915. } else if (NumberConstant.FIVE.equals(calcType)) {
  2916. // 按照责任中心
  2917. bigDecimal = setResponsibilityCode(costDepartmentProfitVO, reportRelationMap, allocationList, allList);
  2918. } else if (NumberConstant.FOUR.equals(calcType)) {
  2919. // bigDecimal = setCalculation(costDepartmentProfitVO, costDepartmentProfitVOS, costShareLevelList, listMap, list, allocationQueryReportVOList, reportRelationMap, allocationList, allList);
  2920. }
  2921. return bigDecimal;
  2922. }
  2923. private void exportDepartmentProfit(String date, OutputStream outputStream, Integer reportType, String name) throws Exception {
  2924. Long hospId = UserContext.getHospId();
  2925. DateTime dateTime = DateUtil.parse(date);
  2926. int year = DateUtil.year(dateTime);
  2927. int month = DateUtil.month(dateTime) + 1;
  2928. // 查询所有的节点
  2929. List<Responsibility> responsibilityList = responsibilityService.list(new QueryWrapper<Responsibility>().lambda().eq(Responsibility::getHospId, hospId));
  2930. if (CollUtil.isEmpty(responsibilityList)) {
  2931. throw new CostException(500, "责任中心不存在");
  2932. }
  2933. Map<String, Long> responseCodeParentIdMap = new HashMap<>();
  2934. Map<Long, Responsibility> responsibilityIdMap = new HashMap<>();
  2935. Map<String, String> responseCodeNameMap = new HashMap<>();
  2936. Map<String, Long> responseCodeIdMap = new HashMap<>();
  2937. Map<String, Responsibility> responsibilityCodeMap = new HashMap<>();
  2938. Map<String, Integer> responsibilityStatusMap = new HashMap<>();
  2939. Map<String, Integer> responsibilitySortMap = new HashMap<>();
  2940. responsibilityList.forEach(responsibility1 -> {
  2941. responsibilityCodeMap.put(responsibility1.getResponsibilityCode(), responsibility1);
  2942. responseCodeIdMap.put(responsibility1.getResponsibilityCode(), responsibility1.getId());
  2943. responseCodeNameMap.put(responsibility1.getResponsibilityCode(), responsibility1.getResponsibilityName());
  2944. responsibilityIdMap.put(responsibility1.getId(), responsibility1);
  2945. responseCodeParentIdMap.put(responsibility1.getResponsibilityCode(), responsibility1.getParentId());
  2946. responsibilityStatusMap.put(responsibility1.getResponsibilityCode(), responsibility1.getStatus());
  2947. Integer sort = responsibility1.getSort();
  2948. responsibilitySortMap.put(responsibility1.getResponsibilityCode(), sort != null ? sort : Integer.valueOf(1));
  2949. });
  2950. // 损益表数据
  2951. List<CostDepartmentProfit> departmentProfits = this.list(new QueryWrapper<CostDepartmentProfit>().lambda()
  2952. .eq(CostDepartmentProfit::getHospId, hospId)
  2953. .eq(CostDepartmentProfit::getYear, year).eq(CostDepartmentProfit::getMonth, month)
  2954. .eq(CostDepartmentProfit::getShareType, reportType));
  2955. if (CollectionUtils.isEmpty(departmentProfits)) {
  2956. throw new CostException(500, name + "未进行科室损益计算");
  2957. }
  2958. //判断一下责任中心是否停用
  2959. List<CostDepartmentProfit> removeList = new ArrayList<>();
  2960. for (CostDepartmentProfit departmentProfit : departmentProfits) {
  2961. Integer i = responsibilityStatusMap.get(departmentProfit.getResponsibilityCode());
  2962. if (i != null && i == 0) {
  2963. removeList.add(departmentProfit);
  2964. }
  2965. Long aLong = responseCodeParentIdMap.get(departmentProfit.getResponsibilityCode());
  2966. if (aLong != null) {
  2967. if (aLong.equals(NumberConstant.ZERO_L)) {
  2968. departmentProfit.setParentResponsibilityCode(departmentProfit.getResponsibilityCode());
  2969. departmentProfit.setParentResponsibilityName(departmentProfit.getResponsibilityName());
  2970. } else {
  2971. Responsibility responsibility = responsibilityIdMap.get(aLong);
  2972. if (Objects.nonNull(responsibility)) {
  2973. departmentProfit.setParentResponsibilityCode(responsibility.getResponsibilityCode());
  2974. departmentProfit.setParentResponsibilityName(responsibility.getResponsibilityName());
  2975. }
  2976. }
  2977. }
  2978. }
  2979. //删除停用责任中心数据
  2980. if (!CollectionUtils.isEmpty(removeList)) {
  2981. departmentProfits.removeAll(removeList);
  2982. }
  2983. List<CostDepartmentProfit> profitList = departmentProfits.stream().filter(f -> StringUtils.isEmpty(f.getParentResponsibilityCode())).collect(Collectors.toList());
  2984. if (!CollectionUtils.isEmpty(profitList)) {
  2985. log.info("{}报表类型存在无上级责任中心的数据,请核实配置数据", reportType);
  2986. }
  2987. Map<Long, List<CostDepartmentProfit>> profitReportIdGroup = departmentProfits.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getReportId));
  2988. Map<Long, Map<String, CostDepartmentProfit>> profitReportResponsibilityGroup = new HashMap<>();
  2989. profitReportIdGroup.forEach((k, v) -> {
  2990. profitReportResponsibilityGroup.put(k, v.stream().collect(Collectors.toMap(CostDepartmentProfit::getResponsibilityCode, costDepartmentProfit -> costDepartmentProfit, (a, b) -> b)));
  2991. });
  2992. // Map<String, List<CostDepartmentProfit>> doctorParentMap = departmentProfits.stream().collect(Collectors.groupingBy(CostDepartmentProfit::getParentResponsibilityCode));
  2993. //损益表设置
  2994. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  2995. .eq(ReportForm::getHospId, hospId)
  2996. .eq(ReportForm::getReportType, reportType));
  2997. if (CollUtil.isEmpty(reportFormList)) {
  2998. throw new CostException(500, "损益表未找到");
  2999. }
  3000. //第一层
  3001. List<TitleEntity> titleList = new ArrayList<>();
  3002. titleList.add(new TitleEntity("0", null, name, false, NumberConstant.ZERO));
  3003. //从损益数据中提取出所有责任中心组装成树
  3004. List<ResponsibilityVo> responsibilityVos = new ArrayList<>();
  3005. for (CostDepartmentProfit departmentProfit : departmentProfits) {
  3006. Responsibility responsibility = responsibilityCodeMap.get(departmentProfit.getResponsibilityCode());
  3007. ResponsibilityVo responsibilityVo = new ResponsibilityVo();
  3008. responsibilityVo.setParentResponsibilityCode(departmentProfit.getParentResponsibilityCode());
  3009. responsibilityVo.setParentResponsibilityName(departmentProfit.getParentResponsibilityName());
  3010. responsibilityVo.setResponsibilityCode(departmentProfit.getResponsibilityCode());
  3011. responsibilityVo.setResponsibilityName(departmentProfit.getResponsibilityName());
  3012. responsibilityVo.setSort(responsibility.getSort());
  3013. responsibilityVos.add(responsibilityVo);
  3014. }
  3015. ListUtils.sort(responsibilityVos, ResponsibilityVo::getParentResponsibilityName);
  3016. Map<String, List<ResponsibilityVo>> responsibilityMap = responsibilityVos.stream().distinct().collect(Collectors.groupingBy(ResponsibilityVo::getParentResponsibilityCode));
  3017. responsibilityMap.forEach((k, v) -> {
  3018. TitleEntity entity = new TitleEntity();
  3019. entity.setId(String.valueOf(responseCodeIdMap.get(k)));
  3020. entity.setPid("0");
  3021. entity.setContent(responseCodeNameMap.get(k));
  3022. entity.setFieldName(responseCodeNameMap.get(k));
  3023. entity.setWidth(15);
  3024. entity.setSort(responsibilitySortMap.get(k));
  3025. boolean child = getChild(titleList, k, responsibilityMap, responseCodeIdMap, String.valueOf(responseCodeIdMap.get(k)));
  3026. entity.setLast(child);
  3027. titleList.add(entity);
  3028. });
  3029. final List<ReportForm> finalReportFormList = reportFormList;
  3030. List<ReportForm> reportForms = reportFormList.stream().filter(i -> i.getParentId() == 0)
  3031. .peek(i -> i.setChild(this.getReportFormChild(i, finalReportFormList)))
  3032. .collect(Collectors.toList());
  3033. //列
  3034. List<TitleEntity> titleEntities = titleList.stream().filter(TitleEntity::isLast).collect(Collectors.toList());
  3035. List<Map<String, String>> rowList = new ArrayList<>();
  3036. List<Integer> depthList = new ArrayList<>();
  3037. for (ReportForm reportForm : reportForms) {
  3038. int child = TreeDepthUtil.getTreeDepthByDfsWithRecursion(reportForm, "child");
  3039. depthList.add(child);
  3040. }
  3041. OptionalInt max = depthList.stream().mapToInt(Integer::intValue).max();
  3042. //根据报表设置最深深度添加 默认列
  3043. int asInt = max.getAsInt();
  3044. for (int i = 1; i <= asInt; i++) {
  3045. TitleEntity parentResponsibility = new TitleEntity();
  3046. parentResponsibility.setId(String.valueOf(i));
  3047. parentResponsibility.setPid("0");
  3048. parentResponsibility.setContent("");
  3049. parentResponsibility.setFieldName(String.valueOf(i));
  3050. parentResponsibility.setWidth(15);
  3051. parentResponsibility.setSort(NumberConstant.ZERO);
  3052. parentResponsibility.setLast(true);
  3053. titleList.add(parentResponsibility);
  3054. }
  3055. titleList.sort(Comparator.comparing(TitleEntity::getSort));
  3056. DecimalFormat df = new DecimalFormat("0.00%");
  3057. //递归排序
  3058. formSort(reportForms);
  3059. //配置单元格数据
  3060. for (ReportForm reportForm : reportForms) {
  3061. int fieldId = 1;
  3062. Map<String, String> m = new HashMap<String, String>();
  3063. if (!CollectionUtils.isEmpty(reportForm.getChild())) {
  3064. m.put(Integer.toString(fieldId), reportForm.getReportName());
  3065. setRowList(fieldId, reportForm.getChild(), profitReportResponsibilityGroup, rowList, df, titleEntities, m, asInt);
  3066. } else {
  3067. for (int i = 1; i <= asInt; i++) {
  3068. m.put(Integer.toString(i), reportForm.getReportName());
  3069. }
  3070. Map<String, CostDepartmentProfit> map = profitReportResponsibilityGroup.get(reportForm.getId());
  3071. for (TitleEntity titleEntity : titleEntities) {
  3072. String fieldName = titleEntity.getFieldName();
  3073. if (fieldName.contains(AMOUNT_FIELD)) {
  3074. String[] split1 = StringUtils.split(fieldName, "|");
  3075. if (split1 != null) {
  3076. CostDepartmentProfit costDepartmentProfit = map.get(split1[0]);
  3077. m.put(fieldName, costDepartmentProfit.getAmount().setScale(2, RoundingMode.HALF_UP).toString());
  3078. } else {
  3079. m.put(fieldName, "");
  3080. }
  3081. } else if (fieldName.contains(PERCENT_FIELD)) {
  3082. String[] split1 = StringUtils.split(fieldName, "|");
  3083. if (split1 != null) {
  3084. CostDepartmentProfit costDepartmentProfit = map.get(split1[0]);
  3085. if (costDepartmentProfit.getPercent() != null) {
  3086. m.put(fieldName, df.format(costDepartmentProfit.getPercent()));
  3087. } else {
  3088. m.put(fieldName, "");
  3089. }
  3090. } else {
  3091. m.put(fieldName, "");
  3092. }
  3093. }
  3094. }
  3095. rowList.add(m);
  3096. }
  3097. }
  3098. //表头树进行excel 元素转换
  3099. ExcelPoiUtil excelTool = new ExcelPoiUtil(name);
  3100. Map<String, String> param = ImmutableMap.<String, String>builder().put("id", "id").put("pid", "pid")
  3101. .put("content", "content").put("fieldName", "fieldName").put("width", "width").build();
  3102. List<ColEntity> titleData = excelTool.colEntityTransformer(titleList, param, "0");
  3103. List<Integer> mergeList = IntStream.range(0, asInt).boxed().collect(Collectors.toList());
  3104. // 多个sheet导出
  3105. XSSFWorkbook workbook = excelTool.exportXssfWorkbook(titleData, rowList, null);
  3106. // CellRangeAddress cellAddresses = new CellRangeAddress(4,7,0,4);
  3107. // workbook.getSheetAt(0).addMergedRegion(cellAddresses);
  3108. //表头占的行数
  3109. int totalRow = titleData.get(0).getTotalRow();
  3110. int[] relationColl = new int[mergeList.size()];
  3111. Map<Integer, int[]> mergeMap = IntStream.range(0, mergeList.size()).boxed().collect(Collectors.toMap(i -> i, i -> relationColl, (a, b) -> b));
  3112. //手动横向合并
  3113. addMergeCells(workbook, mergeMap, asInt);
  3114. //纵向合并
  3115. PoiMergeCellUtil.mergeCells(workbook.getSheetAt(0), mergeMap, totalRow);
  3116. // int[] relationTitleColl = new int[totalRow];
  3117. //
  3118. // Map<Integer,int[]> mergeTitleMap = IntStream.range(asInt-1, titleData.get(0).getTotalCol()).boxed().collect(Collectors.toMap(i -> i, i -> relationTitleColl, (a, b) -> b));
  3119. // PoiMergeCellUtil.mergeCells(workbook.getSheetAt(0),mergeTitleMap,1,totalRow-1);
  3120. // excelTool.save(workbook, "D:\\测试用.xlsx");
  3121. workbook.write(outputStream);
  3122. workbook.close();
  3123. }
  3124. private static void addMergeCells(XSSFWorkbook workbook, Map<Integer, int[]> mergeMap, int asInt) {
  3125. XSSFSheet sheet = workbook.getSheetAt(0);
  3126. //最后一行
  3127. Set<Integer> colKeyList = mergeMap.keySet();
  3128. int endRow = sheet.getLastRowNum();
  3129. for (int i = 4; i <= endRow; i++) {
  3130. //mergeMap
  3131. List<String> cellText = new ArrayList<>();
  3132. for (Integer integer : colKeyList) {
  3133. XSSFRow row = sheet.getRow(i);
  3134. String text = PoiCellUtil.getCellValue(row.getCell(integer));
  3135. cellText.add(text);
  3136. }
  3137. // 过滤出元素出现次数大于 1 (重复元素)的 entry
  3138. List<Integer> collect = cellText.stream().filter(j -> !Objects.equals(j, "")) // list 对应的 Stream 并过滤""
  3139. .collect(Collectors.toMap(e -> e, e -> 1, Integer::sum)) // 获得元素出现频率的 Map,键为元素,值为元素出现的次数
  3140. .values()
  3141. .stream() // 所有 entry 对应的 Stream
  3142. .filter(integer -> integer > 1) // 获得 entry 的键(重复元素)对应的 Stream
  3143. .collect(Collectors.toList());
  3144. if (!CollectionUtils.isEmpty(collect)) {
  3145. sheet.addMergedRegion(new CellRangeAddress(i, i, mergeMap.size() - collect.get(0), asInt - 1));
  3146. }
  3147. }
  3148. }
  3149. private void setRowList(int fieldId, List<ReportForm> child, Map<Long, Map<String, CostDepartmentProfit>> profitReportResponsibilityGroup,
  3150. List<Map<String, String>> rowList, DecimalFormat df, List<TitleEntity> titleEntities,
  3151. Map<String, String> parentFieldMap, int maxDepth) {
  3152. fieldId++;
  3153. for (ReportForm reportForm : child) {
  3154. Map<String, String> m = new HashMap<String, String>(parentFieldMap);
  3155. m.put(Integer.toString(fieldId), reportForm.getReportName());
  3156. if (!CollectionUtils.isEmpty(reportForm.getChild())) {
  3157. setRowList(fieldId, reportForm.getChild(), profitReportResponsibilityGroup, rowList, df, titleEntities, m, maxDepth);
  3158. } else {
  3159. if (fieldId < maxDepth) {
  3160. for (int i = 1; i <= maxDepth - fieldId; i++) {
  3161. m.put(Integer.toString(fieldId + i), reportForm.getReportName());
  3162. }
  3163. }
  3164. Map<String, CostDepartmentProfit> map = profitReportResponsibilityGroup.get(reportForm.getId());
  3165. for (TitleEntity titleEntity : titleEntities) {
  3166. String fieldName = titleEntity.getFieldName();
  3167. if (fieldName.contains(AMOUNT_FIELD)) {
  3168. String[] split1 = StringUtils.split(fieldName, "|");
  3169. if (split1 != null) {
  3170. if (map != null) {
  3171. CostDepartmentProfit costDepartmentProfit = map.get(split1[0]);
  3172. if (Objects.nonNull(costDepartmentProfit)) {
  3173. m.put(fieldName, costDepartmentProfit.getAmount().setScale(2, RoundingMode.HALF_UP).toString());
  3174. } else {
  3175. m.put(fieldName, "");
  3176. }
  3177. } else {
  3178. m.put(fieldName, "");
  3179. }
  3180. } else {
  3181. m.put(fieldName, "");
  3182. }
  3183. } else if (fieldName.contains(PERCENT_FIELD)) {
  3184. String[] split1 = StringUtils.split(fieldName, "|");
  3185. if (split1 != null) {
  3186. if (map != null) {
  3187. CostDepartmentProfit costDepartmentProfit = map.get(split1[0]);
  3188. if (costDepartmentProfit.getPercent() != null) {
  3189. m.put(fieldName, df.format(costDepartmentProfit.getPercent()));
  3190. } else {
  3191. m.put(fieldName, "");
  3192. }
  3193. } else {
  3194. m.put(fieldName, "");
  3195. }
  3196. } else {
  3197. m.put(fieldName, "");
  3198. }
  3199. }
  3200. }
  3201. rowList.add(m);
  3202. }
  3203. }
  3204. }
  3205. private List<ReportForm> getReportFormChild(ReportForm parent, List<ReportForm> reportFormList) {
  3206. return reportFormList.stream().filter(o -> o.getParentId().equals(parent.getId()))
  3207. .peek(o -> o.setChild(this.getReportFormChild(o, reportFormList)))
  3208. .collect(Collectors.toList());
  3209. //
  3210. }
  3211. private void formSort(List<ReportForm> reportFormList) {
  3212. for (ReportForm reportForm : reportFormList) {
  3213. if (!CollectionUtils.isEmpty(reportForm.getChild())) {
  3214. formSort(reportForm.getChild());
  3215. }
  3216. }
  3217. reportFormList.sort(Comparator.comparing(ReportForm::getSort));
  3218. }
  3219. private void responsibilitySort(List<Responsibility> responsibilityList) {
  3220. for (Responsibility responsibility : responsibilityList) {
  3221. if (!CollectionUtils.isEmpty(responsibility.getChildren())) {
  3222. responsibilitySort(responsibility.getChildren());
  3223. }
  3224. }
  3225. responsibilityList.sort(Comparator.comparing(Responsibility::getSort, Comparator.nullsLast(Integer::compareTo)));
  3226. }
  3227. private void commonResponsibilityReportVoSort(List<CommonResponsibilityReportVo> T) {
  3228. for (CommonResponsibilityReportVo responsibility : T) {
  3229. if (!CollectionUtils.isEmpty(responsibility.getChild())) {
  3230. commonResponsibilityReportVoSort(responsibility.getChild());
  3231. }
  3232. }
  3233. T.sort(Comparator.comparing(CommonResponsibilityReportVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  3234. }
  3235. private void costResponsibilityProfitVoSort(List<CostResponsibilityProfitVo> T) {
  3236. for (CostResponsibilityProfitVo responsibility : T) {
  3237. if (!CollectionUtils.isEmpty(responsibility.getChild())) {
  3238. costResponsibilityProfitVoSort(responsibility.getChild());
  3239. }
  3240. }
  3241. T.sort(Comparator.comparing(CostResponsibilityProfitVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  3242. }
  3243. @Override
  3244. public Object getBatchComputeProfitListByYear(String startYear, String endYear, String reportType, String responsibilityCode) {
  3245. Integer integerReportType = Integer.valueOf(reportType);
  3246. Long hospId = UserContext.getHospId();
  3247. List<BatchCostProfitResponse> responses = new ArrayList<>();
  3248. // 解析起止年份
  3249. int startY = Integer.parseInt(startYear);
  3250. int endY = Integer.parseInt(endYear);
  3251. if (startY > endY) {
  3252. throw new CostException(500, "起始年份不能大于结束年份");
  3253. }
  3254. // 获取报表配置
  3255. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  3256. .eq(ReportForm::getHospId, hospId)
  3257. .eq(ReportForm::getDeleteTime, NumberConstant.ZERO)
  3258. .eq(ReportForm::getReportType, integerReportType));
  3259. if (CollUtil.isEmpty(reportFormList)) {
  3260. throw new CostException(500, "损益表未找到");
  3261. }
  3262. Map<Long, ReportForm> reportMap = reportFormList.stream()
  3263. .collect(Collectors.toMap(ReportForm::getId, report -> report));
  3264. // 取非隐藏的报表项目ID
  3265. List<Long> activeReportIdList = reportFormList.stream()
  3266. .filter(report -> NumberConstant.ONE.equals(report.getHide()))
  3267. .map(ReportForm::getId)
  3268. .collect(Collectors.toList());
  3269. // 获取当前年份和上一年度的汇总数据(一次性查询)
  3270. List<CostProfitVo> summaryList = this.baseMapper.getAnnualSummaryByYearRange(startY, endY, hospId, integerReportType, responsibilityCode);
  3271. List<CostProfitVo> preYearSummaryList = this.baseMapper.getAnnualSummaryByYearRange(startY - 1, endY - 1, hospId, integerReportType, responsibilityCode);
  3272. // 按年份分组处理数据
  3273. Map<Integer, List<CostProfitVo>> currentYearMap = new HashMap<>();
  3274. Map<Integer, List<CostProfitVo>> preYearMap = new HashMap<>();
  3275. // 将结果按年份分组
  3276. if (!CollectionUtils.isEmpty(summaryList)) {
  3277. currentYearMap = summaryList.stream()
  3278. .collect(Collectors.groupingBy(CostProfitVo::getYear));
  3279. }
  3280. if (!CollectionUtils.isEmpty(preYearSummaryList)) {
  3281. preYearMap = preYearSummaryList.stream()
  3282. .collect(Collectors.groupingBy(CostProfitVo::getYear));
  3283. }
  3284. // 遍历每个年份进行数据处理
  3285. for (int year = startY; year <= endY; year++) {
  3286. int previousYear = year - 1;
  3287. List<CostProfitVo> currentYearList = currentYearMap.get(year);
  3288. List<CostProfitVo> previousYearList = preYearMap.get(previousYear);
  3289. if (CollectionUtils.isEmpty(currentYearList)) {
  3290. continue;
  3291. }
  3292. // 只处理非隐藏的项目
  3293. List<CostProfitVo> activeCostProfitVo = currentYearList.stream()
  3294. .filter(vo -> activeReportIdList.contains(vo.getReportId()))
  3295. .collect(Collectors.toList());
  3296. if (CollectionUtils.isEmpty(activeCostProfitVo)) {
  3297. continue;
  3298. }
  3299. // 创建映射以便快速查找上一年的数据
  3300. Map<Long, CostProfitVo> preYearCostProfitMap = new HashMap<>();
  3301. if (!CollectionUtils.isEmpty(previousYearList)) {
  3302. preYearCostProfitMap = previousYearList.stream()
  3303. .collect(Collectors.toMap(CostProfitVo::getReportId, costProfitVo -> costProfitVo));
  3304. }
  3305. // 计算完成率、环比、同比
  3306. for (CostProfitVo vo : activeCostProfitVo) {
  3307. // 设置同期数据
  3308. if (preYearCostProfitMap.containsKey(vo.getReportId())) {
  3309. vo.setSamePeriodAmount(preYearCostProfitMap.get(vo.getReportId()).getAmount());
  3310. } else {
  3311. vo.setSamePeriodAmount(BigDecimal.ZERO);
  3312. }
  3313. vo.setCompletionRate(calculateRate(vo.getAmount(), vo.getBudgetAmount()));
  3314. vo.setMomRate(calculateRate(vo.getAmount().subtract(vo.getPrevPeriodAmount()), vo.getPrevPeriodAmount()));
  3315. vo.setYoyRate(calculateRate(vo.getAmount().subtract(vo.getSamePeriodAmount()), vo.getSamePeriodAmount()));
  3316. // 设置报表相关属性
  3317. ReportForm reportForm = reportMap.get(vo.getReportId());
  3318. if (reportForm != null) {
  3319. vo.setDescription(reportForm.getDescription());
  3320. vo.setSort(reportForm.getSort());
  3321. vo.setPermil(reportForm.getPermil());
  3322. vo.setDataType(reportForm.getDataType());
  3323. vo.setDecimalPlace(reportForm.getDecimalPlace());
  3324. }
  3325. }
  3326. // 构建树状结构
  3327. Map<Long, List<CostProfitVo>> collect = activeCostProfitVo.stream()
  3328. .collect(Collectors.groupingBy(CostProfitVo::getReportParentId));
  3329. List<CostProfitVo> costProfitParentVos = collect.get(NumberConstant.ZERO_L);
  3330. collect.remove(NumberConstant.ZERO_L);
  3331. if (!CollectionUtils.isEmpty(costProfitParentVos)) {
  3332. costProfitParentVos.forEach(parentVo -> {
  3333. parentVo.setChildren(collect.get(parentVo.getReportId()));
  3334. });
  3335. costProfitParentVos.sort(Comparator.comparing(CostProfitVo::getSort, Comparator.nullsLast(Integer::compareTo)));
  3336. }
  3337. BatchCostProfitResponse response = new BatchCostProfitResponse();
  3338. response.setComputeDate(String.valueOf(year));
  3339. response.setProfitVoList(costProfitParentVos);
  3340. response.setAllowDrillDown(false);
  3341. responses.add(response);
  3342. }
  3343. return responses;
  3344. }
  3345. /**
  3346. * 安全地计算百分比:避免除零错误
  3347. */
  3348. private BigDecimal calculateRate(BigDecimal current, BigDecimal base) {
  3349. if (base == null || base.compareTo(BigDecimal.ZERO) == 0) {
  3350. return BigDecimal.ZERO;
  3351. }
  3352. BigDecimal rate = current.divide(base, 4, RoundingMode.HALF_UP);
  3353. return rate.setScale(4, RoundingMode.HALF_UP);
  3354. }
  3355. /**
  3356. * 计算科室损益
  3357. * @param computeDate
  3358. * @param hospId
  3359. * @param reportType
  3360. */
  3361. private void calcDeptProfit(String computeDate, Long hospId, String reportType) {
  3362. //获取科室损益计算所需的数据
  3363. ProfitCalculationDataVo profitCalculationData = getProfitCalculationData(computeDate, hospId, reportType);
  3364. //计算科室损益
  3365. List<CostDepartmentProfitVO> costDepartmentProfitVOS = calcDeptProfit(profitCalculationData);
  3366. // 删除这个年月的数据
  3367. deleteDeptProfit(profitCalculationData.getYear(),profitCalculationData.getMonth(),reportType );
  3368. // 添加数据
  3369. List<CostDepartmentProfit> costDepartmentProfits = BeanUtil.convertList(costDepartmentProfitVOS, CostDepartmentProfit.class);
  3370. //统一创建时间
  3371. long l = System.currentTimeMillis();
  3372. costDepartmentProfits.forEach(i -> {
  3373. i.setCreateTime(l);
  3374. });
  3375. //保存科室损益数据
  3376. this.saveBatch(costDepartmentProfits);
  3377. //处理科室损益的会计科目金额数据
  3378. List<CostDepartmentProfitAccount> costDepartmentProfitAccounts=new ArrayList<>();
  3379. for (CostDepartmentProfit deptProfit : costDepartmentProfits) {
  3380. deptProfit.getCostDepartmentProfitAccounts().forEach(account -> {
  3381. account.setDeptProfitId(deptProfit.getId());
  3382. account.setComputeDate(computeDate);
  3383. account.setHospId(hospId);
  3384. });
  3385. costDepartmentProfitAccounts.addAll(deptProfit.getCostDepartmentProfitAccounts());
  3386. }
  3387. //保存科室损益的会计科目金额数据
  3388. costDepartmentProfitAccountRepository.saveBatch(costDepartmentProfitAccounts);
  3389. //记录最后一次 损益计算日期
  3390. computeLastProfitDateRepository.saveLastComputeDate(hospId, computeDate);
  3391. }
  3392. /**
  3393. * 获取科室损益计算所需的数据
  3394. * @param computeDate
  3395. * @param hospId
  3396. * @param reportType
  3397. * @return
  3398. */
  3399. public ProfitCalculationDataVo getProfitCalculationData(String computeDate, Long hospId, String reportType) {
  3400. ProfitCalculationDataVo profitCalculationData = new ProfitCalculationDataVo();
  3401. Integer year = ComputeDateUtils.getComputeYear(computeDate);
  3402. Integer month = ComputeDateUtils.getComputeMonth(computeDate);
  3403. // 获取指定类型的报表配置
  3404. List<ReportForm> reportFormList = getReportForm(reportType);
  3405. if (CollUtil.isEmpty(reportFormList)) {
  3406. throw new CostException(500, "损益表未找到");
  3407. }
  3408. //获取报表设置明细
  3409. List<ReportRelation> reportRelation = getReportRelation();
  3410. Map<Long, List<ReportRelation>> reportRelationMap = reportRelation.stream().collect(Collectors.groupingBy(ReportRelation::getReportId));
  3411. // 查询最后一个层级的责任中心
  3412. List<CostShareLevel> costShareLevelList = getCostShareLevelList();
  3413. if (CollUtil.isEmpty(costShareLevelList)) {
  3414. throw new CostException(500, "分摊层级未设置");
  3415. }
  3416. //获取指定报表对应的责任中心
  3417. List<Responsibility> responsibilityList = getReportResponsibilityList(reportType);
  3418. // 获取归集后的收入数据
  3419. List<IncomeCollection> incomeList = getIncomeCollectionList(year,month);
  3420. if (CollUtil.isEmpty(incomeList)) {
  3421. throw new CostException(500, "归集后数据不存在");
  3422. }
  3423. // 获取分摊后数据
  3424. List<AllocationQuery> allocationQueryList = allocationQueryService.getAllByDate(hospId,year,month);
  3425. if (CollUtil.isEmpty(allocationQueryList)) {
  3426. throw new CostException(500, "分摊后数据不存在");
  3427. }
  3428. // 封装数据
  3429. List<AllocationQueryReportVO> allocationQueryReportVOList = BeanUtil.convertList(allocationQueryList, AllocationQueryReportVO.class);
  3430. allocationQueryReportVOList.forEach(i -> {
  3431. i.setAccountingCodes(Arrays.asList(i.getAccountingCode().split(StrUtil.COMMA)));
  3432. i.setAccountingNames(Arrays.asList(i.getAccountingName().split(StrUtil.COMMA)));
  3433. });
  3434. // 查询分摊的报表数据 后面的计算方式需要使用 小计等需要使用
  3435. List<Allocation> allocationList = allocationService.getByDate( year, month, hospId);
  3436. if (CollUtil.isEmpty(allocationList)) {
  3437. throw new CostException(500, "分摊报表数据不存在");
  3438. }
  3439. profitCalculationData.setComputeDate(computeDate);
  3440. profitCalculationData.setYear(year);
  3441. profitCalculationData.setMonth(month);
  3442. profitCalculationData.setHospId(hospId);
  3443. profitCalculationData.setReportType(reportType);
  3444. profitCalculationData.setReportFormList(reportFormList);
  3445. profitCalculationData.setReportRelationList(reportRelation);
  3446. profitCalculationData.setReportRelationMap(reportRelationMap);
  3447. profitCalculationData.setCostShareLevelList(costShareLevelList);
  3448. profitCalculationData.setResponsibilityList(responsibilityList);
  3449. return profitCalculationData;
  3450. }
  3451. /**
  3452. * 计算科室损益
  3453. * @param profitCalculationData
  3454. * @return
  3455. */
  3456. private List<CostDepartmentProfitVO> calcDeptProfit(ProfitCalculationDataVo profitCalculationData) {
  3457. List<ReportForm> firstLevelReports = profitCalculationData.getReportFormList().stream().filter(i -> i.getParentId().equals(NumberConstant.ZERO)).collect(Collectors.toList());
  3458. if(CollectionUtils.isEmpty(firstLevelReports)){
  3459. throw new CostException(500, "未找到第一层报表项目,请检查报表配置");
  3460. }
  3461. //按顺序号排序
  3462. firstLevelReports.sort(Comparator.comparing(ReportForm::getSort, Comparator.nullsLast(Integer::compareTo)));
  3463. Map<Long, List<ReportForm>> reportParentGroup = profitCalculationData.getReportFormList().stream().collect(Collectors.groupingBy(ReportForm::getParentId));
  3464. List<CostDepartmentProfitVO> costDepartmentProfitVOList = new ArrayList<>();
  3465. //循环设置子级报表项目
  3466. for (ReportForm reportForm : firstLevelReports) {
  3467. setChildReport(reportForm,reportParentGroup);
  3468. }
  3469. //按责任中心计算科室损益
  3470. for (Responsibility responsibility : profitCalculationData.getResponsibilityList()) {
  3471. Map<Integer,CostDepartmentProfitVO> costDepartmentProfitMap=new HashMap<>();
  3472. //计算责任中心的所有报表项目
  3473. for (ReportForm reportForm : firstLevelReports) {
  3474. calcRespReportAmount(responsibility, reportForm,profitCalculationData, costDepartmentProfitMap);
  3475. }
  3476. //添加到结果集合
  3477. costDepartmentProfitVOList.addAll(costDepartmentProfitMap.values());
  3478. }
  3479. return null;
  3480. }
  3481. /**
  3482. * 循环设置子级报表项目
  3483. * @param reportForm
  3484. * @param reportParentGroup
  3485. */
  3486. public void setChildReport(ReportForm reportForm ,Map<Long, List<ReportForm>> reportParentGroup){
  3487. List<ReportForm> childReportForms = reportParentGroup.get(reportForm.getId());
  3488. if(CollUtil.isEmpty(childReportForms)){
  3489. return;
  3490. }
  3491. reportForm.setChild(childReportForms);
  3492. for (ReportForm cildReportForm : childReportForms) {
  3493. setChildReport(cildReportForm,reportParentGroup);
  3494. }
  3495. }
  3496. /**
  3497. * 计算指定责任中心指定报表项目的金额(优先计算子级,再按顺序号计算非公式项,最后计算公式项)
  3498. * @param profitCalculationData
  3499. * @param costDepartmentProfitMap
  3500. * @param parentReport
  3501. * @param responsibility
  3502. */
  3503. public void calcRespReportAmount(Responsibility responsibility,
  3504. ReportForm parentReport ,
  3505. ProfitCalculationDataVo profitCalculationData,
  3506. Map<Integer,CostDepartmentProfitVO> costDepartmentProfitMap){
  3507. //有子级的优先计算子级
  3508. if(!CollUtil.isEmpty(parentReport.getChild())){
  3509. //计算报表项目的金额
  3510. for (ReportForm reportForm : parentReport.getChild()) {
  3511. //计算子级的金额
  3512. calcRespReportAmount(responsibility,reportForm,profitCalculationData,costDepartmentProfitMap);
  3513. }
  3514. //找出所有类型不是计算公式且不是不设置的报表项
  3515. List<ReportForm> calcReportList = parentReport.getChild().stream().filter(i ->!NumberConstant.ZERO.equals(i.getCalcType())&& !NumberConstant.THREE.equals(i.getCalcType())).collect(Collectors.toList());
  3516. //优先计算类型为非计算公式的报表项
  3517. if (!CollUtil.isEmpty(calcReportList)){
  3518. for (ReportForm reportForm : calcReportList) {
  3519. calcReportAmount(responsibility,reportForm,profitCalculationData,costDepartmentProfitMap);
  3520. }
  3521. }
  3522. //找出所有类型为计算公式的报表项
  3523. List<ReportForm> formulaReportList = parentReport.getChild().stream().filter(i -> NumberConstant.THREE.equals(i.getCalcType())).collect(Collectors.toList());
  3524. //再计算类型为计算公式的报表项
  3525. if (!CollUtil.isEmpty(formulaReportList)){
  3526. for (ReportForm reportForm : formulaReportList) {
  3527. calcReportAmount(responsibility,reportForm,profitCalculationData,costDepartmentProfitMap);
  3528. }
  3529. }
  3530. }else{
  3531. //没有子级项目的只要计算自己
  3532. calcReportAmount(responsibility,parentReport,profitCalculationData,costDepartmentProfitMap);
  3533. }
  3534. }
  3535. /**
  3536. * 计算一个报表项目的金额
  3537. * @param profitCalculationData
  3538. * @param costDepartmentProfitMap
  3539. * @param reportForm
  3540. */
  3541. public void calcReportAmount(Responsibility responsibility,ReportForm reportForm,ProfitCalculationDataVo profitCalculationData,Map<Integer,CostDepartmentProfitVO> costDepartmentProfitMap){
  3542. Integer calcType = reportForm.getCalcType();
  3543. CostDepartmentProfitVO costDepartmentProfitVO = createCostDepartmentProfitVO(responsibility,reportForm,profitCalculationData);
  3544. switch (calcType){
  3545. case 1:
  3546. if(NumberConstant.ONE.equals(reportForm.getCostType())){
  3547. //按收入会计科目计算
  3548. setIncomeAccountReportValue(costDepartmentProfitVO,profitCalculationData,responsibility,reportForm);
  3549. }else{
  3550. //按成本会计科目计算
  3551. setCostAccountReportValue(costDepartmentProfitVO,profitCalculationData, responsibility, reportForm);
  3552. }
  3553. break;
  3554. case 2:
  3555. //按分摊层级计算
  3556. setShareLevelReportValue(costDepartmentProfitVO,profitCalculationData,responsibility,reportForm);
  3557. break;
  3558. case 4:
  3559. //按公式计算
  3560. setFormulaReportValue(costDepartmentProfitVO,costDepartmentProfitMap,responsibility,reportForm);
  3561. break;
  3562. case 5:
  3563. //按责任中心计算
  3564. setResponsibilityReportValue(costDepartmentProfitVO,profitCalculationData,costDepartmentProfitMap,responsibility,reportForm);
  3565. break;
  3566. }
  3567. //记录下已计算好的报表项目
  3568. if(!costDepartmentProfitMap.containsKey(reportForm.getNum())){
  3569. costDepartmentProfitMap.put(reportForm.getNum(),costDepartmentProfitVO);
  3570. }
  3571. }
  3572. /**
  3573. * 创建一个科室损益计算结果对象
  3574. * @param responsibility
  3575. * @param reportForm
  3576. * @param profitCalculationData
  3577. * @return
  3578. */
  3579. public CostDepartmentProfitVO createCostDepartmentProfitVO(Responsibility responsibility,ReportForm reportForm,ProfitCalculationDataVo profitCalculationData){
  3580. CostDepartmentProfitVO costDepartmentProfitVO = new CostDepartmentProfitVO();
  3581. costDepartmentProfitVO.setYear(profitCalculationData.getYear());
  3582. costDepartmentProfitVO.setMonth(profitCalculationData.getMonth());
  3583. costDepartmentProfitVO.setReportId(reportForm.getId());
  3584. costDepartmentProfitVO.setReportNum(reportForm.getNum());
  3585. costDepartmentProfitVO.setCalcType(reportForm.getCalcType());
  3586. costDepartmentProfitVO.setReportName(reportForm.getReportName());
  3587. costDepartmentProfitVO.setCalcFormula(reportForm.getCalcFormula());
  3588. costDepartmentProfitVO.setReportParentId(reportForm.getParentId());
  3589. costDepartmentProfitVO.setResponsibilityCode(responsibility.getResponsibilityCode());
  3590. costDepartmentProfitVO.setResponsibilityName(responsibility.getResponsibilityName());
  3591. costDepartmentProfitVO.setCostType(NumberConstant.ONE);
  3592. costDepartmentProfitVO.setIncomeType(NumberConstant.ONE);
  3593. costDepartmentProfitVO.setHospId(UserContext.getHospId());
  3594. costDepartmentProfitVO.setShareType(Integer.valueOf(profitCalculationData.getReportType()));
  3595. costDepartmentProfitVO.setType(reportForm.getCostType());
  3596. costDepartmentProfitVO.setFraction(reportForm.getFraction());
  3597. costDepartmentProfitVO.setAmount(BigDecimal.ZERO);
  3598. costDepartmentProfitVO.setCostDepartmentProfitAccounts(new ArrayList<>());
  3599. return costDepartmentProfitVO;
  3600. }
  3601. /**
  3602. * 获取按责任中心计算结果
  3603. * @param profitCalculationData
  3604. * @param costDepartmentProfitMap
  3605. * @param reportForm
  3606. * @return
  3607. */
  3608. private void setResponsibilityReportValue(CostDepartmentProfitVO costDepartmentProfitVO, ProfitCalculationDataVo profitCalculationData, Map<Integer, CostDepartmentProfitVO> costDepartmentProfitMap, Responsibility responsibility, ReportForm reportForm) {
  3609. List<ReportRelation> reportRelationList = profitCalculationData.getReportRelationMap().get(reportForm.getId());
  3610. if(CollectionUtils.isEmpty(reportRelationList)){
  3611. return ;
  3612. }
  3613. // 获取对应的会计科目信息 筛选会计科目的Code
  3614. List<String> responsibilityCodeList = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  3615. //类型是收入时取分摊结果表单的数据计算
  3616. // 需要查询分摊后的表
  3617. List<AllocationQueryReportVO> reportVOList = profitCalculationData.getAllocationQueryReportVOList().stream().filter(m ->
  3618. m.getTargetResponsibilityCode().equals(responsibility.getResponsibilityCode())
  3619. &&NumberConstant.TWO.equals(m.getOriginType().intValue())
  3620. && responsibilityCodeList.contains(m.getResponsibilityCode())).collect(Collectors.toList());
  3621. if (CollUtil.isEmpty(reportVOList)) {
  3622. return ;
  3623. }
  3624. List<CostDepartmentProfitAccount> costDepartmentProfitAccounts = calculateCostByTypeAndAccount(reportVOList);
  3625. BigDecimal totalAmount = costDepartmentProfitAccounts.stream()
  3626. .map(CostDepartmentProfitAccount::getAmount)
  3627. .filter(Objects::nonNull)
  3628. .reduce(BigDecimal.ZERO, BigDecimal::add);
  3629. costDepartmentProfitVO.setAmount(totalAmount);
  3630. costDepartmentProfitVO.setCostDepartmentProfitAccounts(costDepartmentProfitAccounts);
  3631. }
  3632. /**
  3633. * 获取按公式计算结果
  3634. * @param responsibility
  3635. * @param costDepartmentProfitMap
  3636. * @param reportForm
  3637. * @return
  3638. */
  3639. private void setFormulaReportValue(CostDepartmentProfitVO costDepartmentProfitVO,Map<Integer, CostDepartmentProfitVO> costDepartmentProfitMap,Responsibility responsibility, ReportForm reportForm) {
  3640. // 获取当前报表的计算方式 [1]+[2]类型/ [1]-[2] [1]*[2] [1]/[2]
  3641. String formula = reportForm.getCalcFormula();
  3642. if (StrUtil.isBlank(formula)) {
  3643. costDepartmentProfitVO.setAmount(BigDecimal.ZERO);
  3644. costDepartmentProfitVO.setCostDepartmentProfitAccounts(new ArrayList<>());
  3645. return ;
  3646. }
  3647. String responsibilityCode = responsibility.getResponsibilityCode();
  3648. // 找出公式当中所有代码
  3649. String replace = formula.replace("[", "")
  3650. .replace("]", "")
  3651. .replace("-", ",")
  3652. .replace("+", ",")
  3653. .replace("*", ",")
  3654. .replace("/", ",");
  3655. ArrayList<String> codeList = CollUtil.newArrayList(replace.split(","));
  3656. Map<Integer, String> codeMap = new ConcurrentHashMap<>();
  3657. for (int j = 0; j < codeList.size(); j++) {
  3658. codeMap.put(j, codeList.get(j));
  3659. }
  3660. List<String> expressions = ReUtil.findAll("[^0-9]", "+" + formula.replace("[", "")
  3661. .replace("]", "").trim(), 0)
  3662. .stream().filter(StrUtil::isNotBlank).collect(Collectors.toList());
  3663. // 得到预算表达式 得到所有表达式的Map + - * / 相关的
  3664. Map<Integer, String> expressionMap = new ConcurrentHashMap<>();
  3665. for (int k = 0; k < expressions.size(); k++) {
  3666. expressionMap.put(k, expressions.get(k));
  3667. }
  3668. // 数字的索引和表达式的索引累加计算
  3669. Set<Integer> codeSet = codeMap.keySet();
  3670. List<Integer> codes = new ArrayList<>(codeSet);
  3671. AtomicReference<BigDecimal> totalAmount = new AtomicReference<>(BigDecimal.ZERO);
  3672. List<CostDepartmentProfitAccount> baseDeptProfitAccounts=new ArrayList<>();
  3673. for (int i = 0; i < codes.size(); i++) {
  3674. // 编号
  3675. String code = codeMap.get(i);
  3676. CostDepartmentProfitVO reportData = costDepartmentProfitMap.get(Integer.valueOf(code));
  3677. // 检查报表数据是否存在且属于当前责任中心
  3678. if (reportData == null || !responsibilityCode.equals(reportData.getResponsibilityCode())) {
  3679. continue;
  3680. }
  3681. BigDecimal amount = reportData.getAmount();
  3682. if (amount == null) {
  3683. amount = BigDecimal.ZERO;
  3684. }
  3685. String str = expressionMap.get(i);
  3686. if (str.equals("+")) {
  3687. baseDeptProfitAccounts=calculateAccountAmounts(baseDeptProfitAccounts,reportData.getCostDepartmentProfitAccounts(),NumberConstant.ONE);
  3688. totalAmount.set(totalAmount.get().add(amount));
  3689. } else if (str.contains("-")) {
  3690. baseDeptProfitAccounts=calculateAccountAmounts(baseDeptProfitAccounts,reportData.getCostDepartmentProfitAccounts(),NumberConstant.NEGATIVE);
  3691. totalAmount.set(totalAmount.get().subtract(amount));
  3692. } else if (str.contains("*")) {
  3693. totalAmount.set(totalAmount.get().multiply(amount));
  3694. } else if (str.contains("/")) {
  3695. if (amount.compareTo(BigDecimal.ZERO) == 0) {
  3696. log.error("报表项【" + reportForm.getReportName() + "】公式除数为0,报表编号为【" + code + "】");
  3697. totalAmount.set(BigDecimal.ZERO);
  3698. break;
  3699. } else {
  3700. totalAmount.set(totalAmount.get().divide(amount, 6, RoundingMode.HALF_UP));
  3701. }
  3702. }
  3703. }
  3704. //有乘除算法的只要赋值,不用设置会计科目金额
  3705. if(formula.contains("*")||formula.contains("/")){
  3706. costDepartmentProfitVO.setAmount(totalAmount.get());
  3707. costDepartmentProfitVO.setCostDepartmentProfitAccounts(new ArrayList<>());
  3708. }else{
  3709. costDepartmentProfitVO.setAmount(totalAmount.get());
  3710. costDepartmentProfitVO.setCostDepartmentProfitAccounts(baseDeptProfitAccounts);
  3711. }
  3712. }
  3713. /**
  3714. * 对两个CostDepartmentProfitVO对象的costDepartmentProfitAccounts列表进行加减运算
  3715. * 按照相同的costType和accountType进行合并计算,只处理amount字段
  3716. *
  3717. * @param firstList 第一个CostDepartmentProfitVO对象
  3718. * @param secondList 第二个CostDepartmentProfitVO对象
  3719. * @param operation 运算符,1表示加法,-1表示减法
  3720. * @return 运算结果列表
  3721. */
  3722. public List<CostDepartmentProfitAccount> calculateAccountAmounts(
  3723. List<CostDepartmentProfitAccount> firstList,
  3724. List<CostDepartmentProfitAccount> secondList,
  3725. int operation) {
  3726. // 处理空列表情况
  3727. firstList = firstList != null ?firstList : new ArrayList<>();
  3728. secondList = secondList != null ?secondList: new ArrayList<>();
  3729. // 创建结果列表,先复制第一个列表的所有元素
  3730. List<CostDepartmentProfitAccount> result = firstList.stream()
  3731. .map(account -> {
  3732. CostDepartmentProfitAccount copy = new CostDepartmentProfitAccount();
  3733. copy.setCostType(account.getCostType());
  3734. copy.setAccountType(account.getAccountType());
  3735. copy.setAmount(account.getAmount() != null ? account.getAmount() : BigDecimal.ZERO);
  3736. return copy;
  3737. })
  3738. .collect(Collectors.toList());
  3739. // 处理第二个列表中的每个元素
  3740. for (CostDepartmentProfitAccount secondAccount : secondList) {
  3741. if (secondAccount.getAmount() == null) {
  3742. continue; // 跳过金额为空的记录
  3743. }
  3744. // 查找是否在结果列表中已有相同costType和accountType的记录
  3745. boolean found = false;
  3746. for (CostDepartmentProfitAccount resultAccount : result) {
  3747. if (Objects.equals(resultAccount.getCostType(), secondAccount.getCostType()) &&
  3748. Objects.equals(resultAccount.getAccountType(), secondAccount.getAccountType())) {
  3749. // 找到匹配项,进行加减运算
  3750. BigDecimal firstAmount = resultAccount.getAmount() != null ? resultAccount.getAmount() : BigDecimal.ZERO;
  3751. BigDecimal secondAmount = secondAccount.getAmount() != null ? secondAccount.getAmount() : BigDecimal.ZERO;
  3752. if (operation == 1) {
  3753. resultAccount.setAmount(firstAmount.add(secondAmount));
  3754. } else if (operation == -1) {
  3755. resultAccount.setAmount(firstAmount.subtract(secondAmount));
  3756. }
  3757. found = true;
  3758. break;
  3759. }
  3760. }
  3761. // 如果未找到匹配项,则添加新记录
  3762. if (!found) {
  3763. CostDepartmentProfitAccount newAccount = new CostDepartmentProfitAccount();
  3764. newAccount.setCostType(secondAccount.getCostType());
  3765. newAccount.setAccountType(secondAccount.getAccountType());
  3766. // 加法直接添加金额,减法添加负金额
  3767. if (operation == 1) {
  3768. newAccount.setAmount(secondAccount.getAmount() != null ? secondAccount.getAmount() : BigDecimal.ZERO);
  3769. } else if (operation == -1) {
  3770. BigDecimal amount = secondAccount.getAmount() != null ? secondAccount.getAmount() : BigDecimal.ZERO;
  3771. newAccount.setAmount(amount.negate()); // 设置为负值
  3772. }
  3773. result.add(newAccount);
  3774. }
  3775. }
  3776. return result;
  3777. }
  3778. /**
  3779. * 获取按分摊层级计算结果
  3780. * @param profitCalculationData
  3781. * @param reportForm
  3782. * @return
  3783. */
  3784. private void setShareLevelReportValue(CostDepartmentProfitVO costDepartmentProfitVO, ProfitCalculationDataVo profitCalculationData, Responsibility responsibility, ReportForm reportForm) {
  3785. List<ReportRelation> reportRelationList = profitCalculationData.getReportRelationMap().get(reportForm.getId());
  3786. if(CollectionUtils.isEmpty(reportRelationList)){
  3787. return ;
  3788. }
  3789. // 获取对应的会计科目信息 筛选会计科目的Code
  3790. List<String> shareLevelIdList = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  3791. //类型是收入时取分摊结果表单的数据计算
  3792. // 需要查询分摊后的表
  3793. List<AllocationQueryReportVO> reportVOList = profitCalculationData.getAllocationQueryReportVOList().stream().filter(m ->
  3794. m.getTargetResponsibilityCode().equals(responsibility.getResponsibilityCode())
  3795. &&NumberConstant.TWO.equals(m.getOriginType().intValue())
  3796. && shareLevelIdList.contains(m.getShareLevelId())).collect(Collectors.toList());
  3797. if (CollUtil.isEmpty(reportVOList)) {
  3798. return ;
  3799. }
  3800. List<CostDepartmentProfitAccount> costDepartmentProfitAccounts = calculateCostByTypeAndAccount(reportVOList);
  3801. BigDecimal totalAmount = costDepartmentProfitAccounts.stream()
  3802. .map(CostDepartmentProfitAccount::getAmount)
  3803. .filter(Objects::nonNull)
  3804. .reduce(BigDecimal.ZERO, BigDecimal::add);
  3805. costDepartmentProfitVO.setAmount(totalAmount);
  3806. costDepartmentProfitVO.setCostDepartmentProfitAccounts(costDepartmentProfitAccounts);
  3807. }
  3808. /**
  3809. * 按收入会计科目计算
  3810. * @param costDepartmentProfitVO
  3811. * @param profitCalculationData
  3812. * @param responsibility
  3813. * @param reportForm
  3814. */
  3815. private void setIncomeAccountReportValue(CostDepartmentProfitVO costDepartmentProfitVO,ProfitCalculationDataVo profitCalculationData, Responsibility responsibility,ReportForm reportForm) {
  3816. List<ReportRelation> reportRelationList = profitCalculationData.getReportRelationMap().get(reportForm.getId());
  3817. if(CollectionUtils.isEmpty(reportRelationList)){
  3818. return ;
  3819. }
  3820. // 获取对应的会计科目信息 筛选会计科目的Code
  3821. List<String> accountList = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  3822. // 查找在归集数据里面当前责任中心对应的这些会计科目的金额
  3823. List<IncomeCollection> incomeCollectionList = profitCalculationData.getIncomeList().stream().filter(income ->
  3824. income.getResponsibilityCode().equals(responsibility.getResponsibilityCode())
  3825. && accountList.contains(income.getAccountingCode())).collect(Collectors.toList());
  3826. if(CollectionUtils.isEmpty(incomeCollectionList)){
  3827. return ;
  3828. }
  3829. BigDecimal totalAmount = incomeCollectionList.stream()
  3830. .map(IncomeCollection::getAmount)
  3831. .filter(Objects::nonNull)
  3832. .reduce(BigDecimal.ZERO, BigDecimal::add);
  3833. costDepartmentProfitVO.setAmount(totalAmount);
  3834. }
  3835. /**
  3836. *获取按成本会计科目计算的结果
  3837. * @param profitCalculationData
  3838. * @param responsibility
  3839. * @param reportForm
  3840. * @return
  3841. */
  3842. private void setCostAccountReportValue(CostDepartmentProfitVO costDepartmentProfitVO, ProfitCalculationDataVo profitCalculationData, Responsibility responsibility, ReportForm reportForm) {
  3843. List<ReportRelation> reportRelationList = profitCalculationData.getReportRelationMap().get(reportForm.getId());
  3844. if(CollectionUtils.isEmpty(reportRelationList)){
  3845. return ;
  3846. }
  3847. // 获取对应的会计科目信息 筛选会计科目的Code
  3848. List<String> accountList = reportRelationList.stream().map(ReportRelation::getRelationCode).collect(Collectors.toList());
  3849. //类型是收入时取分摊结果表单的数据计算
  3850. // 需要查询分摊后的表
  3851. List<AllocationQueryReportVO> reportVOList = profitCalculationData.getAllocationQueryReportVOList().stream().filter(m ->
  3852. m.getTargetResponsibilityCode().equals(responsibility.getResponsibilityCode())
  3853. &&(NumberConstant.ZERO.equals(reportForm.getCostType())||reportForm.getCostType().equals(m.getOriginType().intValue()))
  3854. &&accountList.contains(m.getAccountingCode())).collect(Collectors.toList());
  3855. if (CollUtil.isEmpty(reportVOList)) {
  3856. return ;
  3857. }
  3858. List<CostDepartmentProfitAccount> costDepartmentProfitAccounts = calculateCostByTypeAndAccount(reportVOList);
  3859. BigDecimal totalAmount = costDepartmentProfitAccounts.stream()
  3860. .map(CostDepartmentProfitAccount::getAmount)
  3861. .filter(Objects::nonNull)
  3862. .reduce(BigDecimal.ZERO, BigDecimal::add);
  3863. costDepartmentProfitVO.setAmount(totalAmount);
  3864. costDepartmentProfitVO.setCostDepartmentProfitAccounts(costDepartmentProfitAccounts);
  3865. }
  3866. /**
  3867. * 将分摊结果按会计科目类别+成本类别分组汇总
  3868. * @param reportVOList
  3869. * @return
  3870. */
  3871. public List<CostDepartmentProfitAccount> calculateCostByTypeAndAccount(List<AllocationQueryReportVO> reportVOList) {
  3872. // 按costType+accountType分组计算各组的金额amount
  3873. Map<String, BigDecimal> groupedAmounts = reportVOList.stream()
  3874. .collect(Collectors.groupingBy(
  3875. vo -> vo.getCostType() + "_" + vo.getAccountType(),
  3876. Collectors.reducing(
  3877. BigDecimal.ZERO,
  3878. AllocationQueryReportVO::getAmount,
  3879. BigDecimal::add
  3880. )
  3881. ));
  3882. // 按组生成CostDepartmentProfitAccount对象
  3883. return groupedAmounts.entrySet().stream()
  3884. .map(entry -> {
  3885. String[] keys = entry.getKey().split("_", 2);
  3886. CostDepartmentProfitAccount account = new CostDepartmentProfitAccount();
  3887. account.setCostType(keys[0]);
  3888. account.setAccountType(keys[1]);
  3889. account.setAmount(entry.getValue());
  3890. return account;
  3891. })
  3892. .collect(Collectors.toList());
  3893. }
  3894. /**
  3895. * 获取指定类型的报表配置
  3896. * @param reportType
  3897. * @return
  3898. */
  3899. public List<ReportForm> getReportForm(String reportType){
  3900. List<ReportForm> reportFormList = reportFormService.list(new QueryWrapper<ReportForm>().lambda()
  3901. .eq(ReportForm::getHospId, UserContext.getHospId())
  3902. .eq(ReportForm::getReportType, reportType));
  3903. return reportFormList;
  3904. }
  3905. /**
  3906. * 获取报表关系
  3907. * @return
  3908. */
  3909. public List<ReportRelation> getReportRelation(){
  3910. List<ReportRelation> list = reportRelationService.list(new QueryWrapper<ReportRelation>().lambda().eq(ReportRelation::getHospId, UserContext.getHospId()));
  3911. return list;
  3912. }
  3913. /**
  3914. * 获取指定报表对应的责任中心
  3915. * @param reportType
  3916. * @return
  3917. */
  3918. public List<Responsibility> getReportResponsibilityList(String reportType){
  3919. DictDataVo dataVo = getDictDataVo(reportType);
  3920. Long id = Long.valueOf(dataVo.getExpandOne());
  3921. List<Responsibility> list = responsibilityService.list(new QueryWrapper<Responsibility>().lambda()
  3922. .eq(Responsibility::getHospId, UserContext.getHospId())
  3923. .eq(Responsibility::getIsGatherCenter, 2)
  3924. .eq(Responsibility::getDeleteTime, 0)
  3925. .eq(Responsibility::getShareId, id));
  3926. return list;
  3927. }
  3928. /**
  3929. * 获取分摊层级字典
  3930. * @return
  3931. */
  3932. public List<CostShareLevel> getCostShareLevelList(){
  3933. List<CostShareLevel> costShareLevelList = costShareLevelService.list(new QueryWrapper<CostShareLevel>().lambda()
  3934. .eq(CostShareLevel::getHospId, UserContext.getHospId()).orderByDesc(CostShareLevel::getLeverSort));
  3935. return costShareLevelList;
  3936. }
  3937. /**
  3938. * 获取归集后的收入数据
  3939. * @param year
  3940. * @param month
  3941. * @return
  3942. */
  3943. public List<IncomeCollection> getIncomeCollectionList(Integer year, Integer month) {
  3944. List<IncomeCollection> list = incomeCollectionService.list(new QueryWrapper<IncomeCollection>().lambda()
  3945. .eq(IncomeCollection::getHospId, UserContext.getHospId())
  3946. .eq(year > 0, IncomeCollection::getYear, year).eq(month > 0, IncomeCollection::getMonth, month));
  3947. return list;
  3948. }
  3949. /**
  3950. * 删除指定月份的科室损益数据
  3951. * @param year
  3952. * @param month
  3953. * @param reportType
  3954. */
  3955. public void deleteDeptProfit(Integer year, Integer month,String reportType) {
  3956. // 删除这个年月的数据
  3957. this.remove(new QueryWrapper<CostDepartmentProfit>().lambda().eq(CostDepartmentProfit::getHospId, UserContext.getHospId())
  3958. .eq(CostDepartmentProfit::getYear, year).eq(CostDepartmentProfit::getMonth, month).eq(CostDepartmentProfit::getShareType, Integer.valueOf(reportType)));
  3959. }
  3960. }