bid_account_dao.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * @description: 标段账号的-数据库操作
  3. * @Author: CP
  4. * @Date: 2020-10-21 11:54:10
  5. * @FilePath: \construction_management\dao\bid_account_dao.go
  6. */
  7. package dao
  8. import (
  9. "encoding/json"
  10. "errors"
  11. "github.com/go-xorm/xorm"
  12. "go.mod/lib"
  13. "go.mod/models"
  14. )
  15. //数据库操作引擎
  16. type BidAccountDao struct {
  17. engine *xorm.Engine
  18. }
  19. //获得一个DAO对象
  20. func NewBidAccountDao(engine *xorm.Engine) *BidAccountDao {
  21. return &BidAccountDao{
  22. engine: engine,
  23. }
  24. }
  25. //获得标段下的账号数据
  26. func (d *BidAccountDao) GetBidAccount(bidsectionId int, projectId int) []models.CmBidAccount {
  27. datalist := make([]models.CmBidAccount, 0)
  28. err := d.engine.
  29. Where("bidsection_id = ? and project_id=?", bidsectionId, projectId).
  30. Desc("id").
  31. Find(&datalist)
  32. if err != nil {
  33. return datalist
  34. } else {
  35. return datalist
  36. }
  37. }
  38. // 创建标段成员关系
  39. func (d *BidAccountDao) Create(bidsectionId int, accountData *models.CmProjectAccount, treeId int, projectId int) error {
  40. session := d.engine.NewSession()
  41. defer session.Close()
  42. err := session.Begin()
  43. if err != nil {
  44. return errors.New("新增标段成员出错-dbsession")
  45. }
  46. // -更新树成员量
  47. // 1.获得标段成员量
  48. bidAccountData := d.GetBidAccount(bidsectionId, projectId)
  49. accounts := len(bidAccountData) + 1
  50. _, err = session.Exec("UPDATE cm_tree SET `accounts` = ? where id = ?", accounts, treeId)
  51. if err != nil {
  52. session.Rollback()
  53. return errors.New("更新标段成员量出错")
  54. }
  55. // -更新账号表的标段ID
  56. // 1.获得账号中已绑定的标段
  57. bidsectionIdsString := accountData.BidsectionIds
  58. bidsectionIds := make([]lib.BidsectionIds, 0)
  59. bidsectionIdsByte := []byte("")
  60. // 2.未有标段
  61. if bidsectionIdsString == "" {
  62. bidsectionIds[0].Id = bidsectionId
  63. bidsectionIdsByte, err = json.Marshal(bidsectionIds)
  64. if err != nil {
  65. return err
  66. }
  67. } else {
  68. // 2.获得已有的标段ID
  69. err := json.Unmarshal([]byte(bidsectionIdsString), &bidsectionIds)
  70. if err != nil {
  71. return err
  72. }
  73. bidId := lib.BidsectionIds{}
  74. bidId.Id = bidsectionId
  75. // 加入ID
  76. bidsectionIds = append(bidsectionIds, bidId)
  77. // 转换JSON字符byte
  78. bidsectionIdsByte, err = json.Marshal(bidsectionIds)
  79. if err != nil {
  80. return err
  81. }
  82. }
  83. bidsectionIdsString = string(bidsectionIdsByte[:])
  84. // 3.更新账号标段ID组
  85. _, err = session.Exec("UPDATE cm_project_account SET `bidsection_ids` = ? where id = ?", bidsectionIdsString, accountData.Id)
  86. if err != nil {
  87. session.Rollback()
  88. return errors.New("更新账号下标段出错")
  89. }
  90. // -写入标段用户关系
  91. bidAccount := models.CmBidAccount{}
  92. bidAccount.AccountId = accountData.Id
  93. bidAccount.BidsectionId = bidsectionId
  94. bidAccount.ProjectId = projectId
  95. _, err = session.Insert(bidAccount)
  96. if err != nil {
  97. session.Rollback()
  98. return errors.New("账号关联标段出错")
  99. }
  100. err = session.Commit()
  101. if err != nil {
  102. session.Rollback()
  103. return errors.New("新增标段成员出错-dbsession")
  104. }
  105. return nil
  106. }
  107. // 移除标段中的成员
  108. func (d *BidAccountDao) Delete(bidsectionId int, accountData *models.CmProjectAccount, treeId int, projectId int) error {
  109. session := d.engine.NewSession()
  110. defer session.Close()
  111. err := session.Begin()
  112. if err != nil {
  113. return errors.New("移除标段成员出错-dbsession")
  114. }
  115. // 1.移除标段账号关系中的账号
  116. _, err = session.Exec("DELETE FROM `cm_bid_account` WHERE account_id = ? and bidsection_id = ? and project_id= ? limit 1",
  117. accountData.Id, bidsectionId, projectId)
  118. if err != nil {
  119. session.Rollback()
  120. return errors.New("移除标段成员出错")
  121. }
  122. // -更新树成员量
  123. // 2.获得标段成员量
  124. bidAccountData := d.GetBidAccount(bidsectionId, projectId)
  125. accounts := len(bidAccountData)
  126. _, err = session.Exec("UPDATE cm_tree SET `accounts` = ? where id = ?", accounts, treeId)
  127. if err != nil {
  128. session.Rollback()
  129. return errors.New("更新标段成员量出错")
  130. }
  131. // 3.移除账号表中的标段ID
  132. bidsectionIdsString := accountData.BidsectionIds
  133. bidsectionIds := make([]lib.BidsectionIds, 0)
  134. bidsectionIdsByte := []byte("")
  135. // 2.未有标段
  136. if bidsectionIdsString == "" {
  137. return errors.New("移除标段成员出错")
  138. }
  139. // 2.获得已有的标段ID
  140. err = json.Unmarshal([]byte(bidsectionIdsString), &bidsectionIds)
  141. if err != nil {
  142. return err
  143. }
  144. // 获得需要删除标段ID的下标
  145. bidsectionIdsIndex := -1
  146. for index, data := range bidsectionIds {
  147. if data.Id == bidsectionId {
  148. bidsectionIdsIndex = index
  149. break
  150. }
  151. }
  152. if bidsectionIdsIndex == -1 {
  153. return errors.New("移除标段成员出错")
  154. }
  155. // 删除标段ID
  156. bidsectionIds = append(bidsectionIds[:bidsectionIdsIndex], bidsectionIds[bidsectionIdsIndex+1:]...)
  157. // 转换JSON字符byte
  158. bidsectionIdsByte, err = json.Marshal(bidsectionIds)
  159. if err != nil {
  160. return err
  161. }
  162. bidsectionIdsString = string(bidsectionIdsByte[:])
  163. // 3.更新账号标段ID组
  164. _, err = session.Exec("UPDATE cm_project_account SET `bidsection_ids` = ? where id = ?", bidsectionIdsString, accountData.Id)
  165. if err != nil {
  166. session.Rollback()
  167. return errors.New("移除账号下标段出错")
  168. }
  169. err = session.Commit()
  170. if err != nil {
  171. session.Rollback()
  172. return errors.New("移除标段成员出错-dbsession")
  173. }
  174. return nil
  175. }