智慧食堂
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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