From 1a8accefefadd29e30f3e01b166481d73ce04cb5 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Thu, 29 Aug 2024 15:15:45 +0800 Subject: [PATCH] pdate --- app/admin/hdl/hdl_data_statistics.go | 4 + app/admin/svc/svc_data_statisstics.go | 183 ++++++++++++++++++++++++++ 2 files changed, 187 insertions(+) diff --git a/app/admin/hdl/hdl_data_statistics.go b/app/admin/hdl/hdl_data_statistics.go index 7062bf4..7415524 100644 --- a/app/admin/hdl/hdl_data_statistics.go +++ b/app/admin/hdl/hdl_data_statistics.go @@ -78,6 +78,10 @@ func CentralKitchenForSchoolDataStatisticsList(c *gin.Context) { "name": "就餐数据统计表", "value": 10, }, + { + "name": "教师就餐明细表", + "value": 11, + }, }, }, nil) return diff --git a/app/admin/svc/svc_data_statisstics.go b/app/admin/svc/svc_data_statisstics.go index 50ef1ab..358c3ca 100644 --- a/app/admin/svc/svc_data_statisstics.go +++ b/app/admin/svc/svc_data_statisstics.go @@ -2257,6 +2257,189 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD //将文件保存至服务器 xls.SaveAs("./static" + downloadPath) break + case 11: + //教师就餐明细表 + fileName = "教师就餐明细表_" + utils.Int64ToStr(now.UnixMilli()) + ".zip" + downloadPath = "/export/central_kitchen_for_school/" + fileName + var m []*db.CentralKitchenForSchoolPackageOrdForReserveWithUserIdentity + sess := db.Db.Desc("central_kitchen_for_school_package_ord_for_reserve.id") + if req.EnterpriseId != 0 { + sess.And("central_kitchen_for_school_package_ord_for_reserve.enterprise_id =?", req.EnterpriseId) + } + var months []time.Month + if req.StartDate != "" { + sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local) + eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local) + _, sMonth, _ := sDate.Date() + _, eMonth, _ := eDate.Date() + diffMonth := sMonth - eMonth + if diffMonth == 0 { + //TODO::查询一个月 + months = append(months, sMonth) + } else { + for i := sMonth; i <= eMonth; i++ { + months = append(months, i) + } + } + } + if len(months) > 0 { + sess.In("central_kitchen_for_school_package_ord_for_reserve.reserve_month", months) + } + if len(req.MealKindList) > 0 { + sess.In("central_kitchen_for_school_package_ord_for_reserve.kind", req.MealKindList) + } + if req.EnterPriseKind != 0 { + sess.And("enterprise.kind = ?", req.EnterPriseKind) + } + sess.And("user_identity.identity != ?", 1) //不查学生的 + _, err := sess. + 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"). + Join("LEFT", "user_identity", "central_kitchen_for_school_package_ord_for_reserve.user_identity_id = user_identity.id"). + Join("LEFT", "enterprise", "enterprise.id = user_identity.enterprise_id"). + Asc("enterprise.id"). + FindAndCount(&m) + if err != nil { + logx.Error(err) + println("<<<>>>>Error:::", err.Error()) + return + } + + var ordNoArr, mealKindListArr []string + for _, v := range m { + ordNoArr = append(ordNoArr, v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo) + } + + var nativeSql = "SELECT COUNT(*) AS total, kind , ord_no FROM `central_kitchen_for_school_user_with_day` WHERE `ord_no` IN (%s) AND (state !=%d) AND (state !=%d) AND (DATE >= '%s') AND (DATE <= '%s') AND `kind` IN (%s) GROUP BY `ord_no`, `kind`" + for _, v := range req.MealKindList { + mealKindListArr = append(mealKindListArr, utils.IntToStr(v)) + } + sDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local) + eDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local) + nativeSql = fmt.Sprintf(nativeSql, strings.Join(ordNoArr, ","), enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling, sDate.Format("2006-01-02"), eDate.Format("2006-01-02"), strings.Join(mealKindListArr, ",")) + nativeResults, err := db.QueryNativeString(db.Db, nativeSql) + var nativeResultsMap = map[string]map[int]int64{} + for _, v := range nativeResults { + if nativeResultsMap[v["ord_no"]] == nil { + nativeResultsMap[v["ord_no"]] = make(map[int]int64) + } + nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"]) + } + + var list = map[string]map[string]map[string]int64{} + var xlsFileName, xlsDownloadPath []string + centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{} + centralKitchenForSchoolUserWithDayDb.Set(0) + for _, v := range m { + if list[v.Enterprise.Name] == nil { + list[v.Enterprise.Name] = make(map[string]map[string]int64) + } + if list[v.Enterprise.Name][v.UserIdentity.Name] == nil { + list[v.Enterprise.Name][v.UserIdentity.Name] = make(map[string]int64) + } + if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast { + list[v.Enterprise.Name][v.UserIdentity.Name]["breakfast"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] + } + if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch { + list[v.Enterprise.Name][v.UserIdentity.Name]["lunch"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] + } + if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner { + list[v.Enterprise.Name][v.UserIdentity.Name]["dinner"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] + } + + } + + for k1, v1 := range list { + xlsx := excelize.NewFile() + tmpFileName := k1 + "_教师明细_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx" + tmpDownloadPath := "./static/export/central_kitchen_for_school/" + tmpFileName + startDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local) + endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local) + + xlsx.NewSheet(k1) + titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + "] 教师明细"} + xlsx.SetSheetRow(k1, "A1", &titleList) + titleList = []string{"姓名", "早餐", "午餐", "晚餐"} + xlsx.SetSheetRow(k1, "A2", &titleList) + xlsx.MergeCell(k1, "A1", "D1") //合并单元格 + style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`) + if err != nil { + logx.Error(err) + println("<<<>>>>Error:::", err.Error()) + panic(err) + } + xlsx.SetCellStyle(k1, "A1", "D1", style) + xlsx.SetColWidth(k1, "A", "D", 20) + var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0) + j := 3 //表头前两行用了,只能从第三行开始 + for k2, v2 := range v1 { + if v2["breakfast"] == 0 && v2["lunch"] == 0 && v2["dinner"] == 0 { + continue + } + xlsx.SetSheetRow(k1, "A"+strconv.Itoa(j), &[]interface{}{ + k2, v2["breakfast"], v2["lunch"], v2["dinner"]}) + sumBreakfast += v2["breakfast"] + sumLunch += v2["lunch"] + sumDinner += v2["dinner"] + j++ + } + + xlsx.SetSheetRow(k1, "A"+strconv.Itoa(j), &[]interface{}{ + "合计", sumBreakfast, sumLunch, sumDinner}) + //将文件保存至服务器 + xlsx.DeleteSheet("Sheet1") //TODO::只能放这里删除 + err1 := xlsx.SaveAs(tmpDownloadPath) + if err1 != nil { + logx.Error(err1) + println("<<<>>>>Error:::", err1.Error()) + panic(err1) + } + xlsDownloadPath = append(xlsDownloadPath, tmpDownloadPath) + xlsFileName = append(xlsFileName, tmpFileName) + } + + archive, err := os.Create("./static" + downloadPath) //第一步,创建 zip 文件 + if err != nil { + logx.Error(err) + println("<<<>>>>Error:::", err.Error()) + panic(err) + } + defer archive.Close() + + zipWriter := zip.NewWriter(archive) //第二步,创建一个新的 *Writer 对象 + for k, v := range xlsDownloadPath { + w, err1 := zipWriter.Create("教师就餐明细/" + xlsFileName[k]) //向 zip 文件中添加一个文件,返回一个待压缩的文件内容应写入的 Writer + if err1 != nil { + logx.Error(err1) + println("<<<>>>>Error:::", err1.Error()) + panic(err1) + } + + f, err1 := os.Open(v) //打开待压缩的文件 + if err1 != nil { + logx.Error(err1) + println("<<<>>>>Error:::", err1.Error()) + panic(err1) + } + + if _, err1 = io.Copy(w, f); err != nil { + logx.Error(err1) + println("<<<>>>>Error:::", err1.Error()) + panic(err1) + } + if err1 != nil { + logx.Error(err1) + println("<<<>>>>Error:::", err1.Error()) + panic(err1) + } + f.Close() + os.RemoveAll(v) //TODO::移除源文件 + } + + zipWriter.Close() // 第四步,关闭 zip writer,将所有数据写入指向基础 zip 文件的数据流 + if err != nil { + panic(err) + } + break } //新增数据