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

1615 lines
69 KiB

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