diff --git a/app/admin/hdl/hdl_data_statistics.go b/app/admin/hdl/hdl_data_statistics.go index e00ec72..21deae9 100644 --- a/app/admin/hdl/hdl_data_statistics.go +++ b/app/admin/hdl/hdl_data_statistics.go @@ -297,9 +297,13 @@ func SelfSupportForSchoolDataStatisticsList(c *gin.Context) { "value": 2, }, { - "name": "就餐数据统计表", + "name": "学校就餐数据统计表", "value": 3, }, + { + "name": "设备就餐数据统计表", + "value": 4, + }, }, }, nil) return diff --git a/app/admin/svc/svc_data_statisstics.go b/app/admin/svc/svc_data_statisstics.go index c4145c4..eac2fa1 100644 --- a/app/admin/svc/svc_data_statisstics.go +++ b/app/admin/svc/svc_data_statisstics.go @@ -532,6 +532,174 @@ func SelfSupportForSchoolDataStatisticsExport(req md.SelfSupportForSchoolDataSta //将文件保存至服务器 xlsx.SaveAs("./static" + downloadPath) break + case 4: + //设备就餐数据统计表 + + //查询出所有的设备 + var deviceList []*model.Device + if req.EnterpriseId != 0 { + err := db.Db.Where("enterprise_id != 0").Find(&deviceList) + if err != nil { + logx.Error(err) + println("<<<>>>>1111Error:::", err.Error()) + return + } + } else { + err := db.Db.Where("enterprise_id = ?", req.EnterpriseId).Find(&deviceList) + if err != nil { + logx.Error(err) + println("<<<>>>>1111Error:::", err.Error()) + return + } + } + + date1, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartDate, time.Local) + if err != nil { + logx.Error(err) + println("<<<>>>>Error:::", err.Error()) + return + } + date2, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndDate, time.Local) + if err != nil { + logx.Error(err) + println("<<<>>>>Error:::", err.Error()) + return + } + diffDays := int(date2.Sub(date1).Hours() / 24) + + var deviceListMapping = map[string]model.Device{} + //整合数据 + var data = map[string]map[string]map[string]map[string]float64{} + var xlsFileName, xlsDownloadPath []string + for _, v := range deviceList { + deviceListMapping[v.DeviceSn] = *v + var m []*db.SelfSupportForSchoolOrdWithUserIdentity + sess := db.Db.Where("device_sn = ?", v.DeviceSn).Desc("self_support_for_school_ord.id") + if req.EnterpriseId != 0 { + sess.And("self_support_for_school_ord.enterprise_id =?", req.EnterpriseId) + } + if req.StartDate != "" { + sess.And("self_support_for_school_ord.face_time >= ?", req.StartDate) + } + if req.EndDate != "" { + sess.And("self_support_for_school_ord.face_time <= ?", req.EndDate) + } + _, err = sess.Join("LEFT", "enterprise", "enterprise.id = self_support_for_school_ord.enterprise_id").FindAndCount(&m) + if err != nil { + logx.Error(err) + println("<<<>>>>Error:::", err.Error()) + return + } + for _, vv := range m { + tmpTime, err1 := time.ParseInLocation("2006-01-02 15:04:05", vv.SelfSupportForSchoolOrd.FaceTime, time.Local) + if err1 != nil { + logx.Error(err1) + println("<<<>>>>Error:::", err1.Error()) + return + } + tmpDate := tmpTime.Format("2006-01-02") + if data[vv.Enterprise.Name] == nil { + data[vv.Enterprise.Name] = make(map[string]map[string]map[string]float64) + } + if data[vv.Enterprise.Name][v.Name] == nil { + data[vv.Enterprise.Name][v.Name] = make(map[string]map[string]float64) + } + if data[vv.Enterprise.Name][v.Name][tmpDate] == nil { + data[vv.Enterprise.Name][v.Name][tmpDate] = make(map[string]float64) + } + data[vv.Enterprise.Name][v.Name][tmpDate]["num"]++ + data[vv.Enterprise.Name][v.Name][tmpDate]["amount"] += utils.StrToFloat64(vv.SelfSupportForSchoolOrd.TradeAmount) + } + } + + for k1, v := range data { + tmpFileName := k1 + "设备就餐数据统计表_" + utils.Int64ToStr(now.UnixMilli()) + ".xlsx" + tmpDownloadPath := "./static/export/self_support_for_school/" + tmpFileName + xlsx := excelize.NewFile() + var sumNum, sumAmount = float64(0), float64(0) + + for k2, vv := range v { + xlsx.NewSheet(k2) + titleList = []string{" 设 备 分 餐 数 据 统 计 表 "} + xlsx.SetSheetRow(k2, "A1", &titleList) + titleList = []string{"日 期", "消费笔数", "消费金额"} + xlsx.SetSheetRow(k2, "A2", &titleList) + 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(k2, "A1", "C1", style) + xlsx.SetColWidth(k2, "A", "C", 20) + j := 3 //表头前两行用了,只能从第三行开始 + + for i := 0; i <= diffDays; i++ { + date := date1.AddDate(0, 0, i).Format("2006-01-02") + if vv[date] == nil { + xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{date, vv[date]["num"], vv[date]["amount"]}) + sumNum += vv[date]["num"] + sumAmount += vv[date]["amount"] + j++ + } + } + xlsx.SetSheetRow(k2, "A"+strconv.Itoa(j), &[]interface{}{ + "合计", sumNum, sumAmount}) + //将文件保存至服务器 + 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 } //新增数据