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