bidsection_service.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * @description: 标段相关数据操作相关
  3. * @Author: CP
  4. * @Date: 2020-09-28 10:31:31
  5. * @FilePath: \construction_management\services\bidsection_service.go
  6. */
  7. package services
  8. import (
  9. "errors"
  10. "fmt"
  11. "log"
  12. "strconv"
  13. "time"
  14. "github.com/kataras/iris/v12"
  15. "go.mod/dao"
  16. "go.mod/datasource"
  17. "go.mod/models"
  18. "go.mod/web/utils"
  19. "go.mod/web/viewmodels"
  20. )
  21. //定义标段Service接口
  22. type BidsectionService interface {
  23. ValidRule(iris.Context) (viewmodels.Bidsection, error)
  24. Create(viewmodels.Bidsection) error
  25. }
  26. //返回service操作类
  27. type bidsectionService struct {
  28. dao *dao.BidsectionDao
  29. treeDao *dao.TreeDao
  30. }
  31. //创建标段service
  32. func NewBidsectionService() BidsectionService {
  33. return &bidsectionService{
  34. dao: dao.NewBidsectionDao(datasource.InstanceDbMaster()),
  35. treeDao: dao.NewTreeDao(datasource.InstanceDbMaster()),
  36. }
  37. }
  38. // 文件夹规则验证
  39. func (s *bidsectionService) ValidRule(ctx iris.Context) (viewmodels.Bidsection, error) {
  40. bidsectionVaild := viewmodels.Bidsection{}
  41. err := ctx.ReadJSON(&bidsectionVaild)
  42. if err != nil {
  43. log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
  44. return bidsectionVaild, err
  45. }
  46. err = bidsectionVaild.Validate()
  47. if err != nil {
  48. log.Println("标段验证, error=", err)
  49. return bidsectionVaild, err
  50. }
  51. return bidsectionVaild, nil
  52. }
  53. // 新增一个标段
  54. func (s *bidsectionService) Create(data viewmodels.Bidsection) error {
  55. // 类型校验
  56. folder := models.CmTree{}
  57. folder.Name = data.Name
  58. ProjectId, err := strconv.Atoi(data.ProjectId)
  59. if err != nil {
  60. return err
  61. }
  62. // 获得该目录ID
  63. Id, err := utils.GetDecryptId(data.FolderId)
  64. if err != nil {
  65. return err
  66. }
  67. // 该目录中是否有目录 -判断是否是叶子节点--TODO
  68. folderList := s.treeDao.GetChildFolder(Id)
  69. if len(folderList) > 0 {
  70. return errors.New("该目录中存在其他目录,不能新增标段")
  71. }
  72. // 创建在树中的标段
  73. // 获得该深度的文件夹最大序号
  74. serial := 0
  75. treeNode := s.treeDao.Get(Id, ProjectId)
  76. if treeNode.Id == 0 {
  77. return errors.New("上级目录不正确")
  78. }
  79. depth := treeNode.Depth + 1
  80. attribution := fmt.Sprintf("%s%d-", treeNode.Attribution, treeNode.Serial)
  81. datalist := s.treeDao.GetALLDepthByAttribution(depth, ProjectId, attribution)
  82. maxIndex := len(datalist)
  83. if maxIndex != 0 {
  84. serial = datalist[maxIndex-1].Serial + 1
  85. }
  86. // 设置归属
  87. folder.Attribution = attribution //treeNode.Attribution + strconv.Itoa(treeNode.Serial) + "-"
  88. folder.ParentId = Id
  89. folder.ProjectId = ProjectId
  90. folder.Serial = serial
  91. folder.Depth = depth
  92. folder.Isfolder = 0
  93. folder.CreateTime = time.Now()
  94. folder.UpdateTime = time.Now()
  95. // 创建标段和标段在树中的联系
  96. err = s.dao.Create(&folder)
  97. if err != nil {
  98. return err
  99. }
  100. return nil
  101. }