knowledge_approve_flow.go 5.4 KB

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