lanjianrong 4 vuotta sitten
vanhempi
commit
9369248486

+ 64 - 0
dao/annex_dao.go

@@ -0,0 +1,64 @@
+/*
+ * @description: 安全巡检数据库操作相关
+ * @Author: LanJianRong
+ * @Date: 2020-11-20
+ * @FilePath: \construction_management\dao\safe_dao.go
+ */
+
+package dao
+
+import (
+	"github.com/go-xorm/xorm"
+	"go.mod/models"
+	"go.mod/web/viewmodels"
+)
+
+// 数据库操作引擎
+type AnnexDao struct {
+	engine *xorm.Engine
+}
+
+// 获得一个DAO对象
+func NewAnnexDao(engine *xorm.Engine) *AnnexDao {
+	return &AnnexDao{
+		engine: engine,
+	}
+}
+
+// 获取附件列表
+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).
+		Find(&dataList)
+	if err != nil {
+		return dataList
+	}
+	return dataList
+}
+
+// 获取总数
+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)
+	if err != nil {
+		return 0, err
+	}
+	return total, err
+}
+
+// 批量插入数据
+func (d *AnnexDao) InsertByList(data []models.CmAnnex) error {
+	_, err := d.engine.Insert(&data)
+	return err
+}
+
+// 删除数据(记录id)
+func (d *AnnexDao) DeleteById(id int) error {
+	file := models.CmAnnex{Id: id}
+	_, err := d.engine.Delete(&file)
+	return err
+}

+ 1 - 1
dao/safe_file_dao.go

@@ -29,7 +29,7 @@ func (d *SafeFileDao) GetListBySid(id int) []models.CmSafeFile {
 	dataList := make([]models.CmSafeFile, 0)
 	err := d.engine.
 		Asc("create_time").
-		Where("safe_id=?", id).
+		Sql("select f.`create_time`, f.`file_name`, f.`file_path`, a.`name`from `cm_safe_file` as f, `cm_project_acount` as a where f.`uid` = a.`id` and f.`safe_id` = ?", id).
 		Find(&dataList)
 	if err != nil {
 		return dataList

+ 108 - 0
services/annex_service.go

@@ -0,0 +1,108 @@
+/*
+ * @description:附件相关
+ * @Author: Lanjianrong
+ * @Date: 2020-12-04 16:13:32
+ * @FilePath: \construction_management\services\annex_service.go
+ */
+package services
+
+import (
+	"log"
+
+	"go.mod/comm"
+	"go.mod/conf"
+	"go.mod/models"
+
+	"github.com/kataras/iris/v12"
+	"go.mod/dao"
+	"go.mod/datasource"
+	"go.mod/web/viewmodels"
+)
+
+type AnnexService interface {
+	ValidRule(ctx iris.Context) (viewmodels.Annex, error)
+	Create(uid int, dataType int, dataId int, list []viewmodels.AnnexList) error
+	Delete(id int) error
+	GetCounts(dataType int, dataId int) (int64, error)
+	Get(dataType int, dataId int) []viewmodels.AnnexListView
+}
+
+// //返回service操作类
+type annexService struct {
+	daoAnnex  *dao.AnnexDao
+	valideGet string
+	// validDetail       string
+}
+
+// 创建项目用户service
+func NewAnnexService() AnnexService {
+	return &annexService{
+		valideGet: "/api/file",
+		daoAnnex:  dao.NewAnnexDao(datasource.InstanceDbMaster()),
+	}
+}
+
+// 附件列表
+func (s *annexService) Get(dataType int, dataId int) []viewmodels.AnnexListView {
+	fileData := make([]viewmodels.AnnexListView, 0)
+	list := s.daoAnnex.GetList(dataType, dataId)
+	for _, item := range list {
+		annexVM := viewmodels.AnnexListView{}
+		uid, _ := comm.AesEncrypt(item.AccountId, conf.SignSecret)
+		annexVM.AccountId = uid
+		annexVM.AccountName = item.AccountName
+		annexVM.CreateTime = item.CreateTime
+		annexVM.FileName = item.FileName
+		annexVM.FilePath = item.FilePath
+		fileData = append(fileData, annexVM)
+	}
+	return fileData
+}
+
+// 计算附件总数
+func (s *annexService) GetCounts(dataType int, dataId int) (int64, error) {
+	count, err := s.daoAnnex.GetCount(dataType, dataId)
+	return count, err
+}
+
+// 插入数据
+func (s *annexService) Create(uid int, dataType int, dataId int, list []viewmodels.AnnexList) error {
+	fileData := make([]models.CmAnnex, 0)
+	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.CreateTime = file.CreateTime
+		fileData = append(fileData, fileVM)
+	}
+	err := s.daoAnnex.InsertByList(fileData)
+	return err
+}
+
+// 删除数据
+func (s *annexService) Delete(id int) error {
+	err := s.daoAnnex.DeleteById(id)
+	return err
+}
+
+// 规则校验
+func (s *annexService) ValidRule(ctx iris.Context) (viewmodels.Annex, error) {
+	annexVaild := viewmodels.Annex{}
+	if ctx.Method() == "GET" {
+		err := ctx.ReadForm(&annexVaild)
+		if err != nil {
+			log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
+			return annexVaild, err
+		}
+		if ctx.Path() == s.valideGet {
+			err = annexVaild.ValidateGet()
+
+		}
+		return annexVaild, err
+	}
+
+	return annexVaild, nil
+}

+ 5 - 5
services/safe_service.go

@@ -29,8 +29,8 @@ type SafeService interface {
 type safeService struct {
 	daoSafe           *dao.SafeDao
 	daoSafeAudit      *dao.SafeAuditDao
-	daoSafeFile       *dao.SafeFileDao
 	daoProjectAccount *dao.ProjectAccountDao
+	daoAnnex          *dao.AnnexDao
 	daoRule           *dao.RuleDao
 	validDetail       string
 }
@@ -41,7 +41,6 @@ func NewSafeService() SafeService {
 		validDetail:       "/api/safe/detail",
 		daoSafe:           dao.NewSafeDao(datasource.InstanceDbMaster()),
 		daoSafeAudit:      dao.NewSafeAuditDao(datasource.InstanceDbMaster()),
-		daoSafeFile:       dao.NewSafeFileDao(datasource.InstanceDbMaster()),
 		daoProjectAccount: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
 	}
 }
@@ -61,7 +60,8 @@ func (s *safeService) Get(id int, pid int) []viewmodels.SafeList {
 		safeVM.InspectionDetail = item.InspectionDetail
 		safeVM.Position = item.Position
 		safeVM.Status = item.Status
-		safeVM.FileCounts = s.daoSafeFile.GetCount(item.Id)
+		counts, _ := s.daoAnnex.GetCount(3, item.Id)
+		safeVM.FileCounts = counts
 		safeList = append(safeList, safeVM)
 	}
 	return safeList
@@ -105,8 +105,8 @@ func (s *safeService) GetDetail(id int, pid int) viewmodels.SafeDetail {
 	data.Demand = safeData.Demand
 	account := s.daoProjectAccount.Get(safeData.Uid, pid)
 	data.AuditName = account.Name
-	data.CreateTime = safeData.CreateTime.Format(conf.SysTimeform)
-	data.FileList = s.daoSafeFile.GetListBySid(safeData.Id)
+	data.CreateTime = safeData.CreateTime
+	data.FileList = s.daoAnnex.GetList(3, safeData.Id)
 	auditors := s.daoSafeAudit.GetAuditors(safeData.Id, safeData.Times, account.Id)
 	data.Auditors = auditors
 	return data

+ 49 - 0
web/api/annex_api.go

@@ -0,0 +1,49 @@
+/*
+ * @description: 附件相关的接口api
+ * @Author: LanJianRong
+ * @Date: 2020-12-07 10:26:49
+ * @FilePath: \construction_management\web\api\annex_api.go
+ */
+package api
+
+import (
+	"fmt"
+
+	"github.com/kataras/iris/v12"
+	"go.mod/services"
+	"go.mod/web/utils"
+)
+
+type AnnexApi struct {
+	//框架-web应用上下文环境
+	Ctx iris.Context
+	// 需要用的service
+	ServiceAnnex services.AnnexService
+}
+
+// @Summary 获取附件列表
+// @Tags 附件
+// @Description 获得附件列表
+// @Accept  json
+// @Produce  json
+// @Security ApiKeyAuth
+// @Param   dataType     path    int     true        "附件类型"
+// @Param   dataId     path    string     true        "源数据id"
+// @Success 200 {object} viewmodels.AnnexList "{code:0成功,data:viewmodels.AnnexList,msg:}"
+// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
+// @Router /api/file [get]
+func (c *AnnexApi) Get() {
+	// 1.规则验证
+	annexData, err := c.ServiceAnnex.ValidRule(c.Ctx)
+	if err != nil {
+		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
+		return
+	}
+	dataId, err := utils.GetDecryptId(annexData.DataId)
+	data = c.ServiceAnnex.Get(int(annexData.DataType), dataId)
+	c.Ctx.JSON(iris.Map{
+		"code": 0,
+		"msg":  "请求成功",
+		"data": data,
+	})
+}

+ 8 - 0
web/routes/routes.go

@@ -27,6 +27,7 @@ func Configure(b *bootstrap.Bootstrapper) {
 	// RpcService := services.NewRpcService()
 	SafeService := services.NewSafeService()
 	RuleService := services.NewRuleService()
+	AnnexService := services.NewAnnexService()
 	//CSRF相关
 	b.Use(middleware.SetCsrf)
 
@@ -151,4 +152,11 @@ func Configure(b *bootstrap.Bootstrapper) {
 	apiRule.Router.Use(middleware.SessionsAuth)
 	apiRule.Router.Use(middleware.AccessAuth)
 	apiRule.Handle(new(api.RuleApi))
+
+	// file
+	apiAnnex := mvc.New(b.Party("/api/file"))
+	apiAnnex.Register(AnnexService)
+	apiAnnex.Router.Use(middleware.SessionsAuth)
+	apiAnnex.Router.Use(middleware.AccessAuth)
+	apiAnnex.Handle(new(api.AnnexApi))
 }

+ 53 - 0
web/viewmodels/annex.go

@@ -0,0 +1,53 @@
+/*
+ * @description:附件相关
+ * @Author: LanJianRong
+ * @Date: 2020-12-04 16:00:19
+ * @FilePath: \construction_management\web\viewmodels\annex.go
+ */
+package viewmodels
+
+import (
+	"time"
+
+	validation "github.com/go-ozzo/ozzo-validation/v3"
+)
+
+type Annex struct {
+	Id         string    `form:"id" json:"id"`
+	DataType   string    `form:"dataType" json:"dataType"`
+	DataId     string    `form:"dataId" json:"dataId"`
+	Name       string    `form:"name" json:"name"`
+	OssUrl     string    `form:"OSSUrl" json:"OSSUrl"`
+	AccountId  string    `form:"accoundId" json:"accoundId"`
+	CreateTime time.Time `form:"createTime" json:"createTime"`
+	UpdateTime time.Time `form:"updateTime" json:"updateTime"`
+}
+
+type AnnexList struct {
+	Name       string    `form:"name" json:"name"`
+	OssUrl     string    `form:"OSSUrl" json:"OSSUrl"`
+	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"`
+	AccountId   string    `from:"account_id" json:"acountId"`
+	CreateTime  time.Time `from:"create_time" json:"createTime"`
+}
+
+func (l Annex) Validate() error {
+	return validation.ValidateStruct(&l,
+		validation.Field(&l.DataType, validation.Required.Error("存储类型不能为空")),
+		validation.Field(&l.DataId, validation.Required.Error("存储ID不能为空")),
+		validation.Field(&l.AccountId, validation.Required.Error("账号ID不能为空")),
+	)
+}
+
+func (l Annex) ValidateGet() error {
+	return validation.ValidateStruct(&l,
+		validation.Field(&l.DataType, validation.Required.Error("存储类型不能为空")),
+		validation.Field(&l.DataId, validation.Required.Error("存储ID不能为空")),
+	)
+}

+ 4 - 5
web/viewmodels/rule.go

@@ -1,13 +1,12 @@
-package viewmodels
-
-import validation "github.com/go-ozzo/ozzo-validation/v3"
-
 /*
  * @description: 编号规则
  * @Author: LanJianRong
  * @Date: 2020-11-27
- * @FilePath: \construction_management\web\viewmodels\safe.go
+ * @FilePath: \construction_management\web\viewmodels\rule.go
  */
+package viewmodels
+
+import validation "github.com/go-ozzo/ozzo-validation/v3"
 
 type Rule struct {
 	Id           string `form:"id" json:"id" `

+ 20 - 13
web/viewmodels/safe.go

@@ -1,14 +1,17 @@
-package viewmodels
-
 /*
  * @description: 安全巡检
  * @Author: LanJianRong
  * @Date: 2020-11-18
  * @FilePath: \construction_management\web\viewmodels\project.go
  */
+package viewmodels
+
 import (
+	"time"
+
 	validation "github.com/go-ozzo/ozzo-validation/v3"
 	"go.mod/models"
+	"go.mod/web/viewmodels"
 )
 
 type Safe struct {
@@ -42,19 +45,23 @@ type SafeDetail struct {
 	Id           string `form:"id" json:"id" `
 	BidsectionId string `form:"bidsectionId" json:"bidsectionId"`
 
-	Code             string               `form:"code" json:"code"`
-	CreateTime       string               `form:"createTime" json:"createTime"`
-	Position         string               `form:"position" json:"position"`
-	Inspection       string               `form:"inspection" json:"inspection"`
-	InspectionDetail string               `form:"inspectionDetail" json:"inspectionDetail"`
-	Demand           string               `form:"demand" json:"demand"`
-	Status           int                  `form:"status" json:"status"`
-	AuditName        string               `form:"auditName" json:"auditName"`
-	FileList         []models.CmSafeFile  `form:"files" json:"files"`
-	Auditors         []Auditors           `form:"auditors" json:"auditors"`
-	auditorHistory   []models.CmSafeAudit `form:"auditorHistory" json:"auditorHistory"`
+	Code             string                     `form:"code" json:"code"`
+	CreateTime       time.Time                  `form:"createTime" json:"createTime"`
+	Position         string                     `form:"position" json:"position"`
+	Inspection       string                     `form:"inspection" json:"inspection"`
+	InspectionDetail string                     `form:"inspectionDetail" json:"inspectionDetail"`
+	Demand           string                     `form:"demand" json:"demand"`
+	Status           int                        `form:"status" json:"status"`
+	AuditName        string                     `form:"auditName" json:"auditName"`
+	FileList         []viewmodels.AnnexListView `form:"files" json:"files"`
+	Auditors         []Auditors                 `form:"auditors" json:"auditors"`
+	auditorHistory   []models.CmSafeAudit       `form:"auditorHistory" json:"auditorHistory"`
 }
 
+type SafeFile struct {
+	Name       string    `from:"name" json:"name"`
+	CreateTime time.Time `from:"create_time" json:""`
+}
 type Auditors struct {
 	Name string
 }