智慧食堂
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

2020 wiersze
86 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("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  433. return
  434. }
  435. //整合数据
  436. var data = map[string]map[string]map[string]map[string]float64{}
  437. shopName := req.StartDate + " ~ " + req.EndDate
  438. for _, vv := range m {
  439. if data[vv.Enterprise.Name] == nil {
  440. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  441. }
  442. if data[vv.Enterprise.Name][shopName] == nil {
  443. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  444. }
  445. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  446. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  447. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  448. }
  449. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  450. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  451. }
  452. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  453. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  454. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  455. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  456. }
  457. xlsx.SetSheetRow("", "A1", &titleList)
  458. sheetIndex := 2 //第一个表是模板表
  459. for k, v := range data {
  460. if k == "" {
  461. k = "未知"
  462. }
  463. xlsx.NewSheet(k) //1、新建表格
  464. xlsx.CopySheet(1, sheetIndex)
  465. j := 4 //表头前三行被占用了,只能从第四行开始
  466. for kk, vv := range v {
  467. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  468. vv["早餐"]["num"], vv["早餐"]["amount"],
  469. vv["午餐"]["num"], vv["午餐"]["amount"],
  470. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  471. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  472. vv["合计"]["num"], vv["合计"]["amount"],
  473. })
  474. j++
  475. }
  476. sheetIndex++
  477. }
  478. xlsx.DeleteSheet("template") //删除模板表
  479. //将文件保存至服务器
  480. xlsx.SaveAs("./static" + downloadPath)
  481. break
  482. }
  483. //新增数据
  484. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  485. selfSupportForSchoolExportRecordsDb.Set()
  486. marshal, _ := json.Marshal(req)
  487. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  488. Name: fileName,
  489. DownloadPath: downloadPath,
  490. Kind: req.Kind,
  491. ReqContent: string(marshal),
  492. CreateAt: now.Format("2006-01-02 15:04:05"),
  493. UpdateAt: now.Format("2006-01-02 15:04:05"),
  494. })
  495. }
  496. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  497. var titleList []string
  498. var fileName, downloadPath string
  499. var now = time.Now()
  500. for _, kind := range req.Kinds {
  501. xlsx := excelize.NewFile()
  502. switch kind {
  503. case 1:
  504. //订单列表
  505. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  506. downloadPath = "/export/central_kitchen_for_school/" + fileName
  507. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  508. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  509. //查找订单数据
  510. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  511. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  512. if req.EnterpriseId != 0 {
  513. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  514. }
  515. if req.StartDate != "" {
  516. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  517. }
  518. if req.EndDate != "" {
  519. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  520. }
  521. if req.EnterPriseKind != 0 {
  522. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  523. }
  524. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  525. _, err := sess.
  526. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  527. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  528. Join("LEFT", "user", "user.id = user_identity.uid").
  529. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  530. Join("LEFT", "class", "class_with_user.class_id = class.id").
  531. Join("LEFT", "grade", "class.grade_id = grade.id").
  532. FindAndCount(&m)
  533. if err != nil {
  534. logx.Error(err)
  535. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  536. return
  537. }
  538. //查找预定数据
  539. var outTradeNoList []string
  540. for _, v := range m {
  541. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  542. }
  543. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  544. sess1 := db.Db.Desc("id")
  545. if req.EnterpriseId != 0 {
  546. sess1.And("enterprise_id = ?", req.EnterpriseId)
  547. }
  548. if len(req.MealKindList) > 0 {
  549. sess1.In("kind", req.MealKindList)
  550. }
  551. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  552. if err != nil {
  553. logx.Error(err)
  554. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  555. return
  556. }
  557. var exportOutTradeNoList []string
  558. for _, v := range mm {
  559. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  560. }
  561. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  562. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  563. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定餐数", "订单价格", "支付时间", "订单状态"}
  564. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  565. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  566. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  567. if err != nil {
  568. logx.Error(err)
  569. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  570. panic(err)
  571. }
  572. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  573. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  574. j := 3 //表头前两行用了,只能从第三行开始
  575. for _, v := range m {
  576. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  577. continue
  578. }
  579. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  580. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  581. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  582. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  583. break
  584. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  585. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  586. break
  587. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  588. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  589. break
  590. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  591. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  592. break
  593. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  594. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  595. break
  596. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  597. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  598. break
  599. }
  600. //预定餐数、预定日期
  601. var list []*model.CentralKitchenForSchoolUserWithDay
  602. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  603. if err1 != nil {
  604. logx.Error(err)
  605. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  606. return
  607. }
  608. var reserveDays string
  609. for _, v1 := range list {
  610. reserveDays += v1.Date + ", "
  611. }
  612. if v.Grade.Name == "" {
  613. v.Grade.Name = "教师"
  614. }
  615. if v.Class.Name == "" {
  616. v.Class.Name = "教师"
  617. }
  618. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  619. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  620. j++
  621. }
  622. //将文件保存至服务器
  623. xlsx.SaveAs("./static" + downloadPath)
  624. break
  625. case 2:
  626. //退款申请表
  627. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  628. downloadPath = "/export/central_kitchen_for_school/" + fileName
  629. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  630. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  631. //查找退款申请数据
  632. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  633. if req.EnterpriseId != 0 {
  634. sess.And("enterprise.id =?", req.EnterpriseId)
  635. }
  636. if req.StartDate != "" {
  637. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  638. }
  639. if req.EndDate != "" {
  640. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  641. }
  642. if len(req.MealKindList) > 0 {
  643. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  644. }
  645. if req.EnterPriseKind != 0 {
  646. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  647. }
  648. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  649. _, err := sess.
  650. 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").
  651. 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").
  652. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  653. Join("LEFT", "user", "user_identity.uid = user.id").
  654. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  655. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  656. Join("LEFT", "class", "class_with_user.class_id = class.id").
  657. Join("LEFT", "grade", "class.grade_id = grade.id").
  658. Desc("central_kitchen_for_school_user_refund_day.id").
  659. FindAndCount(&m)
  660. if err != nil {
  661. logx.Error(err)
  662. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  663. return
  664. }
  665. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  666. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  667. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  668. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  669. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  670. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  671. if err != nil {
  672. logx.Error(err)
  673. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  674. panic(err)
  675. }
  676. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  677. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  678. j := 3 //表头前两行用了,只能从第三行开始
  679. for _, v := range m {
  680. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  681. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  682. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  683. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  684. break
  685. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  686. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  687. break
  688. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  689. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  690. break
  691. }
  692. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  693. switch v.CentralKitchenForSchoolUserRefundDay.State {
  694. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  695. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  696. break
  697. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  698. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  699. break
  700. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  701. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  702. break
  703. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  704. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  705. break
  706. }
  707. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  708. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  709. j++
  710. }
  711. //将文件保存至服务器
  712. xlsx.SaveAs("./static" + downloadPath)
  713. break
  714. case 3:
  715. //学校预定统计表
  716. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  717. downloadPath = "/export/central_kitchen_for_school/" + fileName
  718. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  719. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  720. if req.EnterpriseId != 0 {
  721. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  722. }
  723. var months []time.Month
  724. if req.StartDate != "" {
  725. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  726. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  727. _, sMonth, _ := sDate.Date()
  728. _, eMonth, _ := eDate.Date()
  729. diffMonth := sMonth - eMonth
  730. if diffMonth == 0 {
  731. //TODO::查询一个月
  732. months = append(months, sMonth)
  733. } else {
  734. for i := sMonth; i <= eMonth; i++ {
  735. months = append(months, i)
  736. }
  737. }
  738. }
  739. if len(months) > 0 {
  740. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  741. }
  742. //if req.EndDate != "" {
  743. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  744. //}
  745. if len(req.MealKindList) > 0 {
  746. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  747. }
  748. if req.EnterPriseKind != 0 {
  749. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  750. }
  751. _, err := sess.
  752. 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").
  753. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  754. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  755. Join("LEFT", "user", "user.id = user_identity.uid").
  756. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  757. Join("LEFT", "class", "class_with_user.class_id = class.id").
  758. Join("LEFT", "grade", "class.grade_id = grade.id").
  759. FindAndCount(&m)
  760. if err != nil {
  761. logx.Error(err)
  762. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  763. return
  764. }
  765. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  766. centralKitchenForSchoolUserWithDayDb.Set(0)
  767. var list = map[string]map[string]map[string]map[string]int64{}
  768. var xlsFileName, xlsDownloadPath []string
  769. for _, v := range m {
  770. if list[v.Enterprise.Name] == nil {
  771. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  772. }
  773. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  774. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  775. }
  776. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  777. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  778. }
  779. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  780. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  781. req.StartDate, req.EndDate, req.MealKindList)
  782. if err2 != nil {
  783. logx.Error(err)
  784. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  785. return
  786. }
  787. time.Sleep(20 * time.Millisecond) // 20毫秒
  788. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  789. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  790. }
  791. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  792. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  793. }
  794. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  795. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  796. }
  797. }
  798. for k1, v1 := range list {
  799. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  800. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  801. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  802. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  803. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  804. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  805. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  806. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  807. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  808. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  809. if err != nil {
  810. logx.Error(err)
  811. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  812. panic(err)
  813. }
  814. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  815. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  816. j := 3 //表头前两行用了,只能从第三行开始
  817. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  818. tmpEnterpriseDb := db.EnterpriseDb{}
  819. tmpEnterpriseDb.Set()
  820. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  821. if err11 != nil {
  822. logx.Error(err11)
  823. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
  824. panic(err11)
  825. }
  826. tmpClassDb := db.ClassDb{}
  827. tmpClassDb.Set(0)
  828. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  829. if err != nil {
  830. logx.Error(err)
  831. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  832. panic(err)
  833. }
  834. for _, class := range *classList {
  835. for k2, v2 := range v1 {
  836. if k2 == "" {
  837. //若年级为空,则为教师餐
  838. k2 = "教师餐"
  839. }
  840. for k3, v3 := range v2 {
  841. if class.Name != k3 {
  842. continue
  843. }
  844. if k3 == "" {
  845. //若班级为空,则为教师餐
  846. k3 = "教师餐"
  847. }
  848. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  849. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  850. sumClassNum++
  851. sumBreakfast += v3["breakfast"]
  852. sumLunch += v3["lunch"]
  853. sumDinner += v3["dinner"]
  854. j++
  855. }
  856. }
  857. }
  858. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  859. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  860. //将文件保存至服务器
  861. err1 := xlsx.SaveAs(tmpDownloadPath)
  862. if err1 != nil {
  863. logx.Error(err1)
  864. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  865. panic(err1)
  866. }
  867. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  868. xlsFileName = append(xlsFileName, tmpFileName)
  869. }
  870. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  871. if err != nil {
  872. logx.Error(err)
  873. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  874. panic(err)
  875. }
  876. defer archive.Close()
  877. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  878. for k, v := range xlsDownloadPath {
  879. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  880. if err1 != nil {
  881. logx.Error(err1)
  882. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  883. panic(err1)
  884. }
  885. f, err1 := os.Open(v) //打开待压缩的文件
  886. if err1 != nil {
  887. logx.Error(err1)
  888. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  889. panic(err1)
  890. }
  891. if _, err1 = io.Copy(w, f); err != nil {
  892. logx.Error(err1)
  893. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  894. panic(err1)
  895. }
  896. if err1 != nil {
  897. logx.Error(err1)
  898. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  899. panic(err1)
  900. }
  901. f.Close()
  902. os.RemoveAll(v) //TODO::移除源文件
  903. }
  904. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  905. if err != nil {
  906. panic(err)
  907. }
  908. break
  909. case 4:
  910. //班级明细表
  911. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  912. downloadPath = "/export/central_kitchen_for_school/" + fileName
  913. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  914. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  915. if req.EnterpriseId != 0 {
  916. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  917. }
  918. var months []time.Month
  919. if req.StartDate != "" {
  920. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  921. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  922. _, sMonth, _ := sDate.Date()
  923. _, eMonth, _ := eDate.Date()
  924. diffMonth := sMonth - eMonth
  925. if diffMonth == 0 {
  926. //TODO::查询一个月
  927. months = append(months, sMonth)
  928. } else {
  929. for i := sMonth; i <= eMonth; i++ {
  930. months = append(months, i)
  931. }
  932. }
  933. }
  934. if len(months) > 0 {
  935. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  936. }
  937. //if req.EndDate != "" {
  938. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  939. //}
  940. if len(req.MealKindList) > 0 {
  941. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  942. }
  943. if req.EnterPriseKind != 0 {
  944. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  945. }
  946. _, err := sess.
  947. 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").
  948. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  949. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  950. Join("LEFT", "user", "user.id = user_identity.uid").
  951. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  952. Join("LEFT", "class", "class_with_user.class_id = class.id").
  953. Join("LEFT", "grade", "class.grade_id = grade.id").
  954. Asc("class.id").
  955. FindAndCount(&m)
  956. if err != nil {
  957. logx.Error(err)
  958. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  959. return
  960. }
  961. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  962. var xlsFileName, xlsDownloadPath []string
  963. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  964. centralKitchenForSchoolUserWithDayDb.Set(0)
  965. for _, v := range m {
  966. if list[v.Enterprise.Name] == nil {
  967. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  968. }
  969. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  970. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  971. }
  972. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  973. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  974. }
  975. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  976. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  977. }
  978. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  979. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  980. req.StartDate, req.EndDate, req.MealKindList)
  981. if err2 != nil {
  982. logx.Error(err)
  983. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  984. return
  985. }
  986. if count == 0 {
  987. continue
  988. }
  989. time.Sleep(20 * time.Millisecond) // 20毫秒
  990. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  991. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  992. }
  993. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  994. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  995. }
  996. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  997. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  998. }
  999. }
  1000. for k1, v1 := range list {
  1001. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1002. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1003. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1004. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1005. //查询当前学校下的班级升序
  1006. tmpEnterpriseDb := db.EnterpriseDb{}
  1007. tmpEnterpriseDb.Set()
  1008. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1009. if err11 != nil {
  1010. logx.Error(err11)
  1011. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1012. panic(err11)
  1013. }
  1014. tmpClassDb := db.ClassDb{}
  1015. tmpClassDb.Set(0)
  1016. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1017. if err != nil {
  1018. logx.Error(err)
  1019. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1020. panic(err)
  1021. }
  1022. for _, class := range *classList {
  1023. for k2, v2 := range v1 {
  1024. if k2 == "" {
  1025. //若年级为空,则为教师餐
  1026. continue
  1027. }
  1028. for k3, v3 := range v2 {
  1029. if class.Name != k3 {
  1030. continue
  1031. }
  1032. xlsx.NewSheet(k3)
  1033. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  1034. xlsx.SetSheetRow(k3, "A1", &titleList)
  1035. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1036. xlsx.SetSheetRow(k3, "A2", &titleList)
  1037. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1038. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1039. if err != nil {
  1040. logx.Error(err)
  1041. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1042. panic(err)
  1043. }
  1044. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1045. xlsx.SetColWidth(k3, "A", "D", 20)
  1046. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  1047. j := 3 //表头前两行用了,只能从第三行开始
  1048. for k4, v4 := range v3 {
  1049. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  1050. continue
  1051. }
  1052. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1053. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1054. sumBreakfast += v4["breakfast"]
  1055. sumLunch += v4["lunch"]
  1056. sumDinner += v4["dinner"]
  1057. j++
  1058. }
  1059. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1060. "合计", sumBreakfast, sumLunch, sumDinner})
  1061. }
  1062. }
  1063. }
  1064. //将文件保存至服务器
  1065. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1066. err1 := xlsx.SaveAs(tmpDownloadPath)
  1067. if err1 != nil {
  1068. logx.Error(err1)
  1069. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1070. panic(err1)
  1071. }
  1072. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1073. xlsFileName = append(xlsFileName, tmpFileName)
  1074. }
  1075. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1076. if err != nil {
  1077. logx.Error(err)
  1078. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1079. panic(err)
  1080. }
  1081. defer archive.Close()
  1082. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1083. for k, v := range xlsDownloadPath {
  1084. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1085. if err1 != nil {
  1086. logx.Error(err1)
  1087. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1088. panic(err1)
  1089. }
  1090. f, err1 := os.Open(v) //打开待压缩的文件
  1091. if err1 != nil {
  1092. logx.Error(err1)
  1093. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1094. panic(err1)
  1095. }
  1096. if _, err1 = io.Copy(w, f); err != nil {
  1097. logx.Error(err1)
  1098. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1099. panic(err1)
  1100. }
  1101. if err1 != nil {
  1102. logx.Error(err1)
  1103. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1104. panic(err1)
  1105. }
  1106. f.Close()
  1107. os.RemoveAll(v) //TODO::移除源文件
  1108. }
  1109. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1110. if err != nil {
  1111. panic(err)
  1112. }
  1113. break
  1114. case 5:
  1115. //退款明细表
  1116. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1117. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1118. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1119. if req.EnterpriseId != 0 {
  1120. sess.And("enterprise.id =?", req.EnterpriseId)
  1121. }
  1122. if req.StartDate != "" {
  1123. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1124. }
  1125. if req.EndDate != "" {
  1126. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1127. }
  1128. if len(req.MealKindList) > 0 {
  1129. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  1130. }
  1131. if req.EnterPriseKind != 0 {
  1132. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1133. }
  1134. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  1135. _, err := sess.
  1136. 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").
  1137. 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").
  1138. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1139. Join("LEFT", "user", "user_identity.uid = user.id").
  1140. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1141. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1142. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1143. Join("LEFT", "grade", "class.grade_id = grade.id").
  1144. Desc("central_kitchen_for_school_user_refund_day.id").
  1145. FindAndCount(&m)
  1146. if err != nil {
  1147. logx.Error(err)
  1148. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1149. return
  1150. }
  1151. var list = map[string]map[string]map[string]map[string]map[string]int{}
  1152. var xlsFileName, xlsDownloadPath []string
  1153. for _, v := range m {
  1154. if list[v.Enterprise.Name] == nil {
  1155. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1156. }
  1157. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1158. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1159. }
  1160. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1161. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1162. }
  1163. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1164. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1165. }
  1166. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1167. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1168. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1169. }
  1170. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1171. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1172. }
  1173. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1174. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1175. }
  1176. }
  1177. for k1, v1 := range list {
  1178. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1179. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1180. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1181. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1182. for k2, v2 := range v1 {
  1183. if k2 == "" {
  1184. //若年级为空,则为教师餐
  1185. continue
  1186. }
  1187. for k3, v3 := range v2 {
  1188. xlsx.NewSheet(k3)
  1189. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1190. xlsx.SetSheetRow(k3, "A1", &titleList)
  1191. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1192. xlsx.SetSheetRow(k3, "A2", &titleList)
  1193. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1194. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1195. if err != nil {
  1196. logx.Error(err)
  1197. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1198. panic(err)
  1199. }
  1200. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1201. xlsx.SetColWidth(k3, "A", "D", 20)
  1202. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1203. j := 3 //表头前两行用了,只能从第三行开始
  1204. for k4, v4 := range v3 {
  1205. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1206. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1207. sumBreakfast += v4["breakfast"]
  1208. sumLunch += v4["lunch"]
  1209. sumDinner += v4["dinner"]
  1210. j++
  1211. }
  1212. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1213. "合计", sumBreakfast, sumLunch, sumDinner})
  1214. }
  1215. }
  1216. //将文件保存至服务器
  1217. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1218. err1 := xlsx.SaveAs(tmpDownloadPath)
  1219. if err1 != nil {
  1220. logx.Error(err1)
  1221. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1222. panic(err1)
  1223. }
  1224. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1225. xlsFileName = append(xlsFileName, tmpFileName)
  1226. }
  1227. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1228. if err != nil {
  1229. logx.Error(err)
  1230. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1231. panic(err)
  1232. }
  1233. defer archive.Close()
  1234. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1235. for k, v := range xlsDownloadPath {
  1236. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1237. if err1 != nil {
  1238. logx.Error(err1)
  1239. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1240. panic(err1)
  1241. }
  1242. f, err1 := os.Open(v) //打开待压缩的文件
  1243. if err1 != nil {
  1244. logx.Error(err1)
  1245. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1246. panic(err1)
  1247. }
  1248. if _, err1 = io.Copy(w, f); err != nil {
  1249. logx.Error(err1)
  1250. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1251. panic(err1)
  1252. }
  1253. if err1 != nil {
  1254. logx.Error(err1)
  1255. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1256. panic(err1)
  1257. }
  1258. f.Close()
  1259. os.RemoveAll(v) //TODO::移除源文件
  1260. }
  1261. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1262. if err != nil {
  1263. panic(err)
  1264. }
  1265. break
  1266. case 6:
  1267. //收款统计表
  1268. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1269. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1270. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1271. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1272. //查找数据
  1273. var m []model.Enterprise
  1274. sess := db.Db.Desc("id")
  1275. if req.EnterPriseKind != 0 {
  1276. sess.And("kind = ?", req.EnterPriseKind)
  1277. }
  1278. if req.EnterpriseId != 0 {
  1279. sess.And("id = ?", req.EnterpriseId)
  1280. }
  1281. err := sess.Find(&m)
  1282. if err != nil {
  1283. logx.Error(err)
  1284. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1285. return
  1286. }
  1287. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1288. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1289. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1290. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1291. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1292. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1293. if err != nil {
  1294. logx.Error(err)
  1295. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1296. panic(err)
  1297. }
  1298. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1299. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1300. j := 3 //表头前两行用了,只能从第三行开始
  1301. for _, v := range m {
  1302. var income, refund string
  1303. //统计收入
  1304. 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)
  1305. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1306. if err != nil {
  1307. logx.Error(err)
  1308. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1309. return
  1310. }
  1311. if mapArr1 != nil {
  1312. income = mapArr1[0]["total"]
  1313. }
  1314. //统计退款
  1315. //查询出当前学校下所有的学生
  1316. userIdentityDb := db.UserIdentityDb{}
  1317. userIdentityDb.Set(0)
  1318. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1319. var userIdentityIds []string
  1320. for _, v := range *userIdentities {
  1321. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1322. }
  1323. if len(userIdentityIds) != 0 {
  1324. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1325. 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'",
  1326. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1327. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1328. if err != nil {
  1329. logx.Error(err)
  1330. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1331. return
  1332. }
  1333. if mapArr2 != nil {
  1334. refund = mapArr2[0]["total"]
  1335. }
  1336. }
  1337. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1338. rateFee := realityAmount * 0.0038
  1339. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1340. j++
  1341. }
  1342. //将文件保存至服务器
  1343. xlsx.SaveAs("./static" + downloadPath)
  1344. break
  1345. case 7:
  1346. //班级收款明细表
  1347. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1348. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1349. //查找订单数据
  1350. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1351. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1352. if req.EnterpriseId != 0 {
  1353. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1354. }
  1355. if req.StartDate != "" {
  1356. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1357. }
  1358. if req.EndDate != "" {
  1359. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1360. }
  1361. if req.EnterPriseKind != 0 {
  1362. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1363. }
  1364. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1365. _, err := sess.
  1366. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1367. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1368. Join("LEFT", "user", "user.id = user_identity.uid").
  1369. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1370. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1371. Join("LEFT", "grade", "class.grade_id = grade.id").
  1372. FindAndCount(&m)
  1373. if err != nil {
  1374. logx.Error(err)
  1375. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1376. return
  1377. }
  1378. 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")
  1379. if req.EnterpriseId != 0 {
  1380. sess1.And("enterprise.id =?", req.EnterpriseId)
  1381. }
  1382. if req.StartDate != "" {
  1383. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1384. }
  1385. if req.EndDate != "" {
  1386. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1387. }
  1388. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1389. _, err = sess1.
  1390. 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").
  1391. 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").
  1392. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1393. Join("LEFT", "user", "user_identity.uid = user.id").
  1394. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1395. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1396. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1397. Join("LEFT", "grade", "class.grade_id = grade.id").
  1398. Desc("central_kitchen_for_school_user_refund_day.id").
  1399. FindAndCount(&mm)
  1400. if err != nil {
  1401. logx.Error(err)
  1402. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1403. return
  1404. }
  1405. var list = map[string]map[string]map[string]map[string]float64{}
  1406. for _, v := range m {
  1407. if list[v.Enterprise.Name] == nil {
  1408. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1409. }
  1410. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1411. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1412. }
  1413. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1414. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1415. }
  1416. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1417. }
  1418. for _, v := range mm {
  1419. if list[v.Enterprise.Name] == nil {
  1420. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1421. }
  1422. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1423. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1424. }
  1425. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1426. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1427. }
  1428. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1429. }
  1430. var xlsFileName, xlsDownloadPath []string
  1431. for k1, v1 := range list {
  1432. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1433. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1434. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1435. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1436. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1437. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1438. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1439. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1440. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1441. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1442. if err != nil {
  1443. logx.Error(err)
  1444. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1445. panic(err)
  1446. }
  1447. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1448. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1449. j := 3 //表头前两行用了,只能从第三行开始
  1450. var sumClassNum = int64(0)
  1451. var sumIncome, sumRefund = float64(0), float64(0)
  1452. for k2, v2 := range v1 {
  1453. if k2 == "" {
  1454. //若年级为空,则为教师餐
  1455. continue
  1456. }
  1457. for k3, v3 := range v2 {
  1458. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1459. sumClassNum++
  1460. sumIncome += v3["income"]
  1461. sumRefund += v3["refund"]
  1462. j++
  1463. }
  1464. }
  1465. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1466. "合计", sumClassNum, sumIncome, sumRefund})
  1467. //将文件保存至服务器
  1468. err1 := xlsx.SaveAs(tmpDownloadPath)
  1469. if err1 != nil {
  1470. logx.Error(err1)
  1471. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1472. panic(err1)
  1473. }
  1474. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1475. xlsFileName = append(xlsFileName, tmpFileName)
  1476. }
  1477. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1478. if err != nil {
  1479. logx.Error(err)
  1480. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1481. panic(err)
  1482. }
  1483. defer archive.Close()
  1484. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1485. for k, v := range xlsDownloadPath {
  1486. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1487. if err1 != nil {
  1488. logx.Error(err1)
  1489. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1490. panic(err1)
  1491. }
  1492. f, err1 := os.Open(v) //打开待压缩的文件
  1493. if err1 != nil {
  1494. logx.Error(err1)
  1495. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1496. panic(err1)
  1497. }
  1498. if _, err1 = io.Copy(w, f); err != nil {
  1499. logx.Error(err1)
  1500. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1501. panic(err1)
  1502. }
  1503. if err1 != nil {
  1504. logx.Error(err1)
  1505. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1506. panic(err1)
  1507. }
  1508. f.Close()
  1509. os.RemoveAll(v) //TODO::移除源文件
  1510. }
  1511. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1512. if err != nil {
  1513. panic(err)
  1514. }
  1515. break
  1516. case 8:
  1517. //学校预定数量统计表
  1518. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1519. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1520. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1521. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1522. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1523. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1524. if req.EnterpriseId != 0 {
  1525. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1526. }
  1527. var months []time.Month
  1528. if req.StartDate != "" {
  1529. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1530. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1531. _, sMonth, _ := sDate.Date()
  1532. _, eMonth, _ := eDate.Date()
  1533. diffMonth := sMonth - eMonth
  1534. if diffMonth == 0 {
  1535. //TODO::查询一个月
  1536. months = append(months, sMonth)
  1537. } else {
  1538. for i := sMonth; i <= eMonth; i++ {
  1539. months = append(months, i)
  1540. }
  1541. }
  1542. }
  1543. if len(months) > 0 {
  1544. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1545. }
  1546. //if req.EndDate != "" {
  1547. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1548. //}
  1549. if len(req.MealKindList) > 0 {
  1550. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1551. }
  1552. if req.EnterPriseKind != 0 {
  1553. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1554. }
  1555. _, err := sess.
  1556. 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").
  1557. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1558. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1559. FindAndCount(&m)
  1560. if err != nil {
  1561. logx.Error(err)
  1562. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1563. return
  1564. }
  1565. var list = map[string]map[string]int64{}
  1566. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1567. centralKitchenForSchoolUserWithDayDb.Set(0)
  1568. for _, v := range m {
  1569. if list[v.Enterprise.Name] == nil {
  1570. list[v.Enterprise.Name] = map[string]int64{}
  1571. }
  1572. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1573. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1574. req.StartDate, req.EndDate, req.MealKindList)
  1575. if err2 != nil {
  1576. logx.Error(err)
  1577. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1578. return
  1579. }
  1580. for _, value := range req.MealKindList {
  1581. if value == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1582. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1583. if err2 != nil {
  1584. logx.Error(err)
  1585. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1586. return
  1587. }
  1588. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1589. }
  1590. if value == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1591. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForLunch, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1592. if err2 != nil {
  1593. logx.Error(err)
  1594. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1595. return
  1596. }
  1597. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1598. }
  1599. if value == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1600. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayKindForDinner, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1601. if err2 != nil {
  1602. logx.Error(err)
  1603. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1604. return
  1605. }
  1606. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1607. }
  1608. }
  1609. list[v.Enterprise.Name]["count"] += count
  1610. time.Sleep(20 * time.Millisecond) // 20毫秒
  1611. }
  1612. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1613. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1614. titleList = []string{"学校名称", "预定总数",
  1615. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1616. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1617. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1618. }
  1619. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1620. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1621. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1622. if err != nil {
  1623. logx.Error(err)
  1624. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1625. panic(err)
  1626. }
  1627. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1628. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1629. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1630. j := 3 //表头前两行用了,只能从第三行开始
  1631. total := int64(0)
  1632. total1 := int64(0)
  1633. total2 := int64(0)
  1634. total3 := int64(0)
  1635. for k, v := range list {
  1636. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1637. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1638. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1639. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1640. total += v["count"]
  1641. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1642. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1643. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1644. j++
  1645. }
  1646. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1647. "合计", total, total1, total2, total3})
  1648. //将文件保存至服务器
  1649. xlsx.SaveAs("./static" + downloadPath)
  1650. break
  1651. }
  1652. //新增数据
  1653. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1654. centralKitchenForSchoolExportRecordsDb.Set()
  1655. marshal, _ := json.Marshal(req)
  1656. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1657. Name: fileName,
  1658. DownloadPath: downloadPath,
  1659. Kind: kind,
  1660. ReqContent: string(marshal),
  1661. CreateAt: now.Format("2006-01-02 15:04:05"),
  1662. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1663. })
  1664. }
  1665. }
  1666. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1667. var titleList []string
  1668. var fileName, downloadPath string
  1669. var now = time.Now()
  1670. xlsx := excelize.NewFile()
  1671. //数据对比
  1672. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1673. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1674. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1675. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1676. if req.EnterpriseId != 0 {
  1677. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1678. }
  1679. if req.Date1[0] != "" {
  1680. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1681. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  1682. }
  1683. if req.Date1[1] != "" {
  1684. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[1], time.Local)
  1685. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  1686. }
  1687. if len(req.MealKindList) > 0 {
  1688. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1689. }
  1690. if req.EnterPriseKind != 0 {
  1691. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1692. }
  1693. _, err := sess.
  1694. 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").
  1695. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1696. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1697. Join("LEFT", "user", "user.id = user_identity.uid").
  1698. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1699. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1700. Join("LEFT", "grade", "class.grade_id = grade.id").
  1701. FindAndCount(&m)
  1702. if err != nil {
  1703. logx.Error(err)
  1704. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1705. return
  1706. }
  1707. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1708. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1709. if req.EnterpriseId != 0 {
  1710. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1711. }
  1712. if req.Date2[0] != "" {
  1713. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1714. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  1715. }
  1716. if req.Date2[1] != "" {
  1717. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[1], time.Local)
  1718. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  1719. }
  1720. if len(req.MealKindList) > 0 {
  1721. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1722. }
  1723. _, err1 := sess1.
  1724. 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").
  1725. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1726. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1727. Join("LEFT", "user", "user.id = user_identity.uid").
  1728. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1729. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1730. Join("LEFT", "grade", "class.grade_id = grade.id").
  1731. FindAndCount(&m1)
  1732. if err1 != nil {
  1733. logx.Error(err1)
  1734. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1735. return
  1736. }
  1737. var list = map[string]map[string]map[string]map[string]string{}
  1738. for _, v := range m {
  1739. if list[v.Enterprise.Name] == nil {
  1740. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1741. }
  1742. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1743. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1744. }
  1745. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1746. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1747. }
  1748. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1749. }
  1750. var list1 = map[string]map[string]map[string]map[string]string{}
  1751. for _, v := range m1 {
  1752. if list1[v.Enterprise.Name] == nil {
  1753. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1754. }
  1755. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1756. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1757. }
  1758. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1759. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1760. }
  1761. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1762. }
  1763. var xlsFileName, xlsDownloadPath []string
  1764. for k1, v1 := range list {
  1765. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1766. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1767. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1768. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1769. //查询当前学校下的班级升序
  1770. tmpEnterpriseDb := db.EnterpriseDb{}
  1771. tmpEnterpriseDb.Set()
  1772. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1773. if err11 != nil {
  1774. logx.Error(err11)
  1775. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1776. panic(err11)
  1777. }
  1778. tmpClassDb := db.ClassDb{}
  1779. tmpClassDb.Set(0)
  1780. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1781. if err != nil {
  1782. logx.Error(err)
  1783. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1784. panic(err)
  1785. }
  1786. for _, class := range *classList {
  1787. for k2, v2 := range v1 {
  1788. if k2 == "" {
  1789. //若年级为空,则为教师
  1790. continue
  1791. }
  1792. for k3, v3 := range v2 {
  1793. if class.Name != k3 {
  1794. continue
  1795. }
  1796. xlsx.NewSheet(k3)
  1797. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  1798. xlsx.SetSheetRow(k3, "A1", &titleList)
  1799. titleList = []string{"姓名", "家长联系电话"}
  1800. xlsx.SetSheetRow(k3, "A2", &titleList)
  1801. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  1802. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1803. if err != nil {
  1804. logx.Error(err)
  1805. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1806. panic(err)
  1807. }
  1808. xlsx.SetCellStyle(k3, "A1", "B1", style)
  1809. xlsx.SetColWidth(k3, "A", "B", 40)
  1810. j := 2 //表头被第一行用了,只能从第二行开始
  1811. for k4, v4 := range v3 {
  1812. //TODO::判断当前用户是否在新的月份里面
  1813. fmt.Println("11111111111111", list1[k1])
  1814. fmt.Println("22222222222222", list1[k1][k2])
  1815. fmt.Println("33333333333333", list1[k1][k2][k3])
  1816. fmt.Println("44444444444444", list1[k1][k2][k3][k4])
  1817. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1818. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1819. k4, v4})
  1820. j++
  1821. }
  1822. }
  1823. }
  1824. }
  1825. }
  1826. //将文件保存至服务器
  1827. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1828. err1 := xlsx.SaveAs(tmpDownloadPath)
  1829. if err1 != nil {
  1830. logx.Error(err1)
  1831. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1832. panic(err1)
  1833. }
  1834. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1835. xlsFileName = append(xlsFileName, tmpFileName)
  1836. }
  1837. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1838. if err != nil {
  1839. logx.Error(err)
  1840. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1841. panic(err)
  1842. }
  1843. defer archive.Close()
  1844. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1845. for k, v := range xlsDownloadPath {
  1846. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1847. if err1 != nil {
  1848. logx.Error(err1)
  1849. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1850. panic(err1)
  1851. }
  1852. f, err1 := os.Open(v) //打开待压缩的文件
  1853. if err1 != nil {
  1854. logx.Error(err1)
  1855. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1856. panic(err1)
  1857. }
  1858. if _, err1 = io.Copy(w, f); err != nil {
  1859. logx.Error(err1)
  1860. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1861. panic(err1)
  1862. }
  1863. if err1 != nil {
  1864. logx.Error(err1)
  1865. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1866. panic(err1)
  1867. }
  1868. f.Close()
  1869. os.RemoveAll(v) //TODO::移除源文件
  1870. }
  1871. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1872. if err != nil {
  1873. panic(err)
  1874. }
  1875. //新增数据
  1876. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1877. centralKitchenForSchoolExportRecordsDb.Set()
  1878. marshal, _ := json.Marshal(req)
  1879. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1880. Name: fileName,
  1881. DownloadPath: downloadPath,
  1882. Kind: 9, //数据对比
  1883. ReqContent: string(marshal),
  1884. CreateAt: now.Format("2006-01-02 15:04:05"),
  1885. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1886. })
  1887. }
  1888. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  1889. mealZh = "未知"
  1890. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  1891. for _, v := range md.SelfSupportForMealTimeMapping {
  1892. //判断时间大小
  1893. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  1894. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  1895. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  1896. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  1897. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  1898. mealZh = v["name"]
  1899. }
  1900. }
  1901. return
  1902. }