智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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