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

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