智慧食堂
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

svc_data_statisstics.go 109 KiB

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