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

1544 lines
66 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. for k2, v2 := range v1 {
  665. if k2 == "" {
  666. //若年级为空,则为教师餐
  667. k2 = "教师餐"
  668. }
  669. for k3, v3 := range v2 {
  670. if k3 == "" {
  671. //若班级为空,则为教师餐
  672. k3 = "教师餐"
  673. }
  674. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  675. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  676. j++
  677. }
  678. }
  679. //将文件保存至服务器
  680. err1 := xlsx.SaveAs(tmpDownloadPath)
  681. if err1 != nil {
  682. logx.Error(err1)
  683. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  684. panic(err1)
  685. }
  686. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  687. xlsFileName = append(xlsFileName, tmpFileName)
  688. }
  689. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  690. if err != nil {
  691. logx.Error(err)
  692. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  693. panic(err)
  694. }
  695. defer archive.Close()
  696. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  697. for k, v := range xlsDownloadPath {
  698. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  699. if err1 != nil {
  700. logx.Error(err1)
  701. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  702. panic(err1)
  703. }
  704. f, err1 := os.Open(v) //打开待压缩的文件
  705. if err1 != nil {
  706. logx.Error(err1)
  707. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  708. panic(err1)
  709. }
  710. if _, err1 = io.Copy(w, f); err != nil {
  711. logx.Error(err1)
  712. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  713. panic(err1)
  714. }
  715. if err1 != nil {
  716. logx.Error(err1)
  717. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  718. panic(err1)
  719. }
  720. f.Close()
  721. os.RemoveAll(v) //TODO::移除源文件
  722. }
  723. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  724. if err != nil {
  725. panic(err)
  726. }
  727. break
  728. case 4:
  729. //班级明细表
  730. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  731. downloadPath = "/export/central_kitchen_for_school/" + fileName
  732. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  733. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  734. if req.EnterpriseId != 0 {
  735. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  736. }
  737. if req.StartDate != "" {
  738. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  739. }
  740. if req.EndDate != "" {
  741. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  742. }
  743. if len(req.MealKindList) > 0 {
  744. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  745. }
  746. _, err := sess.
  747. 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").
  748. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  749. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  750. Join("LEFT", "user", "user.id = user_identity.uid").
  751. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  752. Join("LEFT", "class", "class_with_user.class_id = class.id").
  753. Join("LEFT", "grade", "class.grade_id = grade.id").
  754. FindAndCount(&m)
  755. if err != nil {
  756. logx.Error(err)
  757. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  758. return
  759. }
  760. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  761. var xlsFileName, xlsDownloadPath []string
  762. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  763. centralKitchenForSchoolUserWithDayDb.Set(0)
  764. for _, v := range m {
  765. if list[v.Enterprise.Name] == nil {
  766. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  767. }
  768. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  769. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  770. }
  771. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  772. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  773. }
  774. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  775. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  776. }
  777. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  778. if err2 != nil {
  779. logx.Error(err)
  780. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  781. return
  782. }
  783. time.Sleep(50 * time.Millisecond) // 50毫秒
  784. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  785. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  786. }
  787. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  788. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  789. }
  790. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  791. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  792. }
  793. }
  794. for k1, v1 := range list {
  795. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  796. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  797. for k2, v2 := range v1 {
  798. if k2 == "" {
  799. //若年级为空,则为教师餐
  800. continue
  801. }
  802. for k3, v3 := range v2 {
  803. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  804. xlsx.NewSheet(k3)
  805. xlsx.SetSheetRow(k3, "A1", &titleList)
  806. j := 2 //表头被第一行用了,只能从第二行开始
  807. for k4, v4 := range v3 {
  808. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  809. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  810. j++
  811. }
  812. }
  813. }
  814. //将文件保存至服务器
  815. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  816. err1 := xlsx.SaveAs(tmpDownloadPath)
  817. if err1 != nil {
  818. logx.Error(err1)
  819. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  820. panic(err1)
  821. }
  822. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  823. xlsFileName = append(xlsFileName, tmpFileName)
  824. }
  825. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  826. if err != nil {
  827. logx.Error(err)
  828. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  829. panic(err)
  830. }
  831. defer archive.Close()
  832. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  833. for k, v := range xlsDownloadPath {
  834. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  835. if err1 != nil {
  836. logx.Error(err1)
  837. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  838. panic(err1)
  839. }
  840. f, err1 := os.Open(v) //打开待压缩的文件
  841. if err1 != nil {
  842. logx.Error(err1)
  843. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  844. panic(err1)
  845. }
  846. if _, err1 = io.Copy(w, f); err != nil {
  847. logx.Error(err1)
  848. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  849. panic(err1)
  850. }
  851. if err1 != nil {
  852. logx.Error(err1)
  853. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  854. panic(err1)
  855. }
  856. f.Close()
  857. os.RemoveAll(v) //TODO::移除源文件
  858. }
  859. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  860. if err != nil {
  861. panic(err)
  862. }
  863. break
  864. case 5:
  865. //退款明细表
  866. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  867. downloadPath = "/export/central_kitchen_for_school/" + fileName
  868. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  869. if req.EnterpriseId != 0 {
  870. sess.And("enterprise.id =?", req.EnterpriseId)
  871. }
  872. if req.StartDate != "" {
  873. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  874. }
  875. if req.EndDate != "" {
  876. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  877. }
  878. if len(req.MealKindList) > 0 {
  879. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  880. }
  881. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  882. _, err := sess.
  883. 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").
  884. 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").
  885. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  886. Join("LEFT", "user", "user_identity.uid = user.id").
  887. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  888. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  889. Join("LEFT", "class", "class_with_user.class_id = class.id").
  890. Join("LEFT", "grade", "class.grade_id = grade.id").
  891. Desc("central_kitchen_for_school_user_refund_day.id").
  892. FindAndCount(&m)
  893. if err != nil {
  894. logx.Error(err)
  895. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  896. return
  897. }
  898. var list = map[string]map[string]map[string]map[string]map[string]int{}
  899. var xlsFileName, xlsDownloadPath []string
  900. for _, v := range m {
  901. if list[v.Enterprise.Name] == nil {
  902. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  903. }
  904. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  905. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  906. }
  907. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  908. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  909. }
  910. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  911. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  912. }
  913. //就餐类型(1:早餐 2:午餐 3:晚餐)
  914. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  915. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  916. }
  917. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  918. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  919. }
  920. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  921. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  922. }
  923. }
  924. for k1, v1 := range list {
  925. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  926. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  927. for k2, v2 := range v1 {
  928. if k2 == "" {
  929. //若年级为空,则为教师餐
  930. continue
  931. }
  932. for k3, v3 := range v2 {
  933. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  934. xlsx.NewSheet(k3)
  935. xlsx.SetSheetRow(k3, "A1", &titleList)
  936. j := 2 //表头被第一行用了,只能从第二行开始
  937. for k4, v4 := range v3 {
  938. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  939. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  940. j++
  941. }
  942. }
  943. }
  944. //将文件保存至服务器
  945. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  946. err1 := xlsx.SaveAs(tmpDownloadPath)
  947. if err1 != nil {
  948. logx.Error(err1)
  949. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  950. panic(err1)
  951. }
  952. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  953. xlsFileName = append(xlsFileName, tmpFileName)
  954. }
  955. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  956. if err != nil {
  957. logx.Error(err)
  958. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  959. panic(err)
  960. }
  961. defer archive.Close()
  962. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  963. for k, v := range xlsDownloadPath {
  964. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  965. if err1 != nil {
  966. logx.Error(err1)
  967. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  968. panic(err1)
  969. }
  970. f, err1 := os.Open(v) //打开待压缩的文件
  971. if err1 != nil {
  972. logx.Error(err1)
  973. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  974. panic(err1)
  975. }
  976. if _, err1 = io.Copy(w, f); err != nil {
  977. logx.Error(err1)
  978. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  979. panic(err1)
  980. }
  981. if err1 != nil {
  982. logx.Error(err1)
  983. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  984. panic(err1)
  985. }
  986. f.Close()
  987. os.RemoveAll(v) //TODO::移除源文件
  988. }
  989. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  990. if err != nil {
  991. panic(err)
  992. }
  993. break
  994. case 6:
  995. //收款统计表
  996. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  997. downloadPath = "/export/central_kitchen_for_school/" + fileName
  998. //查找数据
  999. var m []model.Enterprise
  1000. sess := db.Db.Desc("id")
  1001. if req.EnterpriseId != 0 {
  1002. sess.And("id = ?", req.EnterpriseId)
  1003. }
  1004. err := sess.Find(&m)
  1005. if err != nil {
  1006. logx.Error(err)
  1007. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1008. return
  1009. }
  1010. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1011. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1012. j := 2 //表头被第一行用了,只能从第二行开始
  1013. for _, v := range m {
  1014. var income, refund string
  1015. //统计收入
  1016. 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)
  1017. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1018. if err != nil {
  1019. logx.Error(err)
  1020. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1021. return
  1022. }
  1023. if mapArr1 != nil {
  1024. income = mapArr1[0]["total"]
  1025. }
  1026. //统计退款
  1027. //查询出当前学校下所有的学生
  1028. userIdentityDb := db.UserIdentityDb{}
  1029. userIdentityDb.Set(0)
  1030. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1031. var userIdentityIds []string
  1032. for _, v := range *userIdentities {
  1033. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1034. }
  1035. if len(userIdentityIds) != 0 {
  1036. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1037. 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'",
  1038. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1039. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1040. if err != nil {
  1041. logx.Error(err)
  1042. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1043. return
  1044. }
  1045. if mapArr2 != nil {
  1046. refund = mapArr2[0]["total"]
  1047. }
  1048. }
  1049. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1050. rateFee := realityAmount * 0.0038
  1051. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1052. j++
  1053. }
  1054. //将文件保存至服务器
  1055. xlsx.SaveAs("./static" + downloadPath)
  1056. break
  1057. case 7:
  1058. //班级收款明细表
  1059. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1060. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1061. //查找订单数据
  1062. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1063. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  1064. if req.EnterpriseId != 0 {
  1065. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1066. }
  1067. if req.StartDate != "" {
  1068. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1069. }
  1070. if req.EndDate != "" {
  1071. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1072. }
  1073. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1074. _, err := sess.
  1075. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1076. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1077. Join("LEFT", "user", "user.id = user_identity.uid").
  1078. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1079. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1080. Join("LEFT", "grade", "class.grade_id = grade.id").
  1081. FindAndCount(&m)
  1082. if err != nil {
  1083. logx.Error(err)
  1084. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1085. return
  1086. }
  1087. sess1 := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1088. if req.EnterpriseId != 0 {
  1089. sess1.And("enterprise.id =?", req.EnterpriseId)
  1090. }
  1091. if req.StartDate != "" {
  1092. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1093. }
  1094. if req.EndDate != "" {
  1095. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1096. }
  1097. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1098. _, err = sess1.
  1099. 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").
  1100. 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").
  1101. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1102. Join("LEFT", "user", "user_identity.uid = user.id").
  1103. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1104. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1105. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1106. Join("LEFT", "grade", "class.grade_id = grade.id").
  1107. Desc("central_kitchen_for_school_user_refund_day.id").
  1108. FindAndCount(&mm)
  1109. if err != nil {
  1110. logx.Error(err)
  1111. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1112. return
  1113. }
  1114. var list = map[string]map[string]map[string]map[string]float64{}
  1115. for _, v := range m {
  1116. if list[v.Enterprise.Name] == nil {
  1117. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1118. }
  1119. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1120. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1121. }
  1122. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1123. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1124. }
  1125. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1126. }
  1127. for _, v := range mm {
  1128. if list[v.Enterprise.Name] == nil {
  1129. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1130. }
  1131. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1132. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1133. }
  1134. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1135. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1136. }
  1137. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1138. }
  1139. var xlsFileName, xlsDownloadPath []string
  1140. for k1, v1 := range list {
  1141. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1142. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1143. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1144. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1145. j := 2 //表头被第一行用了,只能从第二行开始
  1146. for k2, v2 := range v1 {
  1147. if k2 == "" {
  1148. //若年级为空,则为教师餐
  1149. continue
  1150. }
  1151. for k3, v3 := range v2 {
  1152. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1153. j++
  1154. }
  1155. }
  1156. //将文件保存至服务器
  1157. err1 := xlsx.SaveAs(tmpDownloadPath)
  1158. if err1 != nil {
  1159. logx.Error(err1)
  1160. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1161. panic(err1)
  1162. }
  1163. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1164. xlsFileName = append(xlsFileName, tmpFileName)
  1165. }
  1166. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1167. if err != nil {
  1168. logx.Error(err)
  1169. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1170. panic(err)
  1171. }
  1172. defer archive.Close()
  1173. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1174. for k, v := range xlsDownloadPath {
  1175. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1176. if err1 != nil {
  1177. logx.Error(err1)
  1178. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1179. panic(err1)
  1180. }
  1181. f, err1 := os.Open(v) //打开待压缩的文件
  1182. if err1 != nil {
  1183. logx.Error(err1)
  1184. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1185. panic(err1)
  1186. }
  1187. if _, err1 = io.Copy(w, f); err != nil {
  1188. logx.Error(err1)
  1189. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1190. panic(err1)
  1191. }
  1192. if err1 != nil {
  1193. logx.Error(err1)
  1194. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1195. panic(err1)
  1196. }
  1197. f.Close()
  1198. os.RemoveAll(v) //TODO::移除源文件
  1199. }
  1200. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1201. if err != nil {
  1202. panic(err)
  1203. }
  1204. break
  1205. case 8:
  1206. //学校预定数量统计表
  1207. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1208. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1209. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1210. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1211. if req.EnterpriseId != 0 {
  1212. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1213. }
  1214. if req.StartDate != "" {
  1215. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  1216. }
  1217. if req.EndDate != "" {
  1218. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1219. }
  1220. if len(req.MealKindList) > 0 {
  1221. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1222. }
  1223. _, err := sess.
  1224. 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").
  1225. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1226. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1227. FindAndCount(&m)
  1228. if err != nil {
  1229. logx.Error(err)
  1230. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1231. return
  1232. }
  1233. var list = map[string]int64{}
  1234. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1235. centralKitchenForSchoolUserWithDayDb.Set(0)
  1236. for _, v := range m {
  1237. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1238. if err2 != nil {
  1239. logx.Error(err)
  1240. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1241. return
  1242. }
  1243. time.Sleep(50 * time.Millisecond) // 50毫秒
  1244. list[v.Enterprise.Name] += count
  1245. }
  1246. titleList = []string{"学校名称", "预定总数"}
  1247. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1248. j := 2 //表头被第一行用了,只能从第二行开始
  1249. for k, v := range list {
  1250. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v})
  1251. j++
  1252. }
  1253. //将文件保存至服务器
  1254. xlsx.SaveAs("./static" + downloadPath)
  1255. break
  1256. }
  1257. //新增数据
  1258. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1259. centralKitchenForSchoolExportRecordsDb.Set()
  1260. marshal, _ := json.Marshal(req)
  1261. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1262. Name: fileName,
  1263. DownloadPath: downloadPath,
  1264. Kind: kind,
  1265. ReqContent: string(marshal),
  1266. CreateAt: now.Format("2006-01-02 15:04:05"),
  1267. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1268. })
  1269. }
  1270. }
  1271. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1272. var titleList []string
  1273. var fileName, downloadPath string
  1274. var now = time.Now()
  1275. xlsx := excelize.NewFile()
  1276. //数据对比
  1277. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1278. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1279. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1280. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1281. if req.EnterpriseId != 0 {
  1282. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1283. }
  1284. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date1[0])
  1285. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date1[1])
  1286. if len(req.MealKindList) > 0 {
  1287. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1288. }
  1289. _, err := sess.
  1290. 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").
  1291. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1292. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1293. Join("LEFT", "user", "user.id = user_identity.uid").
  1294. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1295. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1296. Join("LEFT", "grade", "class.grade_id = grade.id").
  1297. FindAndCount(&m)
  1298. if err != nil {
  1299. logx.Error(err)
  1300. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1301. return
  1302. }
  1303. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1304. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1305. if req.EnterpriseId != 0 {
  1306. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1307. }
  1308. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", req.Date2[0])
  1309. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.Date2[1])
  1310. if len(req.MealKindList) > 0 {
  1311. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1312. }
  1313. _, err1 := sess1.
  1314. 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").
  1315. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1316. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1317. Join("LEFT", "user", "user.id = user_identity.uid").
  1318. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1319. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1320. Join("LEFT", "grade", "class.grade_id = grade.id").
  1321. FindAndCount(&m)
  1322. if err1 != nil {
  1323. logx.Error(err1)
  1324. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1325. return
  1326. }
  1327. var list = map[string]map[string]map[string]map[string]string{}
  1328. for _, v := range m {
  1329. if list[v.Enterprise.Name] == nil {
  1330. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1331. }
  1332. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1333. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1334. }
  1335. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1336. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1337. }
  1338. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1339. }
  1340. var list1 = map[string]map[string]map[string]map[string]string{}
  1341. for _, v := range m1 {
  1342. if list1[v.Enterprise.Name] == nil {
  1343. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1344. }
  1345. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1346. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1347. }
  1348. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1349. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1350. }
  1351. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1352. }
  1353. var xlsFileName, xlsDownloadPath []string
  1354. for k1, v1 := range list {
  1355. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1356. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1357. for k2, v2 := range v1 {
  1358. if k2 == "" {
  1359. //若年级为空,则为教师
  1360. continue
  1361. }
  1362. for k3, v3 := range v2 {
  1363. titleList = []string{"姓名", "家长联系电话"}
  1364. xlsx.NewSheet(k3)
  1365. xlsx.SetSheetRow(k3, "A1", &titleList)
  1366. j := 2 //表头被第一行用了,只能从第二行开始
  1367. for k4, v4 := range v3 {
  1368. //TODO::判断当前用户是否在新的月份里面
  1369. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1370. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1371. k4, v4})
  1372. j++
  1373. }
  1374. }
  1375. }
  1376. }
  1377. //将文件保存至服务器
  1378. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1379. err1 := xlsx.SaveAs(tmpDownloadPath)
  1380. if err1 != nil {
  1381. logx.Error(err1)
  1382. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1383. panic(err1)
  1384. }
  1385. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1386. xlsFileName = append(xlsFileName, tmpFileName)
  1387. }
  1388. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1389. if err != nil {
  1390. logx.Error(err)
  1391. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1392. panic(err)
  1393. }
  1394. defer archive.Close()
  1395. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1396. for k, v := range xlsDownloadPath {
  1397. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1398. if err1 != nil {
  1399. logx.Error(err1)
  1400. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1401. panic(err1)
  1402. }
  1403. f, err1 := os.Open(v) //打开待压缩的文件
  1404. if err1 != nil {
  1405. logx.Error(err1)
  1406. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1407. panic(err1)
  1408. }
  1409. if _, err1 = io.Copy(w, f); err != nil {
  1410. logx.Error(err1)
  1411. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1412. panic(err1)
  1413. }
  1414. if err1 != nil {
  1415. logx.Error(err1)
  1416. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1417. panic(err1)
  1418. }
  1419. f.Close()
  1420. os.RemoveAll(v) //TODO::移除源文件
  1421. }
  1422. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1423. if err != nil {
  1424. panic(err)
  1425. }
  1426. //新增数据
  1427. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1428. centralKitchenForSchoolExportRecordsDb.Set()
  1429. marshal, _ := json.Marshal(req)
  1430. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1431. Name: fileName,
  1432. DownloadPath: downloadPath,
  1433. Kind: 9, //数据对比
  1434. ReqContent: string(marshal),
  1435. CreateAt: now.Format("2006-01-02 15:04:05"),
  1436. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1437. })
  1438. }
  1439. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1440. mealZh = "未知"
  1441. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1442. for _, v := range md.SelfSupportForMealTimeMapping {
  1443. //判断时间大小
  1444. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  1445. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  1446. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  1447. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  1448. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1449. mealZh = v["name"]
  1450. }
  1451. }
  1452. return
  1453. }