|
@@ -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
|
|
|
+}
|