bi_approve_flow.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package biapprove
  2. import (
  3. "eta_gn/eta_api/global"
  4. "fmt"
  5. "time"
  6. )
  7. type BiApproveFlow struct {
  8. BiApproveFlowId int `gorm:"column:bi_approve_flow_id;primaryKey"`
  9. FlowName string `gorm:"column:flow_name"`
  10. ClassifyId int `gorm:"column:classify_id"`
  11. ClassifyName string `gorm:"column:classify_name"`
  12. CurrVersion int `gorm:"column:curr_version"`
  13. CreateTime time.Time `gorm:"column:create_time"`
  14. ModifyTime time.Time `gorm:"column:modify_time"`
  15. }
  16. var BiApproveFlowCols = struct {
  17. BiApproveFlowId string
  18. FlowName string
  19. ClassifyId string
  20. CurrVersion string
  21. CreateTime string
  22. ModifyTime string
  23. }{
  24. BiApproveFlowId: "bi_approve_flow_id",
  25. FlowName: "flow_name",
  26. ClassifyId: "classify_id",
  27. CurrVersion: "curr_version",
  28. CreateTime: "create_time",
  29. ModifyTime: "modify_time",
  30. }
  31. func (b *BiApproveFlow) TableName() string {
  32. return "bi_approve_flow"
  33. }
  34. func (b *BiApproveFlow) Add(node []*BiApproveNode) (err error) {
  35. prevNodes := make([]*BiApproveNode, 0)
  36. o := global.DmSQL["rddp"].Begin()
  37. defer func() {
  38. if err != nil {
  39. o.Rollback()
  40. } else {
  41. o.Commit()
  42. }
  43. // 更新每个节点的下一个节点信息, 放在事务中会更新失败
  44. if e := UpdateNextNodes(prevNodes); e != nil {
  45. err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
  46. return
  47. }
  48. }()
  49. err = o.Create(b).Error
  50. if err != nil {
  51. err = fmt.Errorf("insert approve err: %v", err)
  52. return
  53. }
  54. nodesLen := len(node)
  55. if nodesLen == 0 {
  56. return
  57. }
  58. prevId := 0
  59. prevNode := new(BiApproveNode)
  60. for k, v := range node {
  61. v.BiApproveFlowId = b.BiApproveFlowId
  62. v.PrevNodeId = prevId
  63. err = o.Create(v).Error
  64. if err != nil {
  65. err = fmt.Errorf("insert node err: %v", err)
  66. return
  67. }
  68. prevId = v.BiApproveNodeId
  69. // 下一个节点
  70. if prevNode != nil && k > 0 && k < nodesLen {
  71. prevNode.NextNodeId = v.BiApproveNodeId
  72. prevNodes = append(prevNodes, prevNode)
  73. }
  74. prevNode = v
  75. }
  76. return
  77. }
  78. func (b *BiApproveFlow) Update(cols []string, node []*BiApproveNode) (err error) {
  79. prevNodes := make([]*BiApproveNode, 0)
  80. o := global.DmSQL["rddp"].Begin()
  81. defer func() {
  82. if err != nil {
  83. o.Rollback()
  84. } else {
  85. o.Commit()
  86. }
  87. // 更新每个节点的下一个节点信息, 放在事务中会更新失败
  88. if e := UpdateNextNodes(prevNodes); e != nil {
  89. err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
  90. return
  91. }
  92. }()
  93. err = o.Model(b).Select(cols).Updates(b).Error
  94. if err != nil {
  95. return
  96. }
  97. nodesLen := len(node)
  98. if nodesLen == 0 {
  99. return
  100. }
  101. prevId := 0
  102. prevNode := new(BiApproveNode)
  103. for k, v := range node {
  104. v.BiApproveFlowId = b.BiApproveFlowId
  105. v.PrevNodeId = prevId
  106. err = o.Create(v).Error
  107. if err != nil {
  108. err = fmt.Errorf("insert node err: %v", err)
  109. return
  110. }
  111. prevId = v.BiApproveNodeId
  112. // 下一个节点
  113. if prevNode != nil && k > 0 && k < nodesLen {
  114. prevNode.NextNodeId = v.BiApproveNodeId
  115. prevNodes = append(prevNodes, prevNode)
  116. }
  117. prevNode = v
  118. }
  119. return
  120. }
  121. func (b *BiApproveFlow) Delete() error {
  122. return global.DmSQL["rddp"].Delete(b).Error
  123. }
  124. func (m *BiApproveFlow) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BiApproveFlow, err error) {
  125. order := ``
  126. if orderRule != "" {
  127. order = ` ORDER BY ` + orderRule
  128. }
  129. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s `, m.TableName(), condition, order)
  130. err = global.DmSQL["rddp"].Raw(sql, pars...).First(&item).Error
  131. return
  132. }
  133. func GetBiApproveFlowById(biApproveFlowId int) (item *BiApproveFlow, err error) {
  134. err = global.DmSQL["rddp"].Where("bi_approve_flow_id = ?", biApproveFlowId).First(&item).Error
  135. return
  136. }
  137. func GetBiApproveFlowByClassifyId(classifyId int) (item *BiApproveFlow, err error) {
  138. err = global.DmSQL["rddp"].Where("classify_id = ?", classifyId).First(&item).Error
  139. return
  140. }
  141. func GetBiApproveFlowByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*BiApproveFlow, err error) {
  142. o := global.DmSQL["rddp"]
  143. sql := "SELECT * FROM bi_approve_flow WHERE 1=1 "
  144. if condition != "" {
  145. sql += condition
  146. }
  147. sql += " LIMIT ?,?"
  148. pars = append(pars, startSize, pageSize)
  149. err = o.Raw(sql, pars...).Scan(&items).Error
  150. return
  151. }
  152. func GetBiApproveFlowCountByCondition(condition string, pars []interface{}) (count int, err error) {
  153. o := global.DmSQL["rddp"]
  154. sql := "SELECT COUNT(*) AS count FROM bi_approve_flow WHERE 1=1 "
  155. if condition != "" {
  156. sql += condition
  157. }
  158. err = o.Raw(sql, pars...).Scan(&count).Error
  159. return
  160. }
  161. func UpdateFlowClassifyName(classifyId int, classifyName string) (err error) {
  162. o := global.DmSQL["rddp"]
  163. err = o.Model(&BiApproveFlow{}).Where("classify_id = ?", classifyId).Update("classify_name", classifyName).Error
  164. return
  165. }