智慧食堂
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

svc_data_statisstics.go 56 KiB

1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
1年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332
  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]float64
  249. for _, vv := range m {
  250. if data[vv.Enterprise.Name] == nil {
  251. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  252. }
  253. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  254. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]float64)
  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]float64)
  259. }
  260. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]float64{
  261. "id": float64(vv.UserIdentity.Id),
  262. }
  263. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]++
  264. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  265. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]++
  266. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  267. }
  268. xlsx.SetSheetRow("", "A1", &titleList)
  269. sheetIndex := 2 //第一个表是模板表
  270. j := 3 //表头前两行被占用了,只能从第三行开始
  271. for k, v := range data {
  272. xlsx.NewSheet(k) //1、新建表格
  273. xlsx.CopySheet(1, sheetIndex)
  274. for kk, vv := range v {
  275. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{utils.Float64ToStr(vv["编号"]["id"]), kk,
  276. vv["早餐"]["num"], vv["早餐"]["amount"],
  277. vv["午餐"]["num"], vv["午餐"]["amount"],
  278. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  279. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  280. vv["合计"]["num"], vv["合计"]["amount"],
  281. })
  282. j++
  283. }
  284. sheetIndex++
  285. }
  286. xlsx.DeleteSheet("template") //删除模板表
  287. //将文件保存至服务器
  288. xlsx.SaveAs("./static" + downloadPath)
  289. break
  290. case 2:
  291. //商家分餐数据统计表
  292. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  293. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  294. downloadPath = "/export/self_support_for_school/" + fileName
  295. //查询出所有的设备
  296. var deviceList []*db.Device
  297. err := db.Db.Find(&deviceList)
  298. if err != nil {
  299. logx.Error(err)
  300. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  301. return
  302. }
  303. var deviceListMapping map[string]model.Device
  304. for _, v := range deviceListMapping {
  305. deviceListMapping[v.DeviceSn] = v
  306. }
  307. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  308. sess := db.Db.Desc("self_support_for_school_ord.id")
  309. if req.EnterpriseId != 0 {
  310. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  311. }
  312. if req.StartDate != "" {
  313. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  314. }
  315. if req.EndDate != "" {
  316. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  317. }
  318. _, err = sess.
  319. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  320. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  321. Join("LEFT", "user", "user.id = user_identity.uid").
  322. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  323. Join("LEFT", "class", "class_with_user.class_id = class.id").
  324. Join("LEFT", "grade", "class.grade_id = grade.id").
  325. FindAndCount(&m)
  326. if err != nil {
  327. logx.Error(err)
  328. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  329. return
  330. }
  331. //整合数据
  332. var data map[string]map[string]map[string]map[string]float64
  333. for _, vv := range m {
  334. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  335. if data[vv.Enterprise.Name] == nil {
  336. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  337. }
  338. if data[vv.Enterprise.Name][shopName] == nil {
  339. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  340. }
  341. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  342. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  343. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  344. }
  345. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  346. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  347. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  348. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  349. }
  350. xlsx.SetSheetRow("", "A1", &titleList)
  351. sheetIndex := 2 //第一个表是模板表
  352. j := 4 //表头前三行被占用了,只能从第四行开始
  353. for k, v := range data {
  354. xlsx.NewSheet(k) //1、新建表格
  355. xlsx.CopySheet(1, sheetIndex)
  356. for kk, vv := range v {
  357. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  358. vv["早餐"]["num"], vv["早餐"]["amount"],
  359. vv["午餐"]["num"], vv["午餐"]["amount"],
  360. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  361. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  362. vv["合计"]["num"], vv["合计"]["amount"],
  363. })
  364. j++
  365. }
  366. sheetIndex++
  367. }
  368. xlsx.DeleteSheet("template") //删除模板表
  369. //将文件保存至服务器
  370. xlsx.SaveAs("./static" + downloadPath)
  371. break
  372. }
  373. //新增数据
  374. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  375. selfSupportForSchoolExportRecordsDb.Set()
  376. marshal, _ := json.Marshal(req)
  377. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  378. Name: fileName,
  379. DownloadPath: downloadPath,
  380. Kind: req.Kind,
  381. ReqContent: string(marshal),
  382. CreateAt: now.Format("2006-01-02 15:04:05"),
  383. UpdateAt: now.Format("2006-01-02 15:04:05"),
  384. })
  385. }
  386. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  387. var titleList []string
  388. var fileName, downloadPath string
  389. var now = time.Now()
  390. xlsx := excelize.NewFile()
  391. switch req.Kind {
  392. case 1:
  393. //订单列表
  394. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  395. downloadPath = "/export/central_kitchen_for_school/" + fileName
  396. //查找订单数据
  397. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  398. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  399. if req.EnterpriseId != 0 {
  400. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  401. }
  402. if req.StartDate != "" {
  403. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  404. }
  405. if req.EndDate != "" {
  406. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  407. }
  408. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  409. _, err := sess.
  410. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  411. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  412. Join("LEFT", "user", "user.id = user_identity.uid").
  413. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  414. Join("LEFT", "class", "class_with_user.class_id = class.id").
  415. Join("LEFT", "grade", "class.grade_id = grade.id").
  416. FindAndCount(&m)
  417. if err != nil {
  418. logx.Error(err)
  419. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  420. return
  421. }
  422. //查找预定数据
  423. var outTradeNoList []string
  424. for _, v := range m {
  425. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  426. }
  427. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  428. sess1 := db.Db.Desc("id")
  429. if req.EnterpriseId != 0 {
  430. sess1.And("enterprise_id = ?", req.EnterpriseId)
  431. }
  432. if len(req.MealKindList) > 0 {
  433. sess1.In("kind", req.MealKindList)
  434. }
  435. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  436. if err != nil {
  437. logx.Error(err)
  438. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  439. return
  440. }
  441. var exportOutTradeNoList []string
  442. for _, v := range mm {
  443. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  444. }
  445. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定天数", "订单价格", "支付时间", "订单状态"}
  446. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  447. j := 2 //表头被第一行用了,只能从第二行开始
  448. for _, v := range m {
  449. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  450. continue
  451. }
  452. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  453. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  454. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  455. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  456. break
  457. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  458. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  459. break
  460. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  461. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  462. break
  463. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  464. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  465. break
  466. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  467. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  468. break
  469. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  470. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  471. break
  472. }
  473. //预定天数、预定日期
  474. var list []*model.CentralKitchenForSchoolUserWithDay
  475. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  476. if err1 != nil {
  477. logx.Error(err)
  478. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  479. return
  480. }
  481. var reserveDays string
  482. for _, v1 := range list {
  483. reserveDays += v1.Date
  484. }
  485. if v.Grade.Name == "" {
  486. v.Grade.Name = "教师"
  487. }
  488. if v.Class.Name == "" {
  489. v.Class.Name = "教师"
  490. }
  491. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  492. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  493. j++
  494. }
  495. //将文件保存至服务器
  496. xlsx.SaveAs("./static" + downloadPath)
  497. break
  498. case 2:
  499. //退款申请表
  500. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  501. downloadPath = "/export/central_kitchen_for_school/" + fileName
  502. //查找退款申请数据
  503. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  504. if req.EnterpriseId != 0 {
  505. sess.And("enterprise.id =?", req.EnterpriseId)
  506. }
  507. if req.StartDate != "" {
  508. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  509. }
  510. if req.EndDate != "" {
  511. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  512. }
  513. if len(req.MealKindList) > 0 {
  514. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  515. }
  516. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  517. _, err := sess.
  518. 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").
  519. 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").
  520. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  521. Join("LEFT", "user", "user_identity.uid = user.id").
  522. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  523. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  524. Join("LEFT", "class", "class_with_user.class_id = class.id").
  525. Join("LEFT", "grade", "class.grade_id = grade.id").
  526. Desc("central_kitchen_for_school_user_refund_day.id").
  527. FindAndCount(&m)
  528. if err != nil {
  529. logx.Error(err)
  530. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  531. return
  532. }
  533. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  534. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  535. j := 2 //表头被第一行用了,只能从第二行开始
  536. for _, v := range m {
  537. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  538. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  539. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  540. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  541. break
  542. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  543. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  544. break
  545. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  546. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  547. break
  548. }
  549. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  550. switch v.CentralKitchenForSchoolUserRefundDay.State {
  551. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  552. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  553. break
  554. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  555. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  556. break
  557. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  558. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  559. break
  560. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  561. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  562. break
  563. }
  564. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  565. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  566. j++
  567. }
  568. //将文件保存至服务器
  569. xlsx.SaveAs("./static" + downloadPath)
  570. break
  571. case 3:
  572. //学校预定统计表
  573. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  574. downloadPath = "/export/central_kitchen_for_school/" + fileName
  575. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  576. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  577. if req.EnterpriseId != 0 {
  578. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  579. }
  580. if req.StartDate != "" {
  581. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  582. }
  583. if req.EndDate != "" {
  584. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  585. }
  586. if len(req.MealKindList) > 0 {
  587. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  588. }
  589. _, err := sess.
  590. 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").
  591. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  592. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  593. Join("LEFT", "user", "user.id = user_identity.uid").
  594. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  595. Join("LEFT", "class", "class_with_user.class_id = class.id").
  596. Join("LEFT", "grade", "class.grade_id = grade.id").
  597. FindAndCount(&m)
  598. if err != nil {
  599. logx.Error(err)
  600. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  601. return
  602. }
  603. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  604. centralKitchenForSchoolUserWithDayDb.Set(0)
  605. var list = map[string]map[string]map[string]map[string]int64{}
  606. var xlsFileName, xlsDownloadPath []string
  607. for _, v := range m {
  608. if list[v.Enterprise.Name] == nil {
  609. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  610. }
  611. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  612. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  613. }
  614. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  615. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  616. }
  617. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  618. if err2 != nil {
  619. logx.Error(err)
  620. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  621. return
  622. }
  623. time.Sleep(50 * time.Millisecond) // 50毫秒
  624. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  625. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  626. }
  627. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  628. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  629. }
  630. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  631. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  632. }
  633. }
  634. for k1, v1 := range list {
  635. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  636. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  637. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  638. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  639. j := 2 //表头被第一行用了,只能从第二行开始
  640. for k2, v2 := range v1 {
  641. if k2 == "" {
  642. //若年级为空,则为教师餐
  643. k2 = "教师餐"
  644. }
  645. for k3, v3 := range v2 {
  646. if k3 == "" {
  647. //若班级为空,则为教师餐
  648. k3 = "教师餐"
  649. }
  650. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  651. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  652. j++
  653. }
  654. }
  655. //将文件保存至服务器
  656. err1 := xlsx.SaveAs(tmpDownloadPath)
  657. if err1 != nil {
  658. logx.Error(err1)
  659. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  660. panic(err1)
  661. }
  662. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  663. xlsFileName = append(xlsFileName, tmpFileName)
  664. }
  665. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  666. if err != nil {
  667. logx.Error(err)
  668. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  669. panic(err)
  670. }
  671. defer archive.Close()
  672. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  673. for k, v := range xlsDownloadPath {
  674. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  675. if err1 != nil {
  676. logx.Error(err1)
  677. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  678. panic(err1)
  679. }
  680. f, err1 := os.Open(v) //打开待压缩的文件
  681. if err1 != nil {
  682. logx.Error(err1)
  683. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  684. panic(err1)
  685. }
  686. if _, err1 = io.Copy(w, f); err != nil {
  687. logx.Error(err1)
  688. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  689. panic(err1)
  690. }
  691. if err1 != nil {
  692. logx.Error(err1)
  693. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  694. panic(err1)
  695. }
  696. f.Close()
  697. os.RemoveAll(v) //TODO::移除源文件
  698. }
  699. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  700. if err != nil {
  701. panic(err)
  702. }
  703. break
  704. case 4:
  705. //班级明细表
  706. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  707. downloadPath = "/export/central_kitchen_for_school/" + fileName
  708. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  709. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  710. if req.EnterpriseId != 0 {
  711. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  712. }
  713. if req.StartDate != "" {
  714. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  715. }
  716. if req.EndDate != "" {
  717. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  718. }
  719. if len(req.MealKindList) > 0 {
  720. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  721. }
  722. _, err := sess.
  723. 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").
  724. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  725. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  726. Join("LEFT", "user", "user.id = user_identity.uid").
  727. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  728. Join("LEFT", "class", "class_with_user.class_id = class.id").
  729. Join("LEFT", "grade", "class.grade_id = grade.id").
  730. FindAndCount(&m)
  731. if err != nil {
  732. logx.Error(err)
  733. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  734. return
  735. }
  736. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  737. var xlsFileName, xlsDownloadPath []string
  738. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  739. centralKitchenForSchoolUserWithDayDb.Set(0)
  740. for _, v := range m {
  741. if list[v.Enterprise.Name] == nil {
  742. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  743. }
  744. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  745. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  746. }
  747. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  748. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  749. }
  750. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  751. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  752. }
  753. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  754. if err2 != nil {
  755. logx.Error(err)
  756. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  757. return
  758. }
  759. time.Sleep(50 * time.Millisecond) // 50毫秒
  760. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  761. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  762. }
  763. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  764. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  765. }
  766. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  767. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  768. }
  769. }
  770. for k1, v1 := range list {
  771. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  772. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  773. for k2, v2 := range v1 {
  774. if k2 == "" {
  775. //若年级为空,则为教师餐
  776. continue
  777. }
  778. for k3, v3 := range v2 {
  779. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  780. xlsx.NewSheet(k3)
  781. xlsx.SetSheetRow(k3, "A1", &titleList)
  782. j := 2 //表头被第一行用了,只能从第二行开始
  783. for k4, v4 := range v3 {
  784. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  785. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  786. j++
  787. }
  788. }
  789. }
  790. //将文件保存至服务器
  791. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  792. err1 := xlsx.SaveAs(tmpDownloadPath)
  793. if err1 != nil {
  794. logx.Error(err1)
  795. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  796. panic(err1)
  797. }
  798. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  799. xlsFileName = append(xlsFileName, tmpFileName)
  800. }
  801. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  802. if err != nil {
  803. logx.Error(err)
  804. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  805. panic(err)
  806. }
  807. defer archive.Close()
  808. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  809. for k, v := range xlsDownloadPath {
  810. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  811. if err1 != nil {
  812. logx.Error(err1)
  813. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  814. panic(err1)
  815. }
  816. f, err1 := os.Open(v) //打开待压缩的文件
  817. if err1 != nil {
  818. logx.Error(err1)
  819. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  820. panic(err1)
  821. }
  822. if _, err1 = io.Copy(w, f); err != nil {
  823. logx.Error(err1)
  824. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  825. panic(err1)
  826. }
  827. if err1 != nil {
  828. logx.Error(err1)
  829. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  830. panic(err1)
  831. }
  832. f.Close()
  833. os.RemoveAll(v) //TODO::移除源文件
  834. }
  835. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  836. if err != nil {
  837. panic(err)
  838. }
  839. break
  840. case 5:
  841. //退款明细表
  842. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  843. downloadPath = "/export/central_kitchen_for_school/" + fileName
  844. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  845. if req.EnterpriseId != 0 {
  846. sess.And("enterprise.id =?", req.EnterpriseId)
  847. }
  848. if req.StartDate != "" {
  849. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  850. }
  851. if req.EndDate != "" {
  852. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  853. }
  854. if len(req.MealKindList) > 0 {
  855. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  856. }
  857. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  858. _, err := sess.
  859. 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").
  860. 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").
  861. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  862. Join("LEFT", "user", "user_identity.uid = user.id").
  863. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  864. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  865. Join("LEFT", "class", "class_with_user.class_id = class.id").
  866. Join("LEFT", "grade", "class.grade_id = grade.id").
  867. Desc("central_kitchen_for_school_user_refund_day.id").
  868. FindAndCount(&m)
  869. if err != nil {
  870. logx.Error(err)
  871. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  872. return
  873. }
  874. var list = map[string]map[string]map[string]map[string]map[string]int{}
  875. var xlsFileName, xlsDownloadPath []string
  876. for _, v := range m {
  877. if list[v.Enterprise.Name] == nil {
  878. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  879. }
  880. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  881. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  882. }
  883. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  884. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  885. }
  886. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  887. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  888. }
  889. //就餐类型(1:早餐 2:午餐 3:晚餐)
  890. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  891. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  892. }
  893. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  894. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  895. }
  896. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  897. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  898. }
  899. }
  900. for k1, v1 := range list {
  901. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  902. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  903. for k2, v2 := range v1 {
  904. if k2 == "" {
  905. //若年级为空,则为教师餐
  906. continue
  907. }
  908. for k3, v3 := range v2 {
  909. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  910. xlsx.NewSheet(k3)
  911. xlsx.SetSheetRow(k3, "A1", &titleList)
  912. j := 2 //表头被第一行用了,只能从第二行开始
  913. for k4, v4 := range v3 {
  914. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  915. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  916. j++
  917. }
  918. }
  919. }
  920. //将文件保存至服务器
  921. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  922. err1 := xlsx.SaveAs(tmpDownloadPath)
  923. if err1 != nil {
  924. logx.Error(err1)
  925. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  926. panic(err1)
  927. }
  928. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  929. xlsFileName = append(xlsFileName, tmpFileName)
  930. }
  931. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  932. if err != nil {
  933. logx.Error(err)
  934. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  935. panic(err)
  936. }
  937. defer archive.Close()
  938. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  939. for k, v := range xlsDownloadPath {
  940. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  941. if err1 != nil {
  942. logx.Error(err1)
  943. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  944. panic(err1)
  945. }
  946. f, err1 := os.Open(v) //打开待压缩的文件
  947. if err1 != nil {
  948. logx.Error(err1)
  949. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  950. panic(err1)
  951. }
  952. if _, err1 = io.Copy(w, f); err != nil {
  953. logx.Error(err1)
  954. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  955. panic(err1)
  956. }
  957. if err1 != nil {
  958. logx.Error(err1)
  959. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  960. panic(err1)
  961. }
  962. f.Close()
  963. os.RemoveAll(v) //TODO::移除源文件
  964. }
  965. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  966. if err != nil {
  967. panic(err)
  968. }
  969. break
  970. case 6:
  971. //收款统计表
  972. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  973. downloadPath = "/export/central_kitchen_for_school/" + fileName
  974. //查找数据
  975. var m []model.Enterprise
  976. sess := db.Db.Desc("id")
  977. if req.EnterpriseId != 0 {
  978. sess.And("id = ?", req.EnterpriseId)
  979. }
  980. err := sess.Find(&m)
  981. if err != nil {
  982. logx.Error(err)
  983. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  984. return
  985. }
  986. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  987. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  988. j := 2 //表头被第一行用了,只能从第二行开始
  989. for _, v := range m {
  990. var income, refund string
  991. //统计收入
  992. 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)
  993. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  994. if err != nil {
  995. logx.Error(err)
  996. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  997. return
  998. }
  999. if mapArr1 != nil {
  1000. income = mapArr1[0]["total"]
  1001. }
  1002. //统计退款
  1003. //查询出当前学校下所有的学生
  1004. userIdentityDb := db.UserIdentityDb{}
  1005. userIdentityDb.Set(0)
  1006. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1007. var userIdentityIds []string
  1008. for _, v := range *userIdentities {
  1009. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1010. }
  1011. if len(userIdentityIds) != 0 {
  1012. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1013. 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'",
  1014. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1015. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1016. if err != nil {
  1017. logx.Error(err)
  1018. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1019. return
  1020. }
  1021. if mapArr2 != nil {
  1022. refund = mapArr2[0]["total"]
  1023. }
  1024. }
  1025. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1026. rateFee := realityAmount * 0.0038
  1027. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1028. j++
  1029. }
  1030. //将文件保存至服务器
  1031. xlsx.SaveAs("./static" + downloadPath)
  1032. break
  1033. case 7:
  1034. //班级收款明细表
  1035. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1036. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1037. //查找订单数据
  1038. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1039. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  1040. if req.EnterpriseId != 0 {
  1041. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1042. }
  1043. if req.StartDate != "" {
  1044. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1045. }
  1046. if req.EndDate != "" {
  1047. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1048. }
  1049. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1050. _, err := sess.
  1051. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1052. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1053. Join("LEFT", "user", "user.id = user_identity.uid").
  1054. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1055. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1056. Join("LEFT", "grade", "class.grade_id = grade.id").
  1057. FindAndCount(&m)
  1058. if err != nil {
  1059. logx.Error(err)
  1060. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1061. return
  1062. }
  1063. sess1 := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1064. if req.EnterpriseId != 0 {
  1065. sess1.And("enterprise.id =?", req.EnterpriseId)
  1066. }
  1067. if req.StartDate != "" {
  1068. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1069. }
  1070. if req.EndDate != "" {
  1071. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1072. }
  1073. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1074. _, err = sess1.
  1075. 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").
  1076. 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").
  1077. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1078. Join("LEFT", "user", "user_identity.uid = user.id").
  1079. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1080. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1081. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1082. Join("LEFT", "grade", "class.grade_id = grade.id").
  1083. Desc("central_kitchen_for_school_user_refund_day.id").
  1084. FindAndCount(&mm)
  1085. if err != nil {
  1086. logx.Error(err)
  1087. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1088. return
  1089. }
  1090. var list = map[string]map[string]map[string]map[string]float64{}
  1091. for _, v := range m {
  1092. if list[v.Enterprise.Name] == nil {
  1093. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1094. }
  1095. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1096. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1097. }
  1098. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1099. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1100. }
  1101. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1102. }
  1103. for _, v := range mm {
  1104. if list[v.Enterprise.Name] == nil {
  1105. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1106. }
  1107. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1108. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1109. }
  1110. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1111. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1112. }
  1113. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1114. }
  1115. var xlsFileName, xlsDownloadPath []string
  1116. for k1, v1 := range list {
  1117. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1118. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1119. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1120. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1121. j := 2 //表头被第一行用了,只能从第二行开始
  1122. for k2, v2 := range v1 {
  1123. if k2 == "" {
  1124. //若年级为空,则为教师餐
  1125. continue
  1126. }
  1127. for k3, v3 := range v2 {
  1128. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1129. j++
  1130. }
  1131. }
  1132. //将文件保存至服务器
  1133. err1 := xlsx.SaveAs(tmpDownloadPath)
  1134. if err1 != nil {
  1135. logx.Error(err1)
  1136. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1137. panic(err1)
  1138. }
  1139. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1140. xlsFileName = append(xlsFileName, tmpFileName)
  1141. }
  1142. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1143. if err != nil {
  1144. logx.Error(err)
  1145. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1146. panic(err)
  1147. }
  1148. defer archive.Close()
  1149. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1150. for k, v := range xlsDownloadPath {
  1151. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1152. if err1 != nil {
  1153. logx.Error(err1)
  1154. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1155. panic(err1)
  1156. }
  1157. f, err1 := os.Open(v) //打开待压缩的文件
  1158. if err1 != nil {
  1159. logx.Error(err1)
  1160. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1161. panic(err1)
  1162. }
  1163. if _, err1 = io.Copy(w, f); err != nil {
  1164. logx.Error(err1)
  1165. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1166. panic(err1)
  1167. }
  1168. if err1 != nil {
  1169. logx.Error(err1)
  1170. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1171. panic(err1)
  1172. }
  1173. f.Close()
  1174. os.RemoveAll(v) //TODO::移除源文件
  1175. }
  1176. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1177. if err != nil {
  1178. panic(err)
  1179. }
  1180. break
  1181. case 8:
  1182. //学校预定数量统计表
  1183. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1184. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1185. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1186. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1187. if req.EnterpriseId != 0 {
  1188. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1189. }
  1190. if req.StartDate != "" {
  1191. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  1192. }
  1193. if req.EndDate != "" {
  1194. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1195. }
  1196. if len(req.MealKindList) > 0 {
  1197. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1198. }
  1199. _, err := sess.
  1200. 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").
  1201. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1202. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1203. FindAndCount(&m)
  1204. if err != nil {
  1205. logx.Error(err)
  1206. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1207. return
  1208. }
  1209. var list = map[string]int64{}
  1210. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1211. centralKitchenForSchoolUserWithDayDb.Set(0)
  1212. for _, v := range m {
  1213. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1214. if err2 != nil {
  1215. logx.Error(err)
  1216. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1217. return
  1218. }
  1219. time.Sleep(50 * time.Millisecond) // 50毫秒
  1220. list[v.Enterprise.Name] += count
  1221. }
  1222. titleList = []string{"学校名称", "预定总数"}
  1223. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1224. j := 2 //表头被第一行用了,只能从第二行开始
  1225. for k, v := range list {
  1226. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v})
  1227. j++
  1228. }
  1229. //将文件保存至服务器
  1230. xlsx.SaveAs("./static" + downloadPath)
  1231. break
  1232. }
  1233. //新增数据
  1234. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1235. centralKitchenForSchoolExportRecordsDb.Set()
  1236. marshal, _ := json.Marshal(req)
  1237. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1238. Name: fileName,
  1239. DownloadPath: downloadPath,
  1240. Kind: req.Kind,
  1241. ReqContent: string(marshal),
  1242. CreateAt: now.Format("2006-01-02 15:04:05"),
  1243. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1244. })
  1245. }
  1246. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1247. now := time.Now()
  1248. mealZh = "未知"
  1249. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1250. for _, v := range md.SelfSupportForMealTimeMapping {
  1251. //判断时间大小
  1252. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", now.Format("2006-01-02")+" "+v["from"], time.Local)
  1253. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", now.Format("2006-01-02")+" "+v["from"], time.Local)
  1254. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1255. mealZh = v["name"]
  1256. }
  1257. }
  1258. return
  1259. }