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

1271 lines
53 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. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定天数", "订单价格", "支付时间", "订单状态"}
  421. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  422. j := 2 //表头被第一行用了,只能从第二行开始
  423. for _, v := range m {
  424. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  425. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  426. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  427. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  428. break
  429. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  430. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  431. break
  432. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  433. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  434. break
  435. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  436. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  437. break
  438. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  439. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  440. break
  441. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  442. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  443. break
  444. }
  445. //预定天数、预定日期
  446. var list []*model.CentralKitchenForSchoolUserWithDay
  447. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  448. if err1 != nil {
  449. logx.Error(err)
  450. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  451. return
  452. }
  453. var reserveDays string
  454. for _, v1 := range list {
  455. reserveDays += v1.Date
  456. }
  457. if v.Grade.Name == "" {
  458. v.Grade.Name = "教师"
  459. }
  460. if v.Class.Name == "" {
  461. v.Class.Name = "教师"
  462. }
  463. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  464. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  465. j++
  466. }
  467. //将文件保存至服务器
  468. xlsx.SaveAs("./static" + downloadPath)
  469. break
  470. case 2:
  471. //退款申请表
  472. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  473. downloadPath = "/export/central_kitchen_for_school/" + fileName
  474. //查找退款申请数据
  475. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  476. if req.EnterpriseId != 0 {
  477. sess.And("enterprise.id =?", req.EnterpriseId)
  478. }
  479. if req.StartDate != "" {
  480. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  481. }
  482. if req.EndDate != "" {
  483. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  484. }
  485. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  486. _, err := sess.
  487. 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").
  488. 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").
  489. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  490. Join("LEFT", "user", "user_identity.uid = user.id").
  491. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  492. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  493. Join("LEFT", "class", "class_with_user.class_id = class.id").
  494. Join("LEFT", "grade", "class.grade_id = grade.id").
  495. Desc("central_kitchen_for_school_user_refund_day.id").
  496. FindAndCount(&m)
  497. if err != nil {
  498. logx.Error(err)
  499. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  500. return
  501. }
  502. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  503. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  504. j := 2 //表头被第一行用了,只能从第二行开始
  505. for _, v := range m {
  506. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  507. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  508. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  509. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  510. break
  511. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  512. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  513. break
  514. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  515. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  516. break
  517. }
  518. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  519. switch v.CentralKitchenForSchoolUserRefundDay.State {
  520. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  521. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  522. break
  523. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  524. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  525. break
  526. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  527. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  528. break
  529. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  530. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  531. break
  532. }
  533. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  534. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  535. j++
  536. }
  537. //将文件保存至服务器
  538. xlsx.SaveAs("./static" + downloadPath)
  539. break
  540. case 3:
  541. //学校预定统计表
  542. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  543. downloadPath = "/export/central_kitchen_for_school/" + fileName
  544. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  545. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  546. if req.EnterpriseId != 0 {
  547. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  548. }
  549. if req.StartDate != "" {
  550. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  551. }
  552. if req.EndDate != "" {
  553. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  554. }
  555. _, err := sess.
  556. 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").
  557. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  558. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  559. Join("LEFT", "user", "user.id = user_identity.uid").
  560. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  561. Join("LEFT", "class", "class_with_user.class_id = class.id").
  562. Join("LEFT", "grade", "class.grade_id = grade.id").
  563. FindAndCount(&m)
  564. if err != nil {
  565. logx.Error(err)
  566. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  567. return
  568. }
  569. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  570. centralKitchenForSchoolUserWithDayDb.Set(0)
  571. var list = map[string]map[string]map[string]map[string]int64{}
  572. var xlsFileName, xlsDownloadPath []string
  573. for _, v := range m {
  574. if list[v.Enterprise.Name] == nil {
  575. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  576. }
  577. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  578. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  579. }
  580. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  581. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  582. }
  583. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  584. if err2 != nil {
  585. logx.Error(err)
  586. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  587. return
  588. }
  589. time.Sleep(50 * time.Millisecond) // 50毫秒
  590. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  591. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  592. }
  593. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  594. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  595. }
  596. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  597. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  598. }
  599. }
  600. for k1, v1 := range list {
  601. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  602. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  603. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  604. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  605. j := 2 //表头被第一行用了,只能从第二行开始
  606. for k2, v2 := range v1 {
  607. if k2 == "" {
  608. //若年级为空,则为教师餐
  609. k2 = "教师餐"
  610. }
  611. for k3, v3 := range v2 {
  612. if k3 == "" {
  613. //若班级为空,则为教师餐
  614. k3 = "教师餐"
  615. }
  616. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  617. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  618. j++
  619. }
  620. }
  621. //将文件保存至服务器
  622. err1 := xlsx.SaveAs(tmpDownloadPath)
  623. if err1 != nil {
  624. logx.Error(err1)
  625. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  626. panic(err1)
  627. }
  628. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  629. xlsFileName = append(xlsFileName, tmpFileName)
  630. }
  631. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  632. if err != nil {
  633. logx.Error(err)
  634. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  635. panic(err)
  636. }
  637. defer archive.Close()
  638. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  639. for k, v := range xlsDownloadPath {
  640. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  641. if err1 != nil {
  642. logx.Error(err1)
  643. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  644. panic(err1)
  645. }
  646. f, err1 := os.Open(v) //打开待压缩的文件
  647. if err1 != nil {
  648. logx.Error(err1)
  649. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  650. panic(err1)
  651. }
  652. if _, err1 = io.Copy(w, f); err != nil {
  653. logx.Error(err1)
  654. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  655. panic(err1)
  656. }
  657. if err1 != nil {
  658. logx.Error(err1)
  659. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  660. panic(err1)
  661. }
  662. f.Close()
  663. os.RemoveAll(v) //TODO::移除源文件
  664. }
  665. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  666. if err != nil {
  667. panic(err)
  668. }
  669. break
  670. case 4:
  671. //班级明细表
  672. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  673. downloadPath = "/export/central_kitchen_for_school/" + fileName
  674. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  675. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  676. if req.EnterpriseId != 0 {
  677. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  678. }
  679. if req.StartDate != "" {
  680. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  681. }
  682. if req.EndDate != "" {
  683. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  684. }
  685. _, err := sess.
  686. 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").
  687. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  688. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  689. Join("LEFT", "user", "user.id = user_identity.uid").
  690. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  691. Join("LEFT", "class", "class_with_user.class_id = class.id").
  692. Join("LEFT", "grade", "class.grade_id = grade.id").
  693. FindAndCount(&m)
  694. if err != nil {
  695. logx.Error(err)
  696. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  697. return
  698. }
  699. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  700. var xlsFileName, xlsDownloadPath []string
  701. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  702. centralKitchenForSchoolUserWithDayDb.Set(0)
  703. for _, v := range m {
  704. if list[v.Enterprise.Name] == nil {
  705. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  706. }
  707. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  708. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  709. }
  710. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  711. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  712. }
  713. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  714. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  715. }
  716. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  717. if err2 != nil {
  718. logx.Error(err)
  719. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  720. return
  721. }
  722. time.Sleep(50 * time.Millisecond) // 50毫秒
  723. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  724. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  725. }
  726. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  727. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  728. }
  729. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  730. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  731. }
  732. }
  733. for k1, v1 := range list {
  734. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  735. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  736. for k2, v2 := range v1 {
  737. if k2 == "" {
  738. //若年级为空,则为教师餐
  739. continue
  740. }
  741. for k3, v3 := range v2 {
  742. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  743. xlsx.NewSheet(k3)
  744. xlsx.SetSheetRow(k3, "A1", &titleList)
  745. j := 2 //表头被第一行用了,只能从第二行开始
  746. for k4, v4 := range v3 {
  747. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  748. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  749. j++
  750. }
  751. }
  752. }
  753. //将文件保存至服务器
  754. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  755. err1 := xlsx.SaveAs(tmpDownloadPath)
  756. if err1 != nil {
  757. logx.Error(err1)
  758. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  759. panic(err1)
  760. }
  761. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  762. xlsFileName = append(xlsFileName, tmpFileName)
  763. }
  764. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  765. if err != nil {
  766. logx.Error(err)
  767. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  768. panic(err)
  769. }
  770. defer archive.Close()
  771. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  772. for k, v := range xlsDownloadPath {
  773. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  774. if err1 != nil {
  775. logx.Error(err1)
  776. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  777. panic(err1)
  778. }
  779. f, err1 := os.Open(v) //打开待压缩的文件
  780. if err1 != nil {
  781. logx.Error(err1)
  782. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  783. panic(err1)
  784. }
  785. if _, err1 = io.Copy(w, f); err != nil {
  786. logx.Error(err1)
  787. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  788. panic(err1)
  789. }
  790. if err1 != nil {
  791. logx.Error(err1)
  792. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  793. panic(err1)
  794. }
  795. f.Close()
  796. os.RemoveAll(v) //TODO::移除源文件
  797. }
  798. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  799. if err != nil {
  800. panic(err)
  801. }
  802. break
  803. case 5:
  804. //退款明细表
  805. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  806. downloadPath = "/export/central_kitchen_for_school/" + fileName
  807. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  808. if req.EnterpriseId != 0 {
  809. sess.And("enterprise.id =?", req.EnterpriseId)
  810. }
  811. if req.StartDate != "" {
  812. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  813. }
  814. if req.EndDate != "" {
  815. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  816. }
  817. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  818. _, err := sess.
  819. 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").
  820. 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").
  821. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  822. Join("LEFT", "user", "user_identity.uid = user.id").
  823. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  824. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  825. Join("LEFT", "class", "class_with_user.class_id = class.id").
  826. Join("LEFT", "grade", "class.grade_id = grade.id").
  827. Desc("central_kitchen_for_school_user_refund_day.id").
  828. FindAndCount(&m)
  829. if err != nil {
  830. logx.Error(err)
  831. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  832. return
  833. }
  834. var list = map[string]map[string]map[string]map[string]map[string]int{}
  835. var xlsFileName, xlsDownloadPath []string
  836. for _, v := range m {
  837. if list[v.Enterprise.Name] == nil {
  838. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  839. }
  840. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  841. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  842. }
  843. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  844. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  845. }
  846. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  847. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  848. }
  849. //就餐类型(1:早餐 2:午餐 3:晚餐)
  850. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  851. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  852. }
  853. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  854. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  855. }
  856. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  857. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  858. }
  859. }
  860. for k1, v1 := range list {
  861. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  862. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  863. for k2, v2 := range v1 {
  864. if k2 == "" {
  865. //若年级为空,则为教师餐
  866. continue
  867. }
  868. for k3, v3 := range v2 {
  869. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  870. xlsx.NewSheet(k3)
  871. xlsx.SetSheetRow(k3, "A1", &titleList)
  872. j := 2 //表头被第一行用了,只能从第二行开始
  873. for k4, v4 := range v3 {
  874. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  875. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  876. j++
  877. }
  878. }
  879. }
  880. //将文件保存至服务器
  881. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  882. err1 := xlsx.SaveAs(tmpDownloadPath)
  883. if err1 != nil {
  884. logx.Error(err1)
  885. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  886. panic(err1)
  887. }
  888. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  889. xlsFileName = append(xlsFileName, tmpFileName)
  890. }
  891. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  892. if err != nil {
  893. logx.Error(err)
  894. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  895. panic(err)
  896. }
  897. defer archive.Close()
  898. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  899. for k, v := range xlsDownloadPath {
  900. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  901. if err1 != nil {
  902. logx.Error(err1)
  903. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  904. panic(err1)
  905. }
  906. f, err1 := os.Open(v) //打开待压缩的文件
  907. if err1 != nil {
  908. logx.Error(err1)
  909. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  910. panic(err1)
  911. }
  912. if _, err1 = io.Copy(w, f); err != nil {
  913. logx.Error(err1)
  914. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  915. panic(err1)
  916. }
  917. if err1 != nil {
  918. logx.Error(err1)
  919. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  920. panic(err1)
  921. }
  922. f.Close()
  923. os.RemoveAll(v) //TODO::移除源文件
  924. }
  925. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  926. if err != nil {
  927. panic(err)
  928. }
  929. break
  930. case 6:
  931. //收款统计表
  932. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  933. downloadPath = "/export/central_kitchen_for_school/" + fileName
  934. //查找数据
  935. var m []model.Enterprise
  936. sess := db.Db.Desc("id")
  937. if req.EnterpriseId != 0 {
  938. sess.And("id = ?", req.EnterpriseId)
  939. }
  940. err := sess.Find(&m)
  941. if err != nil {
  942. logx.Error(err)
  943. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  944. return
  945. }
  946. titleList = []string{"学校名称", "预定总费用", "退款总计"}
  947. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  948. for _, v := range m {
  949. var income, refund string
  950. //统计收入
  951. 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)
  952. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  953. if err != nil {
  954. logx.Error(err)
  955. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  956. return
  957. }
  958. if mapArr1 != nil {
  959. income = mapArr1[0]["total"]
  960. }
  961. //统计退款
  962. //查询出当前学校下所有的学生
  963. userIdentityDb := db.UserIdentityDb{}
  964. userIdentityDb.Set(0)
  965. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  966. var userIdentityIds []string
  967. for _, v := range *userIdentities {
  968. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  969. }
  970. userIdentityStr := php2go.Implode(",", userIdentityIds)
  971. 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'",
  972. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  973. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  974. if err != nil {
  975. logx.Error(err)
  976. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  977. return
  978. }
  979. if mapArr2 != nil {
  980. refund = mapArr2[0]["total"]
  981. }
  982. j := 2 //表头被第一行用了,只能从第二行开始
  983. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund})
  984. j++
  985. }
  986. //将文件保存至服务器
  987. xlsx.SaveAs("./static" + downloadPath)
  988. break
  989. case 7:
  990. //班级收款明细表
  991. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  992. downloadPath = "/export/central_kitchen_for_school/" + fileName
  993. //查找订单数据
  994. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  995. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  996. if req.EnterpriseId != 0 {
  997. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  998. }
  999. if req.StartDate != "" {
  1000. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1001. }
  1002. if req.EndDate != "" {
  1003. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1004. }
  1005. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1006. _, err := sess.
  1007. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1008. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1009. Join("LEFT", "user", "user.id = user_identity.uid").
  1010. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1011. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1012. Join("LEFT", "grade", "class.grade_id = grade.id").
  1013. FindAndCount(&m)
  1014. if err != nil {
  1015. logx.Error(err)
  1016. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1017. return
  1018. }
  1019. sess1 := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1020. if req.EnterpriseId != 0 {
  1021. sess1.And("enterprise.id =?", req.EnterpriseId)
  1022. }
  1023. if req.StartDate != "" {
  1024. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1025. }
  1026. if req.EndDate != "" {
  1027. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1028. }
  1029. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1030. _, err = sess1.
  1031. 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").
  1032. 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").
  1033. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1034. Join("LEFT", "user", "user_identity.uid = user.id").
  1035. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1036. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1037. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1038. Join("LEFT", "grade", "class.grade_id = grade.id").
  1039. Desc("central_kitchen_for_school_user_refund_day.id").
  1040. FindAndCount(&mm)
  1041. if err != nil {
  1042. logx.Error(err)
  1043. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1044. return
  1045. }
  1046. var list = map[string]map[string]map[string]map[string]float64{}
  1047. for _, v := range m {
  1048. if list[v.Enterprise.Name] == nil {
  1049. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1050. }
  1051. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1052. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1053. }
  1054. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1055. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1056. }
  1057. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1058. }
  1059. for _, v := range mm {
  1060. if list[v.Enterprise.Name] == nil {
  1061. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1062. }
  1063. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1064. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1065. }
  1066. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1067. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1068. }
  1069. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1070. }
  1071. var xlsFileName, xlsDownloadPath []string
  1072. for k1, v1 := range list {
  1073. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1074. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1075. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1076. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1077. j := 2 //表头被第一行用了,只能从第二行开始
  1078. for k2, v2 := range v1 {
  1079. if k2 == "" {
  1080. //若年级为空,则为教师餐
  1081. continue
  1082. }
  1083. for k3, v3 := range v2 {
  1084. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1085. j++
  1086. }
  1087. }
  1088. //将文件保存至服务器
  1089. err1 := xlsx.SaveAs(tmpDownloadPath)
  1090. if err1 != nil {
  1091. logx.Error(err1)
  1092. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1093. panic(err1)
  1094. }
  1095. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1096. xlsFileName = append(xlsFileName, tmpFileName)
  1097. }
  1098. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1099. if err != nil {
  1100. logx.Error(err)
  1101. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1102. panic(err)
  1103. }
  1104. defer archive.Close()
  1105. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1106. for k, v := range xlsDownloadPath {
  1107. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1108. if err1 != nil {
  1109. logx.Error(err1)
  1110. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1111. panic(err1)
  1112. }
  1113. f, err1 := os.Open(v) //打开待压缩的文件
  1114. if err1 != nil {
  1115. logx.Error(err1)
  1116. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1117. panic(err1)
  1118. }
  1119. if _, err1 = io.Copy(w, f); err != nil {
  1120. logx.Error(err1)
  1121. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1122. panic(err1)
  1123. }
  1124. if err1 != nil {
  1125. logx.Error(err1)
  1126. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1127. panic(err1)
  1128. }
  1129. f.Close()
  1130. os.RemoveAll(v) //TODO::移除源文件
  1131. }
  1132. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1133. if err != nil {
  1134. panic(err)
  1135. }
  1136. break
  1137. case 8:
  1138. //学校预定数量统计表
  1139. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1140. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1141. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1142. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1143. if req.EnterpriseId != 0 {
  1144. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  1145. }
  1146. if req.StartDate != "" {
  1147. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  1148. }
  1149. if req.EndDate != "" {
  1150. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1151. }
  1152. _, err := sess.
  1153. 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").
  1154. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1155. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1156. FindAndCount(&m)
  1157. if err != nil {
  1158. logx.Error(err)
  1159. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1160. return
  1161. }
  1162. var list = map[string]int64{}
  1163. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1164. centralKitchenForSchoolUserWithDayDb.Set(0)
  1165. for _, v := range m {
  1166. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1167. if err2 != nil {
  1168. logx.Error(err)
  1169. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1170. return
  1171. }
  1172. time.Sleep(50 * time.Millisecond) // 50毫秒
  1173. list[v.Enterprise.Name] += count
  1174. }
  1175. titleList = []string{"学校名称", "预定总数"}
  1176. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1177. j := 2 //表头被第一行用了,只能从第二行开始
  1178. for k, v := range list {
  1179. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v})
  1180. j++
  1181. }
  1182. //将文件保存至服务器
  1183. xlsx.SaveAs("./static" + downloadPath)
  1184. break
  1185. }
  1186. //新增数据
  1187. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1188. centralKitchenForSchoolExportRecordsDb.Set()
  1189. marshal, _ := json.Marshal(req)
  1190. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1191. Name: fileName,
  1192. DownloadPath: downloadPath,
  1193. Kind: req.Kind,
  1194. ReqContent: string(marshal),
  1195. CreateAt: now.Format("2006-01-02 15:04:05"),
  1196. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1197. })
  1198. }