智慧食堂
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

1532 linhas
65 KiB

  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/enum"
  7. "applet/app/utils"
  8. "applet/app/utils/logx"
  9. "archive/zip"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/360EntSecGroup-Skylar/excelize"
  13. "github.com/syyongx/php2go"
  14. "io"
  15. "os"
  16. "strconv"
  17. "time"
  18. )
  19. func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
  20. var titleList []string
  21. var fileName string
  22. var now = time.Now()
  23. xlsx := excelize.NewFile()
  24. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  25. switch req.Kind {
  26. case 1:
  27. //订单列表
  28. fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  29. //查找订单数据
  30. sess := db.Db.Desc("nursing_home_package_ord.id")
  31. if req.EnterpriseId != 0 {
  32. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  33. }
  34. if req.StartDate != "" {
  35. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  36. }
  37. if req.EndDate != "" {
  38. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  39. }
  40. var m []*db.NursingHomePackageOrdWithUserIdentity
  41. _, err := sess.
  42. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  43. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  44. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  45. FindAndCount(&m)
  46. if err != nil {
  47. logx.Error(err)
  48. println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
  49. return
  50. }
  51. titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
  52. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  53. j := 2 //表头被第一行用了,只能从第二行开始
  54. for _, v := range m {
  55. var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
  56. switch v.NursingHomePackageOrd.Kind {
  57. case 1:
  58. kind = "按年购买"
  59. break
  60. case 2:
  61. kind = "按月购买"
  62. break
  63. case 3:
  64. kind = "按天购买"
  65. break
  66. case 4:
  67. kind = "补餐"
  68. break
  69. }
  70. var state string //0:待支付 1:支付成功 2:支付失败
  71. switch v.NursingHomePackageOrd.State {
  72. case enum.NursingHomePackageOrdStateForWait:
  73. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  74. break
  75. case enum.NursingHomePackageOrdStateForSuccess:
  76. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  77. break
  78. case enum.NursingHomePackageOrdStateForFail:
  79. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
  80. break
  81. }
  82. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
  83. v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
  84. j++
  85. }
  86. break
  87. case 2:
  88. //退款申请表
  89. fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  90. var m []*db.NursingHomeUserRefundDayWithData
  91. sess := db.Db.Desc("nursing_home_user_refund_day.id")
  92. if req.EnterpriseId != 0 {
  93. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  94. }
  95. if req.StartDate != "" {
  96. sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
  97. }
  98. if req.EndDate != "" {
  99. sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
  100. }
  101. _, err := sess.
  102. Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
  103. Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
  104. Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
  105. Join("LEFT", "user", "user_identity.uid = user.id").
  106. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  107. Desc("nursing_home_user_refund_day.id").
  108. FindAndCount(&m)
  109. if err != nil {
  110. logx.Error(err)
  111. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  112. return
  113. }
  114. titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
  115. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  116. j := 2 //表头被第一行用了,只能从第二行开始
  117. for _, v := range m {
  118. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  119. switch v.NursingHomeUserWithDay.Kind {
  120. case enum.NursingHomeUserWithDayKindForBreakfast:
  121. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  122. break
  123. case enum.NursingHomeUserWithDayKindForLunch:
  124. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  125. break
  126. case enum.NursingHomeUserWithDayKindForDinner:
  127. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  128. break
  129. }
  130. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  131. switch v.NursingHomeUserRefundDay.State {
  132. case enum.NursingHomeUserRefundDayStateForAuditing:
  133. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
  134. break
  135. case enum.NursingHomeUserRefundDayStateForAuditPass:
  136. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
  137. break
  138. case enum.NursingHomeUserRefundDayStateForAuditReject:
  139. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
  140. break
  141. case enum.NursingHomeUserRefundDayStateForAuditComplete:
  142. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
  143. break
  144. }
  145. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
  146. v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
  147. j++
  148. }
  149. xlsx.SaveAs("./static" + fileName + ".xlsx")
  150. break
  151. case 3:
  152. //预定统计表
  153. fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  154. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  155. sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
  156. if req.EnterpriseId != 0 {
  157. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  158. }
  159. if req.StartDate != "" {
  160. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  161. }
  162. if req.EndDate != "" {
  163. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  164. }
  165. _, err := sess.
  166. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  167. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  168. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  169. Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
  170. if err != nil {
  171. logx.Error(err)
  172. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  173. return
  174. }
  175. titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
  176. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  177. j := 2 //表头被第一行用了,只能从第二行开始
  178. for _, v := range m {
  179. var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
  180. switch v.NursingHomePackageOrdForReserve.Kind {
  181. case enum.NursingHomeUserWithDayKindForBreakfast:
  182. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  183. break
  184. case enum.NursingHomeUserWithDayKindForLunch:
  185. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  186. break
  187. case enum.NursingHomeUserWithDayKindForDinner:
  188. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  189. break
  190. }
  191. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
  192. v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
  193. j++
  194. }
  195. xlsx.SaveAs("./static" + fileName + ".xlsx")
  196. break
  197. }
  198. //新增数据
  199. nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
  200. nursingHomeExportRecordsDb.Set()
  201. marshal, _ := json.Marshal(req)
  202. nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
  203. Name: fileName,
  204. DownloadPath: fileName,
  205. Kind: req.Kind,
  206. ReqContent: string(marshal),
  207. CreateAt: now.Format("2006-01-02 15:04:05"),
  208. UpdateAt: now.Format("2006-01-02 15:04:05"),
  209. })
  210. }
  211. func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
  212. var titleList []string
  213. var fileName, downloadPath string
  214. var now = time.Now()
  215. switch req.Kind {
  216. case 1:
  217. //教师消费统计表
  218. xlsx, _ := excelize.OpenFile("./static/template/自营学校(教师消费统计表).xlsx")
  219. fileName = "教师消费统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  220. downloadPath = "/export/self_support_for_school/" + fileName
  221. //查询数据
  222. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  223. sess := db.Db.Desc("self_support_for_school_ord.id")
  224. if req.EnterpriseId != 0 {
  225. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  226. }
  227. if req.StartDate != "" {
  228. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  229. }
  230. if req.EndDate != "" {
  231. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  232. }
  233. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  234. _, err := sess.
  235. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  236. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  237. Join("LEFT", "user", "user.id = user_identity.uid").
  238. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  239. Join("LEFT", "class", "class_with_user.class_id = class.id").
  240. Join("LEFT", "grade", "class.grade_id = grade.id").
  241. FindAndCount(&m)
  242. if err != nil {
  243. logx.Error(err)
  244. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  245. return
  246. }
  247. //整合数据
  248. var data = map[string]map[string]map[string]map[string]interface{}{}
  249. for _, vv := range m {
  250. if data[vv.Enterprise.Name] == nil {
  251. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  252. }
  253. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  254. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  255. }
  256. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  257. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  258. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  259. }
  260. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  261. "id": vv.UserIdentity.IdNo,
  262. }
  263. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  264. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  265. }
  266. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  267. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  268. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  269. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  270. }
  271. xlsx.SetSheetRow("", "A1", &titleList)
  272. sheetIndex := 2 //第一个表是模板表
  273. j := 3 //表头前两行被占用了,只能从第三行开始
  274. for k, v := range data {
  275. xlsx.NewSheet(k) //1、新建表格
  276. xlsx.CopySheet(1, sheetIndex)
  277. for kk, vv := range v {
  278. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  279. vv["早餐"]["num"], vv["早餐"]["amount"],
  280. vv["午餐"]["num"], vv["午餐"]["amount"],
  281. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  282. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  283. vv["未知"]["num"], vv["未知"]["amount"],
  284. vv["合计"]["num"], vv["合计"]["amount"],
  285. })
  286. j++
  287. }
  288. sheetIndex++
  289. }
  290. xlsx.DeleteSheet("template") //删除模板表
  291. //将文件保存至服务器
  292. xlsx.SaveAs("./static" + downloadPath)
  293. break
  294. case 2:
  295. //商家分餐数据统计表
  296. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  297. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  298. downloadPath = "/export/self_support_for_school/" + fileName
  299. //查询出所有的设备
  300. var deviceList []*model.Device
  301. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  302. if err != nil {
  303. logx.Error(err)
  304. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  305. return
  306. }
  307. var deviceListMapping = map[string]model.Device{}
  308. for _, v := range deviceList {
  309. deviceListMapping[v.DeviceSn] = *v
  310. }
  311. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  312. sess := db.Db.Desc("self_support_for_school_ord.id")
  313. if req.EnterpriseId != 0 {
  314. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  315. }
  316. if req.StartDate != "" {
  317. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  318. }
  319. if req.EndDate != "" {
  320. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  321. }
  322. _, err = sess.
  323. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  324. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  325. Join("LEFT", "user", "user.id = user_identity.uid").
  326. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  327. Join("LEFT", "class", "class_with_user.class_id = class.id").
  328. Join("LEFT", "grade", "class.grade_id = grade.id").
  329. FindAndCount(&m)
  330. if err != nil {
  331. logx.Error(err)
  332. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  333. return
  334. }
  335. //整合数据
  336. var data = map[string]map[string]map[string]map[string]float64{}
  337. for _, vv := range m {
  338. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  339. if data[vv.Enterprise.Name] == nil {
  340. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  341. }
  342. if data[vv.Enterprise.Name][shopName] == nil {
  343. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  344. }
  345. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  346. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  347. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  348. }
  349. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  350. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  351. }
  352. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  353. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  354. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  355. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  356. }
  357. xlsx.SetSheetRow("", "A1", &titleList)
  358. sheetIndex := 2 //第一个表是模板表
  359. for k, v := range data {
  360. if k == "" {
  361. k = "未知"
  362. }
  363. xlsx.NewSheet(k) //1、新建表格
  364. xlsx.CopySheet(1, sheetIndex)
  365. j := 4 //表头前三行被占用了,只能从第四行开始
  366. for kk, vv := range v {
  367. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  368. vv["早餐"]["num"], vv["早餐"]["amount"],
  369. vv["午餐"]["num"], vv["午餐"]["amount"],
  370. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  371. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  372. vv["合计"]["num"], vv["合计"]["amount"],
  373. })
  374. j++
  375. }
  376. sheetIndex++
  377. }
  378. xlsx.DeleteSheet("template") //删除模板表
  379. //将文件保存至服务器
  380. xlsx.SaveAs("./static" + downloadPath)
  381. break
  382. }
  383. //新增数据
  384. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  385. selfSupportForSchoolExportRecordsDb.Set()
  386. marshal, _ := json.Marshal(req)
  387. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  388. Name: fileName,
  389. DownloadPath: downloadPath,
  390. Kind: req.Kind,
  391. ReqContent: string(marshal),
  392. CreateAt: now.Format("2006-01-02 15:04:05"),
  393. UpdateAt: now.Format("2006-01-02 15:04:05"),
  394. })
  395. }
  396. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  397. var titleList []string
  398. var fileName, downloadPath string
  399. var now = time.Now()
  400. for _, kind := range req.Kinds {
  401. xlsx := excelize.NewFile()
  402. switch kind {
  403. case 1:
  404. //订单列表
  405. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  406. downloadPath = "/export/central_kitchen_for_school/" + fileName
  407. //查找订单数据
  408. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  409. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  410. if req.EnterpriseId != 0 {
  411. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  412. }
  413. if req.StartDate != "" {
  414. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  415. }
  416. if req.EndDate != "" {
  417. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  418. }
  419. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  420. _, err := sess.
  421. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  422. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  423. Join("LEFT", "user", "user.id = user_identity.uid").
  424. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  425. Join("LEFT", "class", "class_with_user.class_id = class.id").
  426. Join("LEFT", "grade", "class.grade_id = grade.id").
  427. FindAndCount(&m)
  428. if err != nil {
  429. logx.Error(err)
  430. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  431. return
  432. }
  433. //查找预定数据
  434. var outTradeNoList []string
  435. for _, v := range m {
  436. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  437. }
  438. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  439. sess1 := db.Db.Desc("id")
  440. if req.EnterpriseId != 0 {
  441. sess1.And("enterprise_id = ?", req.EnterpriseId)
  442. }
  443. if len(req.MealKindList) > 0 {
  444. sess1.In("kind", req.MealKindList)
  445. }
  446. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  447. if err != nil {
  448. logx.Error(err)
  449. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  450. return
  451. }
  452. var exportOutTradeNoList []string
  453. for _, v := range mm {
  454. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  455. }
  456. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定天数", "订单价格", "支付时间", "订单状态"}
  457. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  458. j := 2 //表头被第一行用了,只能从第二行开始
  459. for _, v := range m {
  460. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  461. continue
  462. }
  463. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  464. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  465. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  466. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  467. break
  468. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  469. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  470. break
  471. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  472. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  473. break
  474. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  475. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  476. break
  477. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  478. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  479. break
  480. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  481. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  482. break
  483. }
  484. //预定天数、预定日期
  485. var list []*model.CentralKitchenForSchoolUserWithDay
  486. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  487. if err1 != nil {
  488. logx.Error(err)
  489. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  490. return
  491. }
  492. var reserveDays string
  493. for _, v1 := range list {
  494. reserveDays += v1.Date
  495. }
  496. if v.Grade.Name == "" {
  497. v.Grade.Name = "教师"
  498. }
  499. if v.Class.Name == "" {
  500. v.Class.Name = "教师"
  501. }
  502. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  503. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  504. j++
  505. }
  506. //将文件保存至服务器
  507. xlsx.SaveAs("./static" + downloadPath)
  508. break
  509. case 2:
  510. //退款申请表
  511. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  512. downloadPath = "/export/central_kitchen_for_school/" + fileName
  513. //查找退款申请数据
  514. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  515. if req.EnterpriseId != 0 {
  516. sess.And("enterprise.id =?", req.EnterpriseId)
  517. }
  518. if req.StartDate != "" {
  519. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  520. }
  521. if req.EndDate != "" {
  522. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  523. }
  524. if len(req.MealKindList) > 0 {
  525. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  526. }
  527. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  528. _, err := sess.
  529. 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").
  530. 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").
  531. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  532. Join("LEFT", "user", "user_identity.uid = user.id").
  533. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  534. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  535. Join("LEFT", "class", "class_with_user.class_id = class.id").
  536. Join("LEFT", "grade", "class.grade_id = grade.id").
  537. Desc("central_kitchen_for_school_user_refund_day.id").
  538. FindAndCount(&m)
  539. if err != nil {
  540. logx.Error(err)
  541. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  542. return
  543. }
  544. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  545. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  546. j := 2 //表头被第一行用了,只能从第二行开始
  547. for _, v := range m {
  548. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  549. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  550. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  551. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  552. break
  553. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  554. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  555. break
  556. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  557. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  558. break
  559. }
  560. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  561. switch v.CentralKitchenForSchoolUserRefundDay.State {
  562. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  563. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  564. break
  565. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  566. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  567. break
  568. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  569. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  570. break
  571. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  572. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  573. break
  574. }
  575. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  576. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  577. j++
  578. }
  579. //将文件保存至服务器
  580. xlsx.SaveAs("./static" + downloadPath)
  581. break
  582. case 3:
  583. //学校预定统计表
  584. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  585. downloadPath = "/export/central_kitchen_for_school/" + fileName
  586. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  587. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  588. if req.EnterpriseId != 0 {
  589. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  590. }
  591. if req.StartDate != "" {
  592. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  593. }
  594. if req.EndDate != "" {
  595. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  596. }
  597. if len(req.MealKindList) > 0 {
  598. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  599. }
  600. _, err := sess.
  601. 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").
  602. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  603. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  604. Join("LEFT", "user", "user.id = user_identity.uid").
  605. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  606. Join("LEFT", "class", "class_with_user.class_id = class.id").
  607. Join("LEFT", "grade", "class.grade_id = grade.id").
  608. FindAndCount(&m)
  609. if err != nil {
  610. logx.Error(err)
  611. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  612. return
  613. }
  614. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  615. centralKitchenForSchoolUserWithDayDb.Set(0)
  616. var list = map[string]map[string]map[string]map[string]int64{}
  617. var xlsFileName, xlsDownloadPath []string
  618. for _, v := range m {
  619. if list[v.Enterprise.Name] == nil {
  620. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  621. }
  622. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  623. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  624. }
  625. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  626. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  627. }
  628. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  629. if err2 != nil {
  630. logx.Error(err)
  631. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  632. return
  633. }
  634. time.Sleep(50 * time.Millisecond) // 50毫秒
  635. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  636. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  637. }
  638. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  639. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  640. }
  641. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  642. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  643. }
  644. }
  645. for k1, v1 := range list {
  646. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  647. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  648. titleList = []string{req.StartDate + "~" + req.EndDate + " " + k1}
  649. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  650. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  651. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  652. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  653. j := 3 //表头前两行用了,只能从第三行开始
  654. for k2, v2 := range v1 {
  655. if k2 == "" {
  656. //若年级为空,则为教师餐
  657. k2 = "教师餐"
  658. }
  659. for k3, v3 := range v2 {
  660. if k3 == "" {
  661. //若班级为空,则为教师餐
  662. k3 = "教师餐"
  663. }
  664. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  665. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  666. j++
  667. }
  668. }
  669. //将文件保存至服务器
  670. err1 := xlsx.SaveAs(tmpDownloadPath)
  671. if err1 != nil {
  672. logx.Error(err1)
  673. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  674. panic(err1)
  675. }
  676. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  677. xlsFileName = append(xlsFileName, tmpFileName)
  678. }
  679. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  680. if err != nil {
  681. logx.Error(err)
  682. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  683. panic(err)
  684. }
  685. defer archive.Close()
  686. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  687. for k, v := range xlsDownloadPath {
  688. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  689. if err1 != nil {
  690. logx.Error(err1)
  691. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  692. panic(err1)
  693. }
  694. f, err1 := os.Open(v) //打开待压缩的文件
  695. if err1 != nil {
  696. logx.Error(err1)
  697. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  698. panic(err1)
  699. }
  700. if _, err1 = io.Copy(w, f); err != nil {
  701. logx.Error(err1)
  702. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  703. panic(err1)
  704. }
  705. if err1 != nil {
  706. logx.Error(err1)
  707. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  708. panic(err1)
  709. }
  710. f.Close()
  711. os.RemoveAll(v) //TODO::移除源文件
  712. }
  713. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  714. if err != nil {
  715. panic(err)
  716. }
  717. break
  718. case 4:
  719. //班级明细表
  720. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  721. downloadPath = "/export/central_kitchen_for_school/" + fileName
  722. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  723. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  724. if req.EnterpriseId != 0 {
  725. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  726. }
  727. if req.StartDate != "" {
  728. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  729. }
  730. if req.EndDate != "" {
  731. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  732. }
  733. if len(req.MealKindList) > 0 {
  734. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  735. }
  736. _, err := sess.
  737. 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").
  738. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  739. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  740. Join("LEFT", "user", "user.id = user_identity.uid").
  741. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  742. Join("LEFT", "class", "class_with_user.class_id = class.id").
  743. Join("LEFT", "grade", "class.grade_id = grade.id").
  744. FindAndCount(&m)
  745. if err != nil {
  746. logx.Error(err)
  747. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  748. return
  749. }
  750. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  751. var xlsFileName, xlsDownloadPath []string
  752. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  753. centralKitchenForSchoolUserWithDayDb.Set(0)
  754. for _, v := range m {
  755. if list[v.Enterprise.Name] == nil {
  756. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  757. }
  758. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  759. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  760. }
  761. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  762. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  763. }
  764. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  765. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  766. }
  767. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  768. if err2 != nil {
  769. logx.Error(err)
  770. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  771. return
  772. }
  773. time.Sleep(50 * time.Millisecond) // 50毫秒
  774. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  775. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  776. }
  777. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  778. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  779. }
  780. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  781. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  782. }
  783. }
  784. for k1, v1 := range list {
  785. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  786. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  787. for k2, v2 := range v1 {
  788. if k2 == "" {
  789. //若年级为空,则为教师餐
  790. continue
  791. }
  792. for k3, v3 := range v2 {
  793. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  794. xlsx.NewSheet(k3)
  795. xlsx.SetSheetRow(k3, "A1", &titleList)
  796. j := 2 //表头被第一行用了,只能从第二行开始
  797. for k4, v4 := range v3 {
  798. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  799. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  800. j++
  801. }
  802. }
  803. }
  804. //将文件保存至服务器
  805. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  806. err1 := xlsx.SaveAs(tmpDownloadPath)
  807. if err1 != nil {
  808. logx.Error(err1)
  809. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  810. panic(err1)
  811. }
  812. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  813. xlsFileName = append(xlsFileName, tmpFileName)
  814. }
  815. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  816. if err != nil {
  817. logx.Error(err)
  818. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  819. panic(err)
  820. }
  821. defer archive.Close()
  822. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  823. for k, v := range xlsDownloadPath {
  824. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  825. if err1 != nil {
  826. logx.Error(err1)
  827. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  828. panic(err1)
  829. }
  830. f, err1 := os.Open(v) //打开待压缩的文件
  831. if err1 != nil {
  832. logx.Error(err1)
  833. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  834. panic(err1)
  835. }
  836. if _, err1 = io.Copy(w, f); err != nil {
  837. logx.Error(err1)
  838. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  839. panic(err1)
  840. }
  841. if err1 != nil {
  842. logx.Error(err1)
  843. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  844. panic(err1)
  845. }
  846. f.Close()
  847. os.RemoveAll(v) //TODO::移除源文件
  848. }
  849. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  850. if err != nil {
  851. panic(err)
  852. }
  853. break
  854. case 5:
  855. //退款明细表
  856. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  857. downloadPath = "/export/central_kitchen_for_school/" + fileName
  858. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  859. if req.EnterpriseId != 0 {
  860. sess.And("enterprise.id =?", req.EnterpriseId)
  861. }
  862. if req.StartDate != "" {
  863. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  864. }
  865. if req.EndDate != "" {
  866. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  867. }
  868. if len(req.MealKindList) > 0 {
  869. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  870. }
  871. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  872. _, err := sess.
  873. 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").
  874. 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").
  875. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  876. Join("LEFT", "user", "user_identity.uid = user.id").
  877. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  878. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  879. Join("LEFT", "class", "class_with_user.class_id = class.id").
  880. Join("LEFT", "grade", "class.grade_id = grade.id").
  881. Desc("central_kitchen_for_school_user_refund_day.id").
  882. FindAndCount(&m)
  883. if err != nil {
  884. logx.Error(err)
  885. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  886. return
  887. }
  888. var list = map[string]map[string]map[string]map[string]map[string]int{}
  889. var xlsFileName, xlsDownloadPath []string
  890. for _, v := range m {
  891. if list[v.Enterprise.Name] == nil {
  892. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  893. }
  894. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  895. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  896. }
  897. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  898. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  899. }
  900. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  901. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  902. }
  903. //就餐类型(1:早餐 2:午餐 3:晚餐)
  904. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  905. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  906. }
  907. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  908. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  909. }
  910. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  911. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  912. }
  913. }
  914. for k1, v1 := range list {
  915. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  916. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  917. for k2, v2 := range v1 {
  918. if k2 == "" {
  919. //若年级为空,则为教师餐
  920. continue
  921. }
  922. for k3, v3 := range v2 {
  923. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  924. xlsx.NewSheet(k3)
  925. xlsx.SetSheetRow(k3, "A1", &titleList)
  926. j := 2 //表头被第一行用了,只能从第二行开始
  927. for k4, v4 := range v3 {
  928. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  929. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  930. j++
  931. }
  932. }
  933. }
  934. //将文件保存至服务器
  935. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  936. err1 := xlsx.SaveAs(tmpDownloadPath)
  937. if err1 != nil {
  938. logx.Error(err1)
  939. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  940. panic(err1)
  941. }
  942. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  943. xlsFileName = append(xlsFileName, tmpFileName)
  944. }
  945. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  946. if err != nil {
  947. logx.Error(err)
  948. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  949. panic(err)
  950. }
  951. defer archive.Close()
  952. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  953. for k, v := range xlsDownloadPath {
  954. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  955. if err1 != nil {
  956. logx.Error(err1)
  957. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  958. panic(err1)
  959. }
  960. f, err1 := os.Open(v) //打开待压缩的文件
  961. if err1 != nil {
  962. logx.Error(err1)
  963. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  964. panic(err1)
  965. }
  966. if _, err1 = io.Copy(w, f); err != nil {
  967. logx.Error(err1)
  968. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  969. panic(err1)
  970. }
  971. if err1 != nil {
  972. logx.Error(err1)
  973. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  974. panic(err1)
  975. }
  976. f.Close()
  977. os.RemoveAll(v) //TODO::移除源文件
  978. }
  979. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  980. if err != nil {
  981. panic(err)
  982. }
  983. break
  984. case 6:
  985. //收款统计表
  986. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  987. downloadPath = "/export/central_kitchen_for_school/" + fileName
  988. //查找数据
  989. var m []model.Enterprise
  990. sess := db.Db.Desc("id")
  991. if req.EnterpriseId != 0 {
  992. sess.And("id = ?", req.EnterpriseId)
  993. }
  994. err := sess.Find(&m)
  995. if err != nil {
  996. logx.Error(err)
  997. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  998. return
  999. }
  1000. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1001. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1002. j := 2 //表头被第一行用了,只能从第二行开始
  1003. for _, v := range m {
  1004. var income, refund string
  1005. //统计收入
  1006. 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)
  1007. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1008. if err != nil {
  1009. logx.Error(err)
  1010. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1011. return
  1012. }
  1013. if mapArr1 != nil {
  1014. income = mapArr1[0]["total"]
  1015. }
  1016. //统计退款
  1017. //查询出当前学校下所有的学生
  1018. userIdentityDb := db.UserIdentityDb{}
  1019. userIdentityDb.Set(0)
  1020. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1021. var userIdentityIds []string
  1022. for _, v := range *userIdentities {
  1023. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1024. }
  1025. if len(userIdentityIds) != 0 {
  1026. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1027. 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'",
  1028. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1029. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1030. if err != nil {
  1031. logx.Error(err)
  1032. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1033. return
  1034. }
  1035. if mapArr2 != nil {
  1036. refund = mapArr2[0]["total"]
  1037. }
  1038. }
  1039. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1040. rateFee := realityAmount * 0.0038
  1041. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1042. j++
  1043. }
  1044. //将文件保存至服务器
  1045. xlsx.SaveAs("./static" + downloadPath)
  1046. break
  1047. case 7:
  1048. //班级收款明细表
  1049. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1050. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1051. //查找订单数据
  1052. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1053. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  1054. if req.EnterpriseId != 0 {
  1055. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1056. }
  1057. if req.StartDate != "" {
  1058. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1059. }
  1060. if req.EndDate != "" {
  1061. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1062. }
  1063. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1064. _, err := sess.
  1065. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1066. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1067. Join("LEFT", "user", "user.id = user_identity.uid").
  1068. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1069. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1070. Join("LEFT", "grade", "class.grade_id = grade.id").
  1071. FindAndCount(&m)
  1072. if err != nil {
  1073. logx.Error(err)
  1074. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1075. return
  1076. }
  1077. sess1 := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1078. if req.EnterpriseId != 0 {
  1079. sess1.And("enterprise.id =?", req.EnterpriseId)
  1080. }
  1081. if req.StartDate != "" {
  1082. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1083. }
  1084. if req.EndDate != "" {
  1085. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1086. }
  1087. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1088. _, err = sess1.
  1089. 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").
  1090. 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").
  1091. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1092. Join("LEFT", "user", "user_identity.uid = user.id").
  1093. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1094. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1095. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1096. Join("LEFT", "grade", "class.grade_id = grade.id").
  1097. Desc("central_kitchen_for_school_user_refund_day.id").
  1098. FindAndCount(&mm)
  1099. if err != nil {
  1100. logx.Error(err)
  1101. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1102. return
  1103. }
  1104. var list = map[string]map[string]map[string]map[string]float64{}
  1105. for _, v := range m {
  1106. if list[v.Enterprise.Name] == nil {
  1107. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1108. }
  1109. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1110. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1111. }
  1112. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1113. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1114. }
  1115. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1116. }
  1117. for _, v := range mm {
  1118. if list[v.Enterprise.Name] == nil {
  1119. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1120. }
  1121. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1122. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1123. }
  1124. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1125. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1126. }
  1127. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1128. }
  1129. var xlsFileName, xlsDownloadPath []string
  1130. for k1, v1 := range list {
  1131. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1132. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1133. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1134. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1135. j := 2 //表头被第一行用了,只能从第二行开始
  1136. for k2, v2 := range v1 {
  1137. if k2 == "" {
  1138. //若年级为空,则为教师餐
  1139. continue
  1140. }
  1141. for k3, v3 := range v2 {
  1142. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1143. j++
  1144. }
  1145. }
  1146. //将文件保存至服务器
  1147. err1 := xlsx.SaveAs(tmpDownloadPath)
  1148. if err1 != nil {
  1149. logx.Error(err1)
  1150. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1151. panic(err1)
  1152. }
  1153. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1154. xlsFileName = append(xlsFileName, tmpFileName)
  1155. }
  1156. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1157. if err != nil {
  1158. logx.Error(err)
  1159. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1160. panic(err)
  1161. }
  1162. defer archive.Close()
  1163. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1164. for k, v := range xlsDownloadPath {
  1165. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1166. if err1 != nil {
  1167. logx.Error(err1)
  1168. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1169. panic(err1)
  1170. }
  1171. f, err1 := os.Open(v) //打开待压缩的文件
  1172. if err1 != nil {
  1173. logx.Error(err1)
  1174. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1175. panic(err1)
  1176. }
  1177. if _, err1 = io.Copy(w, f); err != nil {
  1178. logx.Error(err1)
  1179. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1180. panic(err1)
  1181. }
  1182. if err1 != nil {
  1183. logx.Error(err1)
  1184. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1185. panic(err1)
  1186. }
  1187. f.Close()
  1188. os.RemoveAll(v) //TODO::移除源文件
  1189. }
  1190. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1191. if err != nil {
  1192. panic(err)
  1193. }
  1194. break
  1195. case 8:
  1196. //学校预定数量统计表
  1197. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1198. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1199. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1200. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1201. if req.EnterpriseId != 0 {
  1202. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1203. }
  1204. if req.StartDate != "" {
  1205. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  1206. }
  1207. if req.EndDate != "" {
  1208. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1209. }
  1210. if len(req.MealKindList) > 0 {
  1211. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1212. }
  1213. _, err := sess.
  1214. 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").
  1215. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1216. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1217. FindAndCount(&m)
  1218. if err != nil {
  1219. logx.Error(err)
  1220. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1221. return
  1222. }
  1223. var list = map[string]int64{}
  1224. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1225. centralKitchenForSchoolUserWithDayDb.Set(0)
  1226. for _, v := range m {
  1227. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1228. if err2 != nil {
  1229. logx.Error(err)
  1230. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1231. return
  1232. }
  1233. time.Sleep(50 * time.Millisecond) // 50毫秒
  1234. list[v.Enterprise.Name] += count
  1235. }
  1236. titleList = []string{"学校名称", "预定总数"}
  1237. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1238. j := 2 //表头被第一行用了,只能从第二行开始
  1239. for k, v := range list {
  1240. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v})
  1241. j++
  1242. }
  1243. //将文件保存至服务器
  1244. xlsx.SaveAs("./static" + downloadPath)
  1245. break
  1246. }
  1247. //新增数据
  1248. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1249. centralKitchenForSchoolExportRecordsDb.Set()
  1250. marshal, _ := json.Marshal(req)
  1251. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1252. Name: fileName,
  1253. DownloadPath: downloadPath,
  1254. Kind: kind,
  1255. ReqContent: string(marshal),
  1256. CreateAt: now.Format("2006-01-02 15:04:05"),
  1257. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1258. })
  1259. }
  1260. }
  1261. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1262. var titleList []string
  1263. var fileName, downloadPath string
  1264. var now = time.Now()
  1265. xlsx := excelize.NewFile()
  1266. //数据对比
  1267. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1268. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1269. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1270. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1271. if req.EnterpriseId != 0 {
  1272. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1273. }
  1274. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date1[0])
  1275. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date1[1])
  1276. if len(req.MealKindList) > 0 {
  1277. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1278. }
  1279. _, err := sess.
  1280. 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").
  1281. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1282. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1283. Join("LEFT", "user", "user.id = user_identity.uid").
  1284. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1285. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1286. Join("LEFT", "grade", "class.grade_id = grade.id").
  1287. FindAndCount(&m)
  1288. if err != nil {
  1289. logx.Error(err)
  1290. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1291. return
  1292. }
  1293. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1294. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1295. if req.EnterpriseId != 0 {
  1296. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1297. }
  1298. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date2[0])
  1299. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date2[1])
  1300. if len(req.MealKindList) > 0 {
  1301. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1302. }
  1303. _, err1 := sess1.
  1304. 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").
  1305. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1306. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1307. Join("LEFT", "user", "user.id = user_identity.uid").
  1308. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1309. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1310. Join("LEFT", "grade", "class.grade_id = grade.id").
  1311. FindAndCount(&m)
  1312. if err1 != nil {
  1313. logx.Error(err1)
  1314. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1315. return
  1316. }
  1317. var list = map[string]map[string]map[string]map[string]string{}
  1318. for _, v := range m {
  1319. if list[v.Enterprise.Name] == nil {
  1320. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1321. }
  1322. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1323. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1324. }
  1325. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1326. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1327. }
  1328. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1329. }
  1330. var list1 = map[string]map[string]map[string]map[string]string{}
  1331. for _, v := range m1 {
  1332. if list1[v.Enterprise.Name] == nil {
  1333. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1334. }
  1335. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1336. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1337. }
  1338. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1339. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1340. }
  1341. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1342. }
  1343. var xlsFileName, xlsDownloadPath []string
  1344. for k1, v1 := range list {
  1345. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1346. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1347. for k2, v2 := range v1 {
  1348. if k2 == "" {
  1349. //若年级为空,则为教师
  1350. continue
  1351. }
  1352. for k3, v3 := range v2 {
  1353. titleList = []string{"姓名", "家长联系电话"}
  1354. xlsx.NewSheet(k3)
  1355. xlsx.SetSheetRow(k3, "A1", &titleList)
  1356. j := 2 //表头被第一行用了,只能从第二行开始
  1357. for k4, v4 := range v3 {
  1358. //TODO::判断当前用户是否在新的月份里面
  1359. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1360. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1361. k4, v4})
  1362. j++
  1363. }
  1364. }
  1365. }
  1366. }
  1367. //将文件保存至服务器
  1368. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1369. err1 := xlsx.SaveAs(tmpDownloadPath)
  1370. if err1 != nil {
  1371. logx.Error(err1)
  1372. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1373. panic(err1)
  1374. }
  1375. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1376. xlsFileName = append(xlsFileName, tmpFileName)
  1377. }
  1378. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1379. if err != nil {
  1380. logx.Error(err)
  1381. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1382. panic(err)
  1383. }
  1384. defer archive.Close()
  1385. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1386. for k, v := range xlsDownloadPath {
  1387. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1388. if err1 != nil {
  1389. logx.Error(err1)
  1390. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1391. panic(err1)
  1392. }
  1393. f, err1 := os.Open(v) //打开待压缩的文件
  1394. if err1 != nil {
  1395. logx.Error(err1)
  1396. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1397. panic(err1)
  1398. }
  1399. if _, err1 = io.Copy(w, f); err != nil {
  1400. logx.Error(err1)
  1401. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1402. panic(err1)
  1403. }
  1404. if err1 != nil {
  1405. logx.Error(err1)
  1406. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1407. panic(err1)
  1408. }
  1409. f.Close()
  1410. os.RemoveAll(v) //TODO::移除源文件
  1411. }
  1412. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1413. if err != nil {
  1414. panic(err)
  1415. }
  1416. //新增数据
  1417. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1418. centralKitchenForSchoolExportRecordsDb.Set()
  1419. marshal, _ := json.Marshal(req)
  1420. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1421. Name: fileName,
  1422. DownloadPath: downloadPath,
  1423. Kind: 9, //数据对比
  1424. ReqContent: string(marshal),
  1425. CreateAt: now.Format("2006-01-02 15:04:05"),
  1426. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1427. })
  1428. }
  1429. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1430. mealZh = "未知"
  1431. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1432. for _, v := range md.SelfSupportForMealTimeMapping {
  1433. //判断时间大小
  1434. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  1435. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  1436. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  1437. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  1438. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1439. mealZh = v["name"]
  1440. }
  1441. }
  1442. return
  1443. }