bid_account_service.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * @description:标段账号相关数据操作
  3. * @Author: CP
  4. * @Date: 2020-10-22 16:13:32
  5. * @FilePath: \construction_management\services\bid_account_service.go
  6. */
  7. package services
  8. import (
  9. "errors"
  10. "log"
  11. "github.com/kataras/iris/v12"
  12. "go.mod/dao"
  13. "go.mod/datasource"
  14. "go.mod/web/utils"
  15. "go.mod/web/viewmodels"
  16. )
  17. //定义项目用户Service接口
  18. type BidAccountService interface {
  19. ValidRule(ctx iris.Context) (viewmodels.BidAccount, error)
  20. ValidRuleDelete(ctx iris.Context) (viewmodels.BidAccount, error)
  21. Create(viewBidAccount viewmodels.BidAccount, projectId int, accountId int) error
  22. Delete(viewBidAccount viewmodels.BidAccount, projectId int) error
  23. }
  24. //返回service操作类
  25. type bidAccountService struct {
  26. projectAccountDao *dao.ProjectAccountDao
  27. projectDao *dao.ProjectDao
  28. bidsectionDao *dao.BidsectionDao
  29. treeDao *dao.TreeDao
  30. bidAccountDao *dao.BidAccountDao
  31. }
  32. //创建项目用户service
  33. func NewBidAccountService() BidAccountService {
  34. return &bidAccountService{
  35. projectAccountDao: dao.NewProjectAccountDao(datasource.InstanceDbMaster()),
  36. projectDao: dao.NewProjectDao(datasource.InstanceDbMaster()),
  37. bidsectionDao: dao.NewBidsectionDao(datasource.InstanceDbMaster()),
  38. treeDao: dao.NewTreeDao(datasource.InstanceDbMaster()),
  39. bidAccountDao: dao.NewBidAccountDao(datasource.InstanceDbMaster()),
  40. }
  41. }
  42. // 登陆验证
  43. func (s *bidAccountService) ValidRule(ctx iris.Context) (viewmodels.BidAccount, error) {
  44. bidAccountVaild := viewmodels.BidAccount{}
  45. err := ctx.ReadJSON(&bidAccountVaild)
  46. if err != nil {
  47. log.Println("ReadForm转换异常, error=", err)
  48. return bidAccountVaild, err
  49. }
  50. err = bidAccountVaild.Validate()
  51. if err != nil {
  52. log.Println("登录验证, error=", err)
  53. return bidAccountVaild, err
  54. }
  55. return bidAccountVaild, nil
  56. }
  57. // 删除标段关系用户验证
  58. func (s *bidAccountService) ValidRuleDelete(ctx iris.Context) (viewmodels.BidAccount, error) {
  59. bidAccountVaild := viewmodels.BidAccount{}
  60. err := ctx.ReadForm(&bidAccountVaild)
  61. if err != nil {
  62. log.Println("ReadForm转换异常, error=", err)
  63. return bidAccountVaild, err
  64. }
  65. err = bidAccountVaild.Validate()
  66. if err != nil {
  67. log.Println("登录验证, error=", err)
  68. return bidAccountVaild, err
  69. }
  70. return bidAccountVaild, nil
  71. }
  72. // 新增标段于账号的关系
  73. func (s *bidAccountService) Create(viewBidAccount viewmodels.BidAccount, projectId int, loginAccountId int) error {
  74. // 写入关系表-标段的成员数量-账号表中标段ID
  75. // 1.检查账号合法性
  76. accountId, err := utils.GetDecryptId(viewBidAccount.AccountId)
  77. if err != nil {
  78. return err
  79. }
  80. if loginAccountId == accountId {
  81. return errors.New("不能添加自己")
  82. }
  83. accountData := s.projectAccountDao.Get(accountId, projectId)
  84. if accountData.Id == 0 {
  85. return errors.New("添加的账号不合法")
  86. }
  87. // 2.检查标段合法性
  88. bidsectionId, err := utils.GetDecryptId(viewBidAccount.BidsectionId)
  89. if err != nil {
  90. return err
  91. }
  92. bidsection := s.bidsectionDao.Get(bidsectionId, projectId)
  93. if bidsection.Id == 0 {
  94. return errors.New("标段不合法")
  95. }
  96. // 3.检查目录的合法性
  97. // 3-1标段ID,,项目ID
  98. treeData := s.treeDao.GetBidParentId(bidsectionId, projectId)
  99. // 3-2获得目录ID
  100. if treeData.Id == 0 {
  101. return errors.New("目录不合法")
  102. }
  103. treeId := treeData.Id
  104. // 4.检查账号是否已经添加过
  105. bidAccountData := s.bidAccountDao.GetAccountId(projectId, bidsectionId, accountId)
  106. if bidAccountData.Id != 0 {
  107. return errors.New("已添加过该账号")
  108. }
  109. // 新增成员到标段
  110. err = s.bidAccountDao.Create(bidsectionId, accountData, treeId, projectId)
  111. if err != nil {
  112. return err
  113. }
  114. return nil
  115. }
  116. //移除标段成员
  117. func (s *bidAccountService) Delete(viewBidAccount viewmodels.BidAccount, projectId int) error {
  118. // 1.检查账号合法性
  119. accountId, err := utils.GetDecryptId(viewBidAccount.AccountId)
  120. if err != nil {
  121. return err
  122. }
  123. accountData := s.projectAccountDao.Get(accountId, projectId)
  124. if accountData.Id == 0 {
  125. return errors.New("添加的账号不合法")
  126. }
  127. // 2.检查标段合法性
  128. bidsectionId, err := utils.GetDecryptId(viewBidAccount.BidsectionId)
  129. if err != nil {
  130. return err
  131. }
  132. bidsection := s.bidsectionDao.Get(bidsectionId, projectId)
  133. if bidsection.Id == 0 {
  134. return errors.New("标段不合法")
  135. }
  136. // 3.检查目录的合法性
  137. // 3-1标段ID,,项目ID
  138. treeData := s.treeDao.GetBidParentId(bidsectionId, projectId)
  139. // 3-2获得目录ID
  140. if treeData.Id == 0 {
  141. return errors.New("目录不合法")
  142. }
  143. treeId := treeData.Id
  144. // 删除成员到标段
  145. err = s.bidAccountDao.Delete(bidsectionId, accountData, treeId, projectId)
  146. if err != nil {
  147. return err
  148. }
  149. return nil
  150. }