package services import ( "errors" "log" "strconv" "github.com/kataras/iris/v12" "go.mod/comm" "go.mod/conf" "go.mod/dao" "go.mod/datasource" "go.mod/models" "go.mod/web/viewmodels" ) type SafeService interface { Get(id int, pid int) []viewmodels.SafeList 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操作类 type safeService struct { daoSafe *dao.SafeDao daoSafeAudit *dao.SafeAuditDao daoProjectAccount *dao.ProjectAccountDao daoAnnex *dao.AnnexDao daoRule *dao.RuleDao validDetail string } //创建项目用户service func NewSafeService() SafeService { return &safeService{ validDetail: "/api/safe/detail", daoSafe: dao.NewSafeDao(datasource.InstanceDbMaster()), daoSafeAudit: dao.NewSafeAuditDao(datasource.InstanceDbMaster()), daoProjectAccount: dao.NewProjectAccountDao(datasource.InstanceDbMaster()), } } func (s *safeService) Get(id int, pid int) []viewmodels.SafeList { datalist := s.daoSafe.GetListByBid(id) safeList := make([]viewmodels.SafeList, 0) for _, item := range datalist { safeVM := viewmodels.SafeList{} safeVM.Code = item.Code account := s.daoProjectAccount.Get(item.Uid, pid) safeVM.AuditName = account.Name safeVM.CreateTime = item.CreateTime.Format(conf.SysTimeform) safeVM.Demand = item.Demand id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret) safeVM.Id = id safeVM.Inspection = item.Inspection safeVM.InspectionDetail = item.InspectionDetail safeVM.Position = item.Position safeVM.Status = item.Status counts, _ := s.daoAnnex.GetCount(3, item.Id) safeVM.FileCounts = counts safeList = append(safeList, safeVM) } return safeList } // post请求,插入单条数据 func (s *safeService) Post(data models.CmSafe) error { has := s.daoSafe.FindByCode(data.Code) if has { return errors.New("该编号已存在!") } Inserted, err := s.daoSafe.InsertRecord(data) if Inserted == true { return nil } return err } // delete请求,删除数据 func (s *safeService) Del(id int) error { Deleted, err := s.daoSafe.DeleteRecord(id) if Deleted == true { return nil } return err } // 详情页数据拼装 func (s *safeService) GetDetail(id int, pid int) viewmodels.SafeDetail { safeData := s.daoSafe.FindById(id) // fmt.Println(safeData) safeId, _ := comm.AesEncrypt(strconv.Itoa(safeData.Id), conf.SignSecret) bid, _ := comm.AesEncrypt(strconv.Itoa(safeData.BidsectionId), conf.SignSecret) data := viewmodels.SafeDetail{} data.Id = string(safeId) data.BidsectionId = string(bid) data.Code = safeData.Code data.Inspection = safeData.Inspection data.InspectionDetail = safeData.InspectionDetail data.Demand = safeData.Demand account := s.daoProjectAccount.Get(safeData.Uid, pid) data.AuditName = account.Name 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 } // 安全巡检附件数据存库 // 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{} // fmt.Println("---------------------------safeVaild", safeVaild) if ctx.Method() == "GET" { err := ctx.ReadForm(&safeVaild) if err != nil { log.Println("safe-ValidRule-ReadForm转换异常, error=", err) return safeVaild, err } if ctx.Path() == s.validDetail { // 一样要传id,所以用delete的方法判断 err = safeVaild.ValidateDelete() } else { err = safeVaild.ValidateList() } return safeVaild, err } if ctx.Method() == "POST" { err := ctx.ReadJSON(&safeVaild) if err != nil { log.Println("safe-ValidRule-ReadJson转换异常, error=", err) return safeVaild, err } err = safeVaild.ValidateCreate() return safeVaild, err // if ctx.Path() == s.validCreate { // } // if ctx.Path() == s.validFile { // err = safeVaild.ValidateFile() // return safeVaild, err // } } if ctx.Method() == "PUT" { err := ctx.ReadForm(&safeVaild) if err != nil { log.Println("safe-ValidRule-ReadForm转换异常, error=", err) return safeVaild, err } err = safeVaild.ValidateDelete() return safeVaild, err } return safeVaild, nil }