智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2363 lines
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,
  1005. enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1006. req.StartDate, req.EndDate, req.MealKindList)
  1007. if err2 != nil {
  1008. logx.Error(err)
  1009. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1010. return
  1011. }
  1012. time.Sleep(20 * time.Millisecond) // 20毫秒
  1013. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1014. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  1015. }
  1016. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1017. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  1018. }
  1019. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1020. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  1021. }
  1022. }
  1023. for k1, v1 := range list {
  1024. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1025. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1026. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1027. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1028. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  1029. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1030. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  1031. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1032. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1033. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1034. if err != nil {
  1035. logx.Error(err)
  1036. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1037. panic(err)
  1038. }
  1039. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1040. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1041. j := 3 //表头前两行用了,只能从第三行开始
  1042. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  1043. tmpEnterpriseDb := db.EnterpriseDb{}
  1044. tmpEnterpriseDb.Set()
  1045. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1046. if err11 != nil {
  1047. logx.Error(err11)
  1048. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
  1049. panic(err11)
  1050. }
  1051. tmpClassDb := db.ClassDb{}
  1052. tmpClassDb.Set(0)
  1053. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1054. if err != nil {
  1055. logx.Error(err)
  1056. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1057. panic(err)
  1058. }
  1059. for _, class := range *classList {
  1060. for k2, v2 := range v1 {
  1061. if k2 == "" {
  1062. //若年级为空,则为教师餐
  1063. k2 = "教师餐"
  1064. }
  1065. for k3, v3 := range v2 {
  1066. if class.Name != k3 {
  1067. continue
  1068. }
  1069. if k3 == "" {
  1070. //若班级为空,则为教师餐
  1071. k3 = "教师餐"
  1072. }
  1073. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1074. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  1075. sumClassNum++
  1076. sumBreakfast += v3["breakfast"]
  1077. sumLunch += v3["lunch"]
  1078. sumDinner += v3["dinner"]
  1079. j++
  1080. }
  1081. }
  1082. }
  1083. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1084. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  1085. //将文件保存至服务器
  1086. err1 := xlsx.SaveAs(tmpDownloadPath)
  1087. if err1 != nil {
  1088. logx.Error(err1)
  1089. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1090. panic(err1)
  1091. }
  1092. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1093. xlsFileName = append(xlsFileName, tmpFileName)
  1094. }
  1095. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1096. if err != nil {
  1097. logx.Error(err)
  1098. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1099. panic(err)
  1100. }
  1101. defer archive.Close()
  1102. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1103. for k, v := range xlsDownloadPath {
  1104. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1105. if err1 != nil {
  1106. logx.Error(err1)
  1107. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1108. panic(err1)
  1109. }
  1110. f, err1 := os.Open(v) //打开待压缩的文件
  1111. if err1 != nil {
  1112. logx.Error(err1)
  1113. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1114. panic(err1)
  1115. }
  1116. if _, err1 = io.Copy(w, f); err != nil {
  1117. logx.Error(err1)
  1118. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1119. panic(err1)
  1120. }
  1121. if err1 != nil {
  1122. logx.Error(err1)
  1123. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  1124. panic(err1)
  1125. }
  1126. f.Close()
  1127. os.RemoveAll(v) //TODO::移除源文件
  1128. }
  1129. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1130. if err != nil {
  1131. panic(err)
  1132. }
  1133. break
  1134. case 4:
  1135. //班级明细表
  1136. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1137. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1138. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1139. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1140. if req.EnterpriseId != 0 {
  1141. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1142. }
  1143. var months []time.Month
  1144. if req.StartDate != "" {
  1145. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1146. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1147. _, sMonth, _ := sDate.Date()
  1148. _, eMonth, _ := eDate.Date()
  1149. diffMonth := sMonth - eMonth
  1150. if diffMonth == 0 {
  1151. //TODO::查询一个月
  1152. months = append(months, sMonth)
  1153. } else {
  1154. for i := sMonth; i <= eMonth; i++ {
  1155. months = append(months, i)
  1156. }
  1157. }
  1158. }
  1159. if len(months) > 0 {
  1160. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1161. }
  1162. //if req.EndDate != "" {
  1163. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1164. //}
  1165. if len(req.MealKindList) > 0 {
  1166. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1167. }
  1168. if req.EnterPriseKind != 0 {
  1169. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1170. }
  1171. _, err := sess.
  1172. 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").
  1173. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1174. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1175. Join("LEFT", "user", "user.id = user_identity.uid").
  1176. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1177. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1178. Join("LEFT", "grade", "class.grade_id = grade.id").
  1179. Asc("class.id").
  1180. FindAndCount(&m)
  1181. if err != nil {
  1182. logx.Error(err)
  1183. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1184. return
  1185. }
  1186. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  1187. var xlsFileName, xlsDownloadPath []string
  1188. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1189. centralKitchenForSchoolUserWithDayDb.Set(0)
  1190. for _, v := range m {
  1191. if list[v.Enterprise.Name] == nil {
  1192. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  1193. }
  1194. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1195. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  1196. }
  1197. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1198. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  1199. }
  1200. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1201. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  1202. }
  1203. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1204. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
  1205. enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1206. req.StartDate, req.EndDate, req.MealKindList)
  1207. if err2 != nil {
  1208. logx.Error(err)
  1209. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1210. return
  1211. }
  1212. if count == 0 {
  1213. continue
  1214. }
  1215. time.Sleep(20 * time.Millisecond) // 20毫秒
  1216. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1217. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  1218. }
  1219. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1220. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  1221. }
  1222. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1223. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  1224. }
  1225. }
  1226. for k1, v1 := range list {
  1227. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1228. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1229. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1230. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1231. //查询当前学校下的班级升序
  1232. tmpEnterpriseDb := db.EnterpriseDb{}
  1233. tmpEnterpriseDb.Set()
  1234. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1235. if err11 != nil {
  1236. logx.Error(err11)
  1237. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1238. panic(err11)
  1239. }
  1240. tmpClassDb := db.ClassDb{}
  1241. tmpClassDb.Set(0)
  1242. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1243. if err != nil {
  1244. logx.Error(err)
  1245. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1246. panic(err)
  1247. }
  1248. for _, class := range *classList {
  1249. for k2, v2 := range v1 {
  1250. if k2 == "" {
  1251. //若年级为空,则为教师餐
  1252. continue
  1253. }
  1254. for k3, v3 := range v2 {
  1255. if class.Name != k3 {
  1256. continue
  1257. }
  1258. xlsx.NewSheet(k3)
  1259. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  1260. xlsx.SetSheetRow(k3, "A1", &titleList)
  1261. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1262. xlsx.SetSheetRow(k3, "A2", &titleList)
  1263. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1264. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1265. if err != nil {
  1266. logx.Error(err)
  1267. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1268. panic(err)
  1269. }
  1270. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1271. xlsx.SetColWidth(k3, "A", "D", 20)
  1272. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  1273. j := 3 //表头前两行用了,只能从第三行开始
  1274. for k4, v4 := range v3 {
  1275. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  1276. continue
  1277. }
  1278. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1279. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1280. sumBreakfast += v4["breakfast"]
  1281. sumLunch += v4["lunch"]
  1282. sumDinner += v4["dinner"]
  1283. j++
  1284. }
  1285. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1286. "合计", sumBreakfast, sumLunch, sumDinner})
  1287. }
  1288. }
  1289. }
  1290. //将文件保存至服务器
  1291. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1292. err1 := xlsx.SaveAs(tmpDownloadPath)
  1293. if err1 != nil {
  1294. logx.Error(err1)
  1295. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1296. panic(err1)
  1297. }
  1298. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1299. xlsFileName = append(xlsFileName, tmpFileName)
  1300. }
  1301. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1302. if err != nil {
  1303. logx.Error(err)
  1304. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1305. panic(err)
  1306. }
  1307. defer archive.Close()
  1308. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1309. for k, v := range xlsDownloadPath {
  1310. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1311. if err1 != nil {
  1312. logx.Error(err1)
  1313. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1314. panic(err1)
  1315. }
  1316. f, err1 := os.Open(v) //打开待压缩的文件
  1317. if err1 != nil {
  1318. logx.Error(err1)
  1319. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1320. panic(err1)
  1321. }
  1322. if _, err1 = io.Copy(w, f); err != nil {
  1323. logx.Error(err1)
  1324. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1325. panic(err1)
  1326. }
  1327. if err1 != nil {
  1328. logx.Error(err1)
  1329. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1330. panic(err1)
  1331. }
  1332. f.Close()
  1333. os.RemoveAll(v) //TODO::移除源文件
  1334. }
  1335. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1336. if err != nil {
  1337. panic(err)
  1338. }
  1339. break
  1340. case 5:
  1341. //退款明细表
  1342. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1343. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1344. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1345. if req.EnterpriseId != 0 {
  1346. sess.And("enterprise.id =?", req.EnterpriseId)
  1347. }
  1348. if req.StartDate != "" {
  1349. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1350. }
  1351. if req.EndDate != "" {
  1352. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1353. }
  1354. if len(req.MealKindList) > 0 {
  1355. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  1356. }
  1357. if req.EnterPriseKind != 0 {
  1358. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1359. }
  1360. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  1361. _, err := sess.
  1362. 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").
  1363. 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").
  1364. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1365. Join("LEFT", "user", "user_identity.uid = user.id").
  1366. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1367. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1368. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1369. Join("LEFT", "grade", "class.grade_id = grade.id").
  1370. Desc("central_kitchen_for_school_user_refund_day.id").
  1371. FindAndCount(&m)
  1372. if err != nil {
  1373. logx.Error(err)
  1374. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1375. return
  1376. }
  1377. var list = map[string]map[string]map[string]map[string]map[string]int{}
  1378. var xlsFileName, xlsDownloadPath []string
  1379. for _, v := range m {
  1380. if list[v.Enterprise.Name] == nil {
  1381. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1382. }
  1383. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1384. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1385. }
  1386. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1387. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1388. }
  1389. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1390. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1391. }
  1392. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1393. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1394. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1395. }
  1396. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1397. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1398. }
  1399. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1400. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1401. }
  1402. }
  1403. for k1, v1 := range list {
  1404. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1405. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1406. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1407. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1408. for k2, v2 := range v1 {
  1409. if k2 == "" {
  1410. //若年级为空,则为教师餐
  1411. continue
  1412. }
  1413. for k3, v3 := range v2 {
  1414. xlsx.NewSheet(k3)
  1415. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1416. xlsx.SetSheetRow(k3, "A1", &titleList)
  1417. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1418. xlsx.SetSheetRow(k3, "A2", &titleList)
  1419. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1420. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1421. if err != nil {
  1422. logx.Error(err)
  1423. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1424. panic(err)
  1425. }
  1426. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1427. xlsx.SetColWidth(k3, "A", "D", 20)
  1428. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1429. j := 3 //表头前两行用了,只能从第三行开始
  1430. for k4, v4 := range v3 {
  1431. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1432. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1433. sumBreakfast += v4["breakfast"]
  1434. sumLunch += v4["lunch"]
  1435. sumDinner += v4["dinner"]
  1436. j++
  1437. }
  1438. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1439. "合计", sumBreakfast, sumLunch, sumDinner})
  1440. }
  1441. }
  1442. //将文件保存至服务器
  1443. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1444. err1 := xlsx.SaveAs(tmpDownloadPath)
  1445. if err1 != nil {
  1446. logx.Error(err1)
  1447. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1448. panic(err1)
  1449. }
  1450. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1451. xlsFileName = append(xlsFileName, tmpFileName)
  1452. }
  1453. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1454. if err != nil {
  1455. logx.Error(err)
  1456. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1457. panic(err)
  1458. }
  1459. defer archive.Close()
  1460. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1461. for k, v := range xlsDownloadPath {
  1462. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1463. if err1 != nil {
  1464. logx.Error(err1)
  1465. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1466. panic(err1)
  1467. }
  1468. f, err1 := os.Open(v) //打开待压缩的文件
  1469. if err1 != nil {
  1470. logx.Error(err1)
  1471. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1472. panic(err1)
  1473. }
  1474. if _, err1 = io.Copy(w, f); err != nil {
  1475. logx.Error(err1)
  1476. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1477. panic(err1)
  1478. }
  1479. if err1 != nil {
  1480. logx.Error(err1)
  1481. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1482. panic(err1)
  1483. }
  1484. f.Close()
  1485. os.RemoveAll(v) //TODO::移除源文件
  1486. }
  1487. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1488. if err != nil {
  1489. panic(err)
  1490. }
  1491. break
  1492. case 6:
  1493. //收款统计表
  1494. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1495. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1496. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1497. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1498. //查找数据
  1499. var m []model.Enterprise
  1500. sess := db.Db.Desc("id")
  1501. if req.EnterPriseKind != 0 {
  1502. sess.And("kind = ?", req.EnterPriseKind)
  1503. }
  1504. if req.EnterpriseId != 0 {
  1505. sess.And("id = ?", req.EnterpriseId)
  1506. }
  1507. err := sess.Find(&m)
  1508. if err != nil {
  1509. logx.Error(err)
  1510. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1511. return
  1512. }
  1513. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1514. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1515. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1516. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1517. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1518. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1519. if err != nil {
  1520. logx.Error(err)
  1521. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1522. panic(err)
  1523. }
  1524. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1525. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1526. j := 3 //表头前两行用了,只能从第三行开始
  1527. for _, v := range m {
  1528. var income, refund string
  1529. //统计收入
  1530. 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)
  1531. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1532. if err != nil {
  1533. logx.Error(err)
  1534. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1535. return
  1536. }
  1537. if mapArr1 != nil {
  1538. income = mapArr1[0]["total"]
  1539. }
  1540. //统计退款
  1541. //查询出当前学校下所有的学生
  1542. userIdentityDb := db.UserIdentityDb{}
  1543. userIdentityDb.Set(0)
  1544. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1545. var userIdentityIds []string
  1546. for _, v := range *userIdentities {
  1547. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1548. }
  1549. if len(userIdentityIds) != 0 {
  1550. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1551. 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'",
  1552. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1553. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1554. if err != nil {
  1555. logx.Error(err)
  1556. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1557. return
  1558. }
  1559. if mapArr2 != nil {
  1560. refund = mapArr2[0]["total"]
  1561. }
  1562. }
  1563. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1564. rateFee := realityAmount * 0.0038
  1565. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1566. j++
  1567. }
  1568. //将文件保存至服务器
  1569. xlsx.SaveAs("./static" + downloadPath)
  1570. break
  1571. case 7:
  1572. //班级收款明细表
  1573. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1574. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1575. //查找订单数据
  1576. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1577. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1578. if req.EnterpriseId != 0 {
  1579. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1580. }
  1581. if req.StartDate != "" {
  1582. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1583. }
  1584. if req.EndDate != "" {
  1585. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1586. }
  1587. if req.EnterPriseKind != 0 {
  1588. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1589. }
  1590. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1591. _, err := sess.
  1592. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1593. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1594. Join("LEFT", "user", "user.id = user_identity.uid").
  1595. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1596. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1597. Join("LEFT", "grade", "class.grade_id = grade.id").
  1598. FindAndCount(&m)
  1599. if err != nil {
  1600. logx.Error(err)
  1601. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1602. return
  1603. }
  1604. 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")
  1605. if req.EnterpriseId != 0 {
  1606. sess1.And("enterprise.id =?", req.EnterpriseId)
  1607. }
  1608. if req.StartDate != "" {
  1609. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1610. }
  1611. if req.EndDate != "" {
  1612. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1613. }
  1614. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1615. _, err = sess1.
  1616. 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").
  1617. 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").
  1618. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1619. Join("LEFT", "user", "user_identity.uid = user.id").
  1620. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1621. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1622. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1623. Join("LEFT", "grade", "class.grade_id = grade.id").
  1624. Desc("central_kitchen_for_school_user_refund_day.id").
  1625. FindAndCount(&mm)
  1626. if err != nil {
  1627. logx.Error(err)
  1628. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1629. return
  1630. }
  1631. var list = map[string]map[string]map[string]map[string]float64{}
  1632. for _, v := range m {
  1633. if list[v.Enterprise.Name] == nil {
  1634. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1635. }
  1636. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1637. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1638. }
  1639. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1640. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1641. }
  1642. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1643. }
  1644. for _, v := range mm {
  1645. if list[v.Enterprise.Name] == nil {
  1646. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1647. }
  1648. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1649. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1650. }
  1651. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1652. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1653. }
  1654. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1655. }
  1656. var xlsFileName, xlsDownloadPath []string
  1657. for k1, v1 := range list {
  1658. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1659. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1660. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1661. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1662. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1663. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1664. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1665. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1666. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1667. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1668. if err != nil {
  1669. logx.Error(err)
  1670. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1671. panic(err)
  1672. }
  1673. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1674. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1675. j := 3 //表头前两行用了,只能从第三行开始
  1676. var sumClassNum = int64(0)
  1677. var sumIncome, sumRefund = float64(0), float64(0)
  1678. for k2, v2 := range v1 {
  1679. if k2 == "" {
  1680. //若年级为空,则为教师餐
  1681. continue
  1682. }
  1683. for k3, v3 := range v2 {
  1684. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1685. sumClassNum++
  1686. sumIncome += v3["income"]
  1687. sumRefund += v3["refund"]
  1688. j++
  1689. }
  1690. }
  1691. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1692. "合计", sumClassNum, sumIncome, sumRefund})
  1693. //将文件保存至服务器
  1694. err1 := xlsx.SaveAs(tmpDownloadPath)
  1695. if err1 != nil {
  1696. logx.Error(err1)
  1697. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1698. panic(err1)
  1699. }
  1700. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1701. xlsFileName = append(xlsFileName, tmpFileName)
  1702. }
  1703. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1704. if err != nil {
  1705. logx.Error(err)
  1706. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1707. panic(err)
  1708. }
  1709. defer archive.Close()
  1710. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1711. for k, v := range xlsDownloadPath {
  1712. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1713. if err1 != nil {
  1714. logx.Error(err1)
  1715. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1716. panic(err1)
  1717. }
  1718. f, err1 := os.Open(v) //打开待压缩的文件
  1719. if err1 != nil {
  1720. logx.Error(err1)
  1721. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1722. panic(err1)
  1723. }
  1724. if _, err1 = io.Copy(w, f); err != nil {
  1725. logx.Error(err1)
  1726. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1727. panic(err1)
  1728. }
  1729. if err1 != nil {
  1730. logx.Error(err1)
  1731. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1732. panic(err1)
  1733. }
  1734. f.Close()
  1735. os.RemoveAll(v) //TODO::移除源文件
  1736. }
  1737. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1738. if err != nil {
  1739. panic(err)
  1740. }
  1741. break
  1742. case 8:
  1743. //学校预定数量统计表
  1744. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1745. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1746. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1747. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1748. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1749. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1750. if req.EnterpriseId != 0 {
  1751. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1752. }
  1753. var months []time.Month
  1754. if req.StartDate != "" {
  1755. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1756. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1757. _, sMonth, _ := sDate.Date()
  1758. _, eMonth, _ := eDate.Date()
  1759. diffMonth := sMonth - eMonth
  1760. if diffMonth == 0 {
  1761. //TODO::查询一个月
  1762. months = append(months, sMonth)
  1763. } else {
  1764. for i := sMonth; i <= eMonth; i++ {
  1765. months = append(months, i)
  1766. }
  1767. }
  1768. }
  1769. if len(months) > 0 {
  1770. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1771. }
  1772. //if req.EndDate != "" {
  1773. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1774. //}
  1775. if len(req.MealKindList) > 0 {
  1776. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1777. }
  1778. if req.EnterPriseKind != 0 {
  1779. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1780. }
  1781. _, err := sess.
  1782. 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").
  1783. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1784. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1785. FindAndCount(&m)
  1786. if err != nil {
  1787. logx.Error(err)
  1788. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1789. return
  1790. }
  1791. var list = map[string]map[string]int64{}
  1792. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1793. centralKitchenForSchoolUserWithDayDb.Set(0)
  1794. for _, v := range m {
  1795. if list[v.Enterprise.Name] == nil {
  1796. list[v.Enterprise.Name] = map[string]int64{}
  1797. }
  1798. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1799. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo,
  1800. enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1801. req.StartDate, req.EndDate, req.MealKindList)
  1802. if err2 != nil {
  1803. logx.Error(err)
  1804. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1805. return
  1806. }
  1807. for _, value := range req.MealKindList {
  1808. if value == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1809. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate,
  1810. req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast,
  1811. enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1812. enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1813. )
  1814. if err2 != nil {
  1815. logx.Error(err)
  1816. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1817. return
  1818. }
  1819. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1820. }
  1821. if value == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1822. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate,
  1823. enum.CentralKitchenForSchoolUserWithDayKindForLunch,
  1824. enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1825. enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1826. )
  1827. if err2 != nil {
  1828. logx.Error(err)
  1829. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1830. return
  1831. }
  1832. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1833. }
  1834. if value == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1835. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate,
  1836. enum.CentralKitchenForSchoolUserWithDayKindForDinner,
  1837. enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1838. enum.CentralKitchenForSchoolUserWithDayStateForCanceling,
  1839. )
  1840. if err2 != nil {
  1841. logx.Error(err)
  1842. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1843. return
  1844. }
  1845. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1846. }
  1847. }
  1848. list[v.Enterprise.Name]["count"] += count
  1849. time.Sleep(20 * time.Millisecond) // 20毫秒
  1850. }
  1851. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1852. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1853. titleList = []string{"学校名称", "预定总数",
  1854. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1855. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1856. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1857. }
  1858. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1859. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1860. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1861. if err != nil {
  1862. logx.Error(err)
  1863. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1864. panic(err)
  1865. }
  1866. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1867. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1868. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1869. j := 3 //表头前两行用了,只能从第三行开始
  1870. total := int64(0)
  1871. total1 := int64(0)
  1872. total2 := int64(0)
  1873. total3 := int64(0)
  1874. for k, v := range list {
  1875. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1876. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1877. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1878. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1879. total += v["count"]
  1880. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1881. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1882. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1883. j++
  1884. }
  1885. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1886. "合计", total, total1, total2, total3})
  1887. //将文件保存至服务器
  1888. xlsx.SaveAs("./static" + downloadPath)
  1889. break
  1890. case 10:
  1891. //就餐数据统计表
  1892. sTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1893. eTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1894. xls, _ := excelize.OpenFile("./static/template/央厨学校(就餐数据统计表).xlsx")
  1895. fileName = "【" + sTime.Format("2006-01-02") + "~" + eTime.Format("2006-01-02") + "】就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1896. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1897. var list = map[string]map[string]map[string]float64{}
  1898. var dates []string
  1899. if req.StartDate != "" {
  1900. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1901. centralKitchenForSchoolUserWithDayDb.Set(0)
  1902. diffDays := int(eTime.Sub(sTime).Hours() / 24)
  1903. for i := 0; i <= diffDays; i++ {
  1904. date := sTime.AddDate(0, 0, i)
  1905. dateName := date.Format("2006-01-02")
  1906. dates = append(dates, dateName)
  1907. for _, vv := range req.MealKindList {
  1908. tmpSum, err := centralKitchenForSchoolUserWithDayDb.SumCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling)
  1909. if err != nil {
  1910. logx.Error(err)
  1911. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  1912. panic(err)
  1913. }
  1914. tmpCount, err := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling)
  1915. if err != nil {
  1916. logx.Error(err)
  1917. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  1918. panic(err)
  1919. }
  1920. if list[dateName] == nil {
  1921. list[dateName] = map[string]map[string]float64{}
  1922. }
  1923. if list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] == nil {
  1924. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] = map[string]float64{}
  1925. }
  1926. if list[dateName]["合计"] == nil {
  1927. list[dateName]["合计"] = make(map[string]float64)
  1928. }
  1929. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["sum"] = tmpSum
  1930. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["count"] = float64(tmpCount)
  1931. list[dateName]["合计"]["sum"] += tmpSum
  1932. list[dateName]["合计"]["count"] += float64(tmpCount)
  1933. }
  1934. }
  1935. }
  1936. //xls.SetSheetRow("", "A1", &titleList)
  1937. //sheetIndex := 2 //第一个表是模板表
  1938. //xls.NewSheet("sheet1") //1、新建表格
  1939. //xls.CopySheet(1, sheetIndex)
  1940. j := 4 //表头前三"早餐"行被占用了,只能从第四行开始
  1941. var totalMap = map[string]float64{}
  1942. for _, date := range dates {
  1943. for kk, v := range list {
  1944. if date == kk {
  1945. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{kk,
  1946. v["早餐"]["count"], v["早餐"]["sum"],
  1947. v["午餐"]["count"], v["午餐"]["sum"],
  1948. v["晚餐"]["count"], v["晚餐"]["sum"],
  1949. v["合计"]["count"], v["合计"]["sum"],
  1950. })
  1951. totalMap["breakfast_count"] += v["早餐"]["count"]
  1952. totalMap["breakfast_sum"] += v["早餐"]["sum"]
  1953. totalMap["lunch_count"] += v["午餐"]["count"]
  1954. totalMap["lunch_sum"] += v["午餐"]["sum"]
  1955. totalMap["dinner_count"] += v["晚餐"]["count"]
  1956. totalMap["dinner_sum"] += v["晚餐"]["sum"]
  1957. totalMap["total_count"] += v["合计"]["count"]
  1958. totalMap["total_sum"] += v["合计"]["sum"]
  1959. j++
  1960. }
  1961. }
  1962. }
  1963. //xls.DeleteSheet("template") //删除模板表
  1964. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{"总计",
  1965. totalMap["breakfast_count"], totalMap["breakfast_sum"],
  1966. totalMap["lunch_count"], totalMap["lunch_sum"],
  1967. totalMap["dinner_count"], totalMap["dinner_sum"],
  1968. totalMap["total_count"], totalMap["total_sum"],
  1969. })
  1970. //将文件保存至服务器
  1971. xls.SaveAs("./static" + downloadPath)
  1972. break
  1973. }
  1974. //新增数据
  1975. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1976. centralKitchenForSchoolExportRecordsDb.Set()
  1977. marshal, _ := json.Marshal(req)
  1978. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1979. Name: fileName,
  1980. DownloadPath: downloadPath,
  1981. Kind: kind,
  1982. ReqContent: string(marshal),
  1983. CreateAt: now.Format("2006-01-02 15:04:05"),
  1984. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1985. })
  1986. }
  1987. }
  1988. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1989. var titleList []string
  1990. var fileName, downloadPath string
  1991. var now = time.Now()
  1992. xlsx := excelize.NewFile()
  1993. //数据对比
  1994. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1995. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1996. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1997. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1998. if req.EnterpriseId != 0 {
  1999. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  2000. }
  2001. if req.Date1[0] != "" {
  2002. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  2003. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  2004. }
  2005. if req.Date1[1] != "" {
  2006. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[1], time.Local)
  2007. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  2008. }
  2009. if len(req.MealKindList) > 0 {
  2010. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  2011. }
  2012. if req.EnterPriseKind != 0 {
  2013. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  2014. }
  2015. _, err := sess.
  2016. 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").
  2017. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  2018. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  2019. Join("LEFT", "user", "user.id = user_identity.uid").
  2020. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  2021. Join("LEFT", "class", "class_with_user.class_id = class.id").
  2022. Join("LEFT", "grade", "class.grade_id = grade.id").
  2023. FindAndCount(&m)
  2024. if err != nil {
  2025. logx.Error(err)
  2026. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2027. return
  2028. }
  2029. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  2030. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  2031. if req.EnterpriseId != 0 {
  2032. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  2033. }
  2034. if req.Date2[0] != "" {
  2035. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  2036. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  2037. }
  2038. if req.Date2[1] != "" {
  2039. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[1], time.Local)
  2040. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  2041. }
  2042. if len(req.MealKindList) > 0 {
  2043. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  2044. }
  2045. _, err1 := sess1.
  2046. 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").
  2047. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  2048. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  2049. Join("LEFT", "user", "user.id = user_identity.uid").
  2050. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  2051. Join("LEFT", "class", "class_with_user.class_id = class.id").
  2052. Join("LEFT", "grade", "class.grade_id = grade.id").
  2053. FindAndCount(&m1)
  2054. if err1 != nil {
  2055. logx.Error(err1)
  2056. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2057. return
  2058. }
  2059. var list = map[string]map[string]map[string]map[string]string{}
  2060. for _, v := range m {
  2061. if list[v.Enterprise.Name] == nil {
  2062. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  2063. }
  2064. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  2065. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  2066. }
  2067. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  2068. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  2069. }
  2070. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  2071. }
  2072. var list1 = map[string]map[string]map[string]map[string]string{}
  2073. for _, v := range m1 {
  2074. if list1[v.Enterprise.Name] == nil {
  2075. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  2076. }
  2077. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  2078. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  2079. }
  2080. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  2081. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  2082. }
  2083. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  2084. }
  2085. var xlsFileName, xlsDownloadPath []string
  2086. for k1, v1 := range list {
  2087. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  2088. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  2089. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  2090. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  2091. //查询当前学校下的班级升序
  2092. tmpEnterpriseDb := db.EnterpriseDb{}
  2093. tmpEnterpriseDb.Set()
  2094. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  2095. if err11 != nil {
  2096. logx.Error(err11)
  2097. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  2098. panic(err11)
  2099. }
  2100. tmpClassDb := db.ClassDb{}
  2101. tmpClassDb.Set(0)
  2102. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  2103. if err != nil {
  2104. logx.Error(err)
  2105. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  2106. panic(err)
  2107. }
  2108. for _, class := range *classList {
  2109. for k2, v2 := range v1 {
  2110. if k2 == "" {
  2111. //若年级为空,则为教师
  2112. continue
  2113. }
  2114. for k3, v3 := range v2 {
  2115. if class.Name != k3 {
  2116. continue
  2117. }
  2118. xlsx.NewSheet(k3)
  2119. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  2120. xlsx.SetSheetRow(k3, "A1", &titleList)
  2121. titleList = []string{"姓名", "家长联系电话"}
  2122. xlsx.SetSheetRow(k3, "A2", &titleList)
  2123. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  2124. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  2125. if err != nil {
  2126. logx.Error(err)
  2127. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  2128. panic(err)
  2129. }
  2130. xlsx.SetCellStyle(k3, "A1", "B1", style)
  2131. xlsx.SetColWidth(k3, "A", "B", 40)
  2132. j := 2 //表头被第一行用了,只能从第二行开始
  2133. for k4, v4 := range v3 {
  2134. //TODO::判断当前用户是否在新的月份里面
  2135. fmt.Println("11111111111111", list1[k1])
  2136. fmt.Println("22222222222222", list1[k1][k2])
  2137. fmt.Println("33333333333333", list1[k1][k2][k3])
  2138. fmt.Println("44444444444444", list1[k1][k2][k3][k4])
  2139. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  2140. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  2141. k4, v4})
  2142. j++
  2143. }
  2144. }
  2145. }
  2146. }
  2147. }
  2148. //将文件保存至服务器
  2149. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  2150. err1 := xlsx.SaveAs(tmpDownloadPath)
  2151. if err1 != nil {
  2152. logx.Error(err1)
  2153. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2154. panic(err1)
  2155. }
  2156. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  2157. xlsFileName = append(xlsFileName, tmpFileName)
  2158. }
  2159. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  2160. if err != nil {
  2161. logx.Error(err)
  2162. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  2163. panic(err)
  2164. }
  2165. defer archive.Close()
  2166. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  2167. for k, v := range xlsDownloadPath {
  2168. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  2169. if err1 != nil {
  2170. logx.Error(err1)
  2171. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2172. panic(err1)
  2173. }
  2174. f, err1 := os.Open(v) //打开待压缩的文件
  2175. if err1 != nil {
  2176. logx.Error(err1)
  2177. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2178. panic(err1)
  2179. }
  2180. if _, err1 = io.Copy(w, f); err != nil {
  2181. logx.Error(err1)
  2182. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2183. panic(err1)
  2184. }
  2185. if err1 != nil {
  2186. logx.Error(err1)
  2187. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  2188. panic(err1)
  2189. }
  2190. f.Close()
  2191. os.RemoveAll(v) //TODO::移除源文件
  2192. }
  2193. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  2194. if err != nil {
  2195. panic(err)
  2196. }
  2197. //新增数据
  2198. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  2199. centralKitchenForSchoolExportRecordsDb.Set()
  2200. marshal, _ := json.Marshal(req)
  2201. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  2202. Name: fileName,
  2203. DownloadPath: downloadPath,
  2204. Kind: 9, //数据对比
  2205. ReqContent: string(marshal),
  2206. CreateAt: now.Format("2006-01-02 15:04:05"),
  2207. UpdateAt: now.Format("2006-01-02 15:04:05"),
  2208. })
  2209. }
  2210. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  2211. mealZh = "未知"
  2212. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  2213. for _, v := range md.SelfSupportForMealTimeMapping {
  2214. //判断时间大小
  2215. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  2216. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  2217. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  2218. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  2219. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  2220. mealZh = v["name"]
  2221. }
  2222. }
  2223. return
  2224. }