智慧食堂
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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