智慧食堂
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

2343 行
98 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. if req.EnterPriseKind != 0 {
  234. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  235. }
  236. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  237. _, err := sess.
  238. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  239. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  240. Join("LEFT", "user", "user.id = user_identity.uid").
  241. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  242. Join("LEFT", "class", "class_with_user.class_id = class.id").
  243. Join("LEFT", "grade", "class.grade_id = grade.id").
  244. FindAndCount(&m)
  245. if err != nil {
  246. logx.Error(err)
  247. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  248. return
  249. }
  250. //整合数据
  251. var data = map[string]map[string]map[string]map[string]interface{}{}
  252. for _, vv := range m {
  253. if data[vv.Enterprise.Name] == nil {
  254. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  255. }
  256. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  257. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  258. }
  259. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  260. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  261. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  262. }
  263. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  264. "id": vv.UserIdentity.IdNo,
  265. }
  266. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  267. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  268. }
  269. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  270. 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)
  271. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  272. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  273. }
  274. xlsx.SetSheetRow("", "A1", &titleList)
  275. sheetIndex := 2 //第一个表是模板表
  276. j := 3 //表头前两行被占用了,只能从第三行开始
  277. for k, v := range data {
  278. xlsx.NewSheet(k) //1、新建表格
  279. xlsx.CopySheet(1, sheetIndex)
  280. for kk, vv := range v {
  281. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  282. vv["早餐"]["num"], vv["早餐"]["amount"],
  283. vv["午餐"]["num"], vv["午餐"]["amount"],
  284. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  285. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  286. vv["未知"]["num"], vv["未知"]["amount"],
  287. vv["合计"]["num"], vv["合计"]["amount"],
  288. })
  289. j++
  290. }
  291. sheetIndex++
  292. }
  293. xlsx.DeleteSheet("template") //删除模板表
  294. //将文件保存至服务器
  295. xlsx.SaveAs("./static" + downloadPath)
  296. break
  297. case 2:
  298. //商家分餐数据统计表
  299. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  300. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  301. downloadPath = "/export/self_support_for_school/" + fileName
  302. //查询出所有的设备
  303. var deviceList []*model.Device
  304. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  305. if err != nil {
  306. logx.Error(err)
  307. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  308. return
  309. }
  310. var deviceListMapping = map[string]model.Device{}
  311. for _, v := range deviceList {
  312. deviceListMapping[v.DeviceSn] = *v
  313. }
  314. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  315. sess := db.Db.Desc("self_support_for_school_ord.id")
  316. if req.EnterpriseId != 0 {
  317. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  318. }
  319. if req.StartDate != "" {
  320. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  321. }
  322. if req.EndDate != "" {
  323. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  324. }
  325. if req.EnterPriseKind != 0 {
  326. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  327. }
  328. _, err = sess.
  329. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  330. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  331. Join("LEFT", "user", "user.id = user_identity.uid").
  332. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  333. Join("LEFT", "class", "class_with_user.class_id = class.id").
  334. Join("LEFT", "grade", "class.grade_id = grade.id").
  335. FindAndCount(&m)
  336. if err != nil {
  337. logx.Error(err)
  338. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  339. return
  340. }
  341. //整合数据
  342. var data = map[string]map[string]map[string]map[string]float64{}
  343. for _, vv := range m {
  344. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  345. if data[vv.Enterprise.Name] == nil {
  346. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  347. }
  348. if data[vv.Enterprise.Name][shopName] == nil {
  349. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  350. }
  351. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  352. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  353. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  354. }
  355. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  356. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  357. }
  358. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  359. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  360. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  361. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  362. }
  363. xlsx.SetSheetRow("", "A1", &titleList)
  364. sheetIndex := 2 //第一个表是模板表
  365. for k, v := range data {
  366. if k == "" {
  367. k = "未知"
  368. }
  369. xlsx.NewSheet(k) //1、新建表格
  370. xlsx.CopySheet(1, sheetIndex)
  371. j := 4 //表头前三行被占用了,只能从第四行开始
  372. for kk, vv := range v {
  373. if kk == "" {
  374. kk = k
  375. }
  376. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  377. vv["早餐"]["num"], vv["早餐"]["amount"],
  378. vv["午餐"]["num"], vv["午餐"]["amount"],
  379. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  380. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  381. vv["合计"]["num"], vv["合计"]["amount"],
  382. })
  383. j++
  384. }
  385. sheetIndex++
  386. }
  387. xlsx.DeleteSheet("template") //删除模板表
  388. //将文件保存至服务器
  389. xlsx.SaveAs("./static" + downloadPath)
  390. break
  391. case 3:
  392. //就餐数据统计表
  393. xlsx, _ := excelize.OpenFile("./static/template/自营学校(就餐数据统计表).xlsx")
  394. fileName = "就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  395. downloadPath = "/export/self_support_for_school/" + fileName
  396. //查询出所有的设备
  397. //var deviceList []*model.Device
  398. //err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  399. //if err != nil {
  400. // logx.Error(err)
  401. // println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  402. // return
  403. //}
  404. //var deviceListMapping = map[string]model.Device{}
  405. //for _, v := range deviceList {
  406. // deviceListMapping[v.DeviceSn] = *v
  407. //}
  408. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  409. sess := db.Db.Desc("self_support_for_school_ord.id")
  410. if req.EnterpriseId != 0 {
  411. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  412. }
  413. if req.StartDate != "" {
  414. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  415. }
  416. if req.EndDate != "" {
  417. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  418. }
  419. if req.EnterPriseKind != 0 {
  420. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  421. }
  422. _, err := sess.
  423. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  424. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  425. Join("LEFT", "user", "user.id = user_identity.uid").
  426. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  427. Join("LEFT", "class", "class_with_user.class_id = class.id").
  428. Join("LEFT", "grade", "class.grade_id = grade.id").
  429. FindAndCount(&m)
  430. if err != nil {
  431. logx.Error(err)
  432. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  433. return
  434. }
  435. //整合数据
  436. var data = map[string]map[string]map[string]map[string]float64{}
  437. date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  438. if err != nil {
  439. logx.Error(err)
  440. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  441. return
  442. }
  443. date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  444. if err != nil {
  445. logx.Error(err)
  446. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  447. return
  448. }
  449. diffDays := int(date2.Sub(date1).Hours() / 24)
  450. var dates []string
  451. for i := 0; i <= diffDays; i++ {
  452. date := date1.AddDate(0, 0, i)
  453. shopName := date.Format("2006-01-02")
  454. dates = append(dates, shopName)
  455. for _, vv := range m {
  456. tmpDate, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
  457. if err1 != nil {
  458. logx.Error(err1)
  459. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  460. return
  461. }
  462. fmt.Println(tmpDate.Format("2006-01-02"))
  463. if shopName == tmpDate.Format("2006-01-02") {
  464. if data[vv.Enterprise.Name] == nil {
  465. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  466. }
  467. if data[vv.Enterprise.Name][shopName] == nil {
  468. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  469. }
  470. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  471. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  472. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  473. }
  474. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  475. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  476. }
  477. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  478. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  479. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  480. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  481. }
  482. }
  483. }
  484. xlsx.SetSheetRow("", "A1", &titleList)
  485. sheetIndex := 2 //第一个表是模板表
  486. for k, v := range data {
  487. if k == "" {
  488. k = "未知"
  489. }
  490. xlsx.NewSheet(k) //1、新建表格
  491. xlsx.CopySheet(1, sheetIndex)
  492. j := 4 //表头前三行被占用了,只能从第四行开始
  493. for _, date := range dates {
  494. for kk, vv := range v {
  495. if date == kk {
  496. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  497. vv["早餐"]["num"], vv["早餐"]["amount"],
  498. vv["午餐"]["num"], vv["午餐"]["amount"],
  499. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  500. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  501. vv["合计"]["num"], vv["合计"]["amount"],
  502. })
  503. j++
  504. }
  505. sheetIndex++
  506. }
  507. }
  508. }
  509. xlsx.DeleteSheet("template") //删除模板表
  510. //将文件保存至服务器
  511. xlsx.SaveAs("./static" + downloadPath)
  512. break
  513. case 4:
  514. //设备就餐数据统计表
  515. fileName = "设备就餐数据统计_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  516. downloadPath = "/export/self_support_for_school/" + fileName
  517. //查询出所有的设备
  518. var deviceList []*model.Device
  519. if req.EnterpriseId != 0 {
  520. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  521. if err != nil {
  522. logx.Error(err)
  523. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>1111Error:::", err.Error())
  524. return
  525. }
  526. } else {
  527. err := db.Db.Where("enterprise_id = ?", req.EnterpriseId).Find(&deviceList)
  528. if err != nil {
  529. logx.Error(err)
  530. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>1111Error:::", err.Error())
  531. return
  532. }
  533. }
  534. date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  535. if err != nil {
  536. logx.Error(err)
  537. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  538. return
  539. }
  540. date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  541. if err != nil {
  542. logx.Error(err)
  543. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  544. return
  545. }
  546. diffDays := int(date2.Sub(date1).Hours() / 24)
  547. var deviceListMapping = map[string]model.Device{}
  548. //整合数据
  549. var data = map[string]map[string]map[string]map[string]float64{}
  550. var xlsFileName, xlsDownloadPath []string
  551. for _, v := range deviceList {
  552. deviceListMapping[v.DeviceSn] = *v
  553. var m []*db.SelfSupportForSchoolOrdWithUserIdentityV2
  554. sess := db.Db.Where("device_sn = ?", v.DeviceSn).Desc("self_support_for_school_ord.id")
  555. if req.EnterpriseId != 0 {
  556. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  557. }
  558. if req.StartDate != "" {
  559. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  560. }
  561. if req.EndDate != "" {
  562. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  563. }
  564. _, err = sess.Join("LEFT", "enterprise", "self_support_for_school_ord.enterprise_id = enterprise.id").FindAndCount(&m)
  565. if err != nil {
  566. logx.Error(err)
  567. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  568. return
  569. }
  570. for _, vv := range m {
  571. tmpTime, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
  572. if err1 != nil {
  573. logx.Error(err1)
  574. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  575. return
  576. }
  577. tmpDate := tmpTime.Format("2006-01-02")
  578. if data[vv.Enterprise.Name] == nil {
  579. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  580. }
  581. if data[vv.Enterprise.Name][v.Name] == nil {
  582. data[vv.Enterprise.Name][v.Name] = make(map[string]map[string]float64)
  583. }
  584. if data[vv.Enterprise.Name][v.Name][tmpDate] == nil {
  585. data[vv.Enterprise.Name][v.Name][tmpDate] = make(map[string]float64)
  586. }
  587. data[vv.Enterprise.Name][v.Name][tmpDate]["num"]++
  588. data[vv.Enterprise.Name][v.Name][tmpDate]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  589. }
  590. }
  591. for k1, v := range data {
  592. tmpFileName := k1 + "设备就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  593. tmpDownloadPath := "./static/export/self_support_for_school/" + tmpFileName
  594. xlsx := excelize.NewFile()
  595. var totalData = map[string]map[string]float64{}
  596. for k2, vv := range v {
  597. var sumNum, sumAmount = float64(0), float64(0)
  598. xlsx.NewSheet(k2)
  599. titleList = []string{" 设 备 分 餐 数 据 统 计 表 "}
  600. xlsx.SetSheetRow(k2, "A1", &titleList)
  601. titleList = []string{"日 期", "消费笔数", "消费金额"}
  602. xlsx.SetSheetRow(k2, "A2", &titleList)
  603. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  604. if err != nil {
  605. logx.Error(err)
  606. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  607. panic(err)
  608. }
  609. xlsx.MergeCell(k2, "A1", "C1") //合并单元格
  610. xlsx.SetCellStyle(k2, "A1", "C1", style)
  611. xlsx.SetColWidth(k2, "A", "C", 20)
  612. j := 3 //表头前两行用了,只能从第三行开始
  613. for i := 0; i <= diffDays; i++ {
  614. date := date1.AddDate(0, 0, i).Format("2006-01-02")
  615. if vv[date] != nil {
  616. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, vv[date]["num"], vv[date]["amount"]})
  617. sumNum += vv[date]["num"]
  618. sumAmount += vv[date]["amount"]
  619. } else {
  620. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, 0, 0})
  621. }
  622. j++
  623. }
  624. xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{
  625. "合计", sumNum, sumAmount})
  626. if totalData[k2] == nil {
  627. totalData[k2] = map[string]float64{}
  628. }
  629. totalData[k2]["num"] = sumNum
  630. totalData[k2]["amount"] = sumAmount
  631. }
  632. xlsx.NewSheet("Sheet1")
  633. titleList = []string{" 设 备 分 餐 数 据 统 计 表 "}
  634. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  635. titleList = []string{"名 称", "消费笔数", "消费金额"}
  636. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  637. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  638. if err != nil {
  639. logx.Error(err)
  640. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  641. panic(err)
  642. }
  643. xlsx.MergeCell("Sheet1", "A1", "C1") //合并单元格
  644. xlsx.SetCellStyle("Sheet1", "A1", "C1", style)
  645. xlsx.SetColWidth("Sheet1", "A", "C", 20)
  646. j := 3 //表头前两行用了,只能从第三行开始
  647. for key, val := range totalData {
  648. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{key, val["num"], val["amount"]})
  649. j++
  650. }
  651. //xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  652. //将文件保存至服务器
  653. err1 := xlsx.SaveAs(tmpDownloadPath)
  654. if err1 != nil {
  655. logx.Error(err1)
  656. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  657. panic(err1)
  658. }
  659. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  660. xlsFileName = append(xlsFileName, tmpFileName)
  661. }
  662. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  663. if err != nil {
  664. logx.Error(err)
  665. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  666. panic(err)
  667. }
  668. defer archive.Close()
  669. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  670. for k, v := range xlsDownloadPath {
  671. w, err1 := zipWriter.Create("设备就餐数据统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  672. if err1 != nil {
  673. logx.Error(err1)
  674. println("<<<<SelfSupportForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  675. panic(err1)
  676. }
  677. f, err1 := os.Open(v) //打开待压缩的文件
  678. if err1 != nil {
  679. logx.Error(err1)
  680. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  681. panic(err1)
  682. }
  683. if _, err1 = io.Copy(w, f); err != nil {
  684. logx.Error(err1)
  685. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  686. panic(err1)
  687. }
  688. if err1 != nil {
  689. logx.Error(err1)
  690. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  691. panic(err1)
  692. }
  693. f.Close()
  694. os.RemoveAll(v) //TODO::移除源文件
  695. }
  696. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  697. if err != nil {
  698. panic(err)
  699. }
  700. break
  701. }
  702. //新增数据
  703. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  704. selfSupportForSchoolExportRecordsDb.Set()
  705. marshal, _ := json.Marshal(req)
  706. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  707. Name: fileName,
  708. DownloadPath: downloadPath,
  709. Kind: req.Kind,
  710. ReqContent: string(marshal),
  711. CreateAt: now.Format("2006-01-02 15:04:05"),
  712. UpdateAt: now.Format("2006-01-02 15:04:05"),
  713. })
  714. }
  715. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  716. var titleList []string
  717. var fileName, downloadPath string
  718. var now = time.Now()
  719. for _, kind := range req.Kinds {
  720. xlsx := excelize.NewFile()
  721. switch kind {
  722. case 1:
  723. //订单列表
  724. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  725. downloadPath = "/export/central_kitchen_for_school/" + fileName
  726. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  727. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  728. //查找订单数据
  729. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  730. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  731. if req.EnterpriseId != 0 {
  732. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  733. }
  734. if req.StartDate != "" {
  735. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  736. }
  737. if req.EndDate != "" {
  738. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  739. }
  740. if req.EnterPriseKind != 0 {
  741. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  742. }
  743. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  744. _, err := sess.
  745. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  746. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  747. Join("LEFT", "user", "user.id = user_identity.uid").
  748. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  749. Join("LEFT", "class", "class_with_user.class_id = class.id").
  750. Join("LEFT", "grade", "class.grade_id = grade.id").
  751. FindAndCount(&m)
  752. if err != nil {
  753. logx.Error(err)
  754. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  755. return
  756. }
  757. //查找预定数据
  758. var outTradeNoList []string
  759. for _, v := range m {
  760. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  761. }
  762. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  763. sess1 := db.Db.Desc("id")
  764. if req.EnterpriseId != 0 {
  765. sess1.And("enterprise_id = ?", req.EnterpriseId)
  766. }
  767. if len(req.MealKindList) > 0 {
  768. sess1.In("kind", req.MealKindList)
  769. }
  770. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  771. if err != nil {
  772. logx.Error(err)
  773. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  774. return
  775. }
  776. var exportOutTradeNoList []string
  777. for _, v := range mm {
  778. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  779. }
  780. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  781. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  782. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定餐数", "订单价格", "支付时间", "订单状态"}
  783. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  784. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  785. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  786. if err != nil {
  787. logx.Error(err)
  788. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  789. panic(err)
  790. }
  791. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  792. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  793. j := 3 //表头前两行用了,只能从第三行开始
  794. for _, v := range m {
  795. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  796. continue
  797. }
  798. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  799. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  800. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  801. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  802. break
  803. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  804. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  805. break
  806. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  807. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  808. break
  809. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  810. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  811. break
  812. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  813. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  814. break
  815. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  816. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  817. break
  818. }
  819. //预定餐数、预定日期
  820. var list []*model.CentralKitchenForSchoolUserWithDay
  821. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  822. if err1 != nil {
  823. logx.Error(err)
  824. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  825. return
  826. }
  827. var reserveDays string
  828. for _, v1 := range list {
  829. reserveDays += v1.Date + ", "
  830. }
  831. if v.Grade.Name == "" {
  832. v.Grade.Name = "教师"
  833. }
  834. if v.Class.Name == "" {
  835. v.Class.Name = "教师"
  836. }
  837. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  838. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  839. j++
  840. }
  841. //将文件保存至服务器
  842. xlsx.SaveAs("./static" + downloadPath)
  843. break
  844. case 2:
  845. //退款申请表
  846. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  847. downloadPath = "/export/central_kitchen_for_school/" + fileName
  848. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  849. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  850. //查找退款申请数据
  851. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  852. if req.EnterpriseId != 0 {
  853. sess.And("enterprise.id =?", req.EnterpriseId)
  854. }
  855. if req.StartDate != "" {
  856. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  857. }
  858. if req.EndDate != "" {
  859. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  860. }
  861. if len(req.MealKindList) > 0 {
  862. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  863. }
  864. if req.EnterPriseKind != 0 {
  865. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  866. }
  867. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  868. _, err := sess.
  869. 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").
  870. 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").
  871. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  872. Join("LEFT", "user", "user_identity.uid = user.id").
  873. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  874. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  875. Join("LEFT", "class", "class_with_user.class_id = class.id").
  876. Join("LEFT", "grade", "class.grade_id = grade.id").
  877. Desc("central_kitchen_for_school_user_refund_day.id").
  878. FindAndCount(&m)
  879. if err != nil {
  880. logx.Error(err)
  881. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  882. return
  883. }
  884. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  885. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  886. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  887. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  888. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  889. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  890. if err != nil {
  891. logx.Error(err)
  892. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  893. panic(err)
  894. }
  895. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  896. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  897. j := 3 //表头前两行用了,只能从第三行开始
  898. for _, v := range m {
  899. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  900. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  901. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  902. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  903. break
  904. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  905. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  906. break
  907. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  908. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  909. break
  910. }
  911. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  912. switch v.CentralKitchenForSchoolUserRefundDay.State {
  913. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  914. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  915. break
  916. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  917. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  918. break
  919. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  920. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  921. break
  922. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  923. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  924. break
  925. }
  926. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  927. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  928. j++
  929. }
  930. //将文件保存至服务器
  931. xlsx.SaveAs("./static" + downloadPath)
  932. break
  933. case 3:
  934. //学校预定统计表
  935. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  936. downloadPath = "/export/central_kitchen_for_school/" + fileName
  937. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  938. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  939. if req.EnterpriseId != 0 {
  940. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  941. }
  942. var months []time.Month
  943. if req.StartDate != "" {
  944. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  945. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  946. _, sMonth, _ := sDate.Date()
  947. _, eMonth, _ := eDate.Date()
  948. diffMonth := sMonth - eMonth
  949. if diffMonth == 0 {
  950. //TODO::查询一个月
  951. months = append(months, sMonth)
  952. } else {
  953. for i := sMonth; i <= eMonth; i++ {
  954. months = append(months, i)
  955. }
  956. }
  957. }
  958. if len(months) > 0 {
  959. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  960. }
  961. //if req.EndDate != "" {
  962. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  963. //}
  964. if len(req.MealKindList) > 0 {
  965. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  966. }
  967. if req.EnterPriseKind != 0 {
  968. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  969. }
  970. _, err := sess.
  971. 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").
  972. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  973. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  974. Join("LEFT", "user", "user.id = user_identity.uid").
  975. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  976. Join("LEFT", "class", "class_with_user.class_id = class.id").
  977. Join("LEFT", "grade", "class.grade_id = grade.id").
  978. FindAndCount(&m)
  979. if err != nil {
  980. logx.Error(err)
  981. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  982. return
  983. }
  984. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  985. centralKitchenForSchoolUserWithDayDb.Set(0)
  986. var list = map[string]map[string]map[string]map[string]int64{}
  987. var xlsFileName, xlsDownloadPath []string
  988. for _, v := range m {
  989. if list[v.Enterprise.Name] == nil {
  990. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  991. }
  992. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  993. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  994. }
  995. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  996. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  997. }
  998. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  999. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1000. req.StartDate, req.EndDate, req.MealKindList)
  1001. if err2 != nil {
  1002. logx.Error(err)
  1003. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1004. return
  1005. }
  1006. time.Sleep(20 * time.Millisecond) // 20毫秒
  1007. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1008. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  1009. }
  1010. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1011. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  1012. }
  1013. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1014. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  1015. }
  1016. }
  1017. for k1, v1 := range list {
  1018. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1019. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1020. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1021. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1022. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  1023. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1024. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  1025. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1026. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1027. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1028. if err != nil {
  1029. logx.Error(err)
  1030. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1031. panic(err)
  1032. }
  1033. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1034. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1035. j := 3 //表头前两行用了,只能从第三行开始
  1036. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  1037. tmpEnterpriseDb := db.EnterpriseDb{}
  1038. tmpEnterpriseDb.Set()
  1039. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1040. if err11 != nil {
  1041. logx.Error(err11)
  1042. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
  1043. panic(err11)
  1044. }
  1045. tmpClassDb := db.ClassDb{}
  1046. tmpClassDb.Set(0)
  1047. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1048. if err != nil {
  1049. logx.Error(err)
  1050. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1051. panic(err)
  1052. }
  1053. for _, class := range *classList {
  1054. for k2, v2 := range v1 {
  1055. if k2 == "" {
  1056. //若年级为空,则为教师餐
  1057. k2 = "教师餐"
  1058. }
  1059. for k3, v3 := range v2 {
  1060. if class.Name != k3 {
  1061. continue
  1062. }
  1063. if k3 == "" {
  1064. //若班级为空,则为教师餐
  1065. k3 = "教师餐"
  1066. }
  1067. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1068. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  1069. sumClassNum++
  1070. sumBreakfast += v3["breakfast"]
  1071. sumLunch += v3["lunch"]
  1072. sumDinner += v3["dinner"]
  1073. j++
  1074. }
  1075. }
  1076. }
  1077. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1078. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  1079. //将文件保存至服务器
  1080. err1 := xlsx.SaveAs(tmpDownloadPath)
  1081. if err1 != nil {
  1082. logx.Error(err1)
  1083. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1084. panic(err1)
  1085. }
  1086. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1087. xlsFileName = append(xlsFileName, tmpFileName)
  1088. }
  1089. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1090. if err != nil {
  1091. logx.Error(err)
  1092. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1093. panic(err)
  1094. }
  1095. defer archive.Close()
  1096. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1097. for k, v := range xlsDownloadPath {
  1098. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1099. if err1 != nil {
  1100. logx.Error(err1)
  1101. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1102. panic(err1)
  1103. }
  1104. f, err1 := os.Open(v) //打开待压缩的文件
  1105. if err1 != nil {
  1106. logx.Error(err1)
  1107. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1108. panic(err1)
  1109. }
  1110. if _, err1 = io.Copy(w, f); err != nil {
  1111. logx.Error(err1)
  1112. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1113. panic(err1)
  1114. }
  1115. if err1 != nil {
  1116. logx.Error(err1)
  1117. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1118. panic(err1)
  1119. }
  1120. f.Close()
  1121. os.RemoveAll(v) //TODO::移除源文件
  1122. }
  1123. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1124. if err != nil {
  1125. panic(err)
  1126. }
  1127. break
  1128. case 4:
  1129. //班级明细表
  1130. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1131. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1132. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1133. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1134. if req.EnterpriseId != 0 {
  1135. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1136. }
  1137. var months []time.Month
  1138. if req.StartDate != "" {
  1139. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1140. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1141. _, sMonth, _ := sDate.Date()
  1142. _, eMonth, _ := eDate.Date()
  1143. diffMonth := sMonth - eMonth
  1144. if diffMonth == 0 {
  1145. //TODO::查询一个月
  1146. months = append(months, sMonth)
  1147. } else {
  1148. for i := sMonth; i <= eMonth; i++ {
  1149. months = append(months, i)
  1150. }
  1151. }
  1152. }
  1153. if len(months) > 0 {
  1154. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1155. }
  1156. //if req.EndDate != "" {
  1157. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1158. //}
  1159. if len(req.MealKindList) > 0 {
  1160. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1161. }
  1162. if req.EnterPriseKind != 0 {
  1163. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1164. }
  1165. _, err := sess.
  1166. 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").
  1167. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1168. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1169. Join("LEFT", "user", "user.id = user_identity.uid").
  1170. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1171. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1172. Join("LEFT", "grade", "class.grade_id = grade.id").
  1173. Asc("class.id").
  1174. FindAndCount(&m)
  1175. if err != nil {
  1176. logx.Error(err)
  1177. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1178. return
  1179. }
  1180. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  1181. var xlsFileName, xlsDownloadPath []string
  1182. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1183. centralKitchenForSchoolUserWithDayDb.Set(0)
  1184. for _, v := range m {
  1185. if list[v.Enterprise.Name] == nil {
  1186. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  1187. }
  1188. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1189. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  1190. }
  1191. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1192. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  1193. }
  1194. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1195. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  1196. }
  1197. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1198. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1199. req.StartDate, req.EndDate, req.MealKindList)
  1200. if err2 != nil {
  1201. logx.Error(err)
  1202. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1203. return
  1204. }
  1205. if count == 0 {
  1206. continue
  1207. }
  1208. time.Sleep(20 * time.Millisecond) // 20毫秒
  1209. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1210. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  1211. }
  1212. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1213. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  1214. }
  1215. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1216. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  1217. }
  1218. }
  1219. for k1, v1 := range list {
  1220. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1221. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1222. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1223. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1224. //查询当前学校下的班级升序
  1225. tmpEnterpriseDb := db.EnterpriseDb{}
  1226. tmpEnterpriseDb.Set()
  1227. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1228. if err11 != nil {
  1229. logx.Error(err11)
  1230. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1231. panic(err11)
  1232. }
  1233. tmpClassDb := db.ClassDb{}
  1234. tmpClassDb.Set(0)
  1235. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1236. if err != nil {
  1237. logx.Error(err)
  1238. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1239. panic(err)
  1240. }
  1241. for _, class := range *classList {
  1242. for k2, v2 := range v1 {
  1243. if k2 == "" {
  1244. //若年级为空,则为教师餐
  1245. continue
  1246. }
  1247. for k3, v3 := range v2 {
  1248. if class.Name != k3 {
  1249. continue
  1250. }
  1251. xlsx.NewSheet(k3)
  1252. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  1253. xlsx.SetSheetRow(k3, "A1", &titleList)
  1254. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1255. xlsx.SetSheetRow(k3, "A2", &titleList)
  1256. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1257. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1258. if err != nil {
  1259. logx.Error(err)
  1260. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1261. panic(err)
  1262. }
  1263. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1264. xlsx.SetColWidth(k3, "A", "D", 20)
  1265. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  1266. j := 3 //表头前两行用了,只能从第三行开始
  1267. for k4, v4 := range v3 {
  1268. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  1269. continue
  1270. }
  1271. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1272. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1273. sumBreakfast += v4["breakfast"]
  1274. sumLunch += v4["lunch"]
  1275. sumDinner += v4["dinner"]
  1276. j++
  1277. }
  1278. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1279. "合计", sumBreakfast, sumLunch, sumDinner})
  1280. }
  1281. }
  1282. }
  1283. //将文件保存至服务器
  1284. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1285. err1 := xlsx.SaveAs(tmpDownloadPath)
  1286. if err1 != nil {
  1287. logx.Error(err1)
  1288. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1289. panic(err1)
  1290. }
  1291. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1292. xlsFileName = append(xlsFileName, tmpFileName)
  1293. }
  1294. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1295. if err != nil {
  1296. logx.Error(err)
  1297. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1298. panic(err)
  1299. }
  1300. defer archive.Close()
  1301. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1302. for k, v := range xlsDownloadPath {
  1303. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1304. if err1 != nil {
  1305. logx.Error(err1)
  1306. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1307. panic(err1)
  1308. }
  1309. f, err1 := os.Open(v) //打开待压缩的文件
  1310. if err1 != nil {
  1311. logx.Error(err1)
  1312. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1313. panic(err1)
  1314. }
  1315. if _, err1 = io.Copy(w, f); err != nil {
  1316. logx.Error(err1)
  1317. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1318. panic(err1)
  1319. }
  1320. if err1 != nil {
  1321. logx.Error(err1)
  1322. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1323. panic(err1)
  1324. }
  1325. f.Close()
  1326. os.RemoveAll(v) //TODO::移除源文件
  1327. }
  1328. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1329. if err != nil {
  1330. panic(err)
  1331. }
  1332. break
  1333. case 5:
  1334. //退款明细表
  1335. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1336. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1337. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1338. if req.EnterpriseId != 0 {
  1339. sess.And("enterprise.id =?", req.EnterpriseId)
  1340. }
  1341. if req.StartDate != "" {
  1342. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1343. }
  1344. if req.EndDate != "" {
  1345. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1346. }
  1347. if len(req.MealKindList) > 0 {
  1348. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  1349. }
  1350. if req.EnterPriseKind != 0 {
  1351. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1352. }
  1353. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  1354. _, err := sess.
  1355. 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").
  1356. 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").
  1357. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1358. Join("LEFT", "user", "user_identity.uid = user.id").
  1359. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1360. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1361. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1362. Join("LEFT", "grade", "class.grade_id = grade.id").
  1363. Desc("central_kitchen_for_school_user_refund_day.id").
  1364. FindAndCount(&m)
  1365. if err != nil {
  1366. logx.Error(err)
  1367. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1368. return
  1369. }
  1370. var list = map[string]map[string]map[string]map[string]map[string]int{}
  1371. var xlsFileName, xlsDownloadPath []string
  1372. for _, v := range m {
  1373. if list[v.Enterprise.Name] == nil {
  1374. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1375. }
  1376. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1377. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1378. }
  1379. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1380. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1381. }
  1382. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1383. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1384. }
  1385. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1386. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1387. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1388. }
  1389. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1390. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1391. }
  1392. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1393. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1394. }
  1395. }
  1396. for k1, v1 := range list {
  1397. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1398. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1399. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1400. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1401. for k2, v2 := range v1 {
  1402. if k2 == "" {
  1403. //若年级为空,则为教师餐
  1404. continue
  1405. }
  1406. for k3, v3 := range v2 {
  1407. xlsx.NewSheet(k3)
  1408. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1409. xlsx.SetSheetRow(k3, "A1", &titleList)
  1410. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1411. xlsx.SetSheetRow(k3, "A2", &titleList)
  1412. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1413. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1414. if err != nil {
  1415. logx.Error(err)
  1416. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1417. panic(err)
  1418. }
  1419. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1420. xlsx.SetColWidth(k3, "A", "D", 20)
  1421. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1422. j := 3 //表头前两行用了,只能从第三行开始
  1423. for k4, v4 := range v3 {
  1424. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1425. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1426. sumBreakfast += v4["breakfast"]
  1427. sumLunch += v4["lunch"]
  1428. sumDinner += v4["dinner"]
  1429. j++
  1430. }
  1431. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1432. "合计", sumBreakfast, sumLunch, sumDinner})
  1433. }
  1434. }
  1435. //将文件保存至服务器
  1436. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1437. err1 := xlsx.SaveAs(tmpDownloadPath)
  1438. if err1 != nil {
  1439. logx.Error(err1)
  1440. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1441. panic(err1)
  1442. }
  1443. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1444. xlsFileName = append(xlsFileName, tmpFileName)
  1445. }
  1446. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1447. if err != nil {
  1448. logx.Error(err)
  1449. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1450. panic(err)
  1451. }
  1452. defer archive.Close()
  1453. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1454. for k, v := range xlsDownloadPath {
  1455. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1456. if err1 != nil {
  1457. logx.Error(err1)
  1458. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1459. panic(err1)
  1460. }
  1461. f, err1 := os.Open(v) //打开待压缩的文件
  1462. if err1 != nil {
  1463. logx.Error(err1)
  1464. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1465. panic(err1)
  1466. }
  1467. if _, err1 = io.Copy(w, f); err != nil {
  1468. logx.Error(err1)
  1469. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1470. panic(err1)
  1471. }
  1472. if err1 != nil {
  1473. logx.Error(err1)
  1474. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1475. panic(err1)
  1476. }
  1477. f.Close()
  1478. os.RemoveAll(v) //TODO::移除源文件
  1479. }
  1480. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1481. if err != nil {
  1482. panic(err)
  1483. }
  1484. break
  1485. case 6:
  1486. //收款统计表
  1487. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1488. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1489. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1490. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1491. //查找数据
  1492. var m []model.Enterprise
  1493. sess := db.Db.Desc("id")
  1494. if req.EnterPriseKind != 0 {
  1495. sess.And("kind = ?", req.EnterPriseKind)
  1496. }
  1497. if req.EnterpriseId != 0 {
  1498. sess.And("id = ?", req.EnterpriseId)
  1499. }
  1500. err := sess.Find(&m)
  1501. if err != nil {
  1502. logx.Error(err)
  1503. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1504. return
  1505. }
  1506. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1507. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1508. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1509. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1510. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1511. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1512. if err != nil {
  1513. logx.Error(err)
  1514. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1515. panic(err)
  1516. }
  1517. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1518. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1519. j := 3 //表头前两行用了,只能从第三行开始
  1520. for _, v := range m {
  1521. var income, refund string
  1522. //统计收入
  1523. 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)
  1524. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1525. if err != nil {
  1526. logx.Error(err)
  1527. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1528. return
  1529. }
  1530. if mapArr1 != nil {
  1531. income = mapArr1[0]["total"]
  1532. }
  1533. //统计退款
  1534. //查询出当前学校下所有的学生
  1535. userIdentityDb := db.UserIdentityDb{}
  1536. userIdentityDb.Set(0)
  1537. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1538. var userIdentityIds []string
  1539. for _, v := range *userIdentities {
  1540. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1541. }
  1542. if len(userIdentityIds) != 0 {
  1543. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1544. 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'",
  1545. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1546. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1547. if err != nil {
  1548. logx.Error(err)
  1549. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1550. return
  1551. }
  1552. if mapArr2 != nil {
  1553. refund = mapArr2[0]["total"]
  1554. }
  1555. }
  1556. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1557. rateFee := realityAmount * 0.0038
  1558. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1559. j++
  1560. }
  1561. //将文件保存至服务器
  1562. xlsx.SaveAs("./static" + downloadPath)
  1563. break
  1564. case 7:
  1565. //班级收款明细表
  1566. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1567. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1568. //查找订单数据
  1569. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1570. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1571. if req.EnterpriseId != 0 {
  1572. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1573. }
  1574. if req.StartDate != "" {
  1575. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1576. }
  1577. if req.EndDate != "" {
  1578. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1579. }
  1580. if req.EnterPriseKind != 0 {
  1581. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1582. }
  1583. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1584. _, err := sess.
  1585. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1586. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1587. Join("LEFT", "user", "user.id = user_identity.uid").
  1588. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1589. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1590. Join("LEFT", "grade", "class.grade_id = grade.id").
  1591. FindAndCount(&m)
  1592. if err != nil {
  1593. logx.Error(err)
  1594. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1595. return
  1596. }
  1597. 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")
  1598. if req.EnterpriseId != 0 {
  1599. sess1.And("enterprise.id =?", req.EnterpriseId)
  1600. }
  1601. if req.StartDate != "" {
  1602. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1603. }
  1604. if req.EndDate != "" {
  1605. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1606. }
  1607. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1608. _, err = sess1.
  1609. 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").
  1610. 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").
  1611. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1612. Join("LEFT", "user", "user_identity.uid = user.id").
  1613. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1614. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1615. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1616. Join("LEFT", "grade", "class.grade_id = grade.id").
  1617. Desc("central_kitchen_for_school_user_refund_day.id").
  1618. FindAndCount(&mm)
  1619. if err != nil {
  1620. logx.Error(err)
  1621. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1622. return
  1623. }
  1624. var list = map[string]map[string]map[string]map[string]float64{}
  1625. for _, v := range m {
  1626. if list[v.Enterprise.Name] == nil {
  1627. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1628. }
  1629. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1630. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1631. }
  1632. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1633. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1634. }
  1635. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1636. }
  1637. for _, v := range mm {
  1638. if list[v.Enterprise.Name] == nil {
  1639. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1640. }
  1641. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1642. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1643. }
  1644. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1645. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1646. }
  1647. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1648. }
  1649. var xlsFileName, xlsDownloadPath []string
  1650. for k1, v1 := range list {
  1651. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1652. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1653. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1654. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1655. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1656. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1657. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1658. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1659. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1660. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1661. if err != nil {
  1662. logx.Error(err)
  1663. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1664. panic(err)
  1665. }
  1666. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1667. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1668. j := 3 //表头前两行用了,只能从第三行开始
  1669. var sumClassNum = int64(0)
  1670. var sumIncome, sumRefund = float64(0), float64(0)
  1671. for k2, v2 := range v1 {
  1672. if k2 == "" {
  1673. //若年级为空,则为教师餐
  1674. continue
  1675. }
  1676. for k3, v3 := range v2 {
  1677. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1678. sumClassNum++
  1679. sumIncome += v3["income"]
  1680. sumRefund += v3["refund"]
  1681. j++
  1682. }
  1683. }
  1684. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1685. "合计", sumClassNum, sumIncome, sumRefund})
  1686. //将文件保存至服务器
  1687. err1 := xlsx.SaveAs(tmpDownloadPath)
  1688. if err1 != nil {
  1689. logx.Error(err1)
  1690. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1691. panic(err1)
  1692. }
  1693. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1694. xlsFileName = append(xlsFileName, tmpFileName)
  1695. }
  1696. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1697. if err != nil {
  1698. logx.Error(err)
  1699. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1700. panic(err)
  1701. }
  1702. defer archive.Close()
  1703. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1704. for k, v := range xlsDownloadPath {
  1705. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1706. if err1 != nil {
  1707. logx.Error(err1)
  1708. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1709. panic(err1)
  1710. }
  1711. f, err1 := os.Open(v) //打开待压缩的文件
  1712. if err1 != nil {
  1713. logx.Error(err1)
  1714. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1715. panic(err1)
  1716. }
  1717. if _, err1 = io.Copy(w, f); err != nil {
  1718. logx.Error(err1)
  1719. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1720. panic(err1)
  1721. }
  1722. if err1 != nil {
  1723. logx.Error(err1)
  1724. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1725. panic(err1)
  1726. }
  1727. f.Close()
  1728. os.RemoveAll(v) //TODO::移除源文件
  1729. }
  1730. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1731. if err != nil {
  1732. panic(err)
  1733. }
  1734. break
  1735. case 8:
  1736. //学校预定数量统计表
  1737. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1738. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1739. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1740. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1741. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1742. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1743. if req.EnterpriseId != 0 {
  1744. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1745. }
  1746. var months []time.Month
  1747. if req.StartDate != "" {
  1748. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1749. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1750. _, sMonth, _ := sDate.Date()
  1751. _, eMonth, _ := eDate.Date()
  1752. diffMonth := sMonth - eMonth
  1753. if diffMonth == 0 {
  1754. //TODO::查询一个月
  1755. months = append(months, sMonth)
  1756. } else {
  1757. for i := sMonth; i <= eMonth; i++ {
  1758. months = append(months, i)
  1759. }
  1760. }
  1761. }
  1762. if len(months) > 0 {
  1763. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1764. }
  1765. //if req.EndDate != "" {
  1766. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1767. //}
  1768. if len(req.MealKindList) > 0 {
  1769. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1770. }
  1771. if req.EnterPriseKind != 0 {
  1772. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1773. }
  1774. _, err := sess.
  1775. 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").
  1776. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1777. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1778. FindAndCount(&m)
  1779. if err != nil {
  1780. logx.Error(err)
  1781. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1782. return
  1783. }
  1784. var list = map[string]map[string]int64{}
  1785. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1786. centralKitchenForSchoolUserWithDayDb.Set(0)
  1787. for _, v := range m {
  1788. if list[v.Enterprise.Name] == nil {
  1789. list[v.Enterprise.Name] = map[string]int64{}
  1790. }
  1791. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1792. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1793. req.StartDate, req.EndDate, req.MealKindList)
  1794. if err2 != nil {
  1795. logx.Error(err)
  1796. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1797. return
  1798. }
  1799. for _, value := range req.MealKindList {
  1800. if value == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1801. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1802. if err2 != nil {
  1803. logx.Error(err)
  1804. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1805. return
  1806. }
  1807. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1808. }
  1809. if value == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1810. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForLunch, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1811. if err2 != nil {
  1812. logx.Error(err)
  1813. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1814. return
  1815. }
  1816. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1817. }
  1818. if value == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1819. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForDinner, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1820. if err2 != nil {
  1821. logx.Error(err)
  1822. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1823. return
  1824. }
  1825. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1826. }
  1827. }
  1828. list[v.Enterprise.Name]["count"] += count
  1829. time.Sleep(20 * time.Millisecond) // 20毫秒
  1830. }
  1831. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1832. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1833. titleList = []string{"学校名称", "预定总数",
  1834. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1835. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1836. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1837. }
  1838. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1839. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1840. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1841. if err != nil {
  1842. logx.Error(err)
  1843. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1844. panic(err)
  1845. }
  1846. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1847. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1848. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1849. j := 3 //表头前两行用了,只能从第三行开始
  1850. total := int64(0)
  1851. total1 := int64(0)
  1852. total2 := int64(0)
  1853. total3 := int64(0)
  1854. for k, v := range list {
  1855. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1856. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1857. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1858. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1859. total += v["count"]
  1860. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1861. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1862. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1863. j++
  1864. }
  1865. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1866. "合计", total, total1, total2, total3})
  1867. //将文件保存至服务器
  1868. xlsx.SaveAs("./static" + downloadPath)
  1869. break
  1870. case 10:
  1871. //就餐数据统计表
  1872. sTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1873. eTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1874. xls, _ := excelize.OpenFile("./static/template/央厨学校(就餐数据统计表).xlsx")
  1875. fileName = "【" + sTime.Format("2006-01-02") + "~" + eTime.Format("2006-01-02") + "】就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1876. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1877. var list = map[string]map[string]map[string]float64{}
  1878. var dates []string
  1879. if req.StartDate != "" {
  1880. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1881. centralKitchenForSchoolUserWithDayDb.Set(0)
  1882. diffDays := int(eTime.Sub(sTime).Hours() / 24)
  1883. for i := 0; i <= diffDays; i++ {
  1884. date := sTime.AddDate(0, 0, i)
  1885. dateName := date.Format("2006-01-02")
  1886. dates = append(dates, dateName)
  1887. for _, vv := range req.MealKindList {
  1888. tmpSum, err := centralKitchenForSchoolUserWithDayDb.SumCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1889. if err != nil {
  1890. logx.Error(err)
  1891. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  1892. panic(err)
  1893. }
  1894. tmpCount, err := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1895. if err != nil {
  1896. logx.Error(err)
  1897. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  1898. panic(err)
  1899. }
  1900. if list[dateName] == nil {
  1901. list[dateName] = map[string]map[string]float64{}
  1902. }
  1903. if list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] == nil {
  1904. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] = map[string]float64{}
  1905. }
  1906. if list[dateName]["合计"] == nil {
  1907. list[dateName]["合计"] = make(map[string]float64)
  1908. }
  1909. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["sum"] = tmpSum
  1910. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["count"] = float64(tmpCount)
  1911. list[dateName]["合计"]["sum"] += tmpSum
  1912. list[dateName]["合计"]["count"] += float64(tmpCount)
  1913. }
  1914. }
  1915. }
  1916. //xls.SetSheetRow("", "A1", &titleList)
  1917. //sheetIndex := 2 //第一个表是模板表
  1918. //xls.NewSheet("sheet1") //1、新建表格
  1919. //xls.CopySheet(1, sheetIndex)
  1920. j := 4 //表头前三"早餐"行被占用了,只能从第四行开始
  1921. var totalMap = map[string]float64{}
  1922. for _, date := range dates {
  1923. for kk, v := range list {
  1924. if date == kk {
  1925. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{kk,
  1926. v["早餐"]["count"], v["早餐"]["sum"],
  1927. v["午餐"]["count"], v["午餐"]["sum"],
  1928. v["晚餐"]["count"], v["晚餐"]["sum"],
  1929. v["合计"]["count"], v["合计"]["sum"],
  1930. })
  1931. totalMap["breakfast_count"] += v["早餐"]["count"]
  1932. totalMap["breakfast_sum"] += v["早餐"]["sum"]
  1933. totalMap["lunch_count"] += v["午餐"]["count"]
  1934. totalMap["lunch_sum"] += v["午餐"]["sum"]
  1935. totalMap["dinner_count"] += v["晚餐"]["count"]
  1936. totalMap["dinner_sum"] += v["晚餐"]["sum"]
  1937. totalMap["total_count"] += v["合计"]["count"]
  1938. totalMap["total_sum"] += v["合计"]["sum"]
  1939. j++
  1940. }
  1941. }
  1942. }
  1943. //xls.DeleteSheet("template") //删除模板表
  1944. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{"总计",
  1945. totalMap["breakfast_count"], totalMap["breakfast_sum"],
  1946. totalMap["lunch_count"], totalMap["lunch_sum"],
  1947. totalMap["dinner_count"], totalMap["dinner_sum"],
  1948. totalMap["total_count"], totalMap["total_sum"],
  1949. })
  1950. //将文件保存至服务器
  1951. xls.SaveAs("./static" + downloadPath)
  1952. break
  1953. }
  1954. //新增数据
  1955. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1956. centralKitchenForSchoolExportRecordsDb.Set()
  1957. marshal, _ := json.Marshal(req)
  1958. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1959. Name: fileName,
  1960. DownloadPath: downloadPath,
  1961. Kind: kind,
  1962. ReqContent: string(marshal),
  1963. CreateAt: now.Format("2006-01-02 15:04:05"),
  1964. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1965. })
  1966. }
  1967. }
  1968. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1969. var titleList []string
  1970. var fileName, downloadPath string
  1971. var now = time.Now()
  1972. xlsx := excelize.NewFile()
  1973. //数据对比
  1974. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1975. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1976. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1977. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1978. if req.EnterpriseId != 0 {
  1979. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1980. }
  1981. if req.Date1[0] != "" {
  1982. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1983. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  1984. }
  1985. if req.Date1[1] != "" {
  1986. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[1], time.Local)
  1987. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  1988. }
  1989. if len(req.MealKindList) > 0 {
  1990. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1991. }
  1992. if req.EnterPriseKind != 0 {
  1993. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1994. }
  1995. _, err := sess.
  1996. 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").
  1997. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1998. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1999. Join("LEFT", "user", "user.id = user_identity.uid").
  2000. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  2001. Join("LEFT", "class", "class_with_user.class_id = class.id").
  2002. Join("LEFT", "grade", "class.grade_id = grade.id").
  2003. FindAndCount(&m)
  2004. if err != nil {
  2005. logx.Error(err)
  2006. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2007. return
  2008. }
  2009. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  2010. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  2011. if req.EnterpriseId != 0 {
  2012. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  2013. }
  2014. if req.Date2[0] != "" {
  2015. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  2016. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  2017. }
  2018. if req.Date2[1] != "" {
  2019. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[1], time.Local)
  2020. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  2021. }
  2022. if len(req.MealKindList) > 0 {
  2023. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  2024. }
  2025. _, err1 := sess1.
  2026. 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").
  2027. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  2028. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  2029. Join("LEFT", "user", "user.id = user_identity.uid").
  2030. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  2031. Join("LEFT", "class", "class_with_user.class_id = class.id").
  2032. Join("LEFT", "grade", "class.grade_id = grade.id").
  2033. FindAndCount(&m1)
  2034. if err1 != nil {
  2035. logx.Error(err1)
  2036. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2037. return
  2038. }
  2039. var list = map[string]map[string]map[string]map[string]string{}
  2040. for _, v := range m {
  2041. if list[v.Enterprise.Name] == nil {
  2042. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  2043. }
  2044. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  2045. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  2046. }
  2047. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  2048. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  2049. }
  2050. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  2051. }
  2052. var list1 = map[string]map[string]map[string]map[string]string{}
  2053. for _, v := range m1 {
  2054. if list1[v.Enterprise.Name] == nil {
  2055. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  2056. }
  2057. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  2058. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  2059. }
  2060. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  2061. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  2062. }
  2063. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  2064. }
  2065. var xlsFileName, xlsDownloadPath []string
  2066. for k1, v1 := range list {
  2067. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  2068. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  2069. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  2070. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  2071. //查询当前学校下的班级升序
  2072. tmpEnterpriseDb := db.EnterpriseDb{}
  2073. tmpEnterpriseDb.Set()
  2074. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  2075. if err11 != nil {
  2076. logx.Error(err11)
  2077. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  2078. panic(err11)
  2079. }
  2080. tmpClassDb := db.ClassDb{}
  2081. tmpClassDb.Set(0)
  2082. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  2083. if err != nil {
  2084. logx.Error(err)
  2085. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  2086. panic(err)
  2087. }
  2088. for _, class := range *classList {
  2089. for k2, v2 := range v1 {
  2090. if k2 == "" {
  2091. //若年级为空,则为教师
  2092. continue
  2093. }
  2094. for k3, v3 := range v2 {
  2095. if class.Name != k3 {
  2096. continue
  2097. }
  2098. xlsx.NewSheet(k3)
  2099. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  2100. xlsx.SetSheetRow(k3, "A1", &titleList)
  2101. titleList = []string{"姓名", "家长联系电话"}
  2102. xlsx.SetSheetRow(k3, "A2", &titleList)
  2103. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  2104. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  2105. if err != nil {
  2106. logx.Error(err)
  2107. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  2108. panic(err)
  2109. }
  2110. xlsx.SetCellStyle(k3, "A1", "B1", style)
  2111. xlsx.SetColWidth(k3, "A", "B", 40)
  2112. j := 2 //表头被第一行用了,只能从第二行开始
  2113. for k4, v4 := range v3 {
  2114. //TODO::判断当前用户是否在新的月份里面
  2115. fmt.Println("11111111111111", list1[k1])
  2116. fmt.Println("22222222222222", list1[k1][k2])
  2117. fmt.Println("33333333333333", list1[k1][k2][k3])
  2118. fmt.Println("44444444444444", list1[k1][k2][k3][k4])
  2119. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  2120. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  2121. k4, v4})
  2122. j++
  2123. }
  2124. }
  2125. }
  2126. }
  2127. }
  2128. //将文件保存至服务器
  2129. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  2130. err1 := xlsx.SaveAs(tmpDownloadPath)
  2131. if err1 != nil {
  2132. logx.Error(err1)
  2133. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2134. panic(err1)
  2135. }
  2136. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  2137. xlsFileName = append(xlsFileName, tmpFileName)
  2138. }
  2139. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  2140. if err != nil {
  2141. logx.Error(err)
  2142. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2143. panic(err)
  2144. }
  2145. defer archive.Close()
  2146. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  2147. for k, v := range xlsDownloadPath {
  2148. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  2149. if err1 != nil {
  2150. logx.Error(err1)
  2151. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2152. panic(err1)
  2153. }
  2154. f, err1 := os.Open(v) //打开待压缩的文件
  2155. if err1 != nil {
  2156. logx.Error(err1)
  2157. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2158. panic(err1)
  2159. }
  2160. if _, err1 = io.Copy(w, f); err != nil {
  2161. logx.Error(err1)
  2162. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2163. panic(err1)
  2164. }
  2165. if err1 != nil {
  2166. logx.Error(err1)
  2167. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2168. panic(err1)
  2169. }
  2170. f.Close()
  2171. os.RemoveAll(v) //TODO::移除源文件
  2172. }
  2173. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  2174. if err != nil {
  2175. panic(err)
  2176. }
  2177. //新增数据
  2178. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  2179. centralKitchenForSchoolExportRecordsDb.Set()
  2180. marshal, _ := json.Marshal(req)
  2181. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  2182. Name: fileName,
  2183. DownloadPath: downloadPath,
  2184. Kind: 9, //数据对比
  2185. ReqContent: string(marshal),
  2186. CreateAt: now.Format("2006-01-02 15:04:05"),
  2187. UpdateAt: now.Format("2006-01-02 15:04:05"),
  2188. })
  2189. }
  2190. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  2191. mealZh = "未知"
  2192. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  2193. for _, v := range md.SelfSupportForMealTimeMapping {
  2194. //判断时间大小
  2195. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  2196. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  2197. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  2198. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  2199. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  2200. mealZh = v["name"]
  2201. }
  2202. }
  2203. return
  2204. }