From 3f3c36750e0c75c7b09fcc366e6eb6c6dabbf242 Mon Sep 17 00:00:00 2001 From: wangjian Date: Thu, 12 Jan 2023 10:21:40 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=89=8D=E7=AB=AF=E5=AF=B9=E6=8E=A5?= =?UTF-8?q?=E5=90=8E=E7=9A=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 1 + config/config.yaml | 1 + internal/handler/disease.go | 78 +++++++++++++++++ internal/proto/request.go | 28 ++++++ internal/proto/response.go | 4 + internal/router/router.go | 10 +++ internal/service/disease.go | 194 +++++++++++++++++++++++++++++++++++++++++ internal/service/fileManage.go | 27 ++---- model/disease.go | 3 +- model/diseaseType.go | 11 +++ model/index.go | 1 + 11 files changed, 339 insertions(+), 19 deletions(-) create mode 100644 internal/handler/disease.go create mode 100644 internal/service/disease.go create mode 100644 model/diseaseType.go diff --git a/config/config.go b/config/config.go index 9a3e4ff..e47f9b9 100644 --- a/config/config.go +++ b/config/config.go @@ -58,6 +58,7 @@ type LogOptions struct { } type MinioConfig struct { + Protocol string `yaml:"protocol"` //http or https Endpoint string `yaml:"endpoint"` AccessKeyId string `yaml:"accessKeyId"` SecretAccessKey string `yaml:"secretAccessKey"` diff --git a/config/config.yaml b/config/config.yaml index 3b62f04..bc8e98c 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -16,6 +16,7 @@ logging: mineData: accessKey: f0bda738033e47ffbfbd5d3f865c19e1 minio: + protocol: http endpoint: 127.0.0.1:9000 accessKeyId: root secretAccessKey: OIxv7QptYBO3 diff --git a/internal/handler/disease.go b/internal/handler/disease.go new file mode 100644 index 0000000..ad28698 --- /dev/null +++ b/internal/handler/disease.go @@ -0,0 +1,78 @@ +package handler + +import ( + "fmt" + "github.com/gin-gonic/gin" + "hpds-iot-web/internal/proto" + "hpds-iot-web/internal/service" + "hpds-iot-web/model" + e "hpds-iot-web/pkg/err" +) + +func (s HandlerService) DiseaseTypeList(c *gin.Context) (data interface{}, err error) { + repo := service.NewDiseaseService(s.AppConfig, s.Engine, s.Logger) + us, _ := c.Get("operatorUser") + userInfo := us.(*model.SystemUser) + var req proto.DiseaseTypeRequest + err = c.ShouldBindJSON(&req) + if err != nil { + go s.SaveLog("DiseaseTypeList", "Manage", "", "", req.ToString(), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return nil, e.NewValidErr(err) + } + if req.Size < 1 { + req.Size = 20 + } + if req.Size > 1000 { + req.Size = 1000 + } + if req.Page < 1 { + req.Page = 1 + } + data, err = repo.DiseaseTypeList(c, req) + go s.SaveLog("获取病害类型列表", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return +} + +func (s HandlerService) AddDiseaseType(c *gin.Context) (data interface{}, err error) { + repo := service.NewDiseaseService(s.AppConfig, s.Engine, s.Logger) + us, _ := c.Get("operatorUser") + userInfo := us.(*model.SystemUser) + var req proto.DiseaseTypeItemRequest + err = c.ShouldBindJSON(&req) + if err != nil { + go s.SaveLog("AddDiseaseType", "Manage", "", "", req.ToString(), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return nil, e.NewValidErr(err) + } + data, err = repo.AddDiseaseType(c, req) + go s.SaveLog("新增病害类型", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return +} +func (s HandlerService) EditDiseaseType(c *gin.Context) (data interface{}, err error) { + repo := service.NewDiseaseService(s.AppConfig, s.Engine, s.Logger) + us, _ := c.Get("operatorUser") + userInfo := us.(*model.SystemUser) + var req proto.DiseaseTypeItemRequest + err = c.ShouldBindJSON(&req) + if err != nil { + go s.SaveLog("AddDiseaseType", "Manage", "", "", req.ToString(), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return nil, e.NewValidErr(err) + } + data, err = repo.EditDiseaseType(c, req) + go s.SaveLog("修改病害类型", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return +} + +func (s HandlerService) DeleteDiseaseType(c *gin.Context) (data interface{}, err error) { + repo := service.NewDiseaseService(s.AppConfig, s.Engine, s.Logger) + us, _ := c.Get("operatorUser") + userInfo := us.(*model.SystemUser) + var req proto.DiseaseTypeItemRequest + err = c.ShouldBindJSON(&req) + if err != nil { + go s.SaveLog("AddDiseaseType", "Manage", "", "", req.ToString(), fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return nil, e.NewValidErr(err) + } + data, err = repo.DeleteDiseaseType(c, req) + go s.SaveLog("删除病害类型", "Manage", "", "", "", fmt.Sprintf("%d", userInfo.UserId), c.Request.RemoteAddr, "") + return +} diff --git a/internal/proto/request.go b/internal/proto/request.go index 6c8f0f1..39c0d94 100644 --- a/internal/proto/request.go +++ b/internal/proto/request.go @@ -405,3 +405,31 @@ func (p BrandItemRequest) ToString() string { } return string(data) } + +type DiseaseTypeRequest struct { + CategoryId int `json:"categoryId"` + Key string `json:"key"` + BasePageList +} + +func (p DiseaseTypeRequest) ToString() string { + data, err := json.Marshal(p) + if err != nil { + return "" + } + return string(data) +} + +type DiseaseTypeItemRequest struct { + TypeId int64 `json:"typeId"` + TypeName string `json:"typeName"` + CategoryId int `json:"categoryId"` +} + +func (p DiseaseTypeItemRequest) ToString() string { + data, err := json.Marshal(p) + if err != nil { + return "" + } + return string(data) +} diff --git a/internal/proto/response.go b/internal/proto/response.go index 3daf080..7ac58fc 100644 --- a/internal/proto/response.go +++ b/internal/proto/response.go @@ -36,3 +36,7 @@ type OwnerItem struct { ChargeUser string `json:"chargeUser"` Phone string `json:"phone"` } + +type UploadResponse struct { + Url string `json:"url"` +} diff --git a/internal/router/router.go b/internal/router/router.go index a2825df..c3755d8 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -139,6 +139,16 @@ func InitRouter(cfg *config.WebConfig, logger *logging.Logger, engine *xorm.Engi brand.POST("/delete", e.ErrorWrapper(hs.DeleteBrand)) } } + disease := r.Group("/disease") + { + diseaseType := disease.Group("/type") + { + diseaseType.POST("/list", e.ErrorWrapper(hs.DiseaseTypeList)) + diseaseType.POST("/add", e.ErrorWrapper(hs.AddDiseaseType)) + diseaseType.POST("/edit", e.ErrorWrapper(hs.EditDiseaseType)) + diseaseType.POST("/delete", e.ErrorWrapper(hs.DeleteDiseaseType)) + } + } } return root } diff --git a/internal/service/disease.go b/internal/service/disease.go new file mode 100644 index 0000000..0c634a3 --- /dev/null +++ b/internal/service/disease.go @@ -0,0 +1,194 @@ +package service + +import ( + "context" + "fmt" + "git.hpds.cc/Component/logging" + "hpds-iot-web/config" + "hpds-iot-web/internal/proto" + "hpds-iot-web/model" + "net/http" + "time" + "xorm.io/xorm" +) + +type DiseaseService interface { + DiseaseTypeList(ctx context.Context, req proto.DiseaseTypeRequest) (rsp *proto.BaseResponse, err error) + AddDiseaseType(ctx context.Context, req proto.DiseaseTypeItemRequest) (rsp *proto.BaseResponse, err error) + EditDiseaseType(ctx context.Context, req proto.DiseaseTypeItemRequest) (rsp *proto.BaseResponse, err error) + DeleteDiseaseType(ctx context.Context, req proto.DiseaseTypeItemRequest) (rsp *proto.BaseResponse, err error) +} + +func NewDiseaseService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) DiseaseService { + return &repo{ + AppConfig: cfg, + engine: engine, + logger: logger, + } +} + +func (rp *repo) DiseaseTypeList(ctx context.Context, req proto.DiseaseTypeRequest) (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.DiseaseType, 0) + count, err := rp.engine.Where("(? = '' or type_name like ?)", req.Key, "%"+req.Key+"%"). + And("(? = 0 or category_id = ?)", req.CategoryId, req.CategoryId). + And("status = 1").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) AddDiseaseType(ctx context.Context, req proto.DiseaseTypeItemRequest) (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: + item := &model.DiseaseType{ + TypeName: req.TypeName, + CategoryId: req.CategoryId, + Status: 1, + CreateAt: time.Now().Unix(), + UpdateAt: time.Now().Unix(), + } + _, err = rp.engine.Insert(item) + if err != nil { + goto ReturnPoint + } + + rsp.Code = http.StatusOK + rsp.Status = http.StatusText(http.StatusOK) + rsp.Message = "新增病害类型成功" + rsp.Err = ctx.Err() + rsp.Data = item + 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) EditDiseaseType(ctx context.Context, req proto.DiseaseTypeItemRequest) (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: + var h bool + item := new(model.DiseaseType) + h, err = rp.engine.ID(req.TypeId).Get(item) + if err != nil { + goto ReturnPoint + } + if !h { + err = fmt.Errorf("未能找到对应的类型") + goto ReturnPoint + } + if len(req.TypeName) > 0 { + item.TypeName = req.TypeName + } + if req.CategoryId > 0 { + item.CategoryId = req.CategoryId + } + item.UpdateAt = time.Now().Unix() + _, err = rp.engine.ID(req.TypeId).AllCols().Update(item) + if err != nil { + goto ReturnPoint + } + rsp.Code = http.StatusOK + rsp.Status = http.StatusText(http.StatusOK) + rsp.Message = "修改病害类型成功" + rsp.Err = ctx.Err() + rsp.Data = item + 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) DeleteDiseaseType(ctx context.Context, req proto.DiseaseTypeItemRequest) (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: + var h bool + item := new(model.DiseaseType) + h, err = rp.engine.ID(req.TypeId).Get(item) + if err != nil { + goto ReturnPoint + } + if !h { + err = fmt.Errorf("未能找到对应的类型") + goto ReturnPoint + } + item.Status = 0 + item.UpdateAt = time.Now().Unix() + _, err = rp.engine.ID(req.TypeId).AllCols().Update(item) + if err != nil { + goto ReturnPoint + } + rsp.Code = http.StatusOK + rsp.Status = http.StatusText(http.StatusOK) + rsp.Message = "删除病害类型成功" + rsp.Err = ctx.Err() + rsp.Data = item + 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 +} diff --git a/internal/service/fileManage.go b/internal/service/fileManage.go index 5aea69a..358ae80 100644 --- a/internal/service/fileManage.go +++ b/internal/service/fileManage.go @@ -13,6 +13,7 @@ import ( "io" "mime/multipart" "net/http" + "strings" "time" "xorm.io/xorm" @@ -21,6 +22,7 @@ import ( type FileService interface { UploadFile(ctx context.Context, req proto.UploadFileRequest) (rsp *proto.BaseResponse, err error) + UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHeader, scene string, creator int64) (data *model.FileManager, err error) } func NewFileService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) FileService { @@ -43,19 +45,23 @@ func (rp *repo) UploadFile(ctx context.Context, req proto.UploadFileRequest) (rs return rsp, ctx.Err() default: list := make([]*model.FileManager, len(req.Files)) + fileUrl := make([]string, len(req.Files)) for k, _ := range req.Files { fileItem, err := rp.UploadFileToMinIo(ctx, req.Files[k], req.Scene, req.Creator) if err != nil { goto ReturnPoint } list[k] = fileItem + fileUrl[k] = fileItem.AccessUrl } _, err = rp.engine.Insert(list) - + res := proto.UploadResponse{ + Url: strings.Join(fileUrl, ","), + } rsp.Code = http.StatusOK rsp.Status = http.StatusText(http.StatusOK) rsp.Message = "成功" - rsp.Data = list + rsp.Data = res rsp.Err = err return rsp, err } @@ -78,21 +84,6 @@ func (rp *repo) UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHe fileName := srcFile.Filename - //out, err := os.Create(fileName) - //defer out.Close() - //if err != nil { - // return nil, err - //} - // - //_, err = io.Copy(out, file) - //if err != nil { - // return nil, err - //} - // - //fileStat, err := out.Stat() - //if err != nil { - // return nil, err - //} opt := &minio.Options{ Creds: credentials.NewStaticV4(rp.AppConfig.Minio.AccessKeyId, rp.AppConfig.Minio.SecretAccessKey, ""), Secure: false, @@ -108,7 +99,7 @@ func (rp *repo) UploadFileToMinIo(ctx context.Context, srcFile *multipart.FileHe return nil, err } fmt.Println("info =====> ", info) - accessUrl := fmt.Sprintf("%s/jky-data/%s", rp.AppConfig.Minio.Endpoint, objPath) + accessUrl := fmt.Sprintf("%s://%s/jky-data/%s", rp.AppConfig.Minio.Protocol, rp.AppConfig.Minio.Endpoint, objPath) md5hash := md5.New() if _, err := io.Copy(md5hash, file); err != nil { diff --git a/model/disease.go b/model/disease.go index 63bbe75..8983058 100644 --- a/model/disease.go +++ b/model/disease.go @@ -4,7 +4,8 @@ package model type Disease struct { DiseaseId int64 `xorm:"not null pk autoincr INT(11)" json:"diseaseId"` DiseaseName string `xorm:"varchar(200) not null " json:"diseaseName"` - DiseaseType int `xorm:"not null SMALLINT default 0" json:"diseaseType"` + DiseaseType int `xorm:"not null INT(11) default 0" json:"diseaseType"` + CategoryId int `xorm:"not null SMALLINT default 1" json:"categoryId"` //病害分类, 1:道路 2:桥梁 3:隧道 4:边坡 DiseaseLevel string `xorm:"varchar(20) not null" json:"diseaseLevel"` DetectionMethod string `xorm:"varchar(200) not null " json:"detectionMethod"` DiseaseDesc string `xorm:"TEXT" json:"diseaseDesc"` diff --git a/model/diseaseType.go b/model/diseaseType.go new file mode 100644 index 0000000..33e862b --- /dev/null +++ b/model/diseaseType.go @@ -0,0 +1,11 @@ +package model + +// DiseaseType 病害类别 +type DiseaseType struct { + TypeId int64 `xorm:"not null pk autoincr INT(11)" json:"typeId"` + TypeName string `xorm:"varchar(200) not null" json:"typeName"` + CategoryId int `xorm:"not null SMALLINT default 1" json:"categoryId"` //病害分类, 1:道路 2:桥梁 3:隧道 4:边坡 + Status int `xorm:"not null INT(11) default 0" json:"status"` + CreateAt int64 `xorm:"created" json:"createAt"` + UpdateAt int64 `xorm:"updated" json:"updateAt"` +} diff --git a/model/index.go b/model/index.go index b39dd31..c07c31d 100644 --- a/model/index.go +++ b/model/index.go @@ -27,6 +27,7 @@ func New(driveName, dsn string) { &Device{}, &DeviceType{}, &Disease{}, + &DiseaseType{}, &FileManager{}, &MatterAttribute{}, &MatterCategory{},