智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_data_statisstics.go 110 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
6 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
6 months ago
8 months ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
8 months ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
1 year ago
8 months ago
7 months ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
8 months ago
7 months ago
8 months ago
7 months ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
7 months ago
8 months ago
8 months ago
8 months ago
8 months ago
1 year ago
8 months ago
8 months ago
8 months ago
1 year ago
6 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
1 year ago
6 months ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
5 months ago
9 months ago
5 months ago
9 months ago
5 months ago
9 months ago
5 months ago
9 months ago
5 months ago
9 months ago
5 months ago
9 months ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
9 months ago
9 months ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
8 months ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
3 months ago
1 year ago
3 months ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
10 months ago
1 year ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
8 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531
  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/enum"
  7. "applet/app/utils"
  8. "applet/app/utils/logx"
  9. "archive/zip"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/360EntSecGroup-Skylar/excelize"
  13. "github.com/syyongx/php2go"
  14. "io"
  15. "os"
  16. "strconv"
  17. "strings"
  18. "time"
  19. )
  20. func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
  21. var titleList []string
  22. var fileName string
  23. var now = time.Now()
  24. xlsx := excelize.NewFile()
  25. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  26. switch req.Kind {
  27. case 1:
  28. //订单列表
  29. fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  30. //查找订单数据
  31. sess := db.Db.Desc("nursing_home_package_ord.id")
  32. if req.EnterpriseId != 0 {
  33. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  34. }
  35. if req.StartDate != "" {
  36. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  37. }
  38. if req.EndDate != "" {
  39. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  40. }
  41. var m []*db.NursingHomePackageOrdWithUserIdentity
  42. _, err := sess.
  43. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  44. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  45. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  46. FindAndCount(&m)
  47. if err != nil {
  48. logx.Error(err)
  49. println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
  50. return
  51. }
  52. titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
  53. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  54. j := 2 //表头被第一行用了,只能从第二行开始
  55. for _, v := range m {
  56. var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
  57. switch v.NursingHomePackageOrd.Kind {
  58. case 1:
  59. kind = "按年购买"
  60. break
  61. case 2:
  62. kind = "按月购买"
  63. break
  64. case 3:
  65. kind = "按天购买"
  66. break
  67. case 4:
  68. kind = "补餐"
  69. break
  70. }
  71. var state string //0:待支付 1:支付成功 2:支付失败
  72. switch v.NursingHomePackageOrd.State {
  73. case enum.NursingHomePackageOrdStateForWait:
  74. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  75. break
  76. case enum.NursingHomePackageOrdStateForSuccess:
  77. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  78. break
  79. case enum.NursingHomePackageOrdStateForFail:
  80. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
  81. break
  82. }
  83. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
  84. v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
  85. j++
  86. }
  87. break
  88. case 2:
  89. //退款申请表
  90. fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  91. var m []*db.NursingHomeUserRefundDayWithData
  92. sess := db.Db.Desc("nursing_home_user_refund_day.id")
  93. if req.EnterpriseId != 0 {
  94. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  95. }
  96. if req.StartDate != "" {
  97. sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
  98. }
  99. if req.EndDate != "" {
  100. sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
  101. }
  102. _, err := sess.
  103. Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
  104. Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
  105. Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
  106. Join("LEFT", "user", "user_identity.uid = user.id").
  107. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  108. Desc("nursing_home_user_refund_day.id").
  109. FindAndCount(&m)
  110. if err != nil {
  111. logx.Error(err)
  112. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  113. return
  114. }
  115. titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
  116. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  117. j := 2 //表头被第一行用了,只能从第二行开始
  118. for _, v := range m {
  119. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  120. switch v.NursingHomeUserWithDay.Kind {
  121. case enum.NursingHomeUserWithDayKindForBreakfast:
  122. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  123. break
  124. case enum.NursingHomeUserWithDayKindForLunch:
  125. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  126. break
  127. case enum.NursingHomeUserWithDayKindForDinner:
  128. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  129. break
  130. }
  131. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  132. switch v.NursingHomeUserRefundDay.State {
  133. case enum.NursingHomeUserRefundDayStateForAuditing:
  134. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
  135. break
  136. case enum.NursingHomeUserRefundDayStateForAuditPass:
  137. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
  138. break
  139. case enum.NursingHomeUserRefundDayStateForAuditReject:
  140. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
  141. break
  142. case enum.NursingHomeUserRefundDayStateForAuditComplete:
  143. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
  144. break
  145. }
  146. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
  147. v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
  148. j++
  149. }
  150. xlsx.SaveAs("./static" + fileName + ".xlsx")
  151. break
  152. case 3:
  153. //预定统计表
  154. fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  155. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  156. sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
  157. if req.EnterpriseId != 0 {
  158. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  159. }
  160. if req.StartDate != "" {
  161. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  162. }
  163. if req.EndDate != "" {
  164. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  165. }
  166. _, err := sess.
  167. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  168. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  169. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  170. Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
  171. if err != nil {
  172. logx.Error(err)
  173. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  174. return
  175. }
  176. titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
  177. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  178. j := 2 //表头被第一行用了,只能从第二行开始
  179. for _, v := range m {
  180. var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
  181. switch v.NursingHomePackageOrdForReserve.Kind {
  182. case enum.NursingHomeUserWithDayKindForBreakfast:
  183. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  184. break
  185. case enum.NursingHomeUserWithDayKindForLunch:
  186. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  187. break
  188. case enum.NursingHomeUserWithDayKindForDinner:
  189. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  190. break
  191. }
  192. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
  193. v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
  194. j++
  195. }
  196. xlsx.SaveAs("./static" + fileName + ".xlsx")
  197. break
  198. }
  199. //新增数据
  200. nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
  201. nursingHomeExportRecordsDb.Set()
  202. marshal, _ := json.Marshal(req)
  203. nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
  204. Name: fileName,
  205. DownloadPath: fileName,
  206. Kind: req.Kind,
  207. ReqContent: string(marshal),
  208. CreateAt: now.Format("2006-01-02 15:04:05"),
  209. UpdateAt: now.Format("2006-01-02 15:04:05"),
  210. })
  211. }
  212. func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
  213. var titleList []string
  214. var fileName, downloadPath string
  215. var now = time.Now()
  216. switch req.Kind {
  217. case 1:
  218. //教师消费统计表
  219. xlsx, _ := excelize.OpenFile("./static/template/自营学校(教师消费统计表).xlsx")
  220. fileName = "教师消费统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  221. downloadPath = "/export/self_support_for_school/" + fileName
  222. //查询数据
  223. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  224. sess := db.Db.Desc("self_support_for_school_ord.id").Where("self_support_for_school_ord.order_status =?", enum.SelfSupportForSchoolOrdOrderStatusForSuccess)
  225. if req.EnterpriseId != 0 {
  226. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  227. }
  228. if req.StartDate != "" {
  229. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  230. }
  231. if req.EndDate != "" {
  232. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  233. }
  234. if req.EnterPriseKind != 0 {
  235. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  236. }
  237. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  238. _, err := sess.
  239. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  240. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  241. Join("LEFT", "user", "user.id = user_identity.uid").
  242. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  243. Join("LEFT", "class", "class_with_user.class_id = class.id").
  244. Join("LEFT", "grade", "class.grade_id = grade.id").
  245. FindAndCount(&m)
  246. if err != nil {
  247. logx.Error(err)
  248. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  249. return
  250. }
  251. //整合数据
  252. var data = map[string]map[string]map[string]map[string]interface{}{}
  253. for _, vv := range m {
  254. if data[vv.Enterprise.Name] == nil {
  255. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  256. }
  257. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  258. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  259. }
  260. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  261. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  262. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  263. }
  264. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  265. "id": vv.UserIdentity.IdNo,
  266. }
  267. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  268. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  269. }
  270. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  271. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  272. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  273. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  274. }
  275. xlsx.SetSheetRow("", "A1", &titleList)
  276. sheetIndex := 2 //第一个表是模板表
  277. j := 3 //表头前两行被占用了,只能从第三行开始
  278. for k, v := range data {
  279. xlsx.NewSheet(k) //1、新建表格
  280. xlsx.CopySheet(1, sheetIndex)
  281. xlsx.SetSheetRow(k, "A1", &[]interface{}{"教 师 消 费 统 计 表 " + "(" + req.StartDate + "至" + req.EndDate + ")"})
  282. var userTotal, breakFastTotal, lunchTotal, dinnerTotal, supperTotal, totalTotal int64
  283. var breakFastAmountTotal, lunchAmountTotal, dinnerAmountTotal, supperAmountTotal, totalAmountTotal float64
  284. for kk, vv := range v {
  285. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  286. vv["早餐"]["num"], vv["早餐"]["amount"],
  287. vv["午餐"]["num"], vv["午餐"]["amount"],
  288. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  289. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  290. vv["合计"]["num"], vv["合计"]["amount"],
  291. })
  292. userTotal++
  293. if vv["早餐"]["num"] != nil {
  294. breakFastTotal += vv["早餐"]["num"].(int64)
  295. }
  296. if vv["午餐"]["num"] != nil {
  297. lunchTotal += vv["午餐"]["num"].(int64)
  298. }
  299. if vv["晚餐"]["num"] != nil {
  300. dinnerTotal += vv["晚餐"]["num"].(int64)
  301. }
  302. if vv["夜宵"]["num"] != nil {
  303. supperTotal += vv["夜宵"]["num"].(int64)
  304. }
  305. if vv["合计"]["num"] != nil {
  306. totalTotal += vv["合计"]["num"].(int64)
  307. }
  308. if vv["早餐"]["amount"] != nil {
  309. breakFastAmountTotal += vv["早餐"]["amount"].(float64)
  310. }
  311. if vv["午餐"]["amount"] != nil {
  312. lunchAmountTotal += vv["午餐"]["amount"].(float64)
  313. }
  314. if vv["晚餐"]["amount"] != nil {
  315. dinnerAmountTotal += vv["晚餐"]["amount"].(float64)
  316. }
  317. if vv["夜宵"]["amount"] != nil {
  318. supperAmountTotal += vv["夜宵"]["amount"].(float64)
  319. }
  320. if vv["合计"]["amount"] != nil {
  321. totalAmountTotal += vv["合计"]["amount"].(float64)
  322. }
  323. j++
  324. }
  325. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{"总计", userTotal,
  326. breakFastTotal, breakFastAmountTotal,
  327. lunchTotal, lunchAmountTotal,
  328. dinnerTotal, dinnerAmountTotal,
  329. supperTotal, supperAmountTotal,
  330. totalTotal, totalAmountTotal,
  331. })
  332. sheetIndex++
  333. }
  334. xlsx.DeleteSheet("template") //删除模板表
  335. //将文件保存至服务器
  336. xlsx.SaveAs("./static" + downloadPath)
  337. break
  338. case 2:
  339. //商家分餐数据统计表
  340. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  341. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  342. downloadPath = "/export/self_support_for_school/" + fileName
  343. //查询出所有的商家
  344. var merchantList []*model.Merchant
  345. err := db.Db.Where("enterprise_id != 0").OrderBy("CONVERT(name USING gbk) ASC").Find(&merchantList)
  346. if err != nil {
  347. logx.Error(err)
  348. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  349. return
  350. }
  351. //查询出所有的设备
  352. var deviceList []*model.Device
  353. err = db.Db.Where("enterprise_id != 0").OrderBy("CONVERT(name USING gbk) ASC").Find(&deviceList)
  354. if err != nil {
  355. logx.Error(err)
  356. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  357. return
  358. }
  359. var merchantListMapping = map[int]model.Merchant{}
  360. var merchantDeviceListMapping = map[int]map[string]model.Device{}
  361. var deviceListMapping = map[string]model.Device{}
  362. var deviceNameArr []string
  363. var isHasDeviceName = map[string]bool{}
  364. for _, v := range deviceList {
  365. if merchantDeviceListMapping[v.MerchantId] == nil {
  366. merchantDeviceListMapping[v.MerchantId] = map[string]model.Device{}
  367. }
  368. merchantDeviceListMapping[v.MerchantId][v.DeviceSn] = *v
  369. deviceListMapping[v.DeviceSn] = *v
  370. if !isHasDeviceName[v.Name] {
  371. deviceNameArr = append(deviceNameArr, v.Name)
  372. isHasDeviceName[v.Name] = true
  373. }
  374. }
  375. for _, v := range merchantList {
  376. merchantListMapping[v.Id] = *v
  377. }
  378. var m []*db.SelfSupportForSchoolOrdWithUserIdentityV2
  379. eg := db.Db
  380. sess := eg.Desc("self_support_for_school_ord.id").Where("self_support_for_school_ord.order_status =?", enum.SelfSupportForSchoolOrdOrderStatusForSuccess)
  381. if req.EnterpriseId != 0 {
  382. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  383. }
  384. if req.StartDate != "" {
  385. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  386. }
  387. if req.EndDate != "" {
  388. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  389. }
  390. if req.EnterPriseKind != 0 {
  391. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  392. }
  393. _, err = sess.
  394. Join("LEFT", "enterprise", "self_support_for_school_ord.enterprise_id = enterprise.id").
  395. FindAndCount(&m)
  396. if err != nil {
  397. logx.Error(err)
  398. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  399. return
  400. }
  401. //整合数据
  402. var data = map[string]map[string]map[string]map[string]map[string]float64{}
  403. for _, vv := range m {
  404. device := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn]
  405. ordCreateAt, _ := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.CreateAt, time.Local)
  406. merchantCreateAt, _ := time.ParseInLocation("2006-01-02 15:04:05", merchantListMapping[device.MerchantId].CreateAt, time.Local)
  407. shopName := merchantListMapping[device.MerchantId].Name
  408. if ordCreateAt.Before(merchantCreateAt) {
  409. shopName = "设备已更换商户"
  410. }
  411. deviceName := device.Name
  412. if data[vv.Enterprise.Name] == nil {
  413. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]map[string]float64)
  414. }
  415. if data[vv.Enterprise.Name][shopName] == nil {
  416. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]map[string]float64)
  417. }
  418. if data[vv.Enterprise.Name][shopName][deviceName] == nil {
  419. data[vv.Enterprise.Name][shopName][deviceName] = make(map[string]map[string]float64)
  420. }
  421. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  422. if data[vv.Enterprise.Name][shopName][deviceName][mealZh] == nil {
  423. data[vv.Enterprise.Name][shopName][deviceName][mealZh] = make(map[string]float64)
  424. }
  425. if data[vv.Enterprise.Name][shopName][deviceName]["合计"] == nil {
  426. data[vv.Enterprise.Name][shopName][deviceName]["合计"] = make(map[string]float64)
  427. }
  428. data[vv.Enterprise.Name][shopName][deviceName][mealZh]["num"]++
  429. data[vv.Enterprise.Name][shopName][deviceName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  430. data[vv.Enterprise.Name][shopName][deviceName]["合计"]["num"]++
  431. data[vv.Enterprise.Name][shopName][deviceName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  432. }
  433. xlsx.SetSheetRow("", "A1", &titleList)
  434. sheetIndex := 2 //第一个表是模板表
  435. for k, v := range data {
  436. if k == "" {
  437. k = "未知"
  438. }
  439. xlsx.NewSheet(k) //1、新建表格
  440. xlsx.CopySheet(1, sheetIndex)
  441. xlsx.SetSheetRow(k, "A1", &[]interface{}{"商 家 分 餐 数 据 统 计 表 " + "(" + req.StartDate + "至" + req.EndDate + ")"})
  442. j := 4 //表头前三行被占用了,只能从第四行开始
  443. for kk, vv := range v {
  444. var startRow = 0
  445. var endRow = 0
  446. var breakFastTotal, lunchTotal, dinnerTotal, supperTotal, totalTotal float64
  447. var breakFastAmountTotal, lunchAmountTotal, dinnerAmountTotal, supperAmountTotal, totalAmountTotal float64
  448. for _, device := range deviceNameArr {
  449. for kkk, vvv := range vv {
  450. if kkk == "" {
  451. kkk = k
  452. }
  453. if device == kkk {
  454. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk, kkk,
  455. vvv["早餐"]["num"], vvv["早餐"]["amount"],
  456. vvv["午餐"]["num"], vvv["午餐"]["amount"],
  457. vvv["晚餐"]["num"], vvv["晚餐"]["amount"],
  458. vvv["夜宵"]["num"], vvv["夜宵"]["amount"],
  459. vvv["合计"]["num"], vvv["合计"]["amount"],
  460. })
  461. breakFastTotal += vvv["早餐"]["num"]
  462. lunchTotal += vvv["午餐"]["num"]
  463. dinnerTotal += vvv["晚餐"]["num"]
  464. supperTotal += vvv["夜宵"]["num"]
  465. totalTotal += vvv["合计"]["num"]
  466. breakFastAmountTotal += vvv["早餐"]["amount"]
  467. lunchAmountTotal += vvv["午餐"]["amount"]
  468. dinnerAmountTotal += vvv["晚餐"]["amount"]
  469. supperAmountTotal += vvv["夜宵"]["amount"]
  470. totalAmountTotal += vvv["合计"]["amount"]
  471. if startRow == 0 {
  472. startRow = j
  473. }
  474. endRow = j
  475. j++
  476. }
  477. }
  478. }
  479. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{"", "合计",
  480. breakFastTotal, breakFastAmountTotal,
  481. lunchTotal, lunchAmountTotal,
  482. dinnerTotal, dinnerAmountTotal,
  483. supperTotal, supperAmountTotal,
  484. totalTotal, totalAmountTotal,
  485. })
  486. if startRow == 0 {
  487. startRow = j
  488. }
  489. endRow = j
  490. j++
  491. style, err := xlsx.NewStyle(`{"alignment":{"horizontal":"center", "vertical": "center"}}`)
  492. if err != nil {
  493. logx.Error(err)
  494. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  495. panic(err)
  496. }
  497. xlsx.MergeCell(k, "A"+utils.IntToStr(startRow), "A"+utils.IntToStr(endRow)) //合并单元格
  498. xlsx.SetCellStyle(k, "A"+utils.IntToStr(startRow), "A"+utils.IntToStr(endRow), style)
  499. }
  500. sheetIndex++
  501. }
  502. xlsx.DeleteSheet("template") //删除模板表
  503. //将文件保存至服务器
  504. xlsx.SaveAs("./static" + downloadPath)
  505. break
  506. case 3:
  507. //就餐数据统计表
  508. xlsx, _ := excelize.OpenFile("./static/template/自营学校(就餐数据统计表).xlsx")
  509. fileName = "就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  510. downloadPath = "/export/self_support_for_school/" + fileName
  511. //查询出所有的设备
  512. //var deviceList []*model.Device
  513. //err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  514. //if err != nil {
  515. // logx.Error(err)
  516. // println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  517. // return
  518. //}
  519. //var deviceListMapping = map[string]model.Device{}
  520. //for _, v := range deviceList {
  521. // deviceListMapping[v.DeviceSn] = *v
  522. //}
  523. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  524. sess := db.Db.Desc("self_support_for_school_ord.id").Where("self_support_for_school_ord.order_status =?", enum.SelfSupportForSchoolOrdOrderStatusForSuccess)
  525. if req.EnterpriseId != 0 {
  526. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  527. }
  528. if req.StartDate != "" {
  529. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  530. }
  531. if req.EndDate != "" {
  532. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  533. }
  534. if req.EnterPriseKind != 0 {
  535. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  536. }
  537. _, err := sess.
  538. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  539. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  540. Join("LEFT", "user", "user.id = user_identity.uid").
  541. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  542. Join("LEFT", "class", "class_with_user.class_id = class.id").
  543. Join("LEFT", "grade", "class.grade_id = grade.id").
  544. FindAndCount(&m)
  545. if err != nil {
  546. logx.Error(err)
  547. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  548. return
  549. }
  550. //整合数据
  551. var data = map[string]map[string]map[string]map[string]float64{}
  552. date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  553. if err != nil {
  554. logx.Error(err)
  555. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  556. return
  557. }
  558. date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  559. if err != nil {
  560. logx.Error(err)
  561. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  562. return
  563. }
  564. diffDays := int(date2.Sub(date1).Hours() / 24)
  565. var dates []string
  566. for i := 0; i <= diffDays; i++ {
  567. date := date1.AddDate(0, 0, i)
  568. shopName := date.Format("2006-01-02")
  569. dates = append(dates, shopName)
  570. for _, vv := range m {
  571. tmpDate, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
  572. if err1 != nil {
  573. logx.Error(err1)
  574. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  575. return
  576. }
  577. fmt.Println(tmpDate.Format("2006-01-02"))
  578. if shopName == tmpDate.Format("2006-01-02") {
  579. if data[vv.Enterprise.Name] == nil {
  580. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  581. }
  582. if data[vv.Enterprise.Name][shopName] == nil {
  583. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  584. }
  585. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  586. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  587. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  588. }
  589. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  590. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  591. }
  592. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  593. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  594. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  595. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  596. }
  597. }
  598. }
  599. xlsx.SetSheetRow("", "A1", &titleList)
  600. sheetIndex := 2 //第一个表是模板表
  601. for k, v := range data {
  602. if k == "" {
  603. k = "未知"
  604. }
  605. xlsx.NewSheet(k) //1、新建表格
  606. xlsx.CopySheet(1, sheetIndex)
  607. j := 4 //表头前三行被占用了,只能从第四行开始
  608. for _, date := range dates {
  609. for kk, vv := range v {
  610. if date == kk {
  611. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  612. vv["早餐"]["num"], vv["早餐"]["amount"],
  613. vv["午餐"]["num"], vv["午餐"]["amount"],
  614. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  615. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  616. vv["合计"]["num"], vv["合计"]["amount"],
  617. })
  618. j++
  619. }
  620. sheetIndex++
  621. }
  622. }
  623. }
  624. xlsx.DeleteSheet("template") //删除模板表
  625. //将文件保存至服务器
  626. xlsx.SaveAs("./static" + downloadPath)
  627. break
  628. case 4:
  629. //设备就餐数据统计表
  630. fileName = "设备就餐数据统计_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  631. downloadPath = "/export/self_support_for_school/" + fileName
  632. //查询出所有的设备
  633. var deviceList []*model.Device
  634. if req.EnterpriseId == 0 {
  635. err := db.Db.Where("enterprise_id != 0").OrderBy("CONVERT(name USING gbk) ASC").Find(&deviceList)
  636. if err != nil {
  637. logx.Error(err)
  638. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>1111Error:::", err.Error())
  639. return
  640. }
  641. } else {
  642. err := db.Db.Where("enterprise_id = ?", req.EnterpriseId).OrderBy("CONVERT(name USING gbk) ASC").Find(&deviceList)
  643. if err != nil {
  644. logx.Error(err)
  645. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>1111Error:::", err.Error())
  646. return
  647. }
  648. }
  649. date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  650. if err != nil {
  651. logx.Error(err)
  652. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  653. return
  654. }
  655. date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  656. if err != nil {
  657. logx.Error(err)
  658. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  659. return
  660. }
  661. diffDays := int(date2.Sub(date1).Hours() / 24)
  662. var deviceListMapping = map[string]model.Device{}
  663. //整合数据
  664. var data = map[string]map[string]map[string]map[string]float64{}
  665. var xlsFileName, xlsDownloadPath []string
  666. for _, v := range deviceList {
  667. deviceListMapping[v.DeviceSn] = *v
  668. var m []*db.SelfSupportForSchoolOrdWithUserIdentityV2
  669. sess := db.Db.Where("device_sn = ?", v.DeviceSn).And("self_support_for_school_ord.order_status =?", enum.SelfSupportForSchoolOrdOrderStatusForSuccess).And("enterprise_id =?", req.EnterpriseId).Desc("self_support_for_school_ord.id")
  670. if req.EnterpriseId != 0 {
  671. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  672. }
  673. if req.StartDate != "" {
  674. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  675. }
  676. if req.EndDate != "" {
  677. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  678. }
  679. _, err = sess.Join("LEFT", "enterprise", "self_support_for_school_ord.enterprise_id = enterprise.id").FindAndCount(&m)
  680. if err != nil {
  681. logx.Error(err)
  682. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  683. return
  684. }
  685. for _, vv := range m {
  686. tmpTime, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
  687. if err1 != nil {
  688. logx.Error(err1)
  689. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  690. return
  691. }
  692. tmpDate := tmpTime.Format("2006-01-02")
  693. if data[vv.Enterprise.Name] == nil {
  694. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  695. }
  696. if data[vv.Enterprise.Name][v.Name] == nil {
  697. data[vv.Enterprise.Name][v.Name] = make(map[string]map[string]float64)
  698. }
  699. if data[vv.Enterprise.Name][v.Name][tmpDate] == nil {
  700. data[vv.Enterprise.Name][v.Name][tmpDate] = make(map[string]float64)
  701. }
  702. data[vv.Enterprise.Name][v.Name][tmpDate]["num"]++
  703. data[vv.Enterprise.Name][v.Name][tmpDate]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  704. }
  705. }
  706. for k1, v := range data {
  707. tmpFileName := k1 + "设备就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  708. tmpDownloadPath := "./static/export/self_support_for_school/" + tmpFileName
  709. xlsx := excelize.NewFile()
  710. var totalData = map[string]map[string]float64{}
  711. for _, device := range deviceList {
  712. for k2, vv := range v {
  713. if k2 == device.Name {
  714. var sumNum, sumAmount = float64(0), float64(0)
  715. xlsx.NewSheet(k2)
  716. titleList = []string{" 设 备 分 餐 数 据 统 计 表 "}
  717. xlsx.SetSheetRow(k2, "A1", &titleList)
  718. titleList = []string{"日 期", "消费笔数", "消费金额"}
  719. xlsx.SetSheetRow(k2, "A2", &titleList)
  720. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  721. if err != nil {
  722. logx.Error(err)
  723. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  724. panic(err)
  725. }
  726. xlsx.MergeCell(k2, "A1", "C1") //合并单元格
  727. xlsx.SetCellStyle(k2, "A1", "C1", style)
  728. xlsx.SetColWidth(k2, "A", "C", 20)
  729. j := 3 //表头前两行用了,只能从第三行开始
  730. for i := 0; i <= diffDays; i++ {
  731. date := date1.AddDate(0, 0, i).Format("2006-01-02")
  732. if vv[date] != nil {
  733. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, vv[date]["num"], vv[date]["amount"]})
  734. sumNum += vv[date]["num"]
  735. sumAmount += vv[date]["amount"]
  736. } else {
  737. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, 0, 0})
  738. }
  739. j++
  740. }
  741. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{
  742. "合计", sumNum, sumAmount})
  743. if totalData[k2] == nil {
  744. totalData[k2] = map[string]float64{}
  745. }
  746. totalData[k2]["num"] = sumNum
  747. totalData[k2]["amount"] = sumAmount
  748. }
  749. }
  750. }
  751. //xlsx.NewSheet("所有档口")
  752. xlsx.SetSheetName("Sheet1", "所有档口")
  753. titleList = []string{" 设 备 分 餐 数 据 统 计 表 "}
  754. xlsx.SetSheetRow("所有档口", "A1", &titleList)
  755. titleList = []string{"名 称", "消费笔数", "消费金额"}
  756. xlsx.SetSheetRow("所有档口", "A2", &titleList)
  757. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  758. if err != nil {
  759. logx.Error(err)
  760. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  761. panic(err)
  762. }
  763. xlsx.MergeCell("所有档口", "A1", "C1") //合并单元格
  764. xlsx.SetCellStyle("所有档口", "A1", "C1", style)
  765. xlsx.SetColWidth("所有档口", "A", "C", 20)
  766. j := 3 //表头前两行用了,只能从第三行开始
  767. var totalNum, totalAmount float64
  768. for _, device := range deviceList {
  769. for key, val := range totalData {
  770. if key == device.Name {
  771. totalNum += val["num"]
  772. totalAmount += val["amount"]
  773. xlsx.SetSheetRow("所有档口", "A"+strconv.Itoa(j), &[]interface{}{key, val["num"], val["amount"]})
  774. j++
  775. }
  776. }
  777. }
  778. xlsx.SetSheetRow("所有档口", "A"+strconv.Itoa(j), &[]interface{}{"合计", totalNum, totalAmount})
  779. //xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  780. //将文件保存至服务器
  781. err1 := xlsx.SaveAs(tmpDownloadPath)
  782. if err1 != nil {
  783. logx.Error(err1)
  784. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  785. panic(err1)
  786. }
  787. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  788. xlsFileName = append(xlsFileName, tmpFileName)
  789. }
  790. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  791. if err != nil {
  792. logx.Error(err)
  793. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  794. panic(err)
  795. }
  796. defer archive.Close()
  797. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  798. for k, v := range xlsDownloadPath {
  799. w, err1 := zipWriter.Create("设备就餐数据统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  800. if err1 != nil {
  801. logx.Error(err1)
  802. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  803. panic(err1)
  804. }
  805. f, err1 := os.Open(v) //打开待压缩的文件
  806. if err1 != nil {
  807. logx.Error(err1)
  808. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  809. panic(err1)
  810. }
  811. if _, err1 = io.Copy(w, f); err != nil {
  812. logx.Error(err1)
  813. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  814. panic(err1)
  815. }
  816. if err1 != nil {
  817. logx.Error(err1)
  818. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  819. panic(err1)
  820. }
  821. f.Close()
  822. os.RemoveAll(v) //TODO::移除源文件
  823. }
  824. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  825. if err != nil {
  826. panic(err)
  827. }
  828. break
  829. }
  830. //新增数据
  831. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  832. selfSupportForSchoolExportRecordsDb.Set()
  833. marshal, _ := json.Marshal(req)
  834. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  835. Name: fileName,
  836. DownloadPath: downloadPath,
  837. Kind: req.Kind,
  838. ReqContent: string(marshal),
  839. CreateAt: now.Format("2006-01-02 15:04:05"),
  840. UpdateAt: now.Format("2006-01-02 15:04:05"),
  841. })
  842. }
  843. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  844. var titleList []string
  845. var fileName, downloadPath string
  846. var now = time.Now()
  847. for _, kind := range req.Kinds {
  848. switch kind {
  849. case 1:
  850. xlsx := excelize.NewFile()
  851. //订单列表
  852. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  853. downloadPath = "/export/central_kitchen_for_school/" + fileName
  854. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  855. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  856. //查找订单数据
  857. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  858. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  859. if req.EnterpriseId != 0 {
  860. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  861. }
  862. if req.StartDate != "" {
  863. sess.And("central_kitchen_for_school_package_ord.pay_at >= ?", req.StartDate)
  864. }
  865. if req.EndDate != "" {
  866. sess.And("central_kitchen_for_school_package_ord.pay_at <= ?", req.EndDate)
  867. }
  868. if req.EnterPriseKind != 0 {
  869. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  870. }
  871. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  872. _, err := sess.
  873. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  874. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  875. Join("LEFT", "user", "user.id = user_identity.uid").
  876. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  877. Join("LEFT", "class", "class_with_user.class_id = class.id").
  878. Join("LEFT", "grade", "class.grade_id = grade.id").
  879. FindAndCount(&m)
  880. if err != nil {
  881. logx.Error(err)
  882. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  883. return
  884. }
  885. //查找预定数据
  886. var outTradeNoList []string
  887. for _, v := range m {
  888. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  889. }
  890. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  891. sess1 := db.Db.Desc("id")
  892. if req.EnterpriseId != 0 {
  893. sess1.And("enterprise_id = ?", req.EnterpriseId)
  894. }
  895. if len(req.MealKindList) > 0 {
  896. sess1.In("kind", req.MealKindList)
  897. }
  898. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  899. if err != nil {
  900. logx.Error(err)
  901. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  902. return
  903. }
  904. var exportOutTradeNoList []string
  905. for _, v := range mm {
  906. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  907. }
  908. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  909. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  910. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定餐数", "订单价格", "支付时间", "订单状态"}
  911. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  912. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  913. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  914. if err != nil {
  915. logx.Error(err)
  916. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  917. panic(err)
  918. }
  919. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  920. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  921. j := 3 //表头前两行用了,只能从第三行开始
  922. for _, v := range m {
  923. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  924. continue
  925. }
  926. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  927. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  928. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  929. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  930. break
  931. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  932. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  933. break
  934. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  935. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  936. break
  937. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  938. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  939. break
  940. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  941. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  942. break
  943. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  944. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  945. break
  946. }
  947. //预定餐数、预定日期
  948. var list []*model.CentralKitchenForSchoolUserWithDay
  949. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  950. if err1 != nil {
  951. logx.Error(err)
  952. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  953. return
  954. }
  955. var reserveDays string
  956. for _, v1 := range list {
  957. reserveDays += v1.Date + ", "
  958. }
  959. if v.Grade.Name == "" {
  960. v.Grade.Name = "教师"
  961. }
  962. if v.Class.Name == "" {
  963. v.Class.Name = "教师"
  964. }
  965. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  966. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  967. j++
  968. }
  969. //将文件保存至服务器
  970. xlsx.SaveAs("./static" + downloadPath)
  971. break
  972. case 2:
  973. xlsx := excelize.NewFile()
  974. //退款申请表
  975. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  976. downloadPath = "/export/central_kitchen_for_school/" + fileName
  977. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  978. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  979. //查找退款申请数据
  980. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  981. if req.EnterpriseId != 0 {
  982. sess.And("enterprise.id =?", req.EnterpriseId)
  983. }
  984. if req.StartDate != "" {
  985. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  986. }
  987. if req.EndDate != "" {
  988. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  989. }
  990. if len(req.MealKindList) > 0 {
  991. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  992. }
  993. if req.EnterPriseKind != 0 {
  994. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  995. }
  996. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  997. _, err := sess.
  998. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  999. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1000. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1001. Join("LEFT", "user", "user_identity.uid = user.id").
  1002. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1003. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1004. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1005. Join("LEFT", "grade", "class.grade_id = grade.id").
  1006. Desc("central_kitchen_for_school_user_refund_day.id").
  1007. FindAndCount(&m)
  1008. if err != nil {
  1009. logx.Error(err)
  1010. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  1011. return
  1012. }
  1013. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  1014. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1015. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  1016. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1017. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  1018. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1019. if err != nil {
  1020. logx.Error(err)
  1021. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1022. panic(err)
  1023. }
  1024. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  1025. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  1026. j := 3 //表头前两行用了,只能从第三行开始
  1027. for _, v := range m {
  1028. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  1029. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  1030. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  1031. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  1032. break
  1033. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  1034. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  1035. break
  1036. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  1037. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  1038. break
  1039. }
  1040. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  1041. switch v.CentralKitchenForSchoolUserRefundDay.State {
  1042. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  1043. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  1044. break
  1045. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  1046. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  1047. break
  1048. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  1049. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  1050. break
  1051. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  1052. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  1053. break
  1054. }
  1055. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  1056. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  1057. j++
  1058. }
  1059. //将文件保存至服务器
  1060. xlsx.SaveAs("./static" + downloadPath)
  1061. break
  1062. case 3:
  1063. //学校预定统计表
  1064. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1065. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1066. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1067. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1068. if req.EnterpriseId != 0 {
  1069. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1070. }
  1071. var months []time.Month
  1072. if req.StartDate != "" {
  1073. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1074. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1075. _, sMonth, _ := sDate.Date()
  1076. _, eMonth, _ := eDate.Date()
  1077. diffMonth := sMonth - eMonth
  1078. if diffMonth == 0 {
  1079. //TODO::查询一个月
  1080. months = append(months, sMonth)
  1081. } else {
  1082. for i := sMonth; i <= eMonth; i++ {
  1083. months = append(months, i)
  1084. }
  1085. }
  1086. }
  1087. if len(months) > 0 {
  1088. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1089. }
  1090. //if req.EndDate != "" {
  1091. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1092. //}
  1093. if len(req.MealKindList) > 0 {
  1094. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1095. }
  1096. if req.EnterPriseKind != 0 {
  1097. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1098. }
  1099. _, err := sess.
  1100. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1101. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  1102. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1103. Join("LEFT", "user", "user.id = user_identity.uid").
  1104. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1105. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1106. Join("LEFT", "grade", "class.grade_id = grade.id").
  1107. FindAndCount(&m)
  1108. if err != nil {
  1109. logx.Error(err)
  1110. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1111. return
  1112. }
  1113. var ordNoArr, mealKindListArr []string
  1114. for _, v := range m {
  1115. ordNoArr = append(ordNoArr, v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo)
  1116. }
  1117. var nativeSql = "SELECT COUNT(*) AS total, kind , ord_no FROM `central_kitchen_for_school_user_with_day` WHERE `ord_no` IN (%s) AND (state !=%d) AND (state !=%d) AND (DATE >= '%s') AND (DATE <= '%s') AND `kind` IN (%s) GROUP BY `ord_no`, `kind`"
  1118. for _, v := range req.MealKindList {
  1119. mealKindListArr = append(mealKindListArr, utils.IntToStr(v))
  1120. }
  1121. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1122. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1123. nativeSql = fmt.Sprintf(nativeSql, strings.Join(ordNoArr, ","), enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling, sDate.Format("2006-01-02"), eDate.Format("2006-01-02"), strings.Join(mealKindListArr, ","))
  1124. nativeResults, err := db.QueryNativeString(db.Db, nativeSql)
  1125. var nativeResultsMap = map[string]map[int]int64{}
  1126. for _, v := range nativeResults {
  1127. if nativeResultsMap[v["ord_no"]] == nil {
  1128. nativeResultsMap[v["ord_no"]] = make(map[int]int64)
  1129. }
  1130. nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"])
  1131. }
  1132. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1133. centralKitchenForSchoolUserWithDayDb.Set(0)
  1134. var list = map[string]map[string]map[string]map[string]int64{}
  1135. var xlsFileName, xlsDownloadPath []string
  1136. for _, v := range m {
  1137. if list[v.Enterprise.Name] == nil {
  1138. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  1139. }
  1140. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1141. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  1142. }
  1143. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1144. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  1145. }
  1146. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast && nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] != 0 {
  1147. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast]
  1148. nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] = 0
  1149. }
  1150. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch && nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] != 0 {
  1151. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch]
  1152. nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] = 0
  1153. }
  1154. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner && nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] != 0 {
  1155. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner]
  1156. nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] = 0
  1157. }
  1158. }
  1159. for k1, v1 := range list {
  1160. xlsx := excelize.NewFile()
  1161. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1162. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1163. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1164. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1165. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  1166. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1167. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  1168. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1169. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1170. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1171. if err != nil {
  1172. logx.Error(err)
  1173. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1174. panic(err)
  1175. }
  1176. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1177. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1178. j := 3 //表头前两行用了,只能从第三行开始
  1179. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  1180. tmpEnterpriseDb := db.EnterpriseDb{}
  1181. tmpEnterpriseDb.Set()
  1182. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1183. if err11 != nil {
  1184. logx.Error(err11)
  1185. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
  1186. panic(err11)
  1187. }
  1188. tmpClassDb := db.ClassDb{}
  1189. tmpClassDb.Set(0)
  1190. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1191. if err != nil {
  1192. logx.Error(err)
  1193. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1194. panic(err)
  1195. }
  1196. for _, class := range *classList {
  1197. for k2, v2 := range v1 {
  1198. if k2 == "" {
  1199. //若年级为空,则为教师餐
  1200. k2 = "教师餐"
  1201. }
  1202. for k3, v3 := range v2 {
  1203. if class.Name != k3 {
  1204. continue
  1205. }
  1206. if k3 == "" {
  1207. //若班级为空,则为教师餐
  1208. k3 = "教师餐"
  1209. }
  1210. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1211. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  1212. sumClassNum++
  1213. sumBreakfast += v3["breakfast"]
  1214. sumLunch += v3["lunch"]
  1215. sumDinner += v3["dinner"]
  1216. j++
  1217. }
  1218. }
  1219. }
  1220. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1221. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  1222. //将文件保存至服务器
  1223. err1 := xlsx.SaveAs(tmpDownloadPath)
  1224. if err1 != nil {
  1225. logx.Error(err1)
  1226. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1227. panic(err1)
  1228. }
  1229. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1230. xlsFileName = append(xlsFileName, tmpFileName)
  1231. }
  1232. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1233. if err != nil {
  1234. logx.Error(err)
  1235. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1236. panic(err)
  1237. }
  1238. defer archive.Close()
  1239. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1240. for k, v := range xlsDownloadPath {
  1241. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1242. if err1 != nil {
  1243. logx.Error(err1)
  1244. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1245. panic(err1)
  1246. }
  1247. f, err1 := os.Open(v) //打开待压缩的文件
  1248. if err1 != nil {
  1249. logx.Error(err1)
  1250. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1251. panic(err1)
  1252. }
  1253. if _, err1 = io.Copy(w, f); err != nil {
  1254. logx.Error(err1)
  1255. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1256. panic(err1)
  1257. }
  1258. if err1 != nil {
  1259. logx.Error(err1)
  1260. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1261. panic(err1)
  1262. }
  1263. f.Close()
  1264. os.RemoveAll(v) //TODO::移除源文件
  1265. }
  1266. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1267. if err != nil {
  1268. panic(err)
  1269. }
  1270. break
  1271. case 4:
  1272. //班级明细表
  1273. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1274. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1275. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1276. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1277. if req.EnterpriseId != 0 {
  1278. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1279. }
  1280. var months []time.Month
  1281. if req.StartDate != "" {
  1282. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1283. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1284. _, sMonth, _ := sDate.Date()
  1285. _, eMonth, _ := eDate.Date()
  1286. diffMonth := sMonth - eMonth
  1287. if diffMonth == 0 {
  1288. //TODO::查询一个月
  1289. months = append(months, sMonth)
  1290. } else {
  1291. for i := sMonth; i <= eMonth; i++ {
  1292. months = append(months, i)
  1293. }
  1294. }
  1295. }
  1296. if len(months) > 0 {
  1297. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1298. }
  1299. //if req.EndDate != "" {
  1300. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1301. //}
  1302. if len(req.MealKindList) > 0 {
  1303. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1304. }
  1305. if req.EnterPriseKind != 0 {
  1306. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1307. }
  1308. _, err := sess.
  1309. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1310. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  1311. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1312. Join("LEFT", "user", "user.id = user_identity.uid").
  1313. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1314. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1315. Join("LEFT", "grade", "class.grade_id = grade.id").
  1316. Asc("class.id").
  1317. FindAndCount(&m)
  1318. if err != nil {
  1319. logx.Error(err)
  1320. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1321. return
  1322. }
  1323. var ordNoArr, mealKindListArr []string
  1324. for _, v := range m {
  1325. ordNoArr = append(ordNoArr, v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo)
  1326. }
  1327. var nativeSql = "SELECT COUNT(*) AS total, kind , ord_no FROM `central_kitchen_for_school_user_with_day` WHERE `ord_no` IN (%s) AND (state !=%d) AND (state !=%d) AND (DATE >= '%s') AND (DATE <= '%s') AND `kind` IN (%s) GROUP BY `ord_no`, `kind`"
  1328. for _, v := range req.MealKindList {
  1329. mealKindListArr = append(mealKindListArr, utils.IntToStr(v))
  1330. }
  1331. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1332. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1333. nativeSql = fmt.Sprintf(nativeSql, strings.Join(ordNoArr, ","), enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling, sDate.Format("2006-01-02"), eDate.Format("2006-01-02"), strings.Join(mealKindListArr, ","))
  1334. nativeResults, err := db.QueryNativeString(db.Db, nativeSql)
  1335. var nativeResultsMap = map[string]map[int]int64{}
  1336. for _, v := range nativeResults {
  1337. if nativeResultsMap[v["ord_no"]] == nil {
  1338. nativeResultsMap[v["ord_no"]] = make(map[int]int64)
  1339. }
  1340. nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"])
  1341. }
  1342. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  1343. var xlsFileName, xlsDownloadPath []string
  1344. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1345. centralKitchenForSchoolUserWithDayDb.Set(0)
  1346. for _, v := range m {
  1347. if list[v.Enterprise.Name] == nil {
  1348. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  1349. }
  1350. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1351. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  1352. }
  1353. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1354. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  1355. }
  1356. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1357. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  1358. }
  1359. //count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1360. // v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
  1361. // enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1362. // req.StartDate, req.EndDate, []int{v.CentralKitchenForSchoolPackageOrdForReserve.Kind})
  1363. //if err2 != nil {
  1364. // logx.Error(err2)
  1365. // println("<<<<SelfSupportForSchoolDataStatistics Export3>>>>>Error:::", err2.Error())
  1366. // return
  1367. //}
  1368. //if count == 0 {
  1369. // continue
  1370. //}
  1371. //time.Sleep(2 * time.Millisecond) // 2毫秒
  1372. //
  1373. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1374. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  1375. //}
  1376. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1377. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  1378. //}
  1379. //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1380. // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  1381. //}
  1382. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1383. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast]
  1384. }
  1385. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1386. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch]
  1387. }
  1388. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1389. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner]
  1390. }
  1391. }
  1392. for k1, v1 := range list {
  1393. xlsx := excelize.NewFile()
  1394. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1395. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1396. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1397. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1398. //查询当前学校下的班级升序
  1399. tmpEnterpriseDb := db.EnterpriseDb{}
  1400. tmpEnterpriseDb.Set()
  1401. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1402. if err11 != nil {
  1403. logx.Error(err11)
  1404. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1405. panic(err11)
  1406. }
  1407. tmpClassDb := db.ClassDb{}
  1408. tmpClassDb.Set(0)
  1409. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1410. if err != nil {
  1411. logx.Error(err)
  1412. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1413. panic(err)
  1414. }
  1415. for _, class := range *classList {
  1416. for k2, v2 := range v1 {
  1417. if k2 == "" {
  1418. //若年级为空,则为教师餐
  1419. continue
  1420. }
  1421. for k3, v3 := range v2 {
  1422. if class.Name != k3 {
  1423. continue
  1424. }
  1425. xlsx.NewSheet(k3)
  1426. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  1427. xlsx.SetSheetRow(k3, "A1", &titleList)
  1428. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1429. xlsx.SetSheetRow(k3, "A2", &titleList)
  1430. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1431. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1432. if err != nil {
  1433. logx.Error(err)
  1434. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1435. panic(err)
  1436. }
  1437. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1438. xlsx.SetColWidth(k3, "A", "D", 20)
  1439. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  1440. j := 3 //表头前两行用了,只能从第三行开始
  1441. for k4, v4 := range v3 {
  1442. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  1443. continue
  1444. }
  1445. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1446. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1447. sumBreakfast += v4["breakfast"]
  1448. sumLunch += v4["lunch"]
  1449. sumDinner += v4["dinner"]
  1450. j++
  1451. }
  1452. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1453. "合计", sumBreakfast, sumLunch, sumDinner})
  1454. }
  1455. }
  1456. }
  1457. //将文件保存至服务器
  1458. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1459. err1 := xlsx.SaveAs(tmpDownloadPath)
  1460. if err1 != nil {
  1461. logx.Error(err1)
  1462. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1463. panic(err1)
  1464. }
  1465. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1466. xlsFileName = append(xlsFileName, tmpFileName)
  1467. }
  1468. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1469. if err != nil {
  1470. logx.Error(err)
  1471. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1472. panic(err)
  1473. }
  1474. defer archive.Close()
  1475. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1476. for k, v := range xlsDownloadPath {
  1477. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1478. if err1 != nil {
  1479. logx.Error(err1)
  1480. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1481. panic(err1)
  1482. }
  1483. f, err1 := os.Open(v) //打开待压缩的文件
  1484. if err1 != nil {
  1485. logx.Error(err1)
  1486. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1487. panic(err1)
  1488. }
  1489. if _, err1 = io.Copy(w, f); err != nil {
  1490. logx.Error(err1)
  1491. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1492. panic(err1)
  1493. }
  1494. if err1 != nil {
  1495. logx.Error(err1)
  1496. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1497. panic(err1)
  1498. }
  1499. f.Close()
  1500. os.RemoveAll(v) //TODO::移除源文件
  1501. }
  1502. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1503. if err != nil {
  1504. panic(err)
  1505. }
  1506. break
  1507. case 5:
  1508. xlsx := excelize.NewFile()
  1509. //退款明细表
  1510. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1511. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1512. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1513. if req.EnterpriseId != 0 {
  1514. sess.And("enterprise.id =?", req.EnterpriseId)
  1515. }
  1516. if req.StartDate != "" {
  1517. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1518. }
  1519. if req.EndDate != "" {
  1520. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1521. }
  1522. if len(req.MealKindList) > 0 {
  1523. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  1524. }
  1525. if req.EnterPriseKind != 0 {
  1526. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1527. }
  1528. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  1529. _, err := sess.
  1530. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  1531. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1532. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1533. Join("LEFT", "user", "user_identity.uid = user.id").
  1534. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1535. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1536. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1537. Join("LEFT", "grade", "class.grade_id = grade.id").
  1538. Desc("central_kitchen_for_school_user_refund_day.id").
  1539. FindAndCount(&m)
  1540. if err != nil {
  1541. logx.Error(err)
  1542. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1543. return
  1544. }
  1545. var list = map[string]map[string]map[string]map[string]map[string]int{}
  1546. var xlsFileName, xlsDownloadPath []string
  1547. for _, v := range m {
  1548. if list[v.Enterprise.Name] == nil {
  1549. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1550. }
  1551. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1552. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1553. }
  1554. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1555. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1556. }
  1557. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1558. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1559. }
  1560. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1561. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1562. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1563. }
  1564. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1565. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1566. }
  1567. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1568. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1569. }
  1570. }
  1571. for k1, v1 := range list {
  1572. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1573. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1574. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1575. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1576. for k2, v2 := range v1 {
  1577. if k2 == "" {
  1578. //若年级为空,则为教师餐
  1579. continue
  1580. }
  1581. for k3, v3 := range v2 {
  1582. xlsx.NewSheet(k3)
  1583. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1584. xlsx.SetSheetRow(k3, "A1", &titleList)
  1585. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1586. xlsx.SetSheetRow(k3, "A2", &titleList)
  1587. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1588. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1589. if err != nil {
  1590. logx.Error(err)
  1591. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1592. panic(err)
  1593. }
  1594. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1595. xlsx.SetColWidth(k3, "A", "D", 20)
  1596. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1597. j := 3 //表头前两行用了,只能从第三行开始
  1598. for k4, v4 := range v3 {
  1599. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1600. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1601. sumBreakfast += v4["breakfast"]
  1602. sumLunch += v4["lunch"]
  1603. sumDinner += v4["dinner"]
  1604. j++
  1605. }
  1606. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1607. "合计", sumBreakfast, sumLunch, sumDinner})
  1608. }
  1609. }
  1610. //将文件保存至服务器
  1611. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1612. err1 := xlsx.SaveAs(tmpDownloadPath)
  1613. if err1 != nil {
  1614. logx.Error(err1)
  1615. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1616. panic(err1)
  1617. }
  1618. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1619. xlsFileName = append(xlsFileName, tmpFileName)
  1620. }
  1621. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1622. if err != nil {
  1623. logx.Error(err)
  1624. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1625. panic(err)
  1626. }
  1627. defer archive.Close()
  1628. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1629. for k, v := range xlsDownloadPath {
  1630. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1631. if err1 != nil {
  1632. logx.Error(err1)
  1633. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1634. panic(err1)
  1635. }
  1636. f, err1 := os.Open(v) //打开待压缩的文件
  1637. if err1 != nil {
  1638. logx.Error(err1)
  1639. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1640. panic(err1)
  1641. }
  1642. if _, err1 = io.Copy(w, f); err != nil {
  1643. logx.Error(err1)
  1644. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1645. panic(err1)
  1646. }
  1647. if err1 != nil {
  1648. logx.Error(err1)
  1649. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1650. panic(err1)
  1651. }
  1652. f.Close()
  1653. os.RemoveAll(v) //TODO::移除源文件
  1654. }
  1655. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1656. if err != nil {
  1657. panic(err)
  1658. }
  1659. break
  1660. case 6:
  1661. xlsx := excelize.NewFile()
  1662. //收款统计表
  1663. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1664. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1665. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1666. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1667. //查找数据
  1668. var m []model.Enterprise
  1669. sess := db.Db.Desc("id")
  1670. if req.EnterPriseKind != 0 {
  1671. sess.And("kind = ?", req.EnterPriseKind)
  1672. }
  1673. if req.EnterpriseId != 0 {
  1674. sess.And("id = ?", req.EnterpriseId)
  1675. }
  1676. err := sess.Find(&m)
  1677. if err != nil {
  1678. logx.Error(err)
  1679. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1680. return
  1681. }
  1682. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1683. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1684. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1685. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1686. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1687. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1688. if err != nil {
  1689. logx.Error(err)
  1690. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1691. panic(err)
  1692. }
  1693. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1694. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1695. var incomeTotal, refundTotal, realityAmountTotal, rateFeeTotal float64
  1696. j := 3 //表头前两行用了,只能从第三行开始
  1697. for _, v := range m {
  1698. var income, refund string
  1699. //统计收入
  1700. sql1 := fmt.Sprintf("SELECT sum(total_price) as total FROM central_kitchen_for_school_package_ord where state = 1 and enterprise_id = %d and pay_at >= '%s' and pay_at < '%s'", v.Id, req.StartDate, req.EndDate)
  1701. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1702. if err != nil {
  1703. logx.Error(err)
  1704. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1705. return
  1706. }
  1707. if mapArr1 != nil {
  1708. income = mapArr1[0]["total"]
  1709. }
  1710. //统计退款
  1711. //查询出当前学校下所有的学生
  1712. userIdentityDb := db.UserIdentityDb{}
  1713. userIdentityDb.Set(0)
  1714. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1715. var userIdentityIds []string
  1716. for _, v := range *userIdentities {
  1717. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1718. }
  1719. if len(userIdentityIds) != 0 {
  1720. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1721. sql2 := fmt.Sprintf("SELECT sum(amount) as total FROM central_kitchen_for_school_user_refund_day where state = %d and identity_id IN (%s) and refund_date >= '%s' and refund_date < '%s'",
  1722. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1723. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1724. if err != nil {
  1725. logx.Error(err)
  1726. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1727. return
  1728. }
  1729. if mapArr2 != nil {
  1730. refund = mapArr2[0]["total"]
  1731. }
  1732. }
  1733. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1734. rateFee := realityAmount * 0.002
  1735. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1736. incomeTotal += utils.StrToFloat64(income)
  1737. refundTotal += utils.StrToFloat64(refund)
  1738. realityAmountTotal += realityAmount
  1739. rateFeeTotal += rateFee
  1740. j++
  1741. }
  1742. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{"总计", incomeTotal, refundTotal, realityAmountTotal, rateFeeTotal})
  1743. //将文件保存至服务器
  1744. xlsx.SaveAs("./static" + downloadPath)
  1745. break
  1746. case 7:
  1747. xlsx := excelize.NewFile()
  1748. //班级收款明细表
  1749. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1750. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1751. //查找订单数据
  1752. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1753. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1754. if req.EnterpriseId != 0 {
  1755. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1756. }
  1757. if req.StartDate != "" {
  1758. sess.And("central_kitchen_for_school_package_ord.pay_at >= ?", req.StartDate)
  1759. }
  1760. if req.EndDate != "" {
  1761. sess.And("central_kitchen_for_school_package_ord.pay_at <= ?", req.EndDate)
  1762. }
  1763. if req.EnterPriseKind != 0 {
  1764. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1765. }
  1766. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1767. _, err := sess.
  1768. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  1769. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1770. Join("LEFT", "user", "user.id = user_identity.uid").
  1771. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1772. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1773. Join("LEFT", "grade", "class.grade_id = grade.id").
  1774. FindAndCount(&m)
  1775. if err != nil {
  1776. logx.Error(err)
  1777. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1778. return
  1779. }
  1780. sess1 := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent).Desc("central_kitchen_for_school_user_refund_day.id")
  1781. if req.EnterpriseId != 0 {
  1782. sess1.And("enterprise.id =?", req.EnterpriseId)
  1783. }
  1784. if req.StartDate != "" {
  1785. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1786. }
  1787. if req.EndDate != "" {
  1788. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1789. }
  1790. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1791. _, err = sess1.
  1792. Join("LEFT", "central_kitchen_for_school_user_with_day", "central_kitchen_for_school_user_refund_day.records_id = central_kitchen_for_school_user_with_day.id").
  1793. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_user_refund_day.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1794. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1795. Join("LEFT", "user", "user_identity.uid = user.id").
  1796. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1797. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1798. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1799. Join("LEFT", "grade", "class.grade_id = grade.id").
  1800. Desc("central_kitchen_for_school_user_refund_day.id").
  1801. FindAndCount(&mm)
  1802. if err != nil {
  1803. logx.Error(err)
  1804. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1805. return
  1806. }
  1807. var list = map[string]map[string]map[string]map[string]float64{}
  1808. for _, v := range m {
  1809. if list[v.Enterprise.Name] == nil {
  1810. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1811. }
  1812. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1813. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1814. }
  1815. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1816. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1817. }
  1818. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1819. }
  1820. for _, v := range mm {
  1821. if list[v.Enterprise.Name] == nil {
  1822. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1823. }
  1824. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1825. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1826. }
  1827. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1828. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1829. }
  1830. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1831. }
  1832. var xlsFileName, xlsDownloadPath []string
  1833. for k1, v1 := range list {
  1834. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1835. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1836. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1837. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1838. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1839. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1840. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1841. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1842. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1843. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1844. if err != nil {
  1845. logx.Error(err)
  1846. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1847. panic(err)
  1848. }
  1849. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1850. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1851. j := 3 //表头前两行用了,只能从第三行开始
  1852. var sumClassNum = int64(0)
  1853. var sumIncome, sumRefund = float64(0), float64(0)
  1854. for k2, v2 := range v1 {
  1855. if k2 == "" {
  1856. //若年级为空,则为教师餐
  1857. continue
  1858. }
  1859. for k3, v3 := range v2 {
  1860. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1861. sumClassNum++
  1862. sumIncome += v3["income"]
  1863. sumRefund += v3["refund"]
  1864. j++
  1865. }
  1866. }
  1867. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1868. "合计", sumClassNum, sumIncome, sumRefund})
  1869. //将文件保存至服务器
  1870. err1 := xlsx.SaveAs(tmpDownloadPath)
  1871. if err1 != nil {
  1872. logx.Error(err1)
  1873. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1874. panic(err1)
  1875. }
  1876. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1877. xlsFileName = append(xlsFileName, tmpFileName)
  1878. }
  1879. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1880. if err != nil {
  1881. logx.Error(err)
  1882. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1883. panic(err)
  1884. }
  1885. defer archive.Close()
  1886. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1887. for k, v := range xlsDownloadPath {
  1888. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1889. if err1 != nil {
  1890. logx.Error(err1)
  1891. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1892. panic(err1)
  1893. }
  1894. f, err1 := os.Open(v) //打开待压缩的文件
  1895. if err1 != nil {
  1896. logx.Error(err1)
  1897. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1898. panic(err1)
  1899. }
  1900. if _, err1 = io.Copy(w, f); err != nil {
  1901. logx.Error(err1)
  1902. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1903. panic(err1)
  1904. }
  1905. if err1 != nil {
  1906. logx.Error(err1)
  1907. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1908. panic(err1)
  1909. }
  1910. f.Close()
  1911. os.RemoveAll(v) //TODO::移除源文件
  1912. }
  1913. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1914. if err != nil {
  1915. panic(err)
  1916. }
  1917. break
  1918. case 8:
  1919. xlsx := excelize.NewFile()
  1920. //学校预定数量统计表
  1921. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1922. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1923. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1924. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1925. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1926. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1927. if req.EnterpriseId != 0 {
  1928. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1929. }
  1930. //var months []time.Month
  1931. //if req.StartDate != "" {
  1932. // sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1933. // eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1934. // _, sMonth, _ := sDate.Date()
  1935. // _, eMonth, _ := eDate.Date()
  1936. // diffMonth := sMonth - eMonth
  1937. // if diffMonth == 0 {
  1938. // //TODO::查询一个月
  1939. // months = append(months, sMonth)
  1940. // } else {
  1941. // for i := sMonth; i <= eMonth; i++ {
  1942. // months = append(months, i)
  1943. // }
  1944. // }
  1945. //}
  1946. //if len(months) > 0 {
  1947. // sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1948. //}
  1949. if req.StartDate != "" {
  1950. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.StartDate).
  1951. And("central_kitchen_for_school_package_ord_for_reserve.meal_time_end >= ?", req.StartDate)
  1952. }
  1953. if len(req.MealKindList) > 0 {
  1954. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1955. }
  1956. if req.EnterPriseKind != 0 {
  1957. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1958. }
  1959. _, err := sess.
  1960. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  1961. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  1962. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1963. FindAndCount(&m)
  1964. if err != nil {
  1965. logx.Error(err)
  1966. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1967. return
  1968. }
  1969. var ordNoArr, mealKindListArr []string
  1970. for _, v := range m {
  1971. ordNoArr = append(ordNoArr, v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo)
  1972. }
  1973. var nativeSql = "SELECT COUNT(*) AS total, kind , ord_no FROM `central_kitchen_for_school_user_with_day` WHERE `ord_no` IN (%s) AND (state !=%d) AND (state !=%d) AND (DATE >= '%s') AND (DATE <= '%s') AND `kind` IN (%s) GROUP BY `ord_no`, `kind`"
  1974. for _, v := range req.MealKindList {
  1975. mealKindListArr = append(mealKindListArr, utils.IntToStr(v))
  1976. }
  1977. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1978. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1979. nativeSql = fmt.Sprintf(nativeSql, strings.Join(ordNoArr, ","), enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling, sDate.Format("2006-01-02"), eDate.Format("2006-01-02"), strings.Join(mealKindListArr, ","))
  1980. nativeResults, err := db.QueryNativeString(db.Db, nativeSql)
  1981. var nativeResultsMap = map[string]map[int]int64{}
  1982. for _, v := range nativeResults {
  1983. if nativeResultsMap[v["ord_no"]] == nil {
  1984. nativeResultsMap[v["ord_no"]] = make(map[int]int64)
  1985. }
  1986. nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"])
  1987. }
  1988. var list = map[string]map[string]int64{}
  1989. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1990. centralKitchenForSchoolUserWithDayDb.Set(0)
  1991. for _, v := range m {
  1992. if list[v.Enterprise.Name] == nil {
  1993. list[v.Enterprise.Name] = map[string]int64{}
  1994. }
  1995. var count int64
  1996. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1997. count = nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast]
  1998. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast]
  1999. }
  2000. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  2001. count = nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch]
  2002. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch]
  2003. }
  2004. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  2005. count = nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner]
  2006. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner]
  2007. }
  2008. list[v.Enterprise.Name]["count"] += count
  2009. }
  2010. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  2011. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  2012. titleList = []string{"学校名称", "预定总数",
  2013. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  2014. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  2015. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  2016. }
  2017. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  2018. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  2019. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  2020. if err != nil {
  2021. logx.Error(err)
  2022. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  2023. panic(err)
  2024. }
  2025. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  2026. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  2027. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  2028. j := 3 //表头前两行用了,只能从第三行开始
  2029. total := int64(0)
  2030. total1 := int64(0)
  2031. total2 := int64(0)
  2032. total3 := int64(0)
  2033. for k, v := range list {
  2034. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  2035. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  2036. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  2037. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  2038. total += v["count"]
  2039. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  2040. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  2041. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  2042. j++
  2043. }
  2044. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  2045. "合计", total, total1, total2, total3})
  2046. //将文件保存至服务器
  2047. xlsx.SaveAs("./static" + downloadPath)
  2048. break
  2049. case 10:
  2050. //就餐数据统计表
  2051. sTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  2052. eTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  2053. xls, _ := excelize.OpenFile("./static/template/央厨学校(就餐数据统计表).xlsx")
  2054. fileName = "【" + sTime.Format("2006-01-02") + "~" + eTime.Format("2006-01-02") + "】就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  2055. downloadPath = "/export/central_kitchen_for_school/" + fileName
  2056. var list = map[string]map[string]map[string]float64{}
  2057. var dates []string
  2058. if req.StartDate != "" {
  2059. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  2060. centralKitchenForSchoolUserWithDayDb.Set(0)
  2061. diffDays := int(eTime.Sub(sTime).Hours() / 24)
  2062. for i := 0; i <= diffDays; i++ {
  2063. date := sTime.AddDate(0, 0, i)
  2064. dateName := date.Format("2006-01-02")
  2065. dates = append(dates, dateName)
  2066. for _, vv := range req.MealKindList {
  2067. tmpSum, err := centralKitchenForSchoolUserWithDayDb.SumCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling)
  2068. if err != nil {
  2069. logx.Error(err)
  2070. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  2071. panic(err)
  2072. }
  2073. tmpCount, err := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling)
  2074. if err != nil {
  2075. logx.Error(err)
  2076. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  2077. panic(err)
  2078. }
  2079. if list[dateName] == nil {
  2080. list[dateName] = map[string]map[string]float64{}
  2081. }
  2082. if list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] == nil {
  2083. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] = map[string]float64{}
  2084. }
  2085. if list[dateName]["合计"] == nil {
  2086. list[dateName]["合计"] = make(map[string]float64)
  2087. }
  2088. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["sum"] = tmpSum
  2089. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["count"] = float64(tmpCount)
  2090. list[dateName]["合计"]["sum"] += tmpSum
  2091. list[dateName]["合计"]["count"] += float64(tmpCount)
  2092. }
  2093. }
  2094. }
  2095. //xls.SetSheetRow("", "A1", &titleList)
  2096. //sheetIndex := 2 //第一个表是模板表
  2097. //xls.NewSheet("sheet1") //1、新建表格
  2098. //xls.CopySheet(1, sheetIndex)
  2099. j := 4 //表头前三"早餐"行被占用了,只能从第四行开始
  2100. var totalMap = map[string]float64{}
  2101. for _, date := range dates {
  2102. for kk, v := range list {
  2103. if date == kk {
  2104. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{kk,
  2105. v["早餐"]["count"], v["早餐"]["sum"],
  2106. v["午餐"]["count"], v["午餐"]["sum"],
  2107. v["晚餐"]["count"], v["晚餐"]["sum"],
  2108. v["合计"]["count"], v["合计"]["sum"],
  2109. })
  2110. totalMap["breakfast_count"] += v["早餐"]["count"]
  2111. totalMap["breakfast_sum"] += v["早餐"]["sum"]
  2112. totalMap["lunch_count"] += v["午餐"]["count"]
  2113. totalMap["lunch_sum"] += v["午餐"]["sum"]
  2114. totalMap["dinner_count"] += v["晚餐"]["count"]
  2115. totalMap["dinner_sum"] += v["晚餐"]["sum"]
  2116. totalMap["total_count"] += v["合计"]["count"]
  2117. totalMap["total_sum"] += v["合计"]["sum"]
  2118. j++
  2119. }
  2120. }
  2121. }
  2122. //xls.DeleteSheet("template") //删除模板表
  2123. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{"总计",
  2124. totalMap["breakfast_count"], totalMap["breakfast_sum"],
  2125. totalMap["lunch_count"], totalMap["lunch_sum"],
  2126. totalMap["dinner_count"], totalMap["dinner_sum"],
  2127. totalMap["total_count"], totalMap["total_sum"],
  2128. })
  2129. //将文件保存至服务器
  2130. xls.SaveAs("./static" + downloadPath)
  2131. break
  2132. }
  2133. //新增数据
  2134. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  2135. centralKitchenForSchoolExportRecordsDb.Set()
  2136. marshal, _ := json.Marshal(req)
  2137. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  2138. Name: fileName,
  2139. DownloadPath: downloadPath,
  2140. Kind: kind,
  2141. ReqContent: string(marshal),
  2142. CreateAt: now.Format("2006-01-02 15:04:05"),
  2143. UpdateAt: now.Format("2006-01-02 15:04:05"),
  2144. })
  2145. }
  2146. }
  2147. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  2148. var titleList []string
  2149. var fileName, downloadPath string
  2150. var now = time.Now()
  2151. //数据对比
  2152. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  2153. downloadPath = "/export/central_kitchen_for_school/" + fileName
  2154. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  2155. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  2156. if req.EnterpriseId != 0 {
  2157. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  2158. }
  2159. if req.Date1[0] != "" {
  2160. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  2161. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  2162. }
  2163. if req.Date1[1] != "" {
  2164. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[1], time.Local)
  2165. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  2166. }
  2167. if len(req.MealKindList) > 0 {
  2168. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  2169. }
  2170. if req.EnterPriseKind != 0 {
  2171. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  2172. }
  2173. _, err := sess.
  2174. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  2175. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  2176. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  2177. Join("LEFT", "user", "user.id = user_identity.uid").
  2178. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  2179. Join("LEFT", "class", "class_with_user.class_id = class.id").
  2180. Join("LEFT", "grade", "class.grade_id = grade.id").
  2181. FindAndCount(&m)
  2182. if err != nil {
  2183. logx.Error(err)
  2184. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2185. return
  2186. }
  2187. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  2188. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  2189. if req.EnterpriseId != 0 {
  2190. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  2191. }
  2192. if req.Date2[0] != "" {
  2193. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  2194. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  2195. }
  2196. if req.Date2[1] != "" {
  2197. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[1], time.Local)
  2198. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  2199. }
  2200. if len(req.MealKindList) > 0 {
  2201. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  2202. }
  2203. _, err1 := sess1.
  2204. Join("LEFT", "central_kitchen_for_school_package_ord", "central_kitchen_for_school_package_ord_for_reserve.out_trade_no = central_kitchen_for_school_package_ord.out_trade_no").
  2205. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id").
  2206. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  2207. Join("LEFT", "user", "user.id = user_identity.uid").
  2208. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  2209. Join("LEFT", "class", "class_with_user.class_id = class.id").
  2210. Join("LEFT", "grade", "class.grade_id = grade.id").
  2211. FindAndCount(&m1)
  2212. if err1 != nil {
  2213. logx.Error(err1)
  2214. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2215. return
  2216. }
  2217. var list = map[string]map[string]map[string]map[string]string{}
  2218. for _, v := range m {
  2219. if list[v.Enterprise.Name] == nil {
  2220. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  2221. }
  2222. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  2223. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  2224. }
  2225. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  2226. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  2227. }
  2228. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  2229. }
  2230. var list1 = map[string]map[string]map[string]map[string]string{}
  2231. for _, v := range m1 {
  2232. if list1[v.Enterprise.Name] == nil {
  2233. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  2234. }
  2235. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  2236. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  2237. }
  2238. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  2239. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  2240. }
  2241. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  2242. }
  2243. var xlsFileName, xlsDownloadPath []string
  2244. for k1, v1 := range list {
  2245. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  2246. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  2247. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  2248. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  2249. //查询当前学校下的班级升序
  2250. tmpEnterpriseDb := db.EnterpriseDb{}
  2251. tmpEnterpriseDb.Set()
  2252. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  2253. if err11 != nil {
  2254. logx.Error(err11)
  2255. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  2256. panic(err11)
  2257. }
  2258. tmpClassDb := db.ClassDb{}
  2259. tmpClassDb.Set(0)
  2260. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  2261. if err != nil {
  2262. logx.Error(err)
  2263. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  2264. panic(err)
  2265. }
  2266. xlsx := excelize.NewFile()
  2267. for _, class := range *classList {
  2268. for k2, v2 := range v1 {
  2269. if k2 == "" {
  2270. //若年级为空,则为教师
  2271. continue
  2272. }
  2273. for k3, v3 := range v2 {
  2274. if class.Name != k3 {
  2275. continue
  2276. }
  2277. xlsx.NewSheet(k3)
  2278. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  2279. xlsx.SetSheetRow(k3, "A1", &titleList)
  2280. titleList = []string{"姓名", "家长联系电话"}
  2281. xlsx.SetSheetRow(k3, "A2", &titleList)
  2282. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  2283. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  2284. if err != nil {
  2285. logx.Error(err)
  2286. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  2287. panic(err)
  2288. }
  2289. xlsx.SetCellStyle(k3, "A1", "B1", style)
  2290. xlsx.SetColWidth(k3, "A", "B", 40)
  2291. j := 2 //表头被第一行用了,只能从第二行开始
  2292. for k4, v4 := range v3 {
  2293. //TODO::判断当前用户是否在新的月份里面
  2294. fmt.Println("11111111111111", list1[k1])
  2295. fmt.Println("22222222222222", list1[k1][k2])
  2296. fmt.Println("33333333333333", list1[k1][k2][k3])
  2297. fmt.Println("44444444444444", list1[k1][k2][k3][k4])
  2298. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  2299. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  2300. k4, v4})
  2301. j++
  2302. }
  2303. }
  2304. }
  2305. }
  2306. }
  2307. //将文件保存至服务器
  2308. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  2309. err1 := xlsx.SaveAs(tmpDownloadPath)
  2310. if err1 != nil {
  2311. logx.Error(err1)
  2312. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2313. panic(err1)
  2314. }
  2315. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  2316. xlsFileName = append(xlsFileName, tmpFileName)
  2317. }
  2318. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  2319. if err != nil {
  2320. logx.Error(err)
  2321. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2322. panic(err)
  2323. }
  2324. defer archive.Close()
  2325. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  2326. for k, v := range xlsDownloadPath {
  2327. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  2328. if err1 != nil {
  2329. logx.Error(err1)
  2330. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2331. panic(err1)
  2332. }
  2333. f, err1 := os.Open(v) //打开待压缩的文件
  2334. if err1 != nil {
  2335. logx.Error(err1)
  2336. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2337. panic(err1)
  2338. }
  2339. if _, err1 = io.Copy(w, f); err != nil {
  2340. logx.Error(err1)
  2341. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2342. panic(err1)
  2343. }
  2344. if err1 != nil {
  2345. logx.Error(err1)
  2346. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2347. panic(err1)
  2348. }
  2349. f.Close()
  2350. os.RemoveAll(v) //TODO::移除源文件
  2351. }
  2352. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  2353. if err != nil {
  2354. panic(err)
  2355. }
  2356. //新增数据
  2357. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  2358. centralKitchenForSchoolExportRecordsDb.Set()
  2359. marshal, _ := json.Marshal(req)
  2360. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  2361. Name: fileName,
  2362. DownloadPath: downloadPath,
  2363. Kind: 9, //数据对比
  2364. ReqContent: string(marshal),
  2365. CreateAt: now.Format("2006-01-02 15:04:05"),
  2366. UpdateAt: now.Format("2006-01-02 15:04:05"),
  2367. })
  2368. }
  2369. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  2370. mealZh = "未知"
  2371. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  2372. for _, v := range md.SelfSupportForMealTimeMapping {
  2373. //判断时间大小
  2374. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  2375. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  2376. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  2377. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  2378. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  2379. mealZh = v["name"]
  2380. }
  2381. }
  2382. return
  2383. }