智慧食堂
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.

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