safe_service.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package services
  2. import (
  3. "errors"
  4. "log"
  5. "strconv"
  6. "github.com/kataras/iris/v12"
  7. "go.mod/comm"
  8. "go.mod/conf"
  9. "go.mod/dao"
  10. "go.mod/datasource"
  11. "go.mod/models"
  12. "go.mod/web/viewmodels"
  13. )
  14. type SafeService interface {
  15. Get(id int, pid int) []viewmodels.SafeList
  16. Post(data models.CmSafe) error
  17. Del(id int) error
  18. GetDetail(id int, pid int) viewmodels.SafeDetail
  19. // SaveFileInfo(bid int, sid int, uid int, fileList []viewmodels.FileList) error
  20. ValidRule(ctx iris.Context) (viewmodels.Safe, error)
  21. // ValidFile(ctx iris.Context) (viewmodels.File, error)
  22. }
  23. // //返回service操作类
  24. type safeService struct {
  25. daoSafe *dao.SafeDao
  26. daoSafeAudit *dao.SafeAuditDao
  27. daoProjectAccount *dao.ProjectAccountDao
  28. daoAnnex *dao.AnnexDao
  29. daoRule *dao.RuleDao
  30. validDetail string
  31. }
  32. //创建项目用户service
  33. func NewSafeService() SafeService {
  34. return &safeService{
  35. validDetail: "/api/safe/detail",
  36. daoSafe: dao.NewSafeDao(datasource.InstanceDbMaster()),
  37. daoSafeAudit: dao.NewSafeAuditDao(datasource.InstanceDbMaster()),
  38. daoProjectAccount: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
  39. }
  40. }
  41. func (s *safeService) Get(id int, pid int) []viewmodels.SafeList {
  42. datalist := s.daoSafe.GetListByBid(id)
  43. safeList := make([]viewmodels.SafeList, 0)
  44. for _, item := range datalist {
  45. safeVM := viewmodels.SafeList{}
  46. safeVM.Code = item.Code
  47. account := s.daoProjectAccount.Get(item.Uid, pid)
  48. safeVM.AuditName = account.Name
  49. safeVM.CreateTime = item.CreateTime.Format(conf.SysTimeform)
  50. safeVM.Demand = item.Demand
  51. id, _ := comm.AesEncrypt(strconv.Itoa(item.Id), conf.SignSecret)
  52. safeVM.Id = id
  53. safeVM.Inspection = item.Inspection
  54. safeVM.InspectionDetail = item.InspectionDetail
  55. safeVM.Position = item.Position
  56. safeVM.Status = item.Status
  57. counts, _ := s.daoAnnex.GetCount(3, item.Id)
  58. safeVM.FileCounts = counts
  59. safeList = append(safeList, safeVM)
  60. }
  61. return safeList
  62. }
  63. // post请求,插入单条数据
  64. func (s *safeService) Post(data models.CmSafe) error {
  65. has := s.daoSafe.FindByCode(data.Code)
  66. if has {
  67. return errors.New("该编号已存在!")
  68. }
  69. Inserted, err := s.daoSafe.InsertRecord(data)
  70. if Inserted == true {
  71. return nil
  72. }
  73. return err
  74. }
  75. // delete请求,删除数据
  76. func (s *safeService) Del(id int) error {
  77. Deleted, err := s.daoSafe.DeleteRecord(id)
  78. if Deleted == true {
  79. return nil
  80. }
  81. return err
  82. }
  83. // 详情页数据拼装
  84. func (s *safeService) GetDetail(id int, pid int) viewmodels.SafeDetail {
  85. safeData := s.daoSafe.FindById(id)
  86. // fmt.Println(safeData)
  87. safeId, _ := comm.AesEncrypt(strconv.Itoa(safeData.Id), conf.SignSecret)
  88. bid, _ := comm.AesEncrypt(strconv.Itoa(safeData.BidsectionId), conf.SignSecret)
  89. data := viewmodels.SafeDetail{}
  90. data.Id = string(safeId)
  91. data.BidsectionId = string(bid)
  92. data.Code = safeData.Code
  93. data.Inspection = safeData.Inspection
  94. data.InspectionDetail = safeData.InspectionDetail
  95. data.Demand = safeData.Demand
  96. account := s.daoProjectAccount.Get(safeData.Uid, pid)
  97. data.AuditName = account.Name
  98. data.CreateTime = safeData.CreateTime
  99. data.FileList = s.daoAnnex.GetList(3, safeData.Id)
  100. auditors := s.daoSafeAudit.GetAuditors(safeData.Id, safeData.Times, account.Id)
  101. data.Auditors = auditors
  102. return data
  103. }
  104. // 安全巡检附件数据存库
  105. // func (s *safeService) SaveFileInfo(bid int, sid int, uid int, fileList []viewmodels.FileList) error {
  106. // fileData := make([]models.CmSafeFile, 0)
  107. // for _, file := range fileList {
  108. // fileVM := models.CmSafeFile{}
  109. // fileVM.BidsectionId = bid
  110. // fileVM.SafeId = sid
  111. // fileVM.Uid = uid
  112. // createTime, _ := time.Parse("2020-11-11 20:30:12", file.CreateTime)
  113. // fileVM.CreatedTime = createTime
  114. // fileVM.FileName = file.FileName
  115. // fileVM.FilePath = file.FilePath
  116. // fileData = append(fileData, fileVM)
  117. // }
  118. // err := s.daoSafeFile.InsertByList(fileData)
  119. // return err
  120. // }
  121. // 校验文件规则
  122. // func (s *safeService) ValidFile(ctx iris.Context) (viewmodels.File, error) {
  123. // fileValid := viewmodels.File{}
  124. // err := ctx.ReadJSON(&fileValid)
  125. // return fileValid, err
  126. // }
  127. // 规则校验
  128. func (s *safeService) ValidRule(ctx iris.Context) (viewmodels.Safe, error) {
  129. safeVaild := viewmodels.Safe{}
  130. // fmt.Println("---------------------------safeVaild", safeVaild)
  131. if ctx.Method() == "GET" {
  132. err := ctx.ReadForm(&safeVaild)
  133. if err != nil {
  134. log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
  135. return safeVaild, err
  136. }
  137. if ctx.Path() == s.validDetail {
  138. // 一样要传id,所以用delete的方法判断
  139. err = safeVaild.ValidateDelete()
  140. } else {
  141. err = safeVaild.ValidateList()
  142. }
  143. return safeVaild, err
  144. }
  145. if ctx.Method() == "POST" {
  146. err := ctx.ReadJSON(&safeVaild)
  147. if err != nil {
  148. log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
  149. return safeVaild, err
  150. }
  151. err = safeVaild.ValidateCreate()
  152. return safeVaild, err
  153. // if ctx.Path() == s.validCreate {
  154. // }
  155. // if ctx.Path() == s.validFile {
  156. // err = safeVaild.ValidateFile()
  157. // return safeVaild, err
  158. // }
  159. }
  160. if ctx.Method() == "PUT" {
  161. err := ctx.ReadForm(&safeVaild)
  162. if err != nil {
  163. log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
  164. return safeVaild, err
  165. }
  166. err = safeVaild.ValidateDelete()
  167. return safeVaild, err
  168. }
  169. return safeVaild, nil
  170. }