智慧食堂
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 76 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  234. _, err := sess.
  235. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  236. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  237. Join("LEFT", "user", "user.id = user_identity.uid").
  238. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  239. Join("LEFT", "class", "class_with_user.class_id = class.id").
  240. Join("LEFT", "grade", "class.grade_id = grade.id").
  241. FindAndCount(&m)
  242. if err != nil {
  243. logx.Error(err)
  244. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  245. return
  246. }
  247. //整合数据
  248. var data = map[string]map[string]map[string]map[string]interface{}{}
  249. for _, vv := range m {
  250. if data[vv.Enterprise.Name] == nil {
  251. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  252. }
  253. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  254. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  255. }
  256. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  257. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  258. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  259. }
  260. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  261. "id": vv.UserIdentity.IdNo,
  262. }
  263. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  264. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  265. }
  266. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  267. 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)
  268. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  269. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  270. }
  271. xlsx.SetSheetRow("", "A1", &titleList)
  272. sheetIndex := 2 //第一个表是模板表
  273. j := 3 //表头前两行被占用了,只能从第三行开始
  274. for k, v := range data {
  275. xlsx.NewSheet(k) //1、新建表格
  276. xlsx.CopySheet(1, sheetIndex)
  277. for kk, vv := range v {
  278. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  279. vv["早餐"]["num"], vv["早餐"]["amount"],
  280. vv["午餐"]["num"], vv["午餐"]["amount"],
  281. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  282. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  283. vv["未知"]["num"], vv["未知"]["amount"],
  284. vv["合计"]["num"], vv["合计"]["amount"],
  285. })
  286. j++
  287. }
  288. sheetIndex++
  289. }
  290. xlsx.DeleteSheet("template") //删除模板表
  291. //将文件保存至服务器
  292. xlsx.SaveAs("./static" + downloadPath)
  293. break
  294. case 2:
  295. //商家分餐数据统计表
  296. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  297. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  298. downloadPath = "/export/self_support_for_school/" + fileName
  299. //查询出所有的设备
  300. var deviceList []*model.Device
  301. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  302. if err != nil {
  303. logx.Error(err)
  304. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  305. return
  306. }
  307. var deviceListMapping = map[string]model.Device{}
  308. for _, v := range deviceList {
  309. deviceListMapping[v.DeviceSn] = *v
  310. }
  311. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  312. sess := db.Db.Desc("self_support_for_school_ord.id")
  313. if req.EnterpriseId != 0 {
  314. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  315. }
  316. if req.StartDate != "" {
  317. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  318. }
  319. if req.EndDate != "" {
  320. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  321. }
  322. _, err = sess.
  323. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  324. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  325. Join("LEFT", "user", "user.id = user_identity.uid").
  326. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  327. Join("LEFT", "class", "class_with_user.class_id = class.id").
  328. Join("LEFT", "grade", "class.grade_id = grade.id").
  329. FindAndCount(&m)
  330. if err != nil {
  331. logx.Error(err)
  332. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  333. return
  334. }
  335. //整合数据
  336. var data = map[string]map[string]map[string]map[string]float64{}
  337. for _, vv := range m {
  338. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  339. if data[vv.Enterprise.Name] == nil {
  340. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  341. }
  342. if data[vv.Enterprise.Name][shopName] == nil {
  343. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  344. }
  345. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  346. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  347. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  348. }
  349. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  350. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  351. }
  352. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  353. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  354. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  355. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  356. }
  357. xlsx.SetSheetRow("", "A1", &titleList)
  358. sheetIndex := 2 //第一个表是模板表
  359. for k, v := range data {
  360. if k == "" {
  361. k = "未知"
  362. }
  363. xlsx.NewSheet(k) //1、新建表格
  364. xlsx.CopySheet(1, sheetIndex)
  365. j := 4 //表头前三行被占用了,只能从第四行开始
  366. for kk, vv := range v {
  367. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  368. vv["早餐"]["num"], vv["早餐"]["amount"],
  369. vv["午餐"]["num"], vv["午餐"]["amount"],
  370. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  371. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  372. vv["合计"]["num"], vv["合计"]["amount"],
  373. })
  374. j++
  375. }
  376. sheetIndex++
  377. }
  378. xlsx.DeleteSheet("template") //删除模板表
  379. //将文件保存至服务器
  380. xlsx.SaveAs("./static" + downloadPath)
  381. break
  382. }
  383. //新增数据
  384. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  385. selfSupportForSchoolExportRecordsDb.Set()
  386. marshal, _ := json.Marshal(req)
  387. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  388. Name: fileName,
  389. DownloadPath: downloadPath,
  390. Kind: req.Kind,
  391. ReqContent: string(marshal),
  392. CreateAt: now.Format("2006-01-02 15:04:05"),
  393. UpdateAt: now.Format("2006-01-02 15:04:05"),
  394. })
  395. }
  396. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  397. var titleList []string
  398. var fileName, downloadPath string
  399. var now = time.Now()
  400. for _, kind := range req.Kinds {
  401. xlsx := excelize.NewFile()
  402. switch kind {
  403. case 1:
  404. //订单列表
  405. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  406. downloadPath = "/export/central_kitchen_for_school/" + fileName
  407. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  408. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  409. //查找订单数据
  410. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  411. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  412. if req.EnterpriseId != 0 {
  413. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  414. }
  415. if req.StartDate != "" {
  416. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  417. }
  418. if req.EndDate != "" {
  419. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  420. }
  421. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  422. _, err := sess.
  423. Join("LEFT", "user_identity", "central_kitchen_for_school_package_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("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  433. return
  434. }
  435. //查找预定数据
  436. var outTradeNoList []string
  437. for _, v := range m {
  438. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  439. }
  440. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  441. sess1 := db.Db.Desc("id")
  442. if req.EnterpriseId != 0 {
  443. sess1.And("enterprise_id = ?", req.EnterpriseId)
  444. }
  445. if len(req.MealKindList) > 0 {
  446. sess1.In("kind", req.MealKindList)
  447. }
  448. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  449. if err != nil {
  450. logx.Error(err)
  451. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  452. return
  453. }
  454. var exportOutTradeNoList []string
  455. for _, v := range mm {
  456. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  457. }
  458. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  459. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  460. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定天数", "订单价格", "支付时间", "订单状态"}
  461. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  462. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  463. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  464. if err != nil {
  465. logx.Error(err)
  466. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  467. panic(err)
  468. }
  469. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  470. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  471. j := 3 //表头前两行用了,只能从第三行开始
  472. for _, v := range m {
  473. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  474. continue
  475. }
  476. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  477. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  478. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  479. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  480. break
  481. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  482. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  483. break
  484. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  485. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  486. break
  487. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  488. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  489. break
  490. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  491. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  492. break
  493. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  494. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  495. break
  496. }
  497. //预定天数、预定日期
  498. var list []*model.CentralKitchenForSchoolUserWithDay
  499. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  500. if err1 != nil {
  501. logx.Error(err)
  502. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  503. return
  504. }
  505. var reserveDays string
  506. for _, v1 := range list {
  507. reserveDays += v1.Date
  508. }
  509. if v.Grade.Name == "" {
  510. v.Grade.Name = "教师"
  511. }
  512. if v.Class.Name == "" {
  513. v.Class.Name = "教师"
  514. }
  515. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  516. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  517. j++
  518. }
  519. //将文件保存至服务器
  520. xlsx.SaveAs("./static" + downloadPath)
  521. break
  522. case 2:
  523. //退款申请表
  524. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  525. downloadPath = "/export/central_kitchen_for_school/" + fileName
  526. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  527. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  528. //查找退款申请数据
  529. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  530. if req.EnterpriseId != 0 {
  531. sess.And("enterprise.id =?", req.EnterpriseId)
  532. }
  533. if req.StartDate != "" {
  534. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  535. }
  536. if req.EndDate != "" {
  537. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  538. }
  539. if len(req.MealKindList) > 0 {
  540. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  541. }
  542. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  543. _, err := sess.
  544. 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").
  545. 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").
  546. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  547. Join("LEFT", "user", "user_identity.uid = user.id").
  548. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  549. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  550. Join("LEFT", "class", "class_with_user.class_id = class.id").
  551. Join("LEFT", "grade", "class.grade_id = grade.id").
  552. Desc("central_kitchen_for_school_user_refund_day.id").
  553. FindAndCount(&m)
  554. if err != nil {
  555. logx.Error(err)
  556. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  557. return
  558. }
  559. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  560. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  561. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  562. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  563. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  564. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  565. if err != nil {
  566. logx.Error(err)
  567. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  568. panic(err)
  569. }
  570. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  571. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  572. j := 3 //表头前两行用了,只能从第三行开始
  573. for _, v := range m {
  574. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  575. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  576. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  577. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  578. break
  579. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  580. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  581. break
  582. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  583. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  584. break
  585. }
  586. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  587. switch v.CentralKitchenForSchoolUserRefundDay.State {
  588. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  589. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  590. break
  591. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  592. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  593. break
  594. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  595. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  596. break
  597. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  598. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  599. break
  600. }
  601. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  602. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  603. j++
  604. }
  605. //将文件保存至服务器
  606. xlsx.SaveAs("./static" + downloadPath)
  607. break
  608. case 3:
  609. //学校预定统计表
  610. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  611. downloadPath = "/export/central_kitchen_for_school/" + fileName
  612. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  613. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  614. if req.EnterpriseId != 0 {
  615. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  616. }
  617. if req.StartDate != "" {
  618. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  619. }
  620. if req.EndDate != "" {
  621. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  622. }
  623. if len(req.MealKindList) > 0 {
  624. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  625. }
  626. _, err := sess.
  627. 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").
  628. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  629. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  630. Join("LEFT", "user", "user.id = user_identity.uid").
  631. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  632. Join("LEFT", "class", "class_with_user.class_id = class.id").
  633. Join("LEFT", "grade", "class.grade_id = grade.id").
  634. FindAndCount(&m)
  635. if err != nil {
  636. logx.Error(err)
  637. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  638. return
  639. }
  640. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  641. centralKitchenForSchoolUserWithDayDb.Set(0)
  642. var list = map[string]map[string]map[string]map[string]int64{}
  643. var xlsFileName, xlsDownloadPath []string
  644. for _, v := range m {
  645. if list[v.Enterprise.Name] == nil {
  646. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  647. }
  648. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  649. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  650. }
  651. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  652. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  653. }
  654. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  655. if err2 != nil {
  656. logx.Error(err)
  657. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  658. return
  659. }
  660. time.Sleep(50 * time.Millisecond) // 50毫秒
  661. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  662. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  663. }
  664. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  665. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  666. }
  667. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  668. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  669. }
  670. }
  671. for k1, v1 := range list {
  672. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  673. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  674. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  675. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  676. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  677. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  678. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  679. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  680. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  681. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  682. if err != nil {
  683. logx.Error(err)
  684. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  685. panic(err)
  686. }
  687. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  688. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  689. j := 3 //表头前两行用了,只能从第三行开始
  690. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  691. for k2, v2 := range v1 {
  692. if k2 == "" {
  693. //若年级为空,则为教师餐
  694. k2 = "教师餐"
  695. }
  696. for k3, v3 := range v2 {
  697. if k3 == "" {
  698. //若班级为空,则为教师餐
  699. k3 = "教师餐"
  700. }
  701. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  702. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  703. sumClassNum++
  704. sumBreakfast += v3["breakfast"]
  705. sumLunch += v3["lunch"]
  706. sumDinner += v3["dinner"]
  707. j++
  708. }
  709. }
  710. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  711. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  712. //将文件保存至服务器
  713. err1 := xlsx.SaveAs(tmpDownloadPath)
  714. if err1 != nil {
  715. logx.Error(err1)
  716. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  717. panic(err1)
  718. }
  719. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  720. xlsFileName = append(xlsFileName, tmpFileName)
  721. }
  722. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  723. if err != nil {
  724. logx.Error(err)
  725. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  726. panic(err)
  727. }
  728. defer archive.Close()
  729. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  730. for k, v := range xlsDownloadPath {
  731. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  732. if err1 != nil {
  733. logx.Error(err1)
  734. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  735. panic(err1)
  736. }
  737. f, err1 := os.Open(v) //打开待压缩的文件
  738. if err1 != nil {
  739. logx.Error(err1)
  740. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  741. panic(err1)
  742. }
  743. if _, err1 = io.Copy(w, f); err != nil {
  744. logx.Error(err1)
  745. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  746. panic(err1)
  747. }
  748. if err1 != nil {
  749. logx.Error(err1)
  750. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  751. panic(err1)
  752. }
  753. f.Close()
  754. os.RemoveAll(v) //TODO::移除源文件
  755. }
  756. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  757. if err != nil {
  758. panic(err)
  759. }
  760. break
  761. case 4:
  762. //班级明细表
  763. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  764. downloadPath = "/export/central_kitchen_for_school/" + fileName
  765. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  766. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  767. if req.EnterpriseId != 0 {
  768. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  769. }
  770. if req.StartDate != "" {
  771. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  772. }
  773. if req.EndDate != "" {
  774. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  775. }
  776. if len(req.MealKindList) > 0 {
  777. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  778. }
  779. _, err := sess.
  780. 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").
  781. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  782. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  783. Join("LEFT", "user", "user.id = user_identity.uid").
  784. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  785. Join("LEFT", "class", "class_with_user.class_id = class.id").
  786. Join("LEFT", "grade", "class.grade_id = grade.id").
  787. FindAndCount(&m)
  788. if err != nil {
  789. logx.Error(err)
  790. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  791. return
  792. }
  793. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  794. var xlsFileName, xlsDownloadPath []string
  795. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  796. centralKitchenForSchoolUserWithDayDb.Set(0)
  797. for _, v := range m {
  798. if list[v.Enterprise.Name] == nil {
  799. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  800. }
  801. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  802. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  803. }
  804. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  805. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  806. }
  807. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  808. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  809. }
  810. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  811. if err2 != nil {
  812. logx.Error(err)
  813. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  814. return
  815. }
  816. time.Sleep(50 * time.Millisecond) // 50毫秒
  817. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  818. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  819. }
  820. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  821. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  822. }
  823. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  824. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  825. }
  826. }
  827. for k1, v1 := range list {
  828. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  829. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  830. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  831. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  832. for k2, v2 := range v1 {
  833. if k2 == "" {
  834. //若年级为空,则为教师餐
  835. continue
  836. }
  837. for k3, v3 := range v2 {
  838. xlsx.NewSheet(k3)
  839. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  840. xlsx.SetSheetRow(k3, "A1", &titleList)
  841. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  842. xlsx.SetSheetRow(k3, "A2", &titleList)
  843. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  844. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  845. if err != nil {
  846. logx.Error(err)
  847. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  848. panic(err)
  849. }
  850. xlsx.SetCellStyle(k3, "A1", "D1", style)
  851. xlsx.SetColWidth(k3, "A", "D", 20)
  852. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  853. j := 3 //表头前两行用了,只能从第三行开始
  854. for k4, v4 := range v3 {
  855. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  856. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  857. sumBreakfast += v4["breakfast"]
  858. sumLunch += v4["lunch"]
  859. sumDinner += v4["dinner"]
  860. j++
  861. }
  862. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  863. "合计", sumBreakfast, sumLunch, sumDinner})
  864. }
  865. }
  866. //将文件保存至服务器
  867. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  868. err1 := xlsx.SaveAs(tmpDownloadPath)
  869. if err1 != nil {
  870. logx.Error(err1)
  871. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  872. panic(err1)
  873. }
  874. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  875. xlsFileName = append(xlsFileName, tmpFileName)
  876. }
  877. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  878. if err != nil {
  879. logx.Error(err)
  880. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  881. panic(err)
  882. }
  883. defer archive.Close()
  884. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  885. for k, v := range xlsDownloadPath {
  886. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  887. if err1 != nil {
  888. logx.Error(err1)
  889. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  890. panic(err1)
  891. }
  892. f, err1 := os.Open(v) //打开待压缩的文件
  893. if err1 != nil {
  894. logx.Error(err1)
  895. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  896. panic(err1)
  897. }
  898. if _, err1 = io.Copy(w, f); err != nil {
  899. logx.Error(err1)
  900. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  901. panic(err1)
  902. }
  903. if err1 != nil {
  904. logx.Error(err1)
  905. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  906. panic(err1)
  907. }
  908. f.Close()
  909. os.RemoveAll(v) //TODO::移除源文件
  910. }
  911. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  912. if err != nil {
  913. panic(err)
  914. }
  915. break
  916. case 5:
  917. //退款明细表
  918. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  919. downloadPath = "/export/central_kitchen_for_school/" + fileName
  920. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  921. if req.EnterpriseId != 0 {
  922. sess.And("enterprise.id =?", req.EnterpriseId)
  923. }
  924. if req.StartDate != "" {
  925. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  926. }
  927. if req.EndDate != "" {
  928. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  929. }
  930. if len(req.MealKindList) > 0 {
  931. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  932. }
  933. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  934. _, err := sess.
  935. 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").
  936. 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").
  937. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  938. Join("LEFT", "user", "user_identity.uid = user.id").
  939. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  940. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  941. Join("LEFT", "class", "class_with_user.class_id = class.id").
  942. Join("LEFT", "grade", "class.grade_id = grade.id").
  943. Desc("central_kitchen_for_school_user_refund_day.id").
  944. FindAndCount(&m)
  945. if err != nil {
  946. logx.Error(err)
  947. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  948. return
  949. }
  950. var list = map[string]map[string]map[string]map[string]map[string]int{}
  951. var xlsFileName, xlsDownloadPath []string
  952. for _, v := range m {
  953. if list[v.Enterprise.Name] == nil {
  954. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  955. }
  956. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  957. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  958. }
  959. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  960. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  961. }
  962. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  963. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  964. }
  965. //就餐类型(1:早餐 2:午餐 3:晚餐)
  966. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  967. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  968. }
  969. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  970. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  971. }
  972. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  973. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  974. }
  975. }
  976. for k1, v1 := range list {
  977. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  978. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  979. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  980. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  981. for k2, v2 := range v1 {
  982. if k2 == "" {
  983. //若年级为空,则为教师餐
  984. continue
  985. }
  986. for k3, v3 := range v2 {
  987. xlsx.NewSheet(k3)
  988. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  989. xlsx.SetSheetRow(k3, "A1", &titleList)
  990. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  991. xlsx.SetSheetRow(k3, "A2", &titleList)
  992. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  993. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  994. if err != nil {
  995. logx.Error(err)
  996. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  997. panic(err)
  998. }
  999. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1000. xlsx.SetColWidth(k3, "A", "D", 20)
  1001. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1002. j := 3 //表头前两行用了,只能从第三行开始
  1003. for k4, v4 := range v3 {
  1004. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1005. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1006. sumBreakfast += v4["breakfast"]
  1007. sumLunch += v4["lunch"]
  1008. sumDinner += v4["dinner"]
  1009. j++
  1010. }
  1011. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1012. "合计", sumBreakfast, sumLunch, sumDinner})
  1013. }
  1014. }
  1015. //将文件保存至服务器
  1016. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1017. err1 := xlsx.SaveAs(tmpDownloadPath)
  1018. if err1 != nil {
  1019. logx.Error(err1)
  1020. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1021. panic(err1)
  1022. }
  1023. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1024. xlsFileName = append(xlsFileName, tmpFileName)
  1025. }
  1026. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1027. if err != nil {
  1028. logx.Error(err)
  1029. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1030. panic(err)
  1031. }
  1032. defer archive.Close()
  1033. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1034. for k, v := range xlsDownloadPath {
  1035. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1036. if err1 != nil {
  1037. logx.Error(err1)
  1038. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1039. panic(err1)
  1040. }
  1041. f, err1 := os.Open(v) //打开待压缩的文件
  1042. if err1 != nil {
  1043. logx.Error(err1)
  1044. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1045. panic(err1)
  1046. }
  1047. if _, err1 = io.Copy(w, f); err != nil {
  1048. logx.Error(err1)
  1049. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1050. panic(err1)
  1051. }
  1052. if err1 != nil {
  1053. logx.Error(err1)
  1054. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1055. panic(err1)
  1056. }
  1057. f.Close()
  1058. os.RemoveAll(v) //TODO::移除源文件
  1059. }
  1060. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1061. if err != nil {
  1062. panic(err)
  1063. }
  1064. break
  1065. case 6:
  1066. //收款统计表
  1067. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1068. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1069. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1070. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1071. //查找数据
  1072. var m []model.Enterprise
  1073. sess := db.Db.Desc("id")
  1074. if req.EnterpriseId != 0 {
  1075. sess.And("id = ?", req.EnterpriseId)
  1076. }
  1077. err := sess.Find(&m)
  1078. if err != nil {
  1079. logx.Error(err)
  1080. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1081. return
  1082. }
  1083. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1084. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1085. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1086. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1087. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1088. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1089. if err != nil {
  1090. logx.Error(err)
  1091. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1092. panic(err)
  1093. }
  1094. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1095. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1096. j := 3 //表头前两行用了,只能从第三行开始
  1097. for _, v := range m {
  1098. var income, refund string
  1099. //统计收入
  1100. 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)
  1101. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1102. if err != nil {
  1103. logx.Error(err)
  1104. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1105. return
  1106. }
  1107. if mapArr1 != nil {
  1108. income = mapArr1[0]["total"]
  1109. }
  1110. //统计退款
  1111. //查询出当前学校下所有的学生
  1112. userIdentityDb := db.UserIdentityDb{}
  1113. userIdentityDb.Set(0)
  1114. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1115. var userIdentityIds []string
  1116. for _, v := range *userIdentities {
  1117. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1118. }
  1119. if len(userIdentityIds) != 0 {
  1120. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1121. 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'",
  1122. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1123. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1124. if err != nil {
  1125. logx.Error(err)
  1126. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1127. return
  1128. }
  1129. if mapArr2 != nil {
  1130. refund = mapArr2[0]["total"]
  1131. }
  1132. }
  1133. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1134. rateFee := realityAmount * 0.0038
  1135. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1136. j++
  1137. }
  1138. //将文件保存至服务器
  1139. xlsx.SaveAs("./static" + downloadPath)
  1140. break
  1141. case 7:
  1142. //班级收款明细表
  1143. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1144. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1145. //查找订单数据
  1146. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1147. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1148. if req.EnterpriseId != 0 {
  1149. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1150. }
  1151. if req.StartDate != "" {
  1152. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1153. }
  1154. if req.EndDate != "" {
  1155. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1156. }
  1157. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1158. _, err := sess.
  1159. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1160. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1161. Join("LEFT", "user", "user.id = user_identity.uid").
  1162. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1163. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1164. Join("LEFT", "grade", "class.grade_id = grade.id").
  1165. FindAndCount(&m)
  1166. if err != nil {
  1167. logx.Error(err)
  1168. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1169. return
  1170. }
  1171. 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")
  1172. if req.EnterpriseId != 0 {
  1173. sess1.And("enterprise.id =?", req.EnterpriseId)
  1174. }
  1175. if req.StartDate != "" {
  1176. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1177. }
  1178. if req.EndDate != "" {
  1179. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1180. }
  1181. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1182. _, err = sess1.
  1183. 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").
  1184. 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").
  1185. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1186. Join("LEFT", "user", "user_identity.uid = user.id").
  1187. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1188. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1189. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1190. Join("LEFT", "grade", "class.grade_id = grade.id").
  1191. Desc("central_kitchen_for_school_user_refund_day.id").
  1192. FindAndCount(&mm)
  1193. if err != nil {
  1194. logx.Error(err)
  1195. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1196. return
  1197. }
  1198. var list = map[string]map[string]map[string]map[string]float64{}
  1199. for _, v := range m {
  1200. if list[v.Enterprise.Name] == nil {
  1201. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1202. }
  1203. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1204. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1205. }
  1206. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1207. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1208. }
  1209. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1210. }
  1211. for _, v := range mm {
  1212. if list[v.Enterprise.Name] == nil {
  1213. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1214. }
  1215. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1216. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1217. }
  1218. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1219. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1220. }
  1221. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1222. }
  1223. var xlsFileName, xlsDownloadPath []string
  1224. for k1, v1 := range list {
  1225. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1226. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1227. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1228. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1229. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1230. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1231. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1232. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1233. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1234. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1235. if err != nil {
  1236. logx.Error(err)
  1237. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1238. panic(err)
  1239. }
  1240. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1241. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1242. j := 3 //表头前两行用了,只能从第三行开始
  1243. var sumClassNum = int64(0)
  1244. var sumIncome, sumRefund = float64(0), float64(0)
  1245. for k2, v2 := range v1 {
  1246. if k2 == "" {
  1247. //若年级为空,则为教师餐
  1248. continue
  1249. }
  1250. for k3, v3 := range v2 {
  1251. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1252. sumClassNum++
  1253. sumIncome += v3["income"]
  1254. sumRefund += v3["refund"]
  1255. j++
  1256. }
  1257. }
  1258. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1259. "合计", sumClassNum, sumIncome, sumRefund})
  1260. //将文件保存至服务器
  1261. err1 := xlsx.SaveAs(tmpDownloadPath)
  1262. if err1 != nil {
  1263. logx.Error(err1)
  1264. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1265. panic(err1)
  1266. }
  1267. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1268. xlsFileName = append(xlsFileName, tmpFileName)
  1269. }
  1270. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1271. if err != nil {
  1272. logx.Error(err)
  1273. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1274. panic(err)
  1275. }
  1276. defer archive.Close()
  1277. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1278. for k, v := range xlsDownloadPath {
  1279. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1280. if err1 != nil {
  1281. logx.Error(err1)
  1282. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1283. panic(err1)
  1284. }
  1285. f, err1 := os.Open(v) //打开待压缩的文件
  1286. if err1 != nil {
  1287. logx.Error(err1)
  1288. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1289. panic(err1)
  1290. }
  1291. if _, err1 = io.Copy(w, f); err != nil {
  1292. logx.Error(err1)
  1293. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1294. panic(err1)
  1295. }
  1296. if err1 != nil {
  1297. logx.Error(err1)
  1298. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1299. panic(err1)
  1300. }
  1301. f.Close()
  1302. os.RemoveAll(v) //TODO::移除源文件
  1303. }
  1304. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1305. if err != nil {
  1306. panic(err)
  1307. }
  1308. break
  1309. case 8:
  1310. //学校预定数量统计表
  1311. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1312. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1313. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1314. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1315. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1316. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1317. if req.EnterpriseId != 0 {
  1318. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1319. }
  1320. if req.StartDate != "" {
  1321. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  1322. }
  1323. if req.EndDate != "" {
  1324. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1325. }
  1326. if len(req.MealKindList) > 0 {
  1327. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1328. }
  1329. _, err := sess.
  1330. 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").
  1331. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1332. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1333. FindAndCount(&m)
  1334. if err != nil {
  1335. logx.Error(err)
  1336. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1337. return
  1338. }
  1339. var list = map[string]map[string]int64{}
  1340. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1341. centralKitchenForSchoolUserWithDayDb.Set(0)
  1342. for _, v := range m {
  1343. if list[v.Enterprise.Name] == nil {
  1344. list[v.Enterprise.Name] = map[string]int64{}
  1345. }
  1346. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1347. if err2 != nil {
  1348. logx.Error(err)
  1349. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1350. return
  1351. }
  1352. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1353. if err2 != nil {
  1354. logx.Error(err)
  1355. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1356. return
  1357. }
  1358. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForLunch, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1359. if err2 != nil {
  1360. logx.Error(err)
  1361. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1362. return
  1363. }
  1364. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForDinner, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1365. if err2 != nil {
  1366. logx.Error(err)
  1367. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1368. return
  1369. }
  1370. list[v.Enterprise.Name]["count"] += count
  1371. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1372. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1373. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1374. time.Sleep(50 * time.Millisecond) // 50毫秒
  1375. }
  1376. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1377. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1378. titleList = []string{"学校名称", "预定总数",
  1379. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1380. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1381. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1382. }
  1383. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1384. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1385. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1386. if err != nil {
  1387. logx.Error(err)
  1388. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1389. panic(err)
  1390. }
  1391. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1392. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1393. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1394. j := 3 //表头前两行用了,只能从第三行开始
  1395. total := int64(0)
  1396. total1 := int64(0)
  1397. total2 := int64(0)
  1398. total3 := int64(0)
  1399. for k, v := range list {
  1400. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1401. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1402. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1403. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1404. total += v["count"]
  1405. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1406. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1407. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1408. j++
  1409. }
  1410. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1411. "合计", total, total1, total2, total3})
  1412. //将文件保存至服务器
  1413. xlsx.SaveAs("./static" + downloadPath)
  1414. break
  1415. }
  1416. //新增数据
  1417. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1418. centralKitchenForSchoolExportRecordsDb.Set()
  1419. marshal, _ := json.Marshal(req)
  1420. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1421. Name: fileName,
  1422. DownloadPath: downloadPath,
  1423. Kind: kind,
  1424. ReqContent: string(marshal),
  1425. CreateAt: now.Format("2006-01-02 15:04:05"),
  1426. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1427. })
  1428. }
  1429. }
  1430. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1431. var titleList []string
  1432. var fileName, downloadPath string
  1433. var now = time.Now()
  1434. xlsx := excelize.NewFile()
  1435. //数据对比
  1436. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1437. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1438. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1439. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1440. if req.EnterpriseId != 0 {
  1441. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1442. }
  1443. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date1[0])
  1444. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date1[1])
  1445. if len(req.MealKindList) > 0 {
  1446. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1447. }
  1448. _, err := sess.
  1449. 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").
  1450. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1451. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1452. Join("LEFT", "user", "user.id = user_identity.uid").
  1453. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1454. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1455. Join("LEFT", "grade", "class.grade_id = grade.id").
  1456. FindAndCount(&m)
  1457. if err != nil {
  1458. logx.Error(err)
  1459. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1460. return
  1461. }
  1462. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1463. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1464. if req.EnterpriseId != 0 {
  1465. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1466. }
  1467. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date2[0])
  1468. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date2[1])
  1469. if len(req.MealKindList) > 0 {
  1470. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1471. }
  1472. _, err1 := sess1.
  1473. 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").
  1474. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1475. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1476. Join("LEFT", "user", "user.id = user_identity.uid").
  1477. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1478. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1479. Join("LEFT", "grade", "class.grade_id = grade.id").
  1480. FindAndCount(&m)
  1481. if err1 != nil {
  1482. logx.Error(err1)
  1483. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1484. return
  1485. }
  1486. var list = map[string]map[string]map[string]map[string]string{}
  1487. for _, v := range m {
  1488. if list[v.Enterprise.Name] == nil {
  1489. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1490. }
  1491. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1492. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1493. }
  1494. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1495. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1496. }
  1497. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1498. }
  1499. var list1 = map[string]map[string]map[string]map[string]string{}
  1500. for _, v := range m1 {
  1501. if list1[v.Enterprise.Name] == nil {
  1502. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1503. }
  1504. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1505. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1506. }
  1507. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1508. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1509. }
  1510. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1511. }
  1512. var xlsFileName, xlsDownloadPath []string
  1513. for k1, v1 := range list {
  1514. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1515. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1516. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1517. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1518. for k2, v2 := range v1 {
  1519. if k2 == "" {
  1520. //若年级为空,则为教师
  1521. continue
  1522. }
  1523. for k3, v3 := range v2 {
  1524. xlsx.NewSheet(k3)
  1525. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  1526. xlsx.SetSheetRow(k3, "A1", &titleList)
  1527. titleList = []string{"姓名", "家长联系电话"}
  1528. xlsx.SetSheetRow(k3, "A2", &titleList)
  1529. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  1530. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1531. if err != nil {
  1532. logx.Error(err)
  1533. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1534. panic(err)
  1535. }
  1536. xlsx.SetCellStyle(k3, "A1", "B1", style)
  1537. xlsx.SetColWidth(k3, "A", "B", 40)
  1538. j := 2 //表头被第一行用了,只能从第二行开始
  1539. for k4, v4 := range v3 {
  1540. //TODO::判断当前用户是否在新的月份里面
  1541. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1542. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1543. k4, v4})
  1544. j++
  1545. }
  1546. }
  1547. }
  1548. }
  1549. //将文件保存至服务器
  1550. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1551. err1 := xlsx.SaveAs(tmpDownloadPath)
  1552. if err1 != nil {
  1553. logx.Error(err1)
  1554. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1555. panic(err1)
  1556. }
  1557. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1558. xlsFileName = append(xlsFileName, tmpFileName)
  1559. }
  1560. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1561. if err != nil {
  1562. logx.Error(err)
  1563. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1564. panic(err)
  1565. }
  1566. defer archive.Close()
  1567. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1568. for k, v := range xlsDownloadPath {
  1569. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1570. if err1 != nil {
  1571. logx.Error(err1)
  1572. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1573. panic(err1)
  1574. }
  1575. f, err1 := os.Open(v) //打开待压缩的文件
  1576. if err1 != nil {
  1577. logx.Error(err1)
  1578. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1579. panic(err1)
  1580. }
  1581. if _, err1 = io.Copy(w, f); err != nil {
  1582. logx.Error(err1)
  1583. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1584. panic(err1)
  1585. }
  1586. if err1 != nil {
  1587. logx.Error(err1)
  1588. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1589. panic(err1)
  1590. }
  1591. f.Close()
  1592. os.RemoveAll(v) //TODO::移除源文件
  1593. }
  1594. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1595. if err != nil {
  1596. panic(err)
  1597. }
  1598. //新增数据
  1599. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1600. centralKitchenForSchoolExportRecordsDb.Set()
  1601. marshal, _ := json.Marshal(req)
  1602. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1603. Name: fileName,
  1604. DownloadPath: downloadPath,
  1605. Kind: 9, //数据对比
  1606. ReqContent: string(marshal),
  1607. CreateAt: now.Format("2006-01-02 15:04:05"),
  1608. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1609. })
  1610. }
  1611. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1612. mealZh = "未知"
  1613. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1614. for _, v := range md.SelfSupportForMealTimeMapping {
  1615. //判断时间大小
  1616. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  1617. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  1618. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  1619. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  1620. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1621. mealZh = v["name"]
  1622. }
  1623. }
  1624. return
  1625. }