bi_approve_flow.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. if e := UpdateNextNodes(prevNodes); e != nil {
  44. err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
  45. return
  46. }
  47. }()
  48. err = o.Create(b).Error
  49. if err != nil {
  50. err = fmt.Errorf("insert approve err: %v", err)
  51. return
  52. }
  53. nodesLen := len(node)
  54. if nodesLen == 0 {
  55. return
  56. }
  57. prevId := 0
  58. prevNode := new(BiApproveNode)
  59. for k, v := range node {
  60. v.BiApproveFlowId = b.BiApproveFlowId
  61. v.PrevNodeId = prevId
  62. err = o.Create(v).Error
  63. if err != nil {
  64. err = fmt.Errorf("insert node err: %v", err)
  65. return
  66. }
  67. prevId = v.BiApproveNodeId
  68. if prevNode != nil && k > 0 && k < nodesLen {
  69. prevNode.NextNodeId = v.BiApproveNodeId
  70. prevNodes = append(prevNodes, prevNode)
  71. }
  72. prevNode = v
  73. }
  74. return
  75. }
  76. func (b *BiApproveFlow) Update(cols []string, node []*BiApproveNode) (err error) {
  77. prevNodes := make([]*BiApproveNode, 0)
  78. o := global.DmSQL["rddp"].Begin()
  79. defer func() {
  80. if err != nil {
  81. o.Rollback()
  82. } else {
  83. o.Commit()
  84. }
  85. if e := UpdateNextNodes(prevNodes); e != nil {
  86. err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
  87. return
  88. }
  89. }()
  90. err = o.Model(b).Select(cols).Updates(b).Error
  91. if err != nil {
  92. return
  93. }
  94. nodesLen := len(node)
  95. if nodesLen == 0 {
  96. return
  97. }
  98. prevId := 0
  99. prevNode := new(BiApproveNode)
  100. for k, v := range node {
  101. v.BiApproveFlowId = b.BiApproveFlowId
  102. v.PrevNodeId = prevId
  103. err = o.Create(v).Error
  104. if err != nil {
  105. err = fmt.Errorf("insert node err: %v", err)
  106. return
  107. }
  108. prevId = v.BiApproveNodeId
  109. if prevNode != nil && k > 0 && k < nodesLen {
  110. prevNode.NextNodeId = v.BiApproveNodeId
  111. prevNodes = append(prevNodes, prevNode)
  112. }
  113. prevNode = v
  114. }
  115. return
  116. }
  117. func (b *BiApproveFlow) Delete() error {
  118. return global.DmSQL["rddp"].Delete(b).Error
  119. }
  120. func (m *BiApproveFlow) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BiApproveFlow, err error) {
  121. order := ``
  122. if orderRule != "" {
  123. order = ` ORDER BY ` + orderRule
  124. }
  125. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s `, m.TableName(), condition, order)
  126. err = global.DmSQL["rddp"].Raw(sql, pars...).First(&item).Error
  127. return
  128. }
  129. func GetBiApproveFlowById(biApproveFlowId int) (item *BiApproveFlow, err error) {
  130. err = global.DmSQL["rddp"].Where("bi_approve_flow_id = ?", biApproveFlowId).First(&item).Error
  131. return
  132. }
  133. func GetBiApproveFlowByClassifyId(classifyId int) (item *BiApproveFlow, err error) {
  134. err = global.DmSQL["rddp"].Where("classify_id = ?", classifyId).First(&item).Error
  135. return
  136. }
  137. func GetBiApproveFlowByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*BiApproveFlow, err error) {
  138. o := global.DmSQL["rddp"]
  139. sql := "SELECT * FROM bi_approve_flow WHERE 1=1 "
  140. if condition != "" {
  141. sql += condition
  142. }
  143. sql += " LIMIT ?,?"
  144. pars = append(pars, startSize, pageSize)
  145. err = o.Raw(sql, pars...).Scan(&items).Error
  146. return
  147. }
  148. func GetBiApproveFlowCountByCondition(condition string, pars []interface{}) (count int, err error) {
  149. o := global.DmSQL["rddp"]
  150. sql := "SELECT COUNT(*) AS count FROM bi_approve_flow WHERE 1=1 "
  151. if condition != "" {
  152. sql += condition
  153. }
  154. err = o.Raw(sql, pars...).Scan(&count).Error
  155. return
  156. }
  157. func UpdateFlowClassifyName(classifyId int, classifyName string) (err error) {
  158. o := global.DmSQL["rddp"]
  159. err = o.Model(&BiApproveFlow{}).Where("classify_id = ?", classifyId).Update("classify_name", classifyName).Error
  160. return
  161. }