Explorar o código

feat: 附件api迁移

lanjianrong %!s(int64=4) %!d(string=hai) anos
pai
achega
f8b783b48c

+ 1 - 0
.gitignore

@@ -5,3 +5,4 @@
 /web/docs/docs.go
 /web/docs/swagger.json
 /web/docs/swagger.yaml
+/web/main.go

+ 9 - 6
dao/annex_dao.go

@@ -8,6 +8,8 @@
 package dao
 
 import (
+	"fmt"
+
 	"github.com/go-xorm/xorm"
 	"go.mod/models"
 	"go.mod/web/viewmodels"
@@ -30,8 +32,9 @@ func (d *AnnexDao) GetList(dataType int, dataId int) []viewmodels.AnnexListView
 	dataList := make([]viewmodels.AnnexListView, 0)
 	err := d.engine.
 		Asc("create_time").
-		Sql("select f.`account_id`, f.`create_time`, f.`name` as filename, f.`oss_url` as filepath, a.`name` as acount_name from `cm_annex` as f, `cm_project_acount` as a where f.`account_id` = a.`id` and f.`data_type` = ? and f.`data_id` = ?", dataType, dataId).
+		Sql("select f.`account_id`, f.`create_time`, f.`name` as file_name, f.`oss_url` as file_path, a.`name` as account_name from `cm_annex` as f, `cm_project_account` as a where f.`account_id` = a.`id` and f.`data_type` = ? and f.`data_id` = ?", dataType, dataId).
 		Find(&dataList)
+	fmt.Println("dataList", dataList)
 	if err != nil {
 		return dataList
 	}
@@ -40,19 +43,19 @@ func (d *AnnexDao) GetList(dataType int, dataId int) []viewmodels.AnnexListView
 
 // 获取总数
 func (d *AnnexDao) GetCount(dataType int, dataId int) (int64, error) {
-	file := models.CmAnnex{}
-	total, err := d.engine.
-		Where("`data_type`= ? and `data_id` = ?", dataType, dataId).
-		Count(file)
+	file := models.CmAnnex{DataType: dataType, DataId: dataId}
+	total, err := d.engine.Count(&file)
+	fmt.Println(err)
 	if err != nil {
 		return 0, err
 	}
-	return total, err
+	return total, nil
 }
 
 // 批量插入数据
 func (d *AnnexDao) InsertByList(data []models.CmAnnex) error {
 	_, err := d.engine.Insert(&data)
+	// fmt.Println("__________________________________", data)
 	return err
 }
 

+ 20 - 2
services/annex_service.go

@@ -7,7 +7,9 @@
 package services
 
 import (
+	"fmt"
 	"log"
+	"time"
 
 	"go.mod/comm"
 	"go.mod/conf"
@@ -21,6 +23,7 @@ import (
 
 type AnnexService interface {
 	ValidRule(ctx iris.Context) (viewmodels.Annex, error)
+	ValidCreate(ctx iris.Context) (viewmodels.AnnexCreate, error)
 	Create(uid int, dataType int, dataId int, list []viewmodels.AnnexList) error
 	Delete(id int) error
 	GetCounts(dataType int, dataId int) (int64, error)
@@ -68,16 +71,22 @@ func (s *annexService) GetCounts(dataType int, dataId int) (int64, error) {
 // 插入数据
 func (s *annexService) Create(uid int, dataType int, dataId int, list []viewmodels.AnnexList) error {
 	fileData := make([]models.CmAnnex, 0)
+	fmt.Println("-------------------------", list)
 	for _, file := range list {
 		fileVM := models.CmAnnex{}
 		fileVM.DataType = dataType
 		fileVM.DataId = dataId
 		fileVM.AccountId = uid
-		fileVM.Name = file.Name
-		fileVM.OssUrl = file.OssUrl
+		fileVM.Name = file.FileName
+		fileVM.OssUrl = file.FilePath
+		// fileVM.CreateTime = time.Unix(file.CreateTime, 0)
+		// time, _ := time.Parse("2020-11-11 20:11:11", file.CreateTime)
 		fileVM.CreateTime = file.CreateTime
+		fileVM.UpdateTime = time.Now()
+		// fmt.Println("------------------", file.CreateTime)
 		fileData = append(fileData, fileVM)
 	}
+	fmt.Println("fileData", fileData)
 	err := s.daoAnnex.InsertByList(fileData)
 	return err
 }
@@ -103,6 +112,15 @@ func (s *annexService) ValidRule(ctx iris.Context) (viewmodels.Annex, error) {
 		}
 		return annexVaild, err
 	}
+	return annexVaild, nil
+}
 
+func (s *annexService) ValidCreate(ctx iris.Context) (viewmodels.AnnexCreate, error) {
+	annexVaild := viewmodels.AnnexCreate{}
+	err := ctx.ReadJSON(&annexVaild)
+	if err != nil {
+		log.Println("safe-ValidRule-ReadJSON转换异常, error=", err)
+		return annexVaild, err
+	}
 	return annexVaild, nil
 }

+ 1 - 27
services/safe_service.go

@@ -19,9 +19,7 @@ type SafeService interface {
 	Post(data models.CmSafe) error
 	Del(id int) error
 	GetDetail(id int, pid int) viewmodels.SafeDetail
-	// SaveFileInfo(bid int, sid int, uid int, fileList []viewmodels.FileList) error
 	ValidRule(ctx iris.Context) (viewmodels.Safe, error)
-	// ValidFile(ctx iris.Context) (viewmodels.File, error)
 }
 
 // //返回service操作类
@@ -39,6 +37,7 @@ func NewSafeService() SafeService {
 	return &safeService{
 		validDetail:       "/api/safe/detail",
 		daoSafe:           dao.NewSafeDao(datasource.InstanceDbMaster()),
+		daoAnnex:          dao.NewAnnexDao(datasource.InstanceDbMaster()),
 		daoSafeAudit:      dao.NewSafeAuditDao(datasource.InstanceDbMaster()),
 		daoProjectAccount: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
 	}
@@ -111,31 +110,6 @@ func (s *safeService) GetDetail(id int, pid int) viewmodels.SafeDetail {
 	return data
 }
 
-// 安全巡检附件数据存库
-// func (s *safeService) SaveFileInfo(bid int, sid int, uid int, fileList []viewmodels.FileList) error {
-// 	fileData := make([]models.CmSafeFile, 0)
-// 	for _, file := range fileList {
-// 		fileVM := models.CmSafeFile{}
-// 		fileVM.BidsectionId = bid
-// 		fileVM.SafeId = sid
-// 		fileVM.Uid = uid
-// 		createTime, _ := time.Parse("2020-11-11 20:30:12", file.CreateTime)
-// 		fileVM.CreatedTime = createTime
-// 		fileVM.FileName = file.FileName
-// 		fileVM.FilePath = file.FilePath
-// 		fileData = append(fileData, fileVM)
-// 	}
-// 	err := s.daoSafeFile.InsertByList(fileData)
-// 	return err
-// }
-
-// 校验文件规则
-// func (s *safeService) ValidFile(ctx iris.Context) (viewmodels.File, error) {
-// 	fileValid := viewmodels.File{}
-// 	err := ctx.ReadJSON(&fileValid)
-// 	return fileValid, err
-// }
-
 // 规则校验
 func (s *safeService) ValidRule(ctx iris.Context) (viewmodels.Safe, error) {
 	safeVaild := viewmodels.Safe{}

+ 75 - 0
web/api/annex_api.go

@@ -51,3 +51,78 @@ func (c *AnnexApi) Get() {
 		"data": data,
 	})
 }
+
+// @Summary 提交文件记录
+// @Tags 附件
+// @Description 提交文件记录
+// @Accept  json
+// @Produce  json
+// @Security ApiKeyAuth
+// @Param   fileList     body    array     true        "附件数组"
+// @Param   dataType     body    int     true        "类型" eg:"1"
+// @Param   dataId     body    string     true        "数据id"
+// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
+// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
+// @Router /api/file [post]
+func (c *AnnexApi) Post() {
+	// 1.规则验证
+	annexData, err := c.ServiceAnnex.ValidCreate(c.Ctx)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	dataId, err := utils.GetDecryptId(annexData.DataId)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+
+	uid, err := utils.GetProjectAccountId(c.Ctx)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": "未登录或账号失效,请重新登录"})
+		return
+	}
+
+	err = c.ServiceAnnex.Create(uid, annexData.DataType, dataId, annexData.FileList)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	c.Ctx.JSON(iris.Map{
+		"code": 0,
+		"msg":  "请求成功",
+	})
+}
+
+// @Summary 删除附件
+// @Tags 附件
+// @Description 删除附件
+// @Accept  json
+// @Produce  json
+// @Security ApiKeyAuth
+// @Param   id     body    string     true        "数据id"
+// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
+// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
+// @Router /api/file [delete]
+func (c *AnnexApi) Delete() {
+	// 1.规则验证
+	queryId := c.Ctx.URLParam("id")
+	if queryId == "" {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": "id不能为空"})
+		return
+	}
+	id, err := utils.GetDecryptId(queryId)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	err = c.ServiceAnnex.Delete(id)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	c.Ctx.JSON(iris.Map{
+		"code": 0,
+		"msg":  "请求成功",
+	})
+}

+ 0 - 49
web/api/safe_api.go

@@ -188,52 +188,3 @@ func (c *SafeApi) GetDetail() {
 		"data": SafeData,
 	})
 }
-
-// @Summary 巡检附件记录
-// @Tags 安全巡检
-// @Description 上传附件巡检记录
-// @Accept  json
-// @Produce  json
-// @Security ApiKeyAuth
-// @Param   bidsectionId     body    string     true        "标段ID"
-// @Success 200 {object} viewmodels.Safe "{code:0成功,msg:}"
-// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
-// @Router /api/safe/file [post]
-// func (c *SafeApi) PostFile() {
-// 	// 1.规则验证
-// 	safeData, err := c.ServiceSafe.ValidFile(c.Ctx)
-// 	if err != nil {
-// 		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
-// 		return
-// 	}
-// 	bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
-// 	if err != nil {
-// 		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
-// 		return
-// 	}
-// 	sId, err := utils.GetDecryptId(safeData.SaveId)
-// 	if err != nil {
-// 		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
-// 		return
-// 	}
-
-// 	uid, err := utils.GetProjectAccountId(c.Ctx)
-// 	if err != nil {
-// 		c.Ctx.JSON(iris.Map{"code": -1, "msg": "未登录或账号失效,请重新登录"})
-// 		return
-// 	}
-
-// 	err = c.ServiceSafe.SaveFileInfo(bidsectionId, sId, uid, safeData.FileList)
-
-// 	if err != nil {
-// 		c.Ctx.JSON(iris.Map{
-// 			"code": -1,
-// 			"msg":  fmt.Sprintf("%s", err),
-// 		})
-// 		return
-// 	}
-// 	c.Ctx.JSON(iris.Map{
-// 		"code": 0,
-// 		"msg":  "请求成功",
-// 	})
-// }

+ 1 - 1
web/main.go

@@ -51,7 +51,7 @@ func main() {
 
 	// api接口文档配置
 	config := &swagger.Config{
-		URL: "http://cm.com/docs/swagger.json", //The url pointing to API definition
+		URL: "http://localhost:6060/docs/swagger.json", //The url pointing to API definition
 	}
 
 	app.Get("/swagger/{any:path}", swagger.CustomWrapHandler(config, swaggerFiles.Handler))

+ 24 - 3
web/viewmodels/annex.go

@@ -24,19 +24,33 @@ type Annex struct {
 }
 
 type AnnexList struct {
-	Name       string    `form:"name" json:"name"`
-	OssUrl     string    `form:"OSSUrl" json:"OSSUrl"`
+	FileName   string    `form:"filename" json:"filename"`
+	FilePath   string    `form:"filepath" json:"filepath"`
 	CreateTime time.Time `form:"createTime" json:"createTime"`
 }
 
 type AnnexListView struct {
 	FileName    string    `from:"filename" json:"filename"`
 	FilePath    string    `from:"filepath" json:"filepath"`
-	AccountName string    `from:"account_name" json:"acountName"`
+	AccountName string    `from:"accountName" json:"accountName"`
 	AccountId   string    `from:"account_id" json:"acountId"`
 	CreateTime  time.Time `from:"create_time" json:"createTime"`
 }
 
+type AnnexCreate struct {
+	DataType int         `form:"dataType" json:"dataType"`
+	DataId   string      `form:"dataId" json:"dataId"`
+	FileList []AnnexList `form:"fileList" json:"fileList"`
+}
+
+func (l AnnexCreate) ValidateCreate() error {
+	return validation.ValidateStruct(&l,
+		validation.Field(&l.DataType, validation.Required.Error("存储类型不能为空")),
+		validation.Field(&l.DataId, validation.Required.Error("存储ID不能为空")),
+		validation.Field(&l.FileList, validation.Required.Error("附件不能为空")),
+	)
+}
+
 func (l Annex) Validate() error {
 	return validation.ValidateStruct(&l,
 		validation.Field(&l.DataType, validation.Required.Error("存储类型不能为空")),
@@ -51,3 +65,10 @@ func (l Annex) ValidateGet() error {
 		validation.Field(&l.DataId, validation.Required.Error("存储ID不能为空")),
 	)
 }
+
+// func (l Annex) ValidateAdd() error {
+// 	return validation.ValidateStruct(&l,
+// 		validation.Field(&l.DataType, validation.Required.Error("存储类型不能为空")),
+// 		validation.Field(&l.DataId, validation.Required.Error("存储ID不能为空")),
+// 	)
+// }

+ 1 - 1
web/viewmodels/safe.go

@@ -65,7 +65,7 @@ type SafeFile struct {
 	CreateTime  time.Time `from:"create_time" json:"createTime"`
 }
 type Auditors struct {
-	Name string
+	Name string `from:"name" json:"name"`
 }
 
 type SafeListAuditRecord struct {