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

svc_data_statisstics.go 86 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  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. "time"
  18. )
  19. func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
  20. var titleList []string
  21. var fileName string
  22. var now = time.Now()
  23. xlsx := excelize.NewFile()
  24. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  25. switch req.Kind {
  26. case 1:
  27. //订单列表
  28. fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  29. //查找订单数据
  30. sess := db.Db.Desc("nursing_home_package_ord.id")
  31. if req.EnterpriseId != 0 {
  32. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  33. }
  34. if req.StartDate != "" {
  35. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  36. }
  37. if req.EndDate != "" {
  38. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  39. }
  40. var m []*db.NursingHomePackageOrdWithUserIdentity
  41. _, err := sess.
  42. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  43. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  44. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  45. FindAndCount(&m)
  46. if err != nil {
  47. logx.Error(err)
  48. println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
  49. return
  50. }
  51. titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
  52. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  53. j := 2 //表头被第一行用了,只能从第二行开始
  54. for _, v := range m {
  55. var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
  56. switch v.NursingHomePackageOrd.Kind {
  57. case 1:
  58. kind = "按年购买"
  59. break
  60. case 2:
  61. kind = "按月购买"
  62. break
  63. case 3:
  64. kind = "按天购买"
  65. break
  66. case 4:
  67. kind = "补餐"
  68. break
  69. }
  70. var state string //0:待支付 1:支付成功 2:支付失败
  71. switch v.NursingHomePackageOrd.State {
  72. case enum.NursingHomePackageOrdStateForWait:
  73. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  74. break
  75. case enum.NursingHomePackageOrdStateForSuccess:
  76. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  77. break
  78. case enum.NursingHomePackageOrdStateForFail:
  79. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
  80. break
  81. }
  82. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
  83. v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
  84. j++
  85. }
  86. break
  87. case 2:
  88. //退款申请表
  89. fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  90. var m []*db.NursingHomeUserRefundDayWithData
  91. sess := db.Db.Desc("nursing_home_user_refund_day.id")
  92. if req.EnterpriseId != 0 {
  93. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  94. }
  95. if req.StartDate != "" {
  96. sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
  97. }
  98. if req.EndDate != "" {
  99. sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
  100. }
  101. _, err := sess.
  102. Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
  103. Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
  104. Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
  105. Join("LEFT", "user", "user_identity.uid = user.id").
  106. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  107. Desc("nursing_home_user_refund_day.id").
  108. FindAndCount(&m)
  109. if err != nil {
  110. logx.Error(err)
  111. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  112. return
  113. }
  114. titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
  115. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  116. j := 2 //表头被第一行用了,只能从第二行开始
  117. for _, v := range m {
  118. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  119. switch v.NursingHomeUserWithDay.Kind {
  120. case enum.NursingHomeUserWithDayKindForBreakfast:
  121. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  122. break
  123. case enum.NursingHomeUserWithDayKindForLunch:
  124. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  125. break
  126. case enum.NursingHomeUserWithDayKindForDinner:
  127. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  128. break
  129. }
  130. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  131. switch v.NursingHomeUserRefundDay.State {
  132. case enum.NursingHomeUserRefundDayStateForAuditing:
  133. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
  134. break
  135. case enum.NursingHomeUserRefundDayStateForAuditPass:
  136. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
  137. break
  138. case enum.NursingHomeUserRefundDayStateForAuditReject:
  139. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
  140. break
  141. case enum.NursingHomeUserRefundDayStateForAuditComplete:
  142. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
  143. break
  144. }
  145. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
  146. v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
  147. j++
  148. }
  149. xlsx.SaveAs("./static" + fileName + ".xlsx")
  150. break
  151. case 3:
  152. //预定统计表
  153. fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  154. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  155. sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
  156. if req.EnterpriseId != 0 {
  157. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  158. }
  159. if req.StartDate != "" {
  160. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  161. }
  162. if req.EndDate != "" {
  163. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  164. }
  165. _, err := sess.
  166. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  167. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  168. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  169. Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
  170. if err != nil {
  171. logx.Error(err)
  172. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  173. return
  174. }
  175. titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
  176. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  177. j := 2 //表头被第一行用了,只能从第二行开始
  178. for _, v := range m {
  179. var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
  180. switch v.NursingHomePackageOrdForReserve.Kind {
  181. case enum.NursingHomeUserWithDayKindForBreakfast:
  182. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  183. break
  184. case enum.NursingHomeUserWithDayKindForLunch:
  185. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  186. break
  187. case enum.NursingHomeUserWithDayKindForDinner:
  188. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  189. break
  190. }
  191. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
  192. v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
  193. j++
  194. }
  195. xlsx.SaveAs("./static" + fileName + ".xlsx")
  196. break
  197. }
  198. //新增数据
  199. nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
  200. nursingHomeExportRecordsDb.Set()
  201. marshal, _ := json.Marshal(req)
  202. nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
  203. Name: fileName,
  204. DownloadPath: fileName,
  205. Kind: req.Kind,
  206. ReqContent: string(marshal),
  207. CreateAt: now.Format("2006-01-02 15:04:05"),
  208. UpdateAt: now.Format("2006-01-02 15:04:05"),
  209. })
  210. }
  211. func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
  212. var titleList []string
  213. var fileName, downloadPath string
  214. var now = time.Now()
  215. switch req.Kind {
  216. case 1:
  217. //教师消费统计表
  218. xlsx, _ := excelize.OpenFile("./static/template/自营学校(教师消费统计表).xlsx")
  219. fileName = "教师消费统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  220. downloadPath = "/export/self_support_for_school/" + fileName
  221. //查询数据
  222. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  223. sess := db.Db.Desc("self_support_for_school_ord.id")
  224. if req.EnterpriseId != 0 {
  225. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  226. }
  227. if req.StartDate != "" {
  228. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  229. }
  230. if req.EndDate != "" {
  231. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  232. }
  233. if req.EnterPriseKind != 0 {
  234. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  235. }
  236. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  237. _, err := sess.
  238. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  239. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  240. Join("LEFT", "user", "user.id = user_identity.uid").
  241. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  242. Join("LEFT", "class", "class_with_user.class_id = class.id").
  243. Join("LEFT", "grade", "class.grade_id = grade.id").
  244. FindAndCount(&m)
  245. if err != nil {
  246. logx.Error(err)
  247. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  248. return
  249. }
  250. //整合数据
  251. var data = map[string]map[string]map[string]map[string]interface{}{}
  252. for _, vv := range m {
  253. if data[vv.Enterprise.Name] == nil {
  254. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  255. }
  256. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  257. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  258. }
  259. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  260. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  261. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  262. }
  263. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  264. "id": vv.UserIdentity.IdNo,
  265. }
  266. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  267. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  268. }
  269. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  270. 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)
  271. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  272. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  273. }
  274. xlsx.SetSheetRow("", "A1", &titleList)
  275. sheetIndex := 2 //第一个表是模板表
  276. j := 3 //表头前两行被占用了,只能从第三行开始
  277. for k, v := range data {
  278. xlsx.NewSheet(k) //1、新建表格
  279. xlsx.CopySheet(1, sheetIndex)
  280. for kk, vv := range v {
  281. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  282. vv["早餐"]["num"], vv["早餐"]["amount"],
  283. vv["午餐"]["num"], vv["午餐"]["amount"],
  284. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  285. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  286. vv["未知"]["num"], vv["未知"]["amount"],
  287. vv["合计"]["num"], vv["合计"]["amount"],
  288. })
  289. j++
  290. }
  291. sheetIndex++
  292. }
  293. xlsx.DeleteSheet("template") //删除模板表
  294. //将文件保存至服务器
  295. xlsx.SaveAs("./static" + downloadPath)
  296. break
  297. case 2:
  298. //商家分餐数据统计表
  299. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  300. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  301. downloadPath = "/export/self_support_for_school/" + fileName
  302. //查询出所有的设备
  303. var deviceList []*model.Device
  304. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  305. if err != nil {
  306. logx.Error(err)
  307. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  308. return
  309. }
  310. var deviceListMapping = map[string]model.Device{}
  311. for _, v := range deviceList {
  312. deviceListMapping[v.DeviceSn] = *v
  313. }
  314. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  315. sess := db.Db.Desc("self_support_for_school_ord.id")
  316. if req.EnterpriseId != 0 {
  317. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  318. }
  319. if req.StartDate != "" {
  320. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  321. }
  322. if req.EndDate != "" {
  323. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  324. }
  325. if req.EnterPriseKind != 0 {
  326. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  327. }
  328. _, err = sess.
  329. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  330. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  331. Join("LEFT", "user", "user.id = user_identity.uid").
  332. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  333. Join("LEFT", "class", "class_with_user.class_id = class.id").
  334. Join("LEFT", "grade", "class.grade_id = grade.id").
  335. FindAndCount(&m)
  336. if err != nil {
  337. logx.Error(err)
  338. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  339. return
  340. }
  341. //整合数据
  342. var data = map[string]map[string]map[string]map[string]float64{}
  343. for _, vv := range m {
  344. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  345. if data[vv.Enterprise.Name] == nil {
  346. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  347. }
  348. if data[vv.Enterprise.Name][shopName] == nil {
  349. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  350. }
  351. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  352. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  353. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  354. }
  355. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  356. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  357. }
  358. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  359. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  360. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  361. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  362. }
  363. xlsx.SetSheetRow("", "A1", &titleList)
  364. sheetIndex := 2 //第一个表是模板表
  365. for k, v := range data {
  366. if k == "" {
  367. k = "未知"
  368. }
  369. xlsx.NewSheet(k) //1、新建表格
  370. xlsx.CopySheet(1, sheetIndex)
  371. j := 4 //表头前三行被占用了,只能从第四行开始
  372. for kk, vv := range v {
  373. if kk == "" {
  374. kk = k
  375. }
  376. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  377. vv["早餐"]["num"], vv["早餐"]["amount"],
  378. vv["午餐"]["num"], vv["午餐"]["amount"],
  379. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  380. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  381. vv["合计"]["num"], vv["合计"]["amount"],
  382. })
  383. j++
  384. }
  385. sheetIndex++
  386. }
  387. xlsx.DeleteSheet("template") //删除模板表
  388. //将文件保存至服务器
  389. xlsx.SaveAs("./static" + downloadPath)
  390. break
  391. case 3:
  392. //就餐数据统计表
  393. xlsx, _ := excelize.OpenFile("./static/template/自营学校(就餐数据统计表).xlsx")
  394. fileName = "就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  395. downloadPath = "/export/self_support_for_school/" + fileName
  396. //查询出所有的设备
  397. //var deviceList []*model.Device
  398. //err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  399. //if err != nil {
  400. // logx.Error(err)
  401. // println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  402. // return
  403. //}
  404. //var deviceListMapping = map[string]model.Device{}
  405. //for _, v := range deviceList {
  406. // deviceListMapping[v.DeviceSn] = *v
  407. //}
  408. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  409. sess := db.Db.Desc("self_support_for_school_ord.id")
  410. if req.EnterpriseId != 0 {
  411. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  412. }
  413. if req.StartDate != "" {
  414. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  415. }
  416. if req.EndDate != "" {
  417. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  418. }
  419. if req.EnterPriseKind != 0 {
  420. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  421. }
  422. _, err := sess.
  423. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  424. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  425. Join("LEFT", "user", "user.id = user_identity.uid").
  426. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  427. Join("LEFT", "class", "class_with_user.class_id = class.id").
  428. Join("LEFT", "grade", "class.grade_id = grade.id").
  429. FindAndCount(&m)
  430. if err != nil {
  431. logx.Error(err)
  432. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  433. return
  434. }
  435. //整合数据
  436. var data = map[string]map[string]map[string]map[string]float64{}
  437. shopName := req.StartDate + " ~ " + req.EndDate
  438. for _, vv := range m {
  439. if data[vv.Enterprise.Name] == nil {
  440. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  441. }
  442. if data[vv.Enterprise.Name][shopName] == nil {
  443. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  444. }
  445. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  446. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  447. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  448. }
  449. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  450. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  451. }
  452. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  453. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  454. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  455. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  456. }
  457. xlsx.SetSheetRow("", "A1", &titleList)
  458. sheetIndex := 2 //第一个表是模板表
  459. for k, v := range data {
  460. if k == "" {
  461. k = "未知"
  462. }
  463. xlsx.NewSheet(k) //1、新建表格
  464. xlsx.CopySheet(1, sheetIndex)
  465. j := 4 //表头前三行被占用了,只能从第四行开始
  466. for kk, vv := range v {
  467. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  468. vv["早餐"]["num"], vv["早餐"]["amount"],
  469. vv["午餐"]["num"], vv["午餐"]["amount"],
  470. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  471. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  472. vv["合计"]["num"], vv["合计"]["amount"],
  473. })
  474. j++
  475. }
  476. sheetIndex++
  477. }
  478. xlsx.DeleteSheet("template") //删除模板表
  479. //将文件保存至服务器
  480. xlsx.SaveAs("./static" + downloadPath)
  481. break
  482. }
  483. //新增数据
  484. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  485. selfSupportForSchoolExportRecordsDb.Set()
  486. marshal, _ := json.Marshal(req)
  487. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  488. Name: fileName,
  489. DownloadPath: downloadPath,
  490. Kind: req.Kind,
  491. ReqContent: string(marshal),
  492. CreateAt: now.Format("2006-01-02 15:04:05"),
  493. UpdateAt: now.Format("2006-01-02 15:04:05"),
  494. })
  495. }
  496. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  497. var titleList []string
  498. var fileName, downloadPath string
  499. var now = time.Now()
  500. for _, kind := range req.Kinds {
  501. xlsx := excelize.NewFile()
  502. switch kind {
  503. case 1:
  504. //订单列表
  505. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  506. downloadPath = "/export/central_kitchen_for_school/" + fileName
  507. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  508. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  509. //查找订单数据
  510. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  511. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  512. if req.EnterpriseId != 0 {
  513. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  514. }
  515. if req.StartDate != "" {
  516. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  517. }
  518. if req.EndDate != "" {
  519. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  520. }
  521. if req.EnterPriseKind != 0 {
  522. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  523. }
  524. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  525. _, err := sess.
  526. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  527. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  528. Join("LEFT", "user", "user.id = user_identity.uid").
  529. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  530. Join("LEFT", "class", "class_with_user.class_id = class.id").
  531. Join("LEFT", "grade", "class.grade_id = grade.id").
  532. FindAndCount(&m)
  533. if err != nil {
  534. logx.Error(err)
  535. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  536. return
  537. }
  538. //查找预定数据
  539. var outTradeNoList []string
  540. for _, v := range m {
  541. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  542. }
  543. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  544. sess1 := db.Db.Desc("id")
  545. if req.EnterpriseId != 0 {
  546. sess1.And("enterprise_id = ?", req.EnterpriseId)
  547. }
  548. if len(req.MealKindList) > 0 {
  549. sess1.In("kind", req.MealKindList)
  550. }
  551. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  552. if err != nil {
  553. logx.Error(err)
  554. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  555. return
  556. }
  557. var exportOutTradeNoList []string
  558. for _, v := range mm {
  559. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  560. }
  561. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  562. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  563. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定餐数", "订单价格", "支付时间", "订单状态"}
  564. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  565. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  566. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  567. if err != nil {
  568. logx.Error(err)
  569. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  570. panic(err)
  571. }
  572. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  573. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  574. j := 3 //表头前两行用了,只能从第三行开始
  575. for _, v := range m {
  576. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  577. continue
  578. }
  579. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  580. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  581. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  582. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  583. break
  584. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  585. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  586. break
  587. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  588. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  589. break
  590. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  591. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  592. break
  593. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  594. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  595. break
  596. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  597. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  598. break
  599. }
  600. //预定餐数、预定日期
  601. var list []*model.CentralKitchenForSchoolUserWithDay
  602. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  603. if err1 != nil {
  604. logx.Error(err)
  605. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  606. return
  607. }
  608. var reserveDays string
  609. for _, v1 := range list {
  610. reserveDays += v1.Date + ", "
  611. }
  612. if v.Grade.Name == "" {
  613. v.Grade.Name = "教师"
  614. }
  615. if v.Class.Name == "" {
  616. v.Class.Name = "教师"
  617. }
  618. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  619. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  620. j++
  621. }
  622. //将文件保存至服务器
  623. xlsx.SaveAs("./static" + downloadPath)
  624. break
  625. case 2:
  626. //退款申请表
  627. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  628. downloadPath = "/export/central_kitchen_for_school/" + fileName
  629. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  630. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  631. //查找退款申请数据
  632. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  633. if req.EnterpriseId != 0 {
  634. sess.And("enterprise.id =?", req.EnterpriseId)
  635. }
  636. if req.StartDate != "" {
  637. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  638. }
  639. if req.EndDate != "" {
  640. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  641. }
  642. if len(req.MealKindList) > 0 {
  643. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  644. }
  645. if req.EnterPriseKind != 0 {
  646. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  647. }
  648. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  649. _, err := sess.
  650. 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").
  651. 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").
  652. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  653. Join("LEFT", "user", "user_identity.uid = user.id").
  654. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  655. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  656. Join("LEFT", "class", "class_with_user.class_id = class.id").
  657. Join("LEFT", "grade", "class.grade_id = grade.id").
  658. Desc("central_kitchen_for_school_user_refund_day.id").
  659. FindAndCount(&m)
  660. if err != nil {
  661. logx.Error(err)
  662. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  663. return
  664. }
  665. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  666. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  667. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  668. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  669. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  670. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  671. if err != nil {
  672. logx.Error(err)
  673. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  674. panic(err)
  675. }
  676. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  677. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  678. j := 3 //表头前两行用了,只能从第三行开始
  679. for _, v := range m {
  680. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  681. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  682. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  683. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  684. break
  685. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  686. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  687. break
  688. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  689. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  690. break
  691. }
  692. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  693. switch v.CentralKitchenForSchoolUserRefundDay.State {
  694. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  695. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  696. break
  697. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  698. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  699. break
  700. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  701. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  702. break
  703. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  704. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  705. break
  706. }
  707. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  708. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  709. j++
  710. }
  711. //将文件保存至服务器
  712. xlsx.SaveAs("./static" + downloadPath)
  713. break
  714. case 3:
  715. //学校预定统计表
  716. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  717. downloadPath = "/export/central_kitchen_for_school/" + fileName
  718. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  719. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  720. if req.EnterpriseId != 0 {
  721. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  722. }
  723. var months []time.Month
  724. if req.StartDate != "" {
  725. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  726. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  727. _, sMonth, _ := sDate.Date()
  728. _, eMonth, _ := eDate.Date()
  729. diffMonth := sMonth - eMonth
  730. if diffMonth == 0 {
  731. //TODO::查询一个月
  732. months = append(months, sMonth)
  733. } else {
  734. for i := sMonth; i <= eMonth; i++ {
  735. months = append(months, i)
  736. }
  737. }
  738. }
  739. if len(months) > 0 {
  740. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  741. }
  742. //if req.EndDate != "" {
  743. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  744. //}
  745. if len(req.MealKindList) > 0 {
  746. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  747. }
  748. if req.EnterPriseKind != 0 {
  749. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  750. }
  751. _, err := sess.
  752. 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").
  753. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  754. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  755. Join("LEFT", "user", "user.id = user_identity.uid").
  756. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  757. Join("LEFT", "class", "class_with_user.class_id = class.id").
  758. Join("LEFT", "grade", "class.grade_id = grade.id").
  759. FindAndCount(&m)
  760. if err != nil {
  761. logx.Error(err)
  762. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  763. return
  764. }
  765. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  766. centralKitchenForSchoolUserWithDayDb.Set(0)
  767. var list = map[string]map[string]map[string]map[string]int64{}
  768. var xlsFileName, xlsDownloadPath []string
  769. for _, v := range m {
  770. if list[v.Enterprise.Name] == nil {
  771. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  772. }
  773. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  774. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  775. }
  776. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  777. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  778. }
  779. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  780. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  781. req.StartDate, req.EndDate, req.MealKindList)
  782. if err2 != nil {
  783. logx.Error(err)
  784. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  785. return
  786. }
  787. time.Sleep(20 * time.Millisecond) // 20毫秒
  788. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  789. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  790. }
  791. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  792. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  793. }
  794. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  795. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  796. }
  797. }
  798. for k1, v1 := range list {
  799. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  800. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  801. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  802. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  803. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  804. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  805. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  806. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  807. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  808. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  809. if err != nil {
  810. logx.Error(err)
  811. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  812. panic(err)
  813. }
  814. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  815. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  816. j := 3 //表头前两行用了,只能从第三行开始
  817. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  818. tmpEnterpriseDb := db.EnterpriseDb{}
  819. tmpEnterpriseDb.Set()
  820. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  821. if err11 != nil {
  822. logx.Error(err11)
  823. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
  824. panic(err11)
  825. }
  826. tmpClassDb := db.ClassDb{}
  827. tmpClassDb.Set(0)
  828. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  829. if err != nil {
  830. logx.Error(err)
  831. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  832. panic(err)
  833. }
  834. for _, class := range *classList {
  835. for k2, v2 := range v1 {
  836. if k2 == "" {
  837. //若年级为空,则为教师餐
  838. k2 = "教师餐"
  839. }
  840. for k3, v3 := range v2 {
  841. if class.Name != k3 {
  842. continue
  843. }
  844. if k3 == "" {
  845. //若班级为空,则为教师餐
  846. k3 = "教师餐"
  847. }
  848. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  849. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  850. sumClassNum++
  851. sumBreakfast += v3["breakfast"]
  852. sumLunch += v3["lunch"]
  853. sumDinner += v3["dinner"]
  854. j++
  855. }
  856. }
  857. }
  858. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  859. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  860. //将文件保存至服务器
  861. err1 := xlsx.SaveAs(tmpDownloadPath)
  862. if err1 != nil {
  863. logx.Error(err1)
  864. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  865. panic(err1)
  866. }
  867. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  868. xlsFileName = append(xlsFileName, tmpFileName)
  869. }
  870. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  871. if err != nil {
  872. logx.Error(err)
  873. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  874. panic(err)
  875. }
  876. defer archive.Close()
  877. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  878. for k, v := range xlsDownloadPath {
  879. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  880. if err1 != nil {
  881. logx.Error(err1)
  882. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  883. panic(err1)
  884. }
  885. f, err1 := os.Open(v) //打开待压缩的文件
  886. if err1 != nil {
  887. logx.Error(err1)
  888. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  889. panic(err1)
  890. }
  891. if _, err1 = io.Copy(w, f); err != nil {
  892. logx.Error(err1)
  893. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  894. panic(err1)
  895. }
  896. if err1 != nil {
  897. logx.Error(err1)
  898. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  899. panic(err1)
  900. }
  901. f.Close()
  902. os.RemoveAll(v) //TODO::移除源文件
  903. }
  904. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  905. if err != nil {
  906. panic(err)
  907. }
  908. break
  909. case 4:
  910. //班级明细表
  911. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  912. downloadPath = "/export/central_kitchen_for_school/" + fileName
  913. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  914. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  915. if req.EnterpriseId != 0 {
  916. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  917. }
  918. var months []time.Month
  919. if req.StartDate != "" {
  920. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  921. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  922. _, sMonth, _ := sDate.Date()
  923. _, eMonth, _ := eDate.Date()
  924. diffMonth := sMonth - eMonth
  925. if diffMonth == 0 {
  926. //TODO::查询一个月
  927. months = append(months, sMonth)
  928. } else {
  929. for i := sMonth; i <= eMonth; i++ {
  930. months = append(months, i)
  931. }
  932. }
  933. }
  934. if len(months) > 0 {
  935. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  936. }
  937. //if req.EndDate != "" {
  938. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  939. //}
  940. if len(req.MealKindList) > 0 {
  941. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  942. }
  943. if req.EnterPriseKind != 0 {
  944. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  945. }
  946. _, err := sess.
  947. 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").
  948. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  949. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  950. Join("LEFT", "user", "user.id = user_identity.uid").
  951. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  952. Join("LEFT", "class", "class_with_user.class_id = class.id").
  953. Join("LEFT", "grade", "class.grade_id = grade.id").
  954. Asc("class.id").
  955. FindAndCount(&m)
  956. if err != nil {
  957. logx.Error(err)
  958. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  959. return
  960. }
  961. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  962. var xlsFileName, xlsDownloadPath []string
  963. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  964. centralKitchenForSchoolUserWithDayDb.Set(0)
  965. for _, v := range m {
  966. if list[v.Enterprise.Name] == nil {
  967. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  968. }
  969. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  970. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  971. }
  972. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  973. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  974. }
  975. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  976. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  977. }
  978. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  979. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  980. req.StartDate, req.EndDate, req.MealKindList)
  981. if err2 != nil {
  982. logx.Error(err)
  983. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  984. return
  985. }
  986. if count == 0 {
  987. continue
  988. }
  989. time.Sleep(20 * time.Millisecond) // 20毫秒
  990. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  991. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  992. }
  993. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  994. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  995. }
  996. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  997. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  998. }
  999. }
  1000. for k1, v1 := range list {
  1001. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1002. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1003. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1004. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1005. //查询当前学校下的班级升序
  1006. tmpEnterpriseDb := db.EnterpriseDb{}
  1007. tmpEnterpriseDb.Set()
  1008. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1009. if err11 != nil {
  1010. logx.Error(err11)
  1011. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1012. panic(err11)
  1013. }
  1014. tmpClassDb := db.ClassDb{}
  1015. tmpClassDb.Set(0)
  1016. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1017. if err != nil {
  1018. logx.Error(err)
  1019. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1020. panic(err)
  1021. }
  1022. for _, class := range *classList {
  1023. for k2, v2 := range v1 {
  1024. if k2 == "" {
  1025. //若年级为空,则为教师餐
  1026. continue
  1027. }
  1028. for k3, v3 := range v2 {
  1029. if class.Name != k3 {
  1030. continue
  1031. }
  1032. xlsx.NewSheet(k3)
  1033. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  1034. xlsx.SetSheetRow(k3, "A1", &titleList)
  1035. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1036. xlsx.SetSheetRow(k3, "A2", &titleList)
  1037. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1038. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1039. if err != nil {
  1040. logx.Error(err)
  1041. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1042. panic(err)
  1043. }
  1044. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1045. xlsx.SetColWidth(k3, "A", "D", 20)
  1046. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  1047. j := 3 //表头前两行用了,只能从第三行开始
  1048. for k4, v4 := range v3 {
  1049. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  1050. continue
  1051. }
  1052. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1053. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1054. sumBreakfast += v4["breakfast"]
  1055. sumLunch += v4["lunch"]
  1056. sumDinner += v4["dinner"]
  1057. j++
  1058. }
  1059. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1060. "合计", sumBreakfast, sumLunch, sumDinner})
  1061. }
  1062. }
  1063. }
  1064. //将文件保存至服务器
  1065. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1066. err1 := xlsx.SaveAs(tmpDownloadPath)
  1067. if err1 != nil {
  1068. logx.Error(err1)
  1069. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1070. panic(err1)
  1071. }
  1072. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1073. xlsFileName = append(xlsFileName, tmpFileName)
  1074. }
  1075. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1076. if err != nil {
  1077. logx.Error(err)
  1078. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1079. panic(err)
  1080. }
  1081. defer archive.Close()
  1082. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1083. for k, v := range xlsDownloadPath {
  1084. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1085. if err1 != nil {
  1086. logx.Error(err1)
  1087. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1088. panic(err1)
  1089. }
  1090. f, err1 := os.Open(v) //打开待压缩的文件
  1091. if err1 != nil {
  1092. logx.Error(err1)
  1093. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1094. panic(err1)
  1095. }
  1096. if _, err1 = io.Copy(w, f); err != nil {
  1097. logx.Error(err1)
  1098. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1099. panic(err1)
  1100. }
  1101. if err1 != nil {
  1102. logx.Error(err1)
  1103. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1104. panic(err1)
  1105. }
  1106. f.Close()
  1107. os.RemoveAll(v) //TODO::移除源文件
  1108. }
  1109. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1110. if err != nil {
  1111. panic(err)
  1112. }
  1113. break
  1114. case 5:
  1115. //退款明细表
  1116. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1117. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1118. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1119. if req.EnterpriseId != 0 {
  1120. sess.And("enterprise.id =?", req.EnterpriseId)
  1121. }
  1122. if req.StartDate != "" {
  1123. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1124. }
  1125. if req.EndDate != "" {
  1126. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1127. }
  1128. if len(req.MealKindList) > 0 {
  1129. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  1130. }
  1131. if req.EnterPriseKind != 0 {
  1132. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1133. }
  1134. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  1135. _, err := sess.
  1136. 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").
  1137. 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").
  1138. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1139. Join("LEFT", "user", "user_identity.uid = user.id").
  1140. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1141. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1142. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1143. Join("LEFT", "grade", "class.grade_id = grade.id").
  1144. Desc("central_kitchen_for_school_user_refund_day.id").
  1145. FindAndCount(&m)
  1146. if err != nil {
  1147. logx.Error(err)
  1148. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1149. return
  1150. }
  1151. var list = map[string]map[string]map[string]map[string]map[string]int{}
  1152. var xlsFileName, xlsDownloadPath []string
  1153. for _, v := range m {
  1154. if list[v.Enterprise.Name] == nil {
  1155. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1156. }
  1157. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1158. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1159. }
  1160. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1161. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1162. }
  1163. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1164. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1165. }
  1166. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1167. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1168. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1169. }
  1170. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1171. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1172. }
  1173. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1174. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1175. }
  1176. }
  1177. for k1, v1 := range list {
  1178. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1179. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1180. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1181. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1182. for k2, v2 := range v1 {
  1183. if k2 == "" {
  1184. //若年级为空,则为教师餐
  1185. continue
  1186. }
  1187. for k3, v3 := range v2 {
  1188. xlsx.NewSheet(k3)
  1189. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1190. xlsx.SetSheetRow(k3, "A1", &titleList)
  1191. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1192. xlsx.SetSheetRow(k3, "A2", &titleList)
  1193. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1194. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1195. if err != nil {
  1196. logx.Error(err)
  1197. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1198. panic(err)
  1199. }
  1200. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1201. xlsx.SetColWidth(k3, "A", "D", 20)
  1202. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1203. j := 3 //表头前两行用了,只能从第三行开始
  1204. for k4, v4 := range v3 {
  1205. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1206. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1207. sumBreakfast += v4["breakfast"]
  1208. sumLunch += v4["lunch"]
  1209. sumDinner += v4["dinner"]
  1210. j++
  1211. }
  1212. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1213. "合计", sumBreakfast, sumLunch, sumDinner})
  1214. }
  1215. }
  1216. //将文件保存至服务器
  1217. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1218. err1 := xlsx.SaveAs(tmpDownloadPath)
  1219. if err1 != nil {
  1220. logx.Error(err1)
  1221. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1222. panic(err1)
  1223. }
  1224. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1225. xlsFileName = append(xlsFileName, tmpFileName)
  1226. }
  1227. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1228. if err != nil {
  1229. logx.Error(err)
  1230. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1231. panic(err)
  1232. }
  1233. defer archive.Close()
  1234. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1235. for k, v := range xlsDownloadPath {
  1236. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1237. if err1 != nil {
  1238. logx.Error(err1)
  1239. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1240. panic(err1)
  1241. }
  1242. f, err1 := os.Open(v) //打开待压缩的文件
  1243. if err1 != nil {
  1244. logx.Error(err1)
  1245. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1246. panic(err1)
  1247. }
  1248. if _, err1 = io.Copy(w, f); err != nil {
  1249. logx.Error(err1)
  1250. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1251. panic(err1)
  1252. }
  1253. if err1 != nil {
  1254. logx.Error(err1)
  1255. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1256. panic(err1)
  1257. }
  1258. f.Close()
  1259. os.RemoveAll(v) //TODO::移除源文件
  1260. }
  1261. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1262. if err != nil {
  1263. panic(err)
  1264. }
  1265. break
  1266. case 6:
  1267. //收款统计表
  1268. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1269. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1270. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1271. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1272. //查找数据
  1273. var m []model.Enterprise
  1274. sess := db.Db.Desc("id")
  1275. if req.EnterPriseKind != 0 {
  1276. sess.And("kind = ?", req.EnterPriseKind)
  1277. }
  1278. if req.EnterpriseId != 0 {
  1279. sess.And("id = ?", req.EnterpriseId)
  1280. }
  1281. err := sess.Find(&m)
  1282. if err != nil {
  1283. logx.Error(err)
  1284. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1285. return
  1286. }
  1287. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1288. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1289. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1290. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1291. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1292. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1293. if err != nil {
  1294. logx.Error(err)
  1295. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1296. panic(err)
  1297. }
  1298. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1299. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1300. j := 3 //表头前两行用了,只能从第三行开始
  1301. for _, v := range m {
  1302. var income, refund string
  1303. //统计收入
  1304. sql1 := fmt.Sprintf("SELECT sum(total_price) as total FROM central_kitchen_for_school_package_ord where state = 1 and enterprise_id = %d and create_at >= '%s' and create_at < '%s'", v.Id, req.StartDate, req.EndDate)
  1305. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1306. if err != nil {
  1307. logx.Error(err)
  1308. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1309. return
  1310. }
  1311. if mapArr1 != nil {
  1312. income = mapArr1[0]["total"]
  1313. }
  1314. //统计退款
  1315. //查询出当前学校下所有的学生
  1316. userIdentityDb := db.UserIdentityDb{}
  1317. userIdentityDb.Set(0)
  1318. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1319. var userIdentityIds []string
  1320. for _, v := range *userIdentities {
  1321. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1322. }
  1323. if len(userIdentityIds) != 0 {
  1324. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1325. 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'",
  1326. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1327. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1328. if err != nil {
  1329. logx.Error(err)
  1330. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1331. return
  1332. }
  1333. if mapArr2 != nil {
  1334. refund = mapArr2[0]["total"]
  1335. }
  1336. }
  1337. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1338. rateFee := realityAmount * 0.0038
  1339. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1340. j++
  1341. }
  1342. //将文件保存至服务器
  1343. xlsx.SaveAs("./static" + downloadPath)
  1344. break
  1345. case 7:
  1346. //班级收款明细表
  1347. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1348. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1349. //查找订单数据
  1350. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1351. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1352. if req.EnterpriseId != 0 {
  1353. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1354. }
  1355. if req.StartDate != "" {
  1356. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1357. }
  1358. if req.EndDate != "" {
  1359. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1360. }
  1361. if req.EnterPriseKind != 0 {
  1362. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1363. }
  1364. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1365. _, err := sess.
  1366. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1367. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1368. Join("LEFT", "user", "user.id = user_identity.uid").
  1369. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1370. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1371. Join("LEFT", "grade", "class.grade_id = grade.id").
  1372. FindAndCount(&m)
  1373. if err != nil {
  1374. logx.Error(err)
  1375. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1376. return
  1377. }
  1378. 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")
  1379. if req.EnterpriseId != 0 {
  1380. sess1.And("enterprise.id =?", req.EnterpriseId)
  1381. }
  1382. if req.StartDate != "" {
  1383. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1384. }
  1385. if req.EndDate != "" {
  1386. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1387. }
  1388. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1389. _, err = sess1.
  1390. 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").
  1391. 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").
  1392. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1393. Join("LEFT", "user", "user_identity.uid = user.id").
  1394. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1395. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1396. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1397. Join("LEFT", "grade", "class.grade_id = grade.id").
  1398. Desc("central_kitchen_for_school_user_refund_day.id").
  1399. FindAndCount(&mm)
  1400. if err != nil {
  1401. logx.Error(err)
  1402. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1403. return
  1404. }
  1405. var list = map[string]map[string]map[string]map[string]float64{}
  1406. for _, v := range m {
  1407. if list[v.Enterprise.Name] == nil {
  1408. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1409. }
  1410. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1411. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1412. }
  1413. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1414. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1415. }
  1416. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1417. }
  1418. for _, v := range mm {
  1419. if list[v.Enterprise.Name] == nil {
  1420. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1421. }
  1422. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1423. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1424. }
  1425. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1426. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1427. }
  1428. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1429. }
  1430. var xlsFileName, xlsDownloadPath []string
  1431. for k1, v1 := range list {
  1432. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1433. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1434. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1435. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1436. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1437. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1438. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1439. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1440. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1441. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1442. if err != nil {
  1443. logx.Error(err)
  1444. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1445. panic(err)
  1446. }
  1447. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1448. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1449. j := 3 //表头前两行用了,只能从第三行开始
  1450. var sumClassNum = int64(0)
  1451. var sumIncome, sumRefund = float64(0), float64(0)
  1452. for k2, v2 := range v1 {
  1453. if k2 == "" {
  1454. //若年级为空,则为教师餐
  1455. continue
  1456. }
  1457. for k3, v3 := range v2 {
  1458. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1459. sumClassNum++
  1460. sumIncome += v3["income"]
  1461. sumRefund += v3["refund"]
  1462. j++
  1463. }
  1464. }
  1465. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1466. "合计", sumClassNum, sumIncome, sumRefund})
  1467. //将文件保存至服务器
  1468. err1 := xlsx.SaveAs(tmpDownloadPath)
  1469. if err1 != nil {
  1470. logx.Error(err1)
  1471. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>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("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>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("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1490. panic(err1)
  1491. }
  1492. f, err1 := os.Open(v) //打开待压缩的文件
  1493. if err1 != nil {
  1494. logx.Error(err1)
  1495. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1496. panic(err1)
  1497. }
  1498. if _, err1 = io.Copy(w, f); err != nil {
  1499. logx.Error(err1)
  1500. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1501. panic(err1)
  1502. }
  1503. if err1 != nil {
  1504. logx.Error(err1)
  1505. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>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 8:
  1517. //学校预定数量统计表
  1518. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1519. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1520. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1521. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1522. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1523. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1524. if req.EnterpriseId != 0 {
  1525. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1526. }
  1527. var months []time.Month
  1528. if req.StartDate != "" {
  1529. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1530. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1531. _, sMonth, _ := sDate.Date()
  1532. _, eMonth, _ := eDate.Date()
  1533. diffMonth := sMonth - eMonth
  1534. if diffMonth == 0 {
  1535. //TODO::查询一个月
  1536. months = append(months, sMonth)
  1537. } else {
  1538. for i := sMonth; i <= eMonth; i++ {
  1539. months = append(months, i)
  1540. }
  1541. }
  1542. }
  1543. if len(months) > 0 {
  1544. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1545. }
  1546. //if req.EndDate != "" {
  1547. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1548. //}
  1549. if len(req.MealKindList) > 0 {
  1550. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1551. }
  1552. if req.EnterPriseKind != 0 {
  1553. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1554. }
  1555. _, err := sess.
  1556. 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").
  1557. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1558. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1559. FindAndCount(&m)
  1560. if err != nil {
  1561. logx.Error(err)
  1562. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1563. return
  1564. }
  1565. var list = map[string]map[string]int64{}
  1566. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1567. centralKitchenForSchoolUserWithDayDb.Set(0)
  1568. for _, v := range m {
  1569. if list[v.Enterprise.Name] == nil {
  1570. list[v.Enterprise.Name] = map[string]int64{}
  1571. }
  1572. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1573. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1574. req.StartDate, req.EndDate, req.MealKindList)
  1575. if err2 != nil {
  1576. logx.Error(err)
  1577. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1578. return
  1579. }
  1580. for _, value := range req.MealKindList {
  1581. if value == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1582. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1583. if err2 != nil {
  1584. logx.Error(err)
  1585. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1586. return
  1587. }
  1588. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1589. }
  1590. if value == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1591. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForLunch, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1592. if err2 != nil {
  1593. logx.Error(err)
  1594. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1595. return
  1596. }
  1597. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1598. }
  1599. if value == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1600. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForDinner, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1601. if err2 != nil {
  1602. logx.Error(err)
  1603. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1604. return
  1605. }
  1606. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1607. }
  1608. }
  1609. list[v.Enterprise.Name]["count"] += count
  1610. time.Sleep(20 * time.Millisecond) // 20毫秒
  1611. }
  1612. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1613. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1614. titleList = []string{"学校名称", "预定总数",
  1615. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1616. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1617. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1618. }
  1619. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1620. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1621. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1622. if err != nil {
  1623. logx.Error(err)
  1624. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1625. panic(err)
  1626. }
  1627. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1628. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1629. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1630. j := 3 //表头前两行用了,只能从第三行开始
  1631. total := int64(0)
  1632. total1 := int64(0)
  1633. total2 := int64(0)
  1634. total3 := int64(0)
  1635. for k, v := range list {
  1636. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1637. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1638. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1639. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1640. total += v["count"]
  1641. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1642. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1643. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1644. j++
  1645. }
  1646. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1647. "合计", total, total1, total2, total3})
  1648. //将文件保存至服务器
  1649. xlsx.SaveAs("./static" + downloadPath)
  1650. break
  1651. }
  1652. //新增数据
  1653. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1654. centralKitchenForSchoolExportRecordsDb.Set()
  1655. marshal, _ := json.Marshal(req)
  1656. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1657. Name: fileName,
  1658. DownloadPath: downloadPath,
  1659. Kind: kind,
  1660. ReqContent: string(marshal),
  1661. CreateAt: now.Format("2006-01-02 15:04:05"),
  1662. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1663. })
  1664. }
  1665. }
  1666. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1667. var titleList []string
  1668. var fileName, downloadPath string
  1669. var now = time.Now()
  1670. xlsx := excelize.NewFile()
  1671. //数据对比
  1672. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1673. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1674. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1675. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1676. if req.EnterpriseId != 0 {
  1677. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1678. }
  1679. if req.Date1[0] != "" {
  1680. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1681. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  1682. }
  1683. if req.Date1[1] != "" {
  1684. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[1], time.Local)
  1685. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  1686. }
  1687. if len(req.MealKindList) > 0 {
  1688. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1689. }
  1690. if req.EnterPriseKind != 0 {
  1691. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1692. }
  1693. _, err := sess.
  1694. 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").
  1695. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1696. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1697. Join("LEFT", "user", "user.id = user_identity.uid").
  1698. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1699. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1700. Join("LEFT", "grade", "class.grade_id = grade.id").
  1701. FindAndCount(&m)
  1702. if err != nil {
  1703. logx.Error(err)
  1704. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1705. return
  1706. }
  1707. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1708. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1709. if req.EnterpriseId != 0 {
  1710. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1711. }
  1712. if req.Date2[0] != "" {
  1713. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1714. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  1715. }
  1716. if req.Date2[1] != "" {
  1717. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[1], time.Local)
  1718. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  1719. }
  1720. if len(req.MealKindList) > 0 {
  1721. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1722. }
  1723. _, err1 := sess1.
  1724. 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").
  1725. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1726. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1727. Join("LEFT", "user", "user.id = user_identity.uid").
  1728. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1729. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1730. Join("LEFT", "grade", "class.grade_id = grade.id").
  1731. FindAndCount(&m1)
  1732. if err1 != nil {
  1733. logx.Error(err1)
  1734. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1735. return
  1736. }
  1737. var list = map[string]map[string]map[string]map[string]string{}
  1738. for _, v := range m {
  1739. if list[v.Enterprise.Name] == nil {
  1740. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1741. }
  1742. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1743. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1744. }
  1745. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1746. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1747. }
  1748. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1749. }
  1750. var list1 = map[string]map[string]map[string]map[string]string{}
  1751. for _, v := range m1 {
  1752. if list1[v.Enterprise.Name] == nil {
  1753. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1754. }
  1755. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1756. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1757. }
  1758. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1759. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1760. }
  1761. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1762. }
  1763. var xlsFileName, xlsDownloadPath []string
  1764. for k1, v1 := range list {
  1765. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1766. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1767. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1768. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1769. //查询当前学校下的班级升序
  1770. tmpEnterpriseDb := db.EnterpriseDb{}
  1771. tmpEnterpriseDb.Set()
  1772. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1773. if err11 != nil {
  1774. logx.Error(err11)
  1775. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1776. panic(err11)
  1777. }
  1778. tmpClassDb := db.ClassDb{}
  1779. tmpClassDb.Set(0)
  1780. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1781. if err != nil {
  1782. logx.Error(err)
  1783. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1784. panic(err)
  1785. }
  1786. for _, class := range *classList {
  1787. for k2, v2 := range v1 {
  1788. if k2 == "" {
  1789. //若年级为空,则为教师
  1790. continue
  1791. }
  1792. for k3, v3 := range v2 {
  1793. if class.Name != k3 {
  1794. continue
  1795. }
  1796. xlsx.NewSheet(k3)
  1797. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  1798. xlsx.SetSheetRow(k3, "A1", &titleList)
  1799. titleList = []string{"姓名", "家长联系电话"}
  1800. xlsx.SetSheetRow(k3, "A2", &titleList)
  1801. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  1802. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1803. if err != nil {
  1804. logx.Error(err)
  1805. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1806. panic(err)
  1807. }
  1808. xlsx.SetCellStyle(k3, "A1", "B1", style)
  1809. xlsx.SetColWidth(k3, "A", "B", 40)
  1810. j := 2 //表头被第一行用了,只能从第二行开始
  1811. for k4, v4 := range v3 {
  1812. //TODO::判断当前用户是否在新的月份里面
  1813. fmt.Println("11111111111111", list1[k1])
  1814. fmt.Println("22222222222222", list1[k1][k2])
  1815. fmt.Println("33333333333333", list1[k1][k2][k3])
  1816. fmt.Println("44444444444444", list1[k1][k2][k3][k4])
  1817. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1818. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1819. k4, v4})
  1820. j++
  1821. }
  1822. }
  1823. }
  1824. }
  1825. }
  1826. //将文件保存至服务器
  1827. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1828. err1 := xlsx.SaveAs(tmpDownloadPath)
  1829. if err1 != nil {
  1830. logx.Error(err1)
  1831. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1832. panic(err1)
  1833. }
  1834. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1835. xlsFileName = append(xlsFileName, tmpFileName)
  1836. }
  1837. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1838. if err != nil {
  1839. logx.Error(err)
  1840. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1841. panic(err)
  1842. }
  1843. defer archive.Close()
  1844. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1845. for k, v := range xlsDownloadPath {
  1846. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1847. if err1 != nil {
  1848. logx.Error(err1)
  1849. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1850. panic(err1)
  1851. }
  1852. f, err1 := os.Open(v) //打开待压缩的文件
  1853. if err1 != nil {
  1854. logx.Error(err1)
  1855. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1856. panic(err1)
  1857. }
  1858. if _, err1 = io.Copy(w, f); err != nil {
  1859. logx.Error(err1)
  1860. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1861. panic(err1)
  1862. }
  1863. if err1 != nil {
  1864. logx.Error(err1)
  1865. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1866. panic(err1)
  1867. }
  1868. f.Close()
  1869. os.RemoveAll(v) //TODO::移除源文件
  1870. }
  1871. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1872. if err != nil {
  1873. panic(err)
  1874. }
  1875. //新增数据
  1876. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1877. centralKitchenForSchoolExportRecordsDb.Set()
  1878. marshal, _ := json.Marshal(req)
  1879. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1880. Name: fileName,
  1881. DownloadPath: downloadPath,
  1882. Kind: 9, //数据对比
  1883. ReqContent: string(marshal),
  1884. CreateAt: now.Format("2006-01-02 15:04:05"),
  1885. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1886. })
  1887. }
  1888. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1889. mealZh = "未知"
  1890. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1891. for _, v := range md.SelfSupportForMealTimeMapping {
  1892. //判断时间大小
  1893. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  1894. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  1895. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  1896. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  1897. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1898. mealZh = v["name"]
  1899. }
  1900. }
  1901. return
  1902. }