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

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