From 14d2e2a6c017f786efe8c8d790f4631aea695a0c Mon Sep 17 00:00:00 2001 From: dengbiao Date: Thu, 29 Aug 2024 22:01:17 +0800 Subject: [PATCH] update --- app/admin/svc/svc_data_statisstics.go | 359 +++++++++++++++----------- 1 file changed, 208 insertions(+), 151 deletions(-) diff --git a/app/admin/svc/svc_data_statisstics.go b/app/admin/svc/svc_data_statisstics.go index 358c3ca..744e2fc 100644 --- a/app/admin/svc/svc_data_statisstics.go +++ b/app/admin/svc/svc_data_statisstics.go @@ -1152,7 +1152,8 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD if req.EnterPriseKind != 0 { sess.And("enterprise.kind = ?", req.EnterPriseKind) } - _, err := sess. + + _, err := sess.And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent). 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"). @@ -1172,7 +1173,7 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD 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`" + var nativeSql = "SELECT COUNT(*) AS total, kind , enterprise_id, meal_label_name, 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`, `meal_label_name`" for _, v := range req.MealKindList { mealKindListArr = append(mealKindListArr, utils.IntToStr(v)) } @@ -1180,12 +1181,42 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD 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{} + var nativeResultsMap = map[string]map[int]map[string]int64{} + var mealLabelMap = map[int]map[int]map[string]string{} for _, v := range nativeResults { if nativeResultsMap[v["ord_no"]] == nil { - nativeResultsMap[v["ord_no"]] = make(map[int]int64) + nativeResultsMap[v["ord_no"]] = make(map[int]map[string]int64) + } + if nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] == nil { + nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = make(map[string]int64) + } + nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])][v["meal_label_name"]] = utils.StrToInt64(v["total"]) + + if mealLabelMap[utils.StrToInt(v["enterprise_id"])] == nil { + mealLabelMap[utils.StrToInt(v["enterprise_id"])] = make(map[int]map[string]string) + } + if mealLabelMap[utils.StrToInt(v["enterprise_id"])][utils.StrToInt(v["kind"])] == nil { + mealLabelMap[utils.StrToInt(v["enterprise_id"])][utils.StrToInt(v["kind"])] = make(map[string]string) + } + if v["meal_label_name"] == "" { + fmt.Println(v) + } + mealLabelMap[utils.StrToInt(v["enterprise_id"])][utils.StrToInt(v["kind"])][v["meal_label_name"]] = v["meal_label_name"] + } + var mealLabel = map[int][]string{} + for k, v := range mealLabelMap { + if mealLabel[k] == nil { + mealLabel[k] = []string{"年级", "班级"} + } + for _, vv := range v[enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] { + mealLabel[k] = append(mealLabel[k], vv) + } + for _, vv := range v[enum.CentralKitchenForSchoolUserWithDayKindForLunch] { + mealLabel[k] = append(mealLabel[k], vv) + } + for _, vv := range v[enum.CentralKitchenForSchoolUserWithDayKindForDinner] { + mealLabel[k] = append(mealLabel[k], vv) } - nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"]) } centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{} @@ -1203,17 +1234,10 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD list[v.Enterprise.Name][v.Grade.Name][v.Class.Name] = make(map[string]int64) } - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast && nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] != 0 { - list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["breakfast"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] - nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] = 0 - } - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch && nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] != 0 { - list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["lunch"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] - nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] = 0 - } - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner && nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] != 0 { - list[v.Enterprise.Name][v.Grade.Name][v.Class.Name]["dinner"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] - nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] = 0 + for _, vv := range nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo] { + for kkk, vvv := range vv { + list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][kkk] = vvv + } } } @@ -1225,7 +1249,18 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD endDate, _ := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local) titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " (" + k1 + ") "} xlsx.SetSheetRow("Sheet1", "A1", &titleList) - titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"} + + tmpEnterpriseDb := db.EnterpriseDb{} + tmpEnterpriseDb.Set() + tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1) + if err11 != nil { + logx.Error(err11) + println("<<<>>>>Error:::", err11.Error()) + panic(err11) + } + + //titleList = []string{"年级", "班级", "早餐", "午餐", "晚餐"} + titleList = mealLabel[tmpEnterprise.Id] xlsx.SetSheetRow("Sheet1", "A2", &titleList) xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格 @@ -1235,20 +1270,11 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD println("<<<>>>>Error:::", err.Error()) panic(err) } - xlsx.SetCellStyle("Sheet1", "A1", "E1", style) - xlsx.SetColWidth("Sheet1", "A", "E", 20) + xlsx.SetCellStyle("Sheet1", "A1", "M1", style) + xlsx.SetColWidth("Sheet1", "A", "M", 20) j := 3 //表头前两行用了,只能从第三行开始 - var sumClassNum, sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0), int64(0) - - tmpEnterpriseDb := db.EnterpriseDb{} - tmpEnterpriseDb.Set() - tmpEnterprise, err11 := tmpEnterpriseDb.GetEnterpriseByName(k1) - if err11 != nil { - logx.Error(err11) - println("<<<>>>>Error:::", err11.Error()) - panic(err11) - } + var sumTotal = map[string]int64{} tmpClassDb := db.ClassDb{} tmpClassDb.Set(0) classList, err := tmpClassDb.FindClassAscByEnterprise(tmpEnterprise.Id) @@ -1272,19 +1298,27 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD //若班级为空,则为教师餐 k3 = "教师餐" } - - xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{ - k2, k3, v3["breakfast"], v3["lunch"], v3["dinner"]}) - sumClassNum++ - sumBreakfast += v3["breakfast"] - sumLunch += v3["lunch"] - sumDinner += v3["dinner"] + var slice = []interface{}{k2, k3} + for key, meal := range mealLabel[tmpEnterprise.Id] { + if key != 0 && key != 1 { + slice = append(slice, v3[meal]) + sumTotal[meal] += v3[meal] + } + } + xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &slice) j++ } } } - xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{ - "合计", sumClassNum, sumBreakfast, sumLunch, sumDinner}) + var slice = []interface{}{"合计", ""} + for key, meal := range mealLabel[tmpEnterprise.Id] { + if key != 0 && key != 1 { + slice = append(slice, sumTotal[meal]) + sumTotal[meal] += sumTotal[meal] + } + } + xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &slice) + //将文件保存至服务器 err1 := xlsx.SaveAs(tmpDownloadPath) if err1 != nil { @@ -1376,7 +1410,7 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD if req.EnterPriseKind != 0 { sess.And("enterprise.kind = ?", req.EnterPriseKind) } - _, err := sess. + _, err := sess.And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent). 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"). @@ -1397,7 +1431,7 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD 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`" + var nativeSql = "SELECT COUNT(*) AS total, kind , enterprise_id, meal_label_name, 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`, `meal_label_name`" for _, v := range req.MealKindList { mealKindListArr = append(mealKindListArr, utils.IntToStr(v)) } @@ -1405,12 +1439,42 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD 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{} + var nativeResultsMap = map[string]map[int]map[string]int64{} + var mealLabelMap = map[int]map[int]map[string]string{} for _, v := range nativeResults { if nativeResultsMap[v["ord_no"]] == nil { - nativeResultsMap[v["ord_no"]] = make(map[int]int64) + nativeResultsMap[v["ord_no"]] = make(map[int]map[string]int64) + } + if nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] == nil { + nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = make(map[string]int64) + } + nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])][v["meal_label_name"]] = utils.StrToInt64(v["total"]) + + if mealLabelMap[utils.StrToInt(v["enterprise_id"])] == nil { + mealLabelMap[utils.StrToInt(v["enterprise_id"])] = make(map[int]map[string]string) + } + if mealLabelMap[utils.StrToInt(v["enterprise_id"])][utils.StrToInt(v["kind"])] == nil { + mealLabelMap[utils.StrToInt(v["enterprise_id"])][utils.StrToInt(v["kind"])] = make(map[string]string) + } + if v["meal_label_name"] == "" { + fmt.Println(v) + } + mealLabelMap[utils.StrToInt(v["enterprise_id"])][utils.StrToInt(v["kind"])][v["meal_label_name"]] = v["meal_label_name"] + } + var mealLabel = map[int][]string{} + for k, v := range mealLabelMap { + if mealLabel[k] == nil { + mealLabel[k] = []string{"年级"} + } + for _, vv := range v[enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] { + mealLabel[k] = append(mealLabel[k], vv) + } + for _, vv := range v[enum.CentralKitchenForSchoolUserWithDayKindForLunch] { + mealLabel[k] = append(mealLabel[k], vv) + } + for _, vv := range v[enum.CentralKitchenForSchoolUserWithDayKindForDinner] { + mealLabel[k] = append(mealLabel[k], vv) } - nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"]) } var list = map[string]map[string]map[string]map[string]map[string]int64{} @@ -1431,40 +1495,11 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name] = make(map[string]int64) } - //count, err2 := centralKitchenForSchoolUserWithDayDb.CountCentralKitchenForSchoolUserWithDayByOrdNo( - // v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo, - // enum.CentralKitchenForSchoolUserWithDayStateForCancel, enum.CentralKitchenForSchoolUserWithDayStateForCanceling, - // req.StartDate, req.EndDate, []int{v.CentralKitchenForSchoolPackageOrdForReserve.Kind}) - //if err2 != nil { - // logx.Error(err2) - // println("<<<>>>>Error:::", err2.Error()) - // return - //} - //if count == 0 { - // continue - //} - //time.Sleep(2 * time.Millisecond) // 2毫秒 - // - //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast { - // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += count - //} - //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch { - // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += count - //} - //if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner { - // list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += count - //} - - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast { - list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["breakfast"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] - } - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch { - list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["lunch"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] - } - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner { - list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name]["dinner"] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] + for _, vv := range nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo] { + for kkk, vvv := range vv { + list[v.Enterprise.Name][v.Grade.Name][v.Class.Name][v.UserIdentity.Name][kkk] = vvv + } } - } for k1, v1 := range list { @@ -1504,7 +1539,8 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD xlsx.NewSheet(k3) titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " [" + k1 + ", " + k3 + "] 班级明细"} xlsx.SetSheetRow(k3, "A1", &titleList) - titleList = []string{"姓名", "早餐", "午餐", "晚餐"} + //titleList = []string{"姓名", "早餐", "午餐", "晚餐"} + titleList = mealLabel[tmpEnterprise.Id] xlsx.SetSheetRow(k3, "A2", &titleList) xlsx.MergeCell(k3, "A1", "D1") //合并单元格 style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`) @@ -1513,24 +1549,30 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD println("<<<>>>>Error:::", err.Error()) panic(err) } - xlsx.SetCellStyle(k3, "A1", "D1", style) - xlsx.SetColWidth(k3, "A", "D", 20) + xlsx.SetCellStyle(k3, "A1", "M1", style) + xlsx.SetColWidth(k3, "A", "M", 20) - var sumBreakfast, sumLunch, sumDinner = int64(0), int64(0), int64(0) + var sumTotal = map[string]int64{} j := 3 //表头前两行用了,只能从第三行开始 for k4, v4 := range v3 { - if v4["breakfast"] == 0 && v4["lunch"] == 0 && v4["dinner"] == 0 { - continue + var slice = []interface{}{k4} + for key, meal := range mealLabel[tmpEnterprise.Id] { + if key != 0 { + slice = append(slice, v4[meal]) + sumTotal[meal] += v4[meal] + } } - xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{ - k4, v4["breakfast"], v4["lunch"], v4["dinner"]}) - sumBreakfast += v4["breakfast"] - sumLunch += v4["lunch"] - sumDinner += v4["dinner"] + xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &slice) j++ } - xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &[]interface{}{ - "合计", sumBreakfast, sumLunch, sumDinner}) + var slice = []interface{}{"合计"} + for key, meal := range mealLabel[tmpEnterprise.Id] { + if key != 0 { + slice = append(slice, sumTotal[meal]) + sumTotal[meal] += sumTotal[meal] + } + } + xlsx.SetSheetRow(k3, "A"+strconv.Itoa(j), &slice) } } } @@ -2045,36 +2087,36 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD 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) - //} + var months []time.Month if req.StartDate != "" { - sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.StartDate). - And("central_kitchen_for_school_package_ord_for_reserve.meal_time_end >= ?", 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 req.StartDate != "" { + // sess.And("central_kitchen_for_school_package_ord_for_reserve.meal_time_start <= ?", req.StartDate). + // And("central_kitchen_for_school_package_ord_for_reserve.meal_time_end >= ?", req.StartDate) + //} 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) } - _, err := sess. + _, err := sess.And("user_identity.identity = ?", enum.UserIdentityForCentralKitchenForStudent). 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"). @@ -2089,7 +2131,7 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD 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`" + var nativeSql = "SELECT COUNT(*) AS total, kind ,meal_label_name, 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)) } @@ -2097,75 +2139,90 @@ func CentralKitchenForSchoolDataStatisticsExport(req md.CentralKitchenForSchoolD 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{} + var nativeResultsMap = map[string]map[int]map[string]int64{} + var mealLabelMap = map[int]map[string]string{} for _, v := range nativeResults { if nativeResultsMap[v["ord_no"]] == nil { - nativeResultsMap[v["ord_no"]] = make(map[int]int64) + nativeResultsMap[v["ord_no"]] = make(map[int]map[string]int64) } - nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = utils.StrToInt64(v["total"]) + if nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] == nil { + nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])] = make(map[string]int64) + } + nativeResultsMap[v["ord_no"]][utils.StrToInt(v["kind"])][v["meal_label_name"]] = utils.StrToInt64(v["total"]) + + if mealLabelMap[utils.StrToInt(v["kind"])] == nil { + mealLabelMap[utils.StrToInt(v["kind"])] = make(map[string]string) + } + if v["meal_label_name"] == "" { + fmt.Println(v) + } + mealLabelMap[utils.StrToInt(v["kind"])][v["meal_label_name"]] = v["meal_label_name"] + } + var mealLabel = []string{"学校名称", "预定总数"} + for _, v := range mealLabelMap[enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] { + mealLabel = append(mealLabel, v) + } + for _, v := range mealLabelMap[enum.CentralKitchenForSchoolUserWithDayKindForLunch] { + mealLabel = append(mealLabel, v) + } + for _, v := range mealLabelMap[enum.CentralKitchenForSchoolUserWithDayKindForDinner] { + mealLabel = append(mealLabel, v) } var list = map[string]map[string]int64{} centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{} centralKitchenForSchoolUserWithDayDb.Set(0) + var count int64 for _, v := range m { if list[v.Enterprise.Name] == nil { list[v.Enterprise.Name] = map[string]int64{} } - var count int64 - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForBreakfast { - count = nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] - list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForBreakfast] - } - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForLunch { - count = nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] - list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForLunch] - } - if v.CentralKitchenForSchoolPackageOrdForReserve.Kind == enum.CentralKitchenForSchoolUserWithDayKindForDinner { - count = nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] - list[v.Enterprise.Name][enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] += nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo][enum.CentralKitchenForSchoolUserWithDayKindForDinner] + for _, vv := range nativeResultsMap[v.CentralKitchenForSchoolPackageOrdForReserve.OutTradeNo] { + for kkk, vvv := range vv { + list[v.Enterprise.Name][kkk] += vvv + list[v.Enterprise.Name]["count"] += vvv + count += vvv + } } - list[v.Enterprise.Name]["count"] += count } titleList = []string{startDate.Format("2006-01-02") + "~~" + endDate.Format("2006-01-02") + " 预定数量统计表"} xlsx.SetSheetRow("Sheet1", "A1", &titleList) - titleList = []string{"学校名称", "预定总数", - enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String(), - enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String(), - enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String(), - } + titleList = mealLabel xlsx.SetSheetRow("Sheet1", "A2", &titleList) - xlsx.MergeCell("Sheet1", "A1", "E1") //合并单元格 + xlsx.MergeCell("Sheet1", "A1", "M1") //合并单元格 style, err := xlsx.NewStyle(`{"fill":{"type":"pattern","color":["FFFF00"],"pattern":1}, "alignment":{"horizontal":"center"}}`) if err != nil { logx.Error(err) - println("<<<>>>>Error:::", err.Error()) + println("<<<>>>>Error:::", err.Error()) panic(err) } - xlsx.SetCellStyle("Sheet1", "A1", "E1", style) + xlsx.SetCellStyle("Sheet1", "A1", "M1", style) xlsx.SetColWidth("Sheet1", "A", "A", 30) - xlsx.SetColWidth("Sheet1", "B", "E", 15) + xlsx.SetColWidth("Sheet1", "B", "M", 15) j := 3 //表头前两行用了,只能从第三行开始 - total := int64(0) - total1 := int64(0) - total2 := int64(0) - total3 := int64(0) + var sumTotal = map[string]int64{} for k, v := range list { - xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{k, v["count"], - v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()], - v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()], - v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()]}) - total += v["count"] - total1 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForBreakfast).String()] - total2 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForLunch).String()] - total3 += v[enum.CentralKitchenForSchoolUserWithDayKind(enum.CentralKitchenForSchoolUserWithDayKindForDinner).String()] + var slice = []interface{}{k, v["count"]} + for key, meal := range mealLabel { + if key != 0 && key != 1 { + slice = append(slice, v[meal]) + sumTotal[meal] += v[meal] + } + } + xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &slice) j++ } - xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &[]interface{}{ - "合计", total, total1, total2, total3}) + var slice = []interface{}{"合计", count} + for key, meal := range mealLabel { + if key != 0 && key != 1 { + slice = append(slice, sumTotal[meal]) + sumTotal[meal] += sumTotal[meal] + } + } + xlsx.SetSheetRow("Sheet1", "A"+strconv.Itoa(j), &slice) //将文件保存至服务器 xlsx.SaveAs("./static" + downloadPath) break