package service import ( "context" "fmt" "git.hpds.cc/Component/logging" "hpds-iot-web/internal/proto" "hpds-iot-web/model" "net/http" "xorm.io/xorm" "github.com/360EntSecGroup-Skylar/excelize" ) type ReportService interface { ReportList(ctx context.Context, req proto.ReportRequest) (rsp *proto.BaseResponse, err error) GenerateReport(ctx context.Context, req proto.ReportRequest) (rsp *excelize.File, err error) } func NewReportService(engine *xorm.Engine, logger *logging.Logger) ReportService { return &repo{ engine: engine, logger: logger, } } func (rp *repo) ReportList(ctx context.Context, req proto.ReportRequest) (rsp *proto.BaseResponse, err error) { rsp = new(proto.BaseResponse) select { case <-ctx.Done(): err = fmt.Errorf("超时/取消") rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Message = "超时/取消" rsp.Err = ctx.Err() return rsp, ctx.Err() default: data := make([]model.Report, 0) count, err := rp.engine. Where("(? = 0 or m.report_id = ?)", req.ReportId, req.ReportId). And("t.status > 0").Limit(int(req.Size), int(((req.Page)-1)*req.Size)). FindAndCount(&data) if err != nil { goto ReturnPoint } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" rsp = FillPaging(count, req.Page, req.Size, data, rsp) rsp.Err = err return rsp, err } ReturnPoint: if err != nil { rsp.Code = http.StatusInternalServerError rsp.Status = http.StatusText(http.StatusInternalServerError) rsp.Err = err rsp.Message = "失败" } return rsp, err } func (rp *repo) GenerateReport(ctx context.Context, req proto.ReportRequest) (rsp *excelize.File, err error) { //rsp = new(proto.BaseResponse) select { case <-ctx.Done(): err = fmt.Errorf("超时/取消") //rsp.Code = http.StatusInternalServerError //rsp.Status = http.StatusText(http.StatusInternalServerError) //rsp.Message = "超时/取消" //rsp.Err = ctx.Err() return rsp, ctx.Err() default: //data := make([]model.Report, 0) //_, err = rp.engine. // Where("(? = 0 or m.report_id = ?)", req.ReportId, req.ReportId). // And("t.status > 0").Limit(int(req.Size), int(((req.Page)-1)*req.Size)). // FindAndCount(&data) //if err != nil { // goto ReturnPoint //} list := make([]proto.TaskResultItem, 2) list[0] = proto.TaskResultItem{ FileId: 147, FileName: "Y016 2.844 I 1 06272008 Color.jpg", SrcFile: "http://127.0.0.1:9000/jky-data/road/test_dataset/Y016 2.844 I 1 06272008 Color.jpg", DistFile: "http://127.0.0.1:9000/jky-data/road/test_dataset/Y016 2.844 I 1 06272008 Color.jpg", DiseaseType: 4, DiseaseLevel: 2, KPile: "K229+778", UpDown: 1, LineNum: 2, Length: 0.5, Width: 1, Area: 0.5, HorizontalPositions: 0.5, Memo: "有裂缝", } list[1] = proto.TaskResultItem{ FileId: 149, FileName: "C279 0.132 I 1 06272008 Color.jpg", SrcFile: "http://127.0.0.1:9000/jky-data/road/test_dataset/cba/C279 0.132 I 1 06272008 Color.jpg", DistFile: "http://127.0.0.1:9000/jky-data/road/test_dataset/cba/C279 0.132 I 1 06272008 Color.jpg", DiseaseType: 0, DiseaseLevel: 0, KPile: "", UpDown: 0, LineNum: 0, Length: 0, Width: 0, Area: 0, HorizontalPositions: 0, } disTypeList := make([]model.DiseaseType, 0) err = model.DB.Find(&disTypeList) if err != nil { goto ReturnPoint } diseaseTypeList := make(map[int64]string) for _, v := range disTypeList { diseaseTypeList[v.TypeId] = v.TypeName } diseaseLevelList := make(map[int]string) diseaseLevelList[1] = "一级" diseaseLevelList[2] = "二级" diseaseLevelList[3] = "三级" xlsx := excelize.NewFile() xlsx.MergeCell("Sheet1", "A1", "L1") xlsx.MergeCell("Sheet1", "A2", "L2") xlsx.MergeCell("Sheet1", "A3", "L3") xlsx.MergeCell("Sheet1", "A4", "L4") xlsx.SetCellValue("Sheet1", "A4", "病害明细") //序号 路线编码 方向 桩号 车道 病害名称 程度 长度(m) 宽度(m) 面积(㎡) 横向位置 备注 xlsx.SetCellValue("Sheet1", "A5", "序号") xlsx.SetCellValue("Sheet1", "B5", "路线编码") xlsx.SetCellValue("Sheet1", "C5", "方向") xlsx.SetCellValue("Sheet1", "D5", "桩号") xlsx.SetCellValue("Sheet1", "E5", "车道") xlsx.SetCellValue("Sheet1", "F5", "病害名称") xlsx.SetCellValue("Sheet1", "G5", "程度") xlsx.SetCellValue("Sheet1", "H5", "长度(m)") xlsx.SetCellValue("Sheet1", "I5", "宽度(m)") xlsx.SetCellValue("Sheet1", "J5", "面积(㎡)") xlsx.SetCellValue("Sheet1", "K5", "横向位置") xlsx.SetCellValue("Sheet1", "L5", "备注") for k, v := range list { xlsx.SetCellValue("Sheet1", fmt.Sprintf("A%d", k+6), k+1) xlsx.SetCellValue("Sheet1", fmt.Sprintf("B%d", k+6), v.KPile) switch v.UpDown { case 1: xlsx.SetCellValue("Sheet1", fmt.Sprintf("C%d", k+6), "上行") case 2: xlsx.SetCellValue("Sheet1", fmt.Sprintf("C%d", k+6), "下行") } xlsx.SetCellValue("Sheet1", fmt.Sprintf("D%d", k+6), v.KPile) xlsx.SetCellValue("Sheet1", fmt.Sprintf("E%d", k+6), v.LineNum) xlsx.SetCellValue("Sheet1", fmt.Sprintf("F%d", k+6), diseaseTypeList[int64(v.DiseaseType)]) xlsx.SetCellValue("Sheet1", fmt.Sprintf("G%d", k+6), diseaseLevelList[v.DiseaseLevel]) xlsx.SetCellValue("Sheet1", fmt.Sprintf("H%d", k+6), v.Length) xlsx.SetCellValue("Sheet1", fmt.Sprintf("I%d", k+6), v.Width) xlsx.SetCellValue("Sheet1", fmt.Sprintf("J%d", k+6), v.Area) xlsx.SetCellValue("Sheet1", fmt.Sprintf("K%d", k+6), v.HorizontalPositions) xlsx.SetCellValue("Sheet1", fmt.Sprintf("K%d", k+6), v.Memo) } //rsp = xlsx //rsp.Code = http.StatusOK //rsp.Status = http.StatusText(http.StatusOK) //rsp.Message = "成功" //rsp.Data = xlsx //rsp.Err = err return xlsx, err } ReturnPoint: if err != nil { rsp = nil //rsp.Code = http.StatusInternalServerError //rsp.Status = http.StatusText(http.StatusInternalServerError) //rsp.Err = err //rsp.Message = "失败" } return rsp, err }