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

2140 lines
91 KiB

  1. package svc
  2. import (
  3. "applet/app/admin/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/enum"
  7. "applet/app/utils"
  8. "applet/app/utils/logx"
  9. "archive/zip"
  10. "encoding/json"
  11. "fmt"
  12. "github.com/360EntSecGroup-Skylar/excelize"
  13. "github.com/syyongx/php2go"
  14. "io"
  15. "os"
  16. "strconv"
  17. "time"
  18. )
  19. func NursingHomeDataStatisticsExport(req md.NursingHomeDataStatisticsExportReq) {
  20. var titleList []string
  21. var fileName string
  22. var now = time.Now()
  23. xlsx := excelize.NewFile()
  24. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  25. switch req.Kind {
  26. case 1:
  27. //订单列表
  28. fileName = "/export/nursing_home/订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  29. //查找订单数据
  30. sess := db.Db.Desc("nursing_home_package_ord.id")
  31. if req.EnterpriseId != 0 {
  32. sess.And("nursing_home_package_ord.enterprise_id =?", req.EnterpriseId)
  33. }
  34. if req.StartDate != "" {
  35. sess.And("nursing_home_package_ord.create_at >= ?", req.StartDate)
  36. }
  37. if req.EndDate != "" {
  38. sess.And("nursing_home_package_ord.create_at <= ?", req.EndDate)
  39. }
  40. var m []*db.NursingHomePackageOrdWithUserIdentity
  41. _, err := sess.
  42. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  43. Join("LEFT", "enterprise", "enterprise.id = nursing_home_package_ord.enterprise_id").
  44. Join("LEFT", "user", "user.id = nursing_home_package_ord.uid").
  45. FindAndCount(&m)
  46. if err != nil {
  47. logx.Error(err)
  48. println("<<<<NursingHomeDataStatisticsExport1>>>>>Error:::", err.Error())
  49. return
  50. }
  51. titleList = []string{"单位名称", "用户身份名称", "用户手机号", "购买类型", "总价", "商户订单号", "支付状态", "购买时间"}
  52. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  53. j := 2 //表头被第一行用了,只能从第二行开始
  54. for _, v := range m {
  55. var kind string //1:按年购买 2:按月购买 3:按天购买 4:补餐
  56. switch v.NursingHomePackageOrd.Kind {
  57. case 1:
  58. kind = "按年购买"
  59. break
  60. case 2:
  61. kind = "按月购买"
  62. break
  63. case 3:
  64. kind = "按天购买"
  65. break
  66. case 4:
  67. kind = "补餐"
  68. break
  69. }
  70. var state string //0:待支付 1:支付成功 2:支付失败
  71. switch v.NursingHomePackageOrd.State {
  72. case enum.NursingHomePackageOrdStateForWait:
  73. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  74. break
  75. case enum.NursingHomePackageOrdStateForSuccess:
  76. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForWait)
  77. break
  78. case enum.NursingHomePackageOrdStateForFail:
  79. state = enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdStateForFail)
  80. break
  81. }
  82. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomePackageOrd.TotalPrice,
  83. v.NursingHomePackageOrd.OutTradeNo, state, v.NursingHomePackageOrd.CreateAt})
  84. j++
  85. }
  86. break
  87. case 2:
  88. //退款申请表
  89. fileName = "/export/nursing_home/退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  90. var m []*db.NursingHomeUserRefundDayWithData
  91. sess := db.Db.Desc("nursing_home_user_refund_day.id")
  92. if req.EnterpriseId != 0 {
  93. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  94. }
  95. if req.StartDate != "" {
  96. sess.And("nursing_home_user_refund_day.create_at >= ?", req.StartDate)
  97. }
  98. if req.EndDate != "" {
  99. sess.And("nursing_home_user_refund_day.create_at <= ?", req.EndDate)
  100. }
  101. _, err := sess.
  102. Join("LEFT", "nursing_home_user_with_day", "nursing_home_user_refund_day.records_id = nursing_home_user_with_day.id").
  103. Join("LEFT", "nursing_home_package_ord", "nursing_home_user_refund_day.out_trade_no = nursing_home_package_ord.out_trade_no").
  104. Join("LEFT", "user_identity", "nursing_home_user_with_day.identity_id = user_identity.id").
  105. Join("LEFT", "user", "user_identity.uid = user.id").
  106. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  107. Desc("nursing_home_user_refund_day.id").
  108. FindAndCount(&m)
  109. if err != nil {
  110. logx.Error(err)
  111. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  112. return
  113. }
  114. titleList = []string{"退款单号", "所属订单号", "单位名称", "用户身份名称", "用户手机号", "就餐类型", "就餐时间", "金额", "状态", "退款时间"}
  115. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  116. j := 2 //表头被第一行用了,只能从第二行开始
  117. for _, v := range m {
  118. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  119. switch v.NursingHomeUserWithDay.Kind {
  120. case enum.NursingHomeUserWithDayKindForBreakfast:
  121. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  122. break
  123. case enum.NursingHomeUserWithDayKindForLunch:
  124. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  125. break
  126. case enum.NursingHomeUserWithDayKindForDinner:
  127. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  128. break
  129. }
  130. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  131. switch v.NursingHomeUserRefundDay.State {
  132. case enum.NursingHomeUserRefundDayStateForAuditing:
  133. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditing)
  134. break
  135. case enum.NursingHomeUserRefundDayStateForAuditPass:
  136. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditPass)
  137. break
  138. case enum.NursingHomeUserRefundDayStateForAuditReject:
  139. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditReject)
  140. break
  141. case enum.NursingHomeUserRefundDayStateForAuditComplete:
  142. state = enum.NursingHomeUserRefundDayState.String(enum.NursingHomeUserRefundDayStateForAuditComplete)
  143. break
  144. }
  145. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomeUserRefundDay.OutRequestNo, v.NursingHomeUserRefundDay.OutTradeNo, v.Enterprise.Name,
  146. v.NursingHomePackageOrd.UserIdentityName, v.User.Phone, kind, v.NursingHomeUserWithDay.Date, v.NursingHomeUserRefundDay.Amount, state, v.NursingHomeUserRefundDay.RefundDate})
  147. j++
  148. }
  149. xlsx.SaveAs("./static" + fileName + ".xlsx")
  150. break
  151. case 3:
  152. //预定统计表
  153. fileName = "/export/nursing_home/预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  154. var m []*db.NursingHomePackageOrdForReserveWithUserIdentity
  155. sess := db.Db.Desc("nursing_home_package_ord_for_reserve.id")
  156. if req.EnterpriseId != 0 {
  157. sess.And("enterprise.enterprise_id =?", req.EnterpriseId)
  158. }
  159. if req.StartDate != "" {
  160. sess.And("nursing_home_package_ord_for_reserve.meal_time_start >= ?", req.StartDate)
  161. }
  162. if req.EndDate != "" {
  163. sess.And("nursing_home_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  164. }
  165. _, err := sess.
  166. Join("LEFT", "nursing_home_package_ord", "nursing_home_package_ord_for_reserve.out_trade_no = nursing_home_package_ord.out_trade_no").
  167. Join("LEFT", "user_identity", "nursing_home_package_ord.user_identity_id = user_identity.id").
  168. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  169. Join("LEFT", "user", "user.id = user_identity.uid").FindAndCount(&m)
  170. if err != nil {
  171. logx.Error(err)
  172. println("<<<<NursingHomeDataStatisticsExport2>>>>>Error:::", err.Error())
  173. return
  174. }
  175. titleList = []string{"所属订单号", "单位名称", "用户身份名称", "用户手机号", "预定类型", "预定月份", "就餐时间-开始", "就餐时间-截止"}
  176. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  177. j := 2 //表头被第一行用了,只能从第二行开始
  178. for _, v := range m {
  179. var kind string //预定类型(1:早餐 2:午餐 3:晚餐)
  180. switch v.NursingHomePackageOrdForReserve.Kind {
  181. case enum.NursingHomeUserWithDayKindForBreakfast:
  182. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForBreakfast)
  183. break
  184. case enum.NursingHomeUserWithDayKindForLunch:
  185. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForLunch)
  186. break
  187. case enum.NursingHomeUserWithDayKindForDinner:
  188. kind = enum.NursingHomeUserWithDayKind.String(enum.NursingHomeUserWithDayKindForDinner)
  189. break
  190. }
  191. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.NursingHomePackageOrdForReserve.OutTradeNo, v.Enterprise.Name, v.NursingHomePackageOrd.UserIdentityName,
  192. v.User.Phone, kind, v.NursingHomePackageOrdForReserve.ReserveMonth, v.NursingHomePackageOrdForReserve.MealTimeStart, v.NursingHomePackageOrdForReserve.MealTimeEnd})
  193. j++
  194. }
  195. xlsx.SaveAs("./static" + fileName + ".xlsx")
  196. break
  197. }
  198. //新增数据
  199. nursingHomeExportRecordsDb := db.NursingHomeExportRecordsDb{}
  200. nursingHomeExportRecordsDb.Set()
  201. marshal, _ := json.Marshal(req)
  202. nursingHomeExportRecordsDb.NursingHomeExportRecordsInsert(&model.NursingHomeExportRecords{
  203. Name: fileName,
  204. DownloadPath: fileName,
  205. Kind: req.Kind,
  206. ReqContent: string(marshal),
  207. CreateAt: now.Format("2006-01-02 15:04:05"),
  208. UpdateAt: now.Format("2006-01-02 15:04:05"),
  209. })
  210. }
  211. func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataStatisticsExportReq) {
  212. var titleList []string
  213. var fileName, downloadPath string
  214. var now = time.Now()
  215. switch req.Kind {
  216. case 1:
  217. //教师消费统计表
  218. xlsx, _ := excelize.OpenFile("./static/template/自营学校(教师消费统计表).xlsx")
  219. fileName = "教师消费统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  220. downloadPath = "/export/self_support_for_school/" + fileName
  221. //查询数据
  222. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  223. sess := db.Db.Desc("self_support_for_school_ord.id")
  224. if req.EnterpriseId != 0 {
  225. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  226. }
  227. if req.StartDate != "" {
  228. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  229. }
  230. if req.EndDate != "" {
  231. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  232. }
  233. if req.EnterPriseKind != 0 {
  234. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  235. }
  236. sess.And("user_identity.identity = ?", enum.UserIdentityForSelfSupportForTeacher) //只查询老师
  237. _, err := sess.
  238. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  239. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  240. Join("LEFT", "user", "user.id = user_identity.uid").
  241. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  242. Join("LEFT", "class", "class_with_user.class_id = class.id").
  243. Join("LEFT", "grade", "class.grade_id = grade.id").
  244. FindAndCount(&m)
  245. if err != nil {
  246. logx.Error(err)
  247. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  248. return
  249. }
  250. //整合数据
  251. var data = map[string]map[string]map[string]map[string]interface{}{}
  252. for _, vv := range m {
  253. if data[vv.Enterprise.Name] == nil {
  254. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]interface{})
  255. }
  256. if data[vv.Enterprise.Name][vv.UserIdentity.Name] == nil {
  257. data[vv.Enterprise.Name][vv.UserIdentity.Name] = make(map[string]map[string]interface{})
  258. }
  259. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  260. if data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] == nil {
  261. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh] = make(map[string]interface{})
  262. }
  263. data[vv.Enterprise.Name][vv.UserIdentity.Name]["编号"] = map[string]interface{}{
  264. "id": vv.UserIdentity.IdNo,
  265. }
  266. if data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] == nil {
  267. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"] = make(map[string]interface{})
  268. }
  269. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["num"]) + 1
  270. data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name][mealZh]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  271. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"] = utils.AnyToInt64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["num"]) + 1
  272. data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"] = utils.AnyToFloat64(data[vv.Enterprise.Name][vv.UserIdentity.Name]["合计"]["amount"]) + utils.AnyToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  273. }
  274. xlsx.SetSheetRow("", "A1", &titleList)
  275. sheetIndex := 2 //第一个表是模板表
  276. j := 3 //表头前两行被占用了,只能从第三行开始
  277. for k, v := range data {
  278. xlsx.NewSheet(k) //1、新建表格
  279. xlsx.CopySheet(1, sheetIndex)
  280. for kk, vv := range v {
  281. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{vv["编号"]["id"], kk,
  282. vv["早餐"]["num"], vv["早餐"]["amount"],
  283. vv["午餐"]["num"], vv["午餐"]["amount"],
  284. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  285. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  286. vv["未知"]["num"], vv["未知"]["amount"],
  287. vv["合计"]["num"], vv["合计"]["amount"],
  288. })
  289. j++
  290. }
  291. sheetIndex++
  292. }
  293. xlsx.DeleteSheet("template") //删除模板表
  294. //将文件保存至服务器
  295. xlsx.SaveAs("./static" + downloadPath)
  296. break
  297. case 2:
  298. //商家分餐数据统计表
  299. xlsx, _ := excelize.OpenFile("./static/template/自营学校(商家分餐数据统计表).xlsx")
  300. fileName = "商家分餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  301. downloadPath = "/export/self_support_for_school/" + fileName
  302. //查询出所有的设备
  303. var deviceList []*model.Device
  304. err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  305. if err != nil {
  306. logx.Error(err)
  307. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  308. return
  309. }
  310. var deviceListMapping = map[string]model.Device{}
  311. for _, v := range deviceList {
  312. deviceListMapping[v.DeviceSn] = *v
  313. }
  314. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  315. sess := db.Db.Desc("self_support_for_school_ord.id")
  316. if req.EnterpriseId != 0 {
  317. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  318. }
  319. if req.StartDate != "" {
  320. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  321. }
  322. if req.EndDate != "" {
  323. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  324. }
  325. if req.EnterPriseKind != 0 {
  326. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  327. }
  328. _, err = sess.
  329. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  330. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  331. Join("LEFT", "user", "user.id = user_identity.uid").
  332. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  333. Join("LEFT", "class", "class_with_user.class_id = class.id").
  334. Join("LEFT", "grade", "class.grade_id = grade.id").
  335. FindAndCount(&m)
  336. if err != nil {
  337. logx.Error(err)
  338. println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  339. return
  340. }
  341. //整合数据
  342. var data = map[string]map[string]map[string]map[string]float64{}
  343. for _, vv := range m {
  344. shopName := deviceListMapping[vv.SelfSupportForSchoolOrd.DeviceSn].Name
  345. if data[vv.Enterprise.Name] == nil {
  346. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  347. }
  348. if data[vv.Enterprise.Name][shopName] == nil {
  349. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  350. }
  351. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  352. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  353. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  354. }
  355. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  356. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  357. }
  358. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  359. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  360. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  361. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  362. }
  363. xlsx.SetSheetRow("", "A1", &titleList)
  364. sheetIndex := 2 //第一个表是模板表
  365. for k, v := range data {
  366. if k == "" {
  367. k = "未知"
  368. }
  369. xlsx.NewSheet(k) //1、新建表格
  370. xlsx.CopySheet(1, sheetIndex)
  371. j := 4 //表头前三行被占用了,只能从第四行开始
  372. for kk, vv := range v {
  373. if kk == "" {
  374. kk = k
  375. }
  376. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  377. vv["早餐"]["num"], vv["早餐"]["amount"],
  378. vv["午餐"]["num"], vv["午餐"]["amount"],
  379. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  380. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  381. vv["合计"]["num"], vv["合计"]["amount"],
  382. })
  383. j++
  384. }
  385. sheetIndex++
  386. }
  387. xlsx.DeleteSheet("template") //删除模板表
  388. //将文件保存至服务器
  389. xlsx.SaveAs("./static" + downloadPath)
  390. break
  391. case 3:
  392. //就餐数据统计表
  393. xlsx, _ := excelize.OpenFile("./static/template/自营学校(就餐数据统计表).xlsx")
  394. fileName = "就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  395. downloadPath = "/export/self_support_for_school/" + fileName
  396. //查询出所有的设备
  397. //var deviceList []*model.Device
  398. //err := db.Db.Where("enterprise_id != 0").Find(&deviceList)
  399. //if err != nil {
  400. // logx.Error(err)
  401. // println("<<<<SelfSupportForSchoolDataStatisticsExport2>>>>>1111Error:::", err.Error())
  402. // return
  403. //}
  404. //var deviceListMapping = map[string]model.Device{}
  405. //for _, v := range deviceList {
  406. // deviceListMapping[v.DeviceSn] = *v
  407. //}
  408. var m []*db.SelfSupportForSchoolOrdWithUserIdentity
  409. sess := db.Db.Desc("self_support_for_school_ord.id")
  410. if req.EnterpriseId != 0 {
  411. sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId)
  412. }
  413. if req.StartDate != "" {
  414. sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate)
  415. }
  416. if req.EndDate != "" {
  417. sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate)
  418. }
  419. if req.EnterPriseKind != 0 {
  420. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  421. }
  422. _, err := sess.
  423. Join("LEFT", "user_identity", "self_support_for_school_ord.user_identity_id = user_identity.id").
  424. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  425. Join("LEFT", "user", "user.id = user_identity.uid").
  426. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  427. Join("LEFT", "class", "class_with_user.class_id = class.id").
  428. Join("LEFT", "grade", "class.grade_id = grade.id").
  429. FindAndCount(&m)
  430. if err != nil {
  431. logx.Error(err)
  432. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  433. return
  434. }
  435. //整合数据
  436. var data = map[string]map[string]map[string]map[string]float64{}
  437. date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  438. if err != nil {
  439. logx.Error(err)
  440. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  441. return
  442. }
  443. date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  444. if err != nil {
  445. logx.Error(err)
  446. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  447. return
  448. }
  449. diffDays := int(date2.Sub(date1).Hours() / 24)
  450. var dates []string
  451. for i := 0; i <= diffDays; i++ {
  452. date := date1.AddDate(0, 0, i)
  453. shopName := date.Format("2006-01-02")
  454. dates = append(dates, shopName)
  455. for _, vv := range m {
  456. tmpDate, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local)
  457. if err1 != nil {
  458. logx.Error(err1)
  459. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  460. return
  461. }
  462. fmt.Println(tmpDate.Format("2006-01-02"))
  463. if shopName == tmpDate.Format("2006-01-02") {
  464. if data[vv.Enterprise.Name] == nil {
  465. data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  466. }
  467. if data[vv.Enterprise.Name][shopName] == nil {
  468. data[vv.Enterprise.Name][shopName] = make(map[string]map[string]float64)
  469. }
  470. mealZh := JudgeSelfSupportForMealTime(vv.SelfSupportForSchoolOrd.FaceTime)
  471. if data[vv.Enterprise.Name][shopName][mealZh] == nil {
  472. data[vv.Enterprise.Name][shopName][mealZh] = make(map[string]float64)
  473. }
  474. if data[vv.Enterprise.Name][shopName]["合计"] == nil {
  475. data[vv.Enterprise.Name][shopName]["合计"] = make(map[string]float64)
  476. }
  477. data[vv.Enterprise.Name][shopName][mealZh]["num"]++
  478. data[vv.Enterprise.Name][shopName][mealZh]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  479. data[vv.Enterprise.Name][shopName]["合计"]["num"]++
  480. data[vv.Enterprise.Name][shopName]["合计"]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount)
  481. }
  482. }
  483. }
  484. xlsx.SetSheetRow("", "A1", &titleList)
  485. sheetIndex := 2 //第一个表是模板表
  486. for k, v := range data {
  487. if k == "" {
  488. k = "未知"
  489. }
  490. xlsx.NewSheet(k) //1、新建表格
  491. xlsx.CopySheet(1, sheetIndex)
  492. j := 4 //表头前三行被占用了,只能从第四行开始
  493. for _, date := range dates {
  494. for kk, vv := range v {
  495. if date == kk {
  496. xlsx.SetSheetRow(k, "A"+strconv.Itoa(j), &[]interface{}{kk,
  497. vv["早餐"]["num"], vv["早餐"]["amount"],
  498. vv["午餐"]["num"], vv["午餐"]["amount"],
  499. vv["晚餐"]["num"], vv["晚餐"]["amount"],
  500. vv["夜宵"]["num"], vv["夜宵"]["amount"],
  501. vv["合计"]["num"], vv["合计"]["amount"],
  502. })
  503. j++
  504. }
  505. sheetIndex++
  506. }
  507. }
  508. }
  509. xlsx.DeleteSheet("template") //删除模板表
  510. //将文件保存至服务器
  511. xlsx.SaveAs("./static" + downloadPath)
  512. break
  513. }
  514. //新增数据
  515. selfSupportForSchoolExportRecordsDb := db.SelfSupportForSchoolExportRecordsDb{}
  516. selfSupportForSchoolExportRecordsDb.Set()
  517. marshal, _ := json.Marshal(req)
  518. selfSupportForSchoolExportRecordsDb.SelfSupportForSchoolExportRecordsInsert(&model.SelfSupportForSchoolExportRecords{
  519. Name: fileName,
  520. DownloadPath: downloadPath,
  521. Kind: req.Kind,
  522. ReqContent: string(marshal),
  523. CreateAt: now.Format("2006-01-02 15:04:05"),
  524. UpdateAt: now.Format("2006-01-02 15:04:05"),
  525. })
  526. }
  527. func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolDataStatisticsExportReq) {
  528. var titleList []string
  529. var fileName, downloadPath string
  530. var now = time.Now()
  531. for _, kind := range req.Kinds {
  532. xlsx := excelize.NewFile()
  533. switch kind {
  534. case 1:
  535. //订单列表
  536. fileName = "订单列表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  537. downloadPath = "/export/central_kitchen_for_school/" + fileName
  538. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  539. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  540. //查找订单数据
  541. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  542. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess)
  543. if req.EnterpriseId != 0 {
  544. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  545. }
  546. if req.StartDate != "" {
  547. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  548. }
  549. if req.EndDate != "" {
  550. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  551. }
  552. if req.EnterPriseKind != 0 {
  553. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  554. }
  555. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  556. _, err := sess.
  557. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  558. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  559. Join("LEFT", "user", "user.id = user_identity.uid").
  560. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  561. Join("LEFT", "class", "class_with_user.class_id = class.id").
  562. Join("LEFT", "grade", "class.grade_id = grade.id").
  563. FindAndCount(&m)
  564. if err != nil {
  565. logx.Error(err)
  566. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  567. return
  568. }
  569. //查找预定数据
  570. var outTradeNoList []string
  571. for _, v := range m {
  572. outTradeNoList = append(outTradeNoList, v.OutTradeNo)
  573. }
  574. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  575. sess1 := db.Db.Desc("id")
  576. if req.EnterpriseId != 0 {
  577. sess1.And("enterprise_id = ?", req.EnterpriseId)
  578. }
  579. if len(req.MealKindList) > 0 {
  580. sess1.In("kind", req.MealKindList)
  581. }
  582. err = sess1.In("out_trade_no", outTradeNoList).Find(&mm)
  583. if err != nil {
  584. logx.Error(err)
  585. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  586. return
  587. }
  588. var exportOutTradeNoList []string
  589. for _, v := range mm {
  590. exportOutTradeNoList = append(exportOutTradeNoList, v.OutTradeNo)
  591. }
  592. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 订单表"}
  593. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  594. titleList = []string{"订单号", "学生姓名", "学校", "年级", "班级", "家长电话", "预定日期", "预定餐数", "订单价格", "支付时间", "订单状态"}
  595. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  596. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  597. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  598. if err != nil {
  599. logx.Error(err)
  600. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  601. panic(err)
  602. }
  603. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  604. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  605. j := 3 //表头前两行用了,只能从第三行开始
  606. for _, v := range m {
  607. if !utils.InArr(v.OutTradeNo, exportOutTradeNoList) {
  608. continue
  609. }
  610. var ordState string //订单状态(0:待支付 1:预约成功 2:退款中 3:部分退款 4:已退款 5:已完成)
  611. switch v.CentralKitchenForSchoolPackageOrd.OrdState {
  612. case enum.CentralKitchenForSchoolPackageOrdOrdStateForWait:
  613. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForWait)
  614. break
  615. case enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess:
  616. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess)
  617. break
  618. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding:
  619. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunding)
  620. break
  621. case enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded:
  622. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForPartRefunded)
  623. break
  624. case enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded:
  625. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForRefunded)
  626. break
  627. case enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete:
  628. ordState = enum.CentralKitchenForSchoolPackageOrdOrdState.String(enum.CentralKitchenForSchoolPackageOrdOrdStateForComplete)
  629. break
  630. }
  631. //预定餐数、预定日期
  632. var list []*model.CentralKitchenForSchoolUserWithDay
  633. err1 := json.Unmarshal([]byte(v.CentralKitchenForSchoolPackageOrd.WithDayData), &list)
  634. if err1 != nil {
  635. logx.Error(err)
  636. println("<<<<CentralKitchenForSchoolDataStatisticsExport1>>>>>Error:::", err.Error())
  637. return
  638. }
  639. var reserveDays string
  640. for _, v1 := range list {
  641. reserveDays += v1.Date + ", "
  642. }
  643. if v.Grade.Name == "" {
  644. v.Grade.Name = "教师"
  645. }
  646. if v.Class.Name == "" {
  647. v.Class.Name = "教师"
  648. }
  649. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.OutTradeNo, v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name,
  650. v.Grade.Name, v.Class.Name, v.User.Phone, reserveDays, len(list), v.CentralKitchenForSchoolPackageOrd.TotalPrice, v.CentralKitchenForSchoolPackageOrd.CreateAt, ordState})
  651. j++
  652. }
  653. //将文件保存至服务器
  654. xlsx.SaveAs("./static" + downloadPath)
  655. break
  656. case 2:
  657. //退款申请表
  658. fileName = "退款申请表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  659. downloadPath = "/export/central_kitchen_for_school/" + fileName
  660. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  661. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  662. //查找退款申请数据
  663. sess := db.Db.Desc("central_kitchen_for_school_user_refund_day.id")
  664. if req.EnterpriseId != 0 {
  665. sess.And("enterprise.id =?", req.EnterpriseId)
  666. }
  667. if req.StartDate != "" {
  668. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  669. }
  670. if req.EndDate != "" {
  671. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  672. }
  673. if len(req.MealKindList) > 0 {
  674. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  675. }
  676. if req.EnterPriseKind != 0 {
  677. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  678. }
  679. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  680. _, err := sess.
  681. 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").
  682. 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").
  683. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  684. Join("LEFT", "user", "user_identity.uid = user.id").
  685. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  686. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  687. Join("LEFT", "class", "class_with_user.class_id = class.id").
  688. Join("LEFT", "grade", "class.grade_id = grade.id").
  689. Desc("central_kitchen_for_school_user_refund_day.id").
  690. FindAndCount(&m)
  691. if err != nil {
  692. logx.Error(err)
  693. println("<<<<CentralKitchenForSchoolDataStatisticsExport2>>>>>Error:::", err.Error())
  694. return
  695. }
  696. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 退款申请表"}
  697. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  698. titleList = []string{"学生姓名", "学校", "年级", "班级", "家长电话", "退款日期", "餐型", "退款金额", "退款状态", "支付时间", "退款操作时间"}
  699. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  700. xlsx.MergeCell("Sheet1", "A1", "K1") //合并单元格
  701. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  702. if err != nil {
  703. logx.Error(err)
  704. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  705. panic(err)
  706. }
  707. xlsx.SetCellStyle("Sheet1", "A1", "K1", style)
  708. xlsx.SetColWidth("Sheet1", "A", "k", 20)
  709. j := 3 //表头前两行用了,只能从第三行开始
  710. for _, v := range m {
  711. var kind string //就餐类型(1:早餐 2:午餐 3:晚餐)
  712. switch v.CentralKitchenForSchoolUserWithDay.Kind {
  713. case enum.CentralKitchenForSchoolUserWithDayKindForBreakfast:
  714. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast)
  715. break
  716. case enum.CentralKitchenForSchoolUserWithDayKindForLunch:
  717. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForLunch)
  718. break
  719. case enum.CentralKitchenForSchoolUserWithDayKindForDinner:
  720. kind = enum.CentralKitchenForSchoolUserWithDayKind.String(enum.CentralKitchenForSchoolUserWithDayKindForDinner)
  721. break
  722. }
  723. var state string //状态(1:审核中 2:审核通过 3:审核拒绝 4:退款已完成)
  724. switch v.CentralKitchenForSchoolUserRefundDay.State {
  725. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditing:
  726. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditing)
  727. break
  728. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass:
  729. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditPass)
  730. break
  731. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject:
  732. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditReject)
  733. break
  734. case enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete:
  735. state = enum.CentralKitchenForSchoolUserRefundDayState.String(enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete)
  736. break
  737. }
  738. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.CentralKitchenForSchoolPackageOrd.UserIdentityName, v.Enterprise.Name, v.Grade.Name, v.Class.Name, v.User.Phone,
  739. v.CentralKitchenForSchoolUserWithDay.Date, kind, v.CentralKitchenForSchoolUserRefundDay.Amount, state, v.CentralKitchenForSchoolPackageOrd.CreateAt, v.CentralKitchenForSchoolUserRefundDay.RefundDate})
  740. j++
  741. }
  742. //将文件保存至服务器
  743. xlsx.SaveAs("./static" + downloadPath)
  744. break
  745. case 3:
  746. //学校预定统计表
  747. fileName = "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  748. downloadPath = "/export/central_kitchen_for_school/" + fileName
  749. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  750. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  751. if req.EnterpriseId != 0 {
  752. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  753. }
  754. var months []time.Month
  755. if req.StartDate != "" {
  756. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  757. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  758. _, sMonth, _ := sDate.Date()
  759. _, eMonth, _ := eDate.Date()
  760. diffMonth := sMonth - eMonth
  761. if diffMonth == 0 {
  762. //TODO::查询一个月
  763. months = append(months, sMonth)
  764. } else {
  765. for i := sMonth; i <= eMonth; i++ {
  766. months = append(months, i)
  767. }
  768. }
  769. }
  770. if len(months) > 0 {
  771. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  772. }
  773. //if req.EndDate != "" {
  774. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  775. //}
  776. if len(req.MealKindList) > 0 {
  777. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  778. }
  779. if req.EnterPriseKind != 0 {
  780. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  781. }
  782. _, err := sess.
  783. 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").
  784. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  785. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  786. Join("LEFT", "user", "user.id = user_identity.uid").
  787. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  788. Join("LEFT", "class", "class_with_user.class_id = class.id").
  789. Join("LEFT", "grade", "class.grade_id = grade.id").
  790. FindAndCount(&m)
  791. if err != nil {
  792. logx.Error(err)
  793. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  794. return
  795. }
  796. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  797. centralKitchenForSchoolUserWithDayDb.Set(0)
  798. var list = map[string]map[string]map[string]map[string]int64{}
  799. var xlsFileName, xlsDownloadPath []string
  800. for _, v := range m {
  801. if list[v.Enterprise.Name] == nil {
  802. list[v.Enterprise.Name] = make(map[string]map[string]map[string]int64)
  803. }
  804. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  805. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]int64)
  806. }
  807. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  808. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64)
  809. }
  810. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  811. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  812. req.StartDate, req.EndDate, req.MealKindList)
  813. if err2 != nil {
  814. logx.Error(err)
  815. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  816. return
  817. }
  818. time.Sleep(20 * time.Millisecond) // 20毫秒
  819. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  820. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += count
  821. }
  822. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  823. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += count
  824. }
  825. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  826. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += count
  827. }
  828. }
  829. for k1, v1 := range list {
  830. tmpFileName := k1 + "学校预定统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  831. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  832. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  833. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  834. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "}
  835. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  836. titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"}
  837. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  838. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  839. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  840. if err != nil {
  841. logx.Error(err)
  842. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  843. panic(err)
  844. }
  845. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  846. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  847. j := 3 //表头前两行用了,只能从第三行开始
  848. var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0)
  849. tmpEnterpriseDb := db.EnterpriseDb{}
  850. tmpEnterpriseDb.Set()
  851. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  852. if err11 != nil {
  853. logx.Error(err11)
  854. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err11.Error())
  855. panic(err11)
  856. }
  857. tmpClassDb := db.ClassDb{}
  858. tmpClassDb.Set(0)
  859. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  860. if err != nil {
  861. logx.Error(err)
  862. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  863. panic(err)
  864. }
  865. for _, class := range *classList {
  866. for k2, v2 := range v1 {
  867. if k2 == "" {
  868. //若年级为空,则为教师餐
  869. k2 = "教师餐"
  870. }
  871. for k3, v3 := range v2 {
  872. if class.Name != k3 {
  873. continue
  874. }
  875. if k3 == "" {
  876. //若班级为空,则为教师餐
  877. k3 = "教师餐"
  878. }
  879. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  880. k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]})
  881. sumClassNum++
  882. sumBreakfast += v3["breakfast"]
  883. sumLunch += v3["lunch"]
  884. sumDinner += v3["dinner"]
  885. j++
  886. }
  887. }
  888. }
  889. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  890. "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner})
  891. //将文件保存至服务器
  892. err1 := xlsx.SaveAs(tmpDownloadPath)
  893. if err1 != nil {
  894. logx.Error(err1)
  895. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  896. panic(err1)
  897. }
  898. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  899. xlsFileName = append(xlsFileName, tmpFileName)
  900. }
  901. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  902. if err != nil {
  903. logx.Error(err)
  904. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  905. panic(err)
  906. }
  907. defer archive.Close()
  908. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  909. for k, v := range xlsDownloadPath {
  910. w, err1 := zipWriter.Create("学校预定统计/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  911. if err1 != nil {
  912. logx.Error(err1)
  913. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  914. panic(err1)
  915. }
  916. f, err1 := os.Open(v) //打开待压缩的文件
  917. if err1 != nil {
  918. logx.Error(err1)
  919. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  920. panic(err1)
  921. }
  922. if _, err1 = io.Copy(w, f); err != nil {
  923. logx.Error(err1)
  924. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  925. panic(err1)
  926. }
  927. if err1 != nil {
  928. logx.Error(err1)
  929. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err1.Error())
  930. panic(err1)
  931. }
  932. f.Close()
  933. os.RemoveAll(v) //TODO::移除源文件
  934. }
  935. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  936. if err != nil {
  937. panic(err)
  938. }
  939. break
  940. case 4:
  941. //班级明细表
  942. fileName = "班级明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  943. downloadPath = "/export/central_kitchen_for_school/" + fileName
  944. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  945. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  946. if req.EnterpriseId != 0 {
  947. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  948. }
  949. var months []time.Month
  950. if req.StartDate != "" {
  951. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  952. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  953. _, sMonth, _ := sDate.Date()
  954. _, eMonth, _ := eDate.Date()
  955. diffMonth := sMonth - eMonth
  956. if diffMonth == 0 {
  957. //TODO::查询一个月
  958. months = append(months, sMonth)
  959. } else {
  960. for i := sMonth; i <= eMonth; i++ {
  961. months = append(months, i)
  962. }
  963. }
  964. }
  965. if len(months) > 0 {
  966. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  967. }
  968. //if req.EndDate != "" {
  969. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  970. //}
  971. if len(req.MealKindList) > 0 {
  972. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  973. }
  974. if req.EnterPriseKind != 0 {
  975. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  976. }
  977. _, err := sess.
  978. 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").
  979. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  980. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  981. Join("LEFT", "user", "user.id = user_identity.uid").
  982. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  983. Join("LEFT", "class", "class_with_user.class_id = class.id").
  984. Join("LEFT", "grade", "class.grade_id = grade.id").
  985. Asc("class.id").
  986. FindAndCount(&m)
  987. if err != nil {
  988. logx.Error(err)
  989. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  990. return
  991. }
  992. var list = map[string]map[string]map[string]map[string]map[string]int64{}
  993. var xlsFileName, xlsDownloadPath []string
  994. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  995. centralKitchenForSchoolUserWithDayDb.Set(0)
  996. for _, v := range m {
  997. if list[v.Enterprise.Name] == nil {
  998. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int64)
  999. }
  1000. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1001. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int64)
  1002. }
  1003. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1004. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int64)
  1005. }
  1006. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1007. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64)
  1008. }
  1009. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1010. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1011. req.StartDate, req.EndDate, req.MealKindList)
  1012. if err2 != nil {
  1013. logx.Error(err)
  1014. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1015. return
  1016. }
  1017. if count == 0 {
  1018. continue
  1019. }
  1020. time.Sleep(20 * time.Millisecond) // 20毫秒
  1021. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1022. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count
  1023. }
  1024. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1025. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count
  1026. }
  1027. if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1028. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count
  1029. }
  1030. }
  1031. for k1, v1 := range list {
  1032. tmpFileName := k1 + "班级明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1033. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1034. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1035. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1036. //查询当前学校下的班级升序
  1037. tmpEnterpriseDb := db.EnterpriseDb{}
  1038. tmpEnterpriseDb.Set()
  1039. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1040. if err11 != nil {
  1041. logx.Error(err11)
  1042. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1043. panic(err11)
  1044. }
  1045. tmpClassDb := db.ClassDb{}
  1046. tmpClassDb.Set(0)
  1047. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1048. if err != nil {
  1049. logx.Error(err)
  1050. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1051. panic(err)
  1052. }
  1053. for _, class := range *classList {
  1054. for k2, v2 := range v1 {
  1055. if k2 == "" {
  1056. //若年级为空,则为教师餐
  1057. continue
  1058. }
  1059. for k3, v3 := range v2 {
  1060. if class.Name != k3 {
  1061. continue
  1062. }
  1063. xlsx.NewSheet(k3)
  1064. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"}
  1065. xlsx.SetSheetRow(k3, "A1", &titleList)
  1066. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1067. xlsx.SetSheetRow(k3, "A2", &titleList)
  1068. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1069. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1070. if err != nil {
  1071. logx.Error(err)
  1072. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1073. panic(err)
  1074. }
  1075. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1076. xlsx.SetColWidth(k3, "A", "D", 20)
  1077. var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0)
  1078. j := 3 //表头前两行用了,只能从第三行开始
  1079. for k4, v4 := range v3 {
  1080. if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 {
  1081. continue
  1082. }
  1083. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1084. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1085. sumBreakfast += v4["breakfast"]
  1086. sumLunch += v4["lunch"]
  1087. sumDinner += v4["dinner"]
  1088. j++
  1089. }
  1090. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1091. "合计", sumBreakfast, sumLunch, sumDinner})
  1092. }
  1093. }
  1094. }
  1095. //将文件保存至服务器
  1096. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1097. err1 := xlsx.SaveAs(tmpDownloadPath)
  1098. if err1 != nil {
  1099. logx.Error(err1)
  1100. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1101. panic(err1)
  1102. }
  1103. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1104. xlsFileName = append(xlsFileName, tmpFileName)
  1105. }
  1106. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1107. if err != nil {
  1108. logx.Error(err)
  1109. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1110. panic(err)
  1111. }
  1112. defer archive.Close()
  1113. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1114. for k, v := range xlsDownloadPath {
  1115. w, err1 := zipWriter.Create("班级明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1116. if err1 != nil {
  1117. logx.Error(err1)
  1118. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1119. panic(err1)
  1120. }
  1121. f, err1 := os.Open(v) //打开待压缩的文件
  1122. if err1 != nil {
  1123. logx.Error(err1)
  1124. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1125. panic(err1)
  1126. }
  1127. if _, err1 = io.Copy(w, f); err != nil {
  1128. logx.Error(err1)
  1129. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1130. panic(err1)
  1131. }
  1132. if err1 != nil {
  1133. logx.Error(err1)
  1134. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err1.Error())
  1135. panic(err1)
  1136. }
  1137. f.Close()
  1138. os.RemoveAll(v) //TODO::移除源文件
  1139. }
  1140. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1141. if err != nil {
  1142. panic(err)
  1143. }
  1144. break
  1145. case 5:
  1146. //退款明细表
  1147. fileName = "退款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1148. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1149. sess := db.Db.Where("central_kitchen_for_school_user_refund_day.state =?", enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete).Desc("central_kitchen_for_school_user_refund_day.id")
  1150. if req.EnterpriseId != 0 {
  1151. sess.And("enterprise.id =?", req.EnterpriseId)
  1152. }
  1153. if req.StartDate != "" {
  1154. sess.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1155. }
  1156. if req.EndDate != "" {
  1157. sess.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1158. }
  1159. if len(req.MealKindList) > 0 {
  1160. sess.In("central_kitchen_for_school_user_with_day.kind", req.MealKindList)
  1161. }
  1162. if req.EnterPriseKind != 0 {
  1163. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1164. }
  1165. var m []*db.CentralKitchenForSchoolUserRefundDayWithData
  1166. _, err := sess.
  1167. 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").
  1168. 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").
  1169. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1170. Join("LEFT", "user", "user_identity.uid = user.id").
  1171. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1172. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1173. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1174. Join("LEFT", "grade", "class.grade_id = grade.id").
  1175. Desc("central_kitchen_for_school_user_refund_day.id").
  1176. FindAndCount(&m)
  1177. if err != nil {
  1178. logx.Error(err)
  1179. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1180. return
  1181. }
  1182. var list = map[string]map[string]map[string]map[string]map[string]int{}
  1183. var xlsFileName, xlsDownloadPath []string
  1184. for _, v := range m {
  1185. if list[v.Enterprise.Name] == nil {
  1186. list[v.Enterprise.Name] = make(map[string]map[string]map[string]map[string]int)
  1187. }
  1188. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1189. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]map[string]int)
  1190. }
  1191. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1192. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]map[string]int)
  1193. }
  1194. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] == nil {
  1195. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int)
  1196. }
  1197. //就餐类型(1:早餐 2:午餐 3:晚餐)
  1198. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1199. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"]++
  1200. }
  1201. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1202. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"]++
  1203. }
  1204. if v.CentralKitchenForSchoolUserWithDay.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1205. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"]++
  1206. }
  1207. }
  1208. for k1, v1 := range list {
  1209. tmpFileName := k1 + "退款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1210. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1211. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1212. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1213. for k2, v2 := range v1 {
  1214. if k2 == "" {
  1215. //若年级为空,则为教师餐
  1216. continue
  1217. }
  1218. for k3, v3 := range v2 {
  1219. xlsx.NewSheet(k3)
  1220. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 退款明细"}
  1221. xlsx.SetSheetRow(k3, "A1", &titleList)
  1222. titleList = []string{"姓名", "早餐", "午餐", "晚餐"}
  1223. xlsx.SetSheetRow(k3, "A2", &titleList)
  1224. xlsx.MergeCell(k3, "A1", "D1") //合并单元格
  1225. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1226. if err != nil {
  1227. logx.Error(err)
  1228. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1229. panic(err)
  1230. }
  1231. xlsx.SetCellStyle(k3, "A1", "D1", style)
  1232. xlsx.SetColWidth(k3, "A", "D", 20)
  1233. var sumBreakfast, sumLunch, sumDinner = 0, 0, 0
  1234. j := 3 //表头前两行用了,只能从第三行开始
  1235. for k4, v4 := range v3 {
  1236. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1237. k4, v4["breakfast"], v4["lunch"], v4["dinner"]})
  1238. sumBreakfast += v4["breakfast"]
  1239. sumLunch += v4["lunch"]
  1240. sumDinner += v4["dinner"]
  1241. j++
  1242. }
  1243. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1244. "合计", sumBreakfast, sumLunch, sumDinner})
  1245. }
  1246. }
  1247. //将文件保存至服务器
  1248. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1249. err1 := xlsx.SaveAs(tmpDownloadPath)
  1250. if err1 != nil {
  1251. logx.Error(err1)
  1252. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1253. panic(err1)
  1254. }
  1255. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1256. xlsFileName = append(xlsFileName, tmpFileName)
  1257. }
  1258. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1259. if err != nil {
  1260. logx.Error(err)
  1261. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err.Error())
  1262. panic(err)
  1263. }
  1264. defer archive.Close()
  1265. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1266. for k, v := range xlsDownloadPath {
  1267. w, err1 := zipWriter.Create("退款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1268. if err1 != nil {
  1269. logx.Error(err1)
  1270. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1271. panic(err1)
  1272. }
  1273. f, err1 := os.Open(v) //打开待压缩的文件
  1274. if err1 != nil {
  1275. logx.Error(err1)
  1276. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1277. panic(err1)
  1278. }
  1279. if _, err1 = io.Copy(w, f); err != nil {
  1280. logx.Error(err1)
  1281. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1282. panic(err1)
  1283. }
  1284. if err1 != nil {
  1285. logx.Error(err1)
  1286. println("<<<<CentralKitchenForSchoolDataStatisticsExport5>>>>>Error:::", err1.Error())
  1287. panic(err1)
  1288. }
  1289. f.Close()
  1290. os.RemoveAll(v) //TODO::移除源文件
  1291. }
  1292. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1293. if err != nil {
  1294. panic(err)
  1295. }
  1296. break
  1297. case 6:
  1298. //收款统计表
  1299. fileName = "收款统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1300. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1301. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1302. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1303. //查找数据
  1304. var m []model.Enterprise
  1305. sess := db.Db.Desc("id")
  1306. if req.EnterPriseKind != 0 {
  1307. sess.And("kind = ?", req.EnterPriseKind)
  1308. }
  1309. if req.EnterpriseId != 0 {
  1310. sess.And("id = ?", req.EnterpriseId)
  1311. }
  1312. err := sess.Find(&m)
  1313. if err != nil {
  1314. logx.Error(err)
  1315. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1316. return
  1317. }
  1318. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 收款统计表"}
  1319. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1320. titleList = []string{"学校名称", "预定总费用", "退款总计", "实收费用", "预估费率"}
  1321. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1322. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1323. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1324. if err != nil {
  1325. logx.Error(err)
  1326. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1327. panic(err)
  1328. }
  1329. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1330. xlsx.SetColWidth("Sheet1", "A", "E", 20)
  1331. j := 3 //表头前两行用了,只能从第三行开始
  1332. for _, v := range m {
  1333. var income, refund string
  1334. //统计收入
  1335. 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)
  1336. mapArr1, err := db.QueryNativeString(db.Db, sql1)
  1337. if err != nil {
  1338. logx.Error(err)
  1339. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1340. return
  1341. }
  1342. if mapArr1 != nil {
  1343. income = mapArr1[0]["total"]
  1344. }
  1345. //统计退款
  1346. //查询出当前学校下所有的学生
  1347. userIdentityDb := db.UserIdentityDb{}
  1348. userIdentityDb.Set(0)
  1349. userIdentities, _ := userIdentityDb.FindUserIdentityForEnterprise(v.Id)
  1350. var userIdentityIds []string
  1351. for _, v := range *userIdentities {
  1352. userIdentityIds = append(userIdentityIds, utils.IntToStr(v.Id))
  1353. }
  1354. if len(userIdentityIds) != 0 {
  1355. userIdentityStr := php2go.Implode(",", userIdentityIds)
  1356. 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'",
  1357. enum.CentralKitchenForSchoolUserRefundDayStateForAuditComplete, userIdentityStr, req.StartDate, req.EndDate)
  1358. mapArr2, err := db.QueryNativeString(db.Db, sql2)
  1359. if err != nil {
  1360. logx.Error(err)
  1361. println("<<<<CentralKitchenForSchoolDataStatisticsExport6>>>>>Error:::", err.Error())
  1362. return
  1363. }
  1364. if mapArr2 != nil {
  1365. refund = mapArr2[0]["total"]
  1366. }
  1367. }
  1368. realityAmount := utils.StrToFloat64(income) - utils.StrToFloat64(refund)
  1369. rateFee := realityAmount * 0.0038
  1370. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{v.Name, income, refund, realityAmount, rateFee})
  1371. j++
  1372. }
  1373. //将文件保存至服务器
  1374. xlsx.SaveAs("./static" + downloadPath)
  1375. break
  1376. case 7:
  1377. //班级收款明细表
  1378. fileName = "班级收款明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1379. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1380. //查找订单数据
  1381. sess := db.Db.Desc("central_kitchen_for_school_package_ord.id")
  1382. sess.And("central_kitchen_for_school_package_ord.state =?", enum.CentralKitchenForSchoolPackageOrdStateForSuccess).And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent)
  1383. if req.EnterpriseId != 0 {
  1384. sess.And("central_kitchen_for_school_package_ord.enterprise_id =?", req.EnterpriseId)
  1385. }
  1386. if req.StartDate != "" {
  1387. sess.And("central_kitchen_for_school_package_ord.create_at >= ?", req.StartDate)
  1388. }
  1389. if req.EndDate != "" {
  1390. sess.And("central_kitchen_for_school_package_ord.create_at <= ?", req.EndDate)
  1391. }
  1392. if req.EnterPriseKind != 0 {
  1393. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1394. }
  1395. var m []*db.CentralKitchenForSchoolPackageOrdWithUserIdentity
  1396. _, err := sess.
  1397. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1398. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1399. Join("LEFT", "user", "user.id = user_identity.uid").
  1400. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1401. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1402. Join("LEFT", "grade", "class.grade_id = grade.id").
  1403. FindAndCount(&m)
  1404. if err != nil {
  1405. logx.Error(err)
  1406. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1407. return
  1408. }
  1409. 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")
  1410. if req.EnterpriseId != 0 {
  1411. sess1.And("enterprise.id =?", req.EnterpriseId)
  1412. }
  1413. if req.StartDate != "" {
  1414. sess1.And("central_kitchen_for_school_user_refund_day.create_at >= ?", req.StartDate)
  1415. }
  1416. if req.EndDate != "" {
  1417. sess1.And("central_kitchen_for_school_user_refund_day.create_at <= ?", req.EndDate)
  1418. }
  1419. var mm []*db.CentralKitchenForSchoolUserRefundDayWithData
  1420. _, err = sess1.
  1421. 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").
  1422. 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").
  1423. Join("LEFT", "user_identity", "central_kitchen_for_school_user_with_day.identity_id = user_identity.id").
  1424. Join("LEFT", "user", "user_identity.uid = user.id").
  1425. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  1426. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  1427. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1428. Join("LEFT", "grade", "class.grade_id = grade.id").
  1429. Desc("central_kitchen_for_school_user_refund_day.id").
  1430. FindAndCount(&mm)
  1431. if err != nil {
  1432. logx.Error(err)
  1433. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1434. return
  1435. }
  1436. var list = map[string]map[string]map[string]map[string]float64{}
  1437. for _, v := range m {
  1438. if list[v.Enterprise.Name] == nil {
  1439. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1440. }
  1441. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1442. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1443. }
  1444. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1445. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1446. }
  1447. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["income"] += utils.StrToFloat64(v.CentralKitchenForSchoolPackageOrd.TotalPrice)
  1448. }
  1449. for _, v := range mm {
  1450. if list[v.Enterprise.Name] == nil {
  1451. list[v.Enterprise.Name] = make(map[string]map[string]map[string]float64)
  1452. }
  1453. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1454. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]float64)
  1455. }
  1456. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1457. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]float64)
  1458. }
  1459. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["refund"] += utils.StrToFloat64(v.CentralKitchenForSchoolUserRefundDay.Amount)
  1460. }
  1461. var xlsFileName, xlsDownloadPath []string
  1462. for k1, v1 := range list {
  1463. tmpFileName := k1 + "班级收款明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1464. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1465. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1466. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1467. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") 班级收款明细"}
  1468. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1469. titleList = []string{"年级", "班级", "预定总费用", "退款总计"}
  1470. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1471. xlsx.MergeCell("Sheet1", "A1", "D1") //合并单元格
  1472. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1473. if err != nil {
  1474. logx.Error(err)
  1475. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1476. panic(err)
  1477. }
  1478. xlsx.SetCellStyle("Sheet1", "A1", "D1", style)
  1479. xlsx.SetColWidth("Sheet1", "A", "D", 20)
  1480. j := 3 //表头前两行用了,只能从第三行开始
  1481. var sumClassNum = int64(0)
  1482. var sumIncome, sumRefund = float64(0), float64(0)
  1483. for k2, v2 := range v1 {
  1484. if k2 == "" {
  1485. //若年级为空,则为教师餐
  1486. continue
  1487. }
  1488. for k3, v3 := range v2 {
  1489. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k2, k3, utils.Float64ToStr(v3["income"]), utils.Float64ToStr(v3["refund"])})
  1490. sumClassNum++
  1491. sumIncome += v3["income"]
  1492. sumRefund += v3["refund"]
  1493. j++
  1494. }
  1495. }
  1496. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1497. "合计", sumClassNum, sumIncome, sumRefund})
  1498. //将文件保存至服务器
  1499. err1 := xlsx.SaveAs(tmpDownloadPath)
  1500. if err1 != nil {
  1501. logx.Error(err1)
  1502. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1503. panic(err1)
  1504. }
  1505. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1506. xlsFileName = append(xlsFileName, tmpFileName)
  1507. }
  1508. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1509. if err != nil {
  1510. logx.Error(err)
  1511. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err.Error())
  1512. panic(err)
  1513. }
  1514. defer archive.Close()
  1515. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1516. for k, v := range xlsDownloadPath {
  1517. w, err1 := zipWriter.Create("班级收款明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1518. if err1 != nil {
  1519. logx.Error(err1)
  1520. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1521. panic(err1)
  1522. }
  1523. f, err1 := os.Open(v) //打开待压缩的文件
  1524. if err1 != nil {
  1525. logx.Error(err1)
  1526. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1527. panic(err1)
  1528. }
  1529. if _, err1 = io.Copy(w, f); err != nil {
  1530. logx.Error(err1)
  1531. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1532. panic(err1)
  1533. }
  1534. if err1 != nil {
  1535. logx.Error(err1)
  1536. println("<<<<CentralKitchenForSchoolDataStatisticsExport7>>>>>Error:::", err1.Error())
  1537. panic(err1)
  1538. }
  1539. f.Close()
  1540. os.RemoveAll(v) //TODO::移除源文件
  1541. }
  1542. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1543. if err != nil {
  1544. panic(err)
  1545. }
  1546. break
  1547. case 8:
  1548. //学校预定数量统计表
  1549. fileName = "学校预定数量统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1550. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1551. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1552. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1553. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1554. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1555. if req.EnterpriseId != 0 {
  1556. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1557. }
  1558. var months []time.Month
  1559. if req.StartDate != "" {
  1560. sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1561. eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1562. _, sMonth, _ := sDate.Date()
  1563. _, eMonth, _ := eDate.Date()
  1564. diffMonth := sMonth - eMonth
  1565. if diffMonth == 0 {
  1566. //TODO::查询一个月
  1567. months = append(months, sMonth)
  1568. } else {
  1569. for i := sMonth; i <= eMonth; i++ {
  1570. months = append(months, i)
  1571. }
  1572. }
  1573. }
  1574. if len(months) > 0 {
  1575. sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months)
  1576. }
  1577. //if req.EndDate != "" {
  1578. // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.EndDate)
  1579. //}
  1580. if len(req.MealKindList) > 0 {
  1581. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1582. }
  1583. if req.EnterPriseKind != 0 {
  1584. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1585. }
  1586. _, err := sess.
  1587. 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").
  1588. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1589. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1590. FindAndCount(&m)
  1591. if err != nil {
  1592. logx.Error(err)
  1593. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1594. return
  1595. }
  1596. var list = map[string]map[string]int64{}
  1597. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1598. centralKitchenForSchoolUserWithDayDb.Set(0)
  1599. for _, v := range m {
  1600. if list[v.Enterprise.Name] == nil {
  1601. list[v.Enterprise.Name] = map[string]int64{}
  1602. }
  1603. count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo(
  1604. v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, enum.CentralKitchenForSchoolUserWithDayStateForCancel,
  1605. req.StartDate, req.EndDate, req.MealKindList)
  1606. if err2 != nil {
  1607. logx.Error(err)
  1608. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1609. return
  1610. }
  1611. for _, value := range req.MealKindList {
  1612. if value == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast {
  1613. count1, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForBreakfast, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1614. if err2 != nil {
  1615. logx.Error(err)
  1616. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1617. return
  1618. }
  1619. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += count1
  1620. }
  1621. if value == enum.CentralKitchenForSchoolUserWithDayKindForLunch {
  1622. count2, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForLunch, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1623. if err2 != nil {
  1624. logx.Error(err)
  1625. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1626. return
  1627. }
  1628. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += count2
  1629. }
  1630. if value == enum.CentralKitchenForSchoolUserWithDayKindForDinner {
  1631. count3, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNoAndKind(v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, req.StartDate, req.EndDate, enum.CentralKitchenForSchoolUserWithDayKindForDinner, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1632. if err2 != nil {
  1633. logx.Error(err)
  1634. println("<<<<SelfSupportForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1635. return
  1636. }
  1637. list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += count3
  1638. }
  1639. }
  1640. list[v.Enterprise.Name]["count"] += count
  1641. time.Sleep(20 * time.Millisecond) // 20毫秒
  1642. }
  1643. titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"}
  1644. xlsx.SetSheetRow("Sheet1", "A1", &titleList)
  1645. titleList = []string{"学校名称", "预定总数",
  1646. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(),
  1647. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(),
  1648. enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(),
  1649. }
  1650. xlsx.SetSheetRow("Sheet1", "A2", &titleList)
  1651. xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格
  1652. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1653. if err != nil {
  1654. logx.Error(err)
  1655. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1656. panic(err)
  1657. }
  1658. xlsx.SetCellStyle("Sheet1", "A1", "E1", style)
  1659. xlsx.SetColWidth("Sheet1", "A", "A", 30)
  1660. xlsx.SetColWidth("Sheet1", "B", "E", 15)
  1661. j := 3 //表头前两行用了,只能从第三行开始
  1662. total := int64(0)
  1663. total1 := int64(0)
  1664. total2 := int64(0)
  1665. total3 := int64(0)
  1666. for k, v := range list {
  1667. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"],
  1668. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()],
  1669. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()],
  1670. v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]})
  1671. total += v["count"]
  1672. total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()]
  1673. total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()]
  1674. total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]
  1675. j++
  1676. }
  1677. xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{
  1678. "合计", total, total1, total2, total3})
  1679. //将文件保存至服务器
  1680. xlsx.SaveAs("./static" + downloadPath)
  1681. break
  1682. case 10:
  1683. //就餐数据统计表
  1684. xls, _ := excelize.OpenFile("./static/template/央厨学校(就餐数据统计表).xlsx")
  1685. fileName = "就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1686. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1687. var list = map[string]map[string]map[string]float64{}
  1688. var dates []string
  1689. if req.StartDate != "" {
  1690. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  1691. centralKitchenForSchoolUserWithDayDb.Set(0)
  1692. sTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local)
  1693. eTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local)
  1694. diffDays := int(eTime.Sub(sTime).Hours() / 24)
  1695. for i := 0; i <= diffDays; i++ {
  1696. date := sTime.AddDate(0, 0, i)
  1697. dateName := date.Format("2006-01-02")
  1698. dates = append(dates, dateName)
  1699. for _, vv := range req.MealKindList {
  1700. tmpSum, err := centralKitchenForSchoolUserWithDayDb.SumCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1701. if err != nil {
  1702. logx.Error(err)
  1703. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  1704. panic(err)
  1705. }
  1706. tmpCount, err := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByKind(req.EnterpriseId, dateName, dateName, vv, enum.CentralKitchenForSchoolUserWithDayStateForCancel)
  1707. if err != nil {
  1708. logx.Error(err)
  1709. println("<<<<CentralKitchenForSchoolDataStatisticsExport9>>>>>Error:::", err.Error())
  1710. panic(err)
  1711. }
  1712. if list[dateName] == nil {
  1713. list[dateName] = map[string]map[string]float64{}
  1714. }
  1715. if list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] == nil {
  1716. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()] = map[string]float64{}
  1717. }
  1718. if list[dateName]["合计"] == nil {
  1719. list[dateName]["合计"] = make(map[string]float64)
  1720. }
  1721. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["sum"] = tmpSum
  1722. list[dateName][enum.CentralKitchenForSchoolUserWithDayKind(vv).String()]["count"] = float64(tmpCount)
  1723. list[dateName]["合计"]["sum"] += tmpSum
  1724. list[dateName]["合计"]["count"] += float64(tmpCount)
  1725. }
  1726. }
  1727. }
  1728. //xls.SetSheetRow("", "A1", &titleList)
  1729. //sheetIndex := 2 //第一个表是模板表
  1730. //xls.NewSheet("sheet1") //1、新建表格
  1731. //xls.CopySheet(1, sheetIndex)
  1732. j := 4 //表头前三"早餐"行被占用了,只能从第四行开始
  1733. var totalMap = map[string]float64{}
  1734. for _, date := range dates {
  1735. for kk, v := range list {
  1736. if date == kk {
  1737. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{kk,
  1738. v["早餐"]["count"], v["早餐"]["sum"],
  1739. v["午餐"]["count"], v["午餐"]["sum"],
  1740. v["晚餐"]["count"], v["晚餐"]["sum"],
  1741. v["合计"]["count"], v["合计"]["sum"],
  1742. })
  1743. totalMap["breakfast_count"] += v["早餐"]["count"]
  1744. totalMap["breakfast_sum"] += v["早餐"]["sum"]
  1745. totalMap["lunch_count"] += v["早餐"]["count"]
  1746. totalMap["lunch_sum"] += v["早餐"]["sum"]
  1747. totalMap["dinner_count"] += v["早餐"]["count"]
  1748. totalMap["dinner_sum"] += v["早餐"]["sum"]
  1749. totalMap["total_count"] += v["合计"]["count"]
  1750. totalMap["total_sum"] += v["合计"]["sum"]
  1751. j++
  1752. }
  1753. }
  1754. }
  1755. //xls.DeleteSheet("template") //删除模板表
  1756. xls.SetSheetRow("template", "A"+strconv.Itoa(j), &[]interface{}{"总计",
  1757. totalMap["breakfast_count"], totalMap["breakfast_sum"],
  1758. totalMap["lunch_count"], totalMap["lunch_sum"],
  1759. totalMap["dinner_count"], totalMap["dinner_sum"],
  1760. totalMap["total_count"], totalMap["total_sum"],
  1761. })
  1762. //将文件保存至服务器
  1763. xls.SaveAs("./static" + downloadPath)
  1764. break
  1765. }
  1766. //新增数据
  1767. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1768. centralKitchenForSchoolExportRecordsDb.Set()
  1769. marshal, _ := json.Marshal(req)
  1770. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1771. Name: fileName,
  1772. DownloadPath: downloadPath,
  1773. Kind: kind,
  1774. ReqContent: string(marshal),
  1775. CreateAt: now.Format("2006-01-02 15:04:05"),
  1776. UpdateAt: now.Format("2006-01-02 15:04:05"),
  1777. })
  1778. }
  1779. }
  1780. func CentralKitchenForSchoolDataStatisticsContrast(req md.CentralKitchenForSchoolDataStatisticsContrastReq) {
  1781. var titleList []string
  1782. var fileName, downloadPath string
  1783. var now = time.Now()
  1784. xlsx := excelize.NewFile()
  1785. //数据对比
  1786. fileName = "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".zip"
  1787. downloadPath = "/export/central_kitchen_for_school/" + fileName
  1788. var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1789. sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1790. if req.EnterpriseId != 0 {
  1791. sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1792. }
  1793. if req.Date1[0] != "" {
  1794. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1795. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  1796. }
  1797. if req.Date1[1] != "" {
  1798. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[1], time.Local)
  1799. sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  1800. }
  1801. if len(req.MealKindList) > 0 {
  1802. sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1803. }
  1804. if req.EnterPriseKind != 0 {
  1805. sess.And("enterprise.kind = ?", req.EnterPriseKind)
  1806. }
  1807. _, err := sess.
  1808. 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").
  1809. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1810. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1811. Join("LEFT", "user", "user.id = user_identity.uid").
  1812. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1813. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1814. Join("LEFT", "grade", "class.grade_id = grade.id").
  1815. FindAndCount(&m)
  1816. if err != nil {
  1817. logx.Error(err)
  1818. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1819. return
  1820. }
  1821. var m1 []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity
  1822. sess1 := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id")
  1823. if req.EnterpriseId != 0 {
  1824. sess1.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId)
  1825. }
  1826. if req.Date2[0] != "" {
  1827. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1828. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start >= ?", startDate.Format("2006-01-02"))
  1829. }
  1830. if req.Date2[1] != "" {
  1831. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[1], time.Local)
  1832. sess1.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", endDate.Format("2006-01-02"))
  1833. }
  1834. if len(req.MealKindList) > 0 {
  1835. sess1.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList)
  1836. }
  1837. _, err1 := sess1.
  1838. 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").
  1839. Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord.user_identity_id = user_identity.id").
  1840. Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id").
  1841. Join("LEFT", "user", "user.id = user_identity.uid").
  1842. Join("LEFT", "class_with_user", "class_with_user.user_identity_id = user_identity.id").
  1843. Join("LEFT", "class", "class_with_user.class_id = class.id").
  1844. Join("LEFT", "grade", "class.grade_id = grade.id").
  1845. FindAndCount(&m1)
  1846. if err1 != nil {
  1847. logx.Error(err1)
  1848. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1849. return
  1850. }
  1851. var list = map[string]map[string]map[string]map[string]string{}
  1852. for _, v := range m {
  1853. if list[v.Enterprise.Name] == nil {
  1854. list[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1855. }
  1856. if list[v.Enterprise.Name][v.Grade.Name] == nil {
  1857. list[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1858. }
  1859. if list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1860. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1861. }
  1862. list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1863. }
  1864. var list1 = map[string]map[string]map[string]map[string]string{}
  1865. for _, v := range m1 {
  1866. if list1[v.Enterprise.Name] == nil {
  1867. list1[v.Enterprise.Name] = make(map[string]map[string]map[string]string)
  1868. }
  1869. if list1[v.Enterprise.Name][v.Grade.Name] == nil {
  1870. list1[v.Enterprise.Name][v.Grade.Name] = make(map[string]map[string]string)
  1871. }
  1872. if list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] == nil {
  1873. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]string)
  1874. }
  1875. list1[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = v.User.Phone
  1876. }
  1877. var xlsFileName, xlsDownloadPath []string
  1878. for k1, v1 := range list {
  1879. tmpFileName := k1 + "数据对比_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx"
  1880. tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName
  1881. startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date1[0], time.Local)
  1882. endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.Date2[0], time.Local)
  1883. //查询当前学校下的班级升序
  1884. tmpEnterpriseDb := db.EnterpriseDb{}
  1885. tmpEnterpriseDb.Set()
  1886. tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1)
  1887. if err11 != nil {
  1888. logx.Error(err11)
  1889. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err11.Error())
  1890. panic(err11)
  1891. }
  1892. tmpClassDb := db.ClassDb{}
  1893. tmpClassDb.Set(0)
  1894. classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id)
  1895. if err != nil {
  1896. logx.Error(err)
  1897. println("<<<<CentralKitchenForSchoolDataStatisticsExport4>>>>>Error:::", err.Error())
  1898. panic(err)
  1899. }
  1900. for _, class := range *classList {
  1901. for k2, v2 := range v1 {
  1902. if k2 == "" {
  1903. //若年级为空,则为教师
  1904. continue
  1905. }
  1906. for k3, v3 := range v2 {
  1907. if class.Name != k3 {
  1908. continue
  1909. }
  1910. xlsx.NewSheet(k3)
  1911. titleList = []string{startDate.Format("2006-01") + "~~" + endDate.Format("2006-01") + " [" + k1 + ", " + k3 + "] 订餐数据对比"}
  1912. xlsx.SetSheetRow(k3, "A1", &titleList)
  1913. titleList = []string{"姓名", "家长联系电话"}
  1914. xlsx.SetSheetRow(k3, "A2", &titleList)
  1915. xlsx.MergeCell(k3, "A1", "B1") //合并单元格
  1916. style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`)
  1917. if err != nil {
  1918. logx.Error(err)
  1919. println("<<<<CentralKitchenForSchoolDataStatisticsExport3>>>>>Error:::", err.Error())
  1920. panic(err)
  1921. }
  1922. xlsx.SetCellStyle(k3, "A1", "B1", style)
  1923. xlsx.SetColWidth(k3, "A", "B", 40)
  1924. j := 2 //表头被第一行用了,只能从第二行开始
  1925. for k4, v4 := range v3 {
  1926. //TODO::判断当前用户是否在新的月份里面
  1927. fmt.Println("11111111111111", list1[k1])
  1928. fmt.Println("22222222222222", list1[k1][k2])
  1929. fmt.Println("33333333333333", list1[k1][k2][k3])
  1930. fmt.Println("44444444444444", list1[k1][k2][k3][k4])
  1931. if list1[k1] == nil || list1[k1][k2] == nil || list1[k1][k2][k3] == nil || list1[k1][k2][k3][k4] == "" {
  1932. xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{
  1933. k4, v4})
  1934. j++
  1935. }
  1936. }
  1937. }
  1938. }
  1939. }
  1940. //将文件保存至服务器
  1941. xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除
  1942. err1 := xlsx.SaveAs(tmpDownloadPath)
  1943. if err1 != nil {
  1944. logx.Error(err1)
  1945. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1946. panic(err1)
  1947. }
  1948. xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath)
  1949. xlsFileName = append(xlsFileName, tmpFileName)
  1950. }
  1951. archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件
  1952. if err != nil {
  1953. logx.Error(err)
  1954. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err.Error())
  1955. panic(err)
  1956. }
  1957. defer archive.Close()
  1958. zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象
  1959. for k, v := range xlsDownloadPath {
  1960. w, err1 := zipWriter.Create("数据对比/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer
  1961. if err1 != nil {
  1962. logx.Error(err1)
  1963. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1964. panic(err1)
  1965. }
  1966. f, err1 := os.Open(v) //打开待压缩的文件
  1967. if err1 != nil {
  1968. logx.Error(err1)
  1969. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1970. panic(err1)
  1971. }
  1972. if _, err1 = io.Copy(w, f); err != nil {
  1973. logx.Error(err1)
  1974. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1975. panic(err1)
  1976. }
  1977. if err1 != nil {
  1978. logx.Error(err1)
  1979. println("<<<<CentralKitchenForSchoolDataStatisticsContrast>>>>>Error:::", err1.Error())
  1980. panic(err1)
  1981. }
  1982. f.Close()
  1983. os.RemoveAll(v) //TODO::移除源文件
  1984. }
  1985. zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流
  1986. if err != nil {
  1987. panic(err)
  1988. }
  1989. //新增数据
  1990. centralKitchenForSchoolExportRecordsDb := db.CentralKitchenForSchoolExportRecordsDb{}
  1991. centralKitchenForSchoolExportRecordsDb.Set()
  1992. marshal, _ := json.Marshal(req)
  1993. centralKitchenForSchoolExportRecordsDb.CentralKitchenForSchoolExportRecordsInsert(&model.CentralKitchenForSchoolExportRecords{
  1994. Name: fileName,
  1995. DownloadPath: downloadPath,
  1996. Kind: 9, //数据对比
  1997. ReqContent: string(marshal),
  1998. CreateAt: now.Format("2006-01-02 15:04:05"),
  1999. UpdateAt: now.Format("2006-01-02 15:04:05"),
  2000. })
  2001. }
  2002. func JudgeSelfSupportForMealTime(mealDate string) (mealZh string) {
  2003. mealZh = "未知"
  2004. mealTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealDate, time.Local)
  2005. for _, v := range md.SelfSupportForMealTimeMapping {
  2006. //判断时间大小
  2007. tmpFromTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["from"]+":00", time.Local)
  2008. tmpToTime, _ := time.ParseInLocation("2006-01-02 15:04:05", mealTime.Format("2006-01-02")+" "+v["to"]+":00", time.Local)
  2009. fmt.Println(">>>>>>>>>>>>>", mealTime.After(tmpFromTime))
  2010. fmt.Println("<<<<<<<<<<<<<", mealTime.Before(tmpToTime))
  2011. if mealTime.After(tmpFromTime) && mealTime.Before(tmpToTime) {
  2012. mealZh = v["name"]
  2013. }
  2014. }
  2015. return
  2016. }