123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package biapprove
- import (
- "eta_gn/eta_api/global"
- "fmt"
- "time"
- )
- type BiApproveFlow struct {
- BiApproveFlowId int `gorm:"column:bi_approve_flow_id;primaryKey"`
- FlowName string `gorm:"column:flow_name"`
- ClassifyId int `gorm:"column:classify_id"`
- ClassifyName string `gorm:"column:classify_name"`
- CurrVersion int `gorm:"column:curr_version"`
- CreateTime time.Time `gorm:"column:create_time"`
- ModifyTime time.Time `gorm:"column:modify_time"`
- }
- var BiApproveFlowCols = struct {
- BiApproveFlowId string
- FlowName string
- ClassifyId string
- CurrVersion string
- CreateTime string
- ModifyTime string
- }{
- BiApproveFlowId: "bi_approve_flow_id",
- FlowName: "flow_name",
- ClassifyId: "classify_id",
- CurrVersion: "curr_version",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- func (b *BiApproveFlow) TableName() string {
- return "bi_approve_flow"
- }
- func (b *BiApproveFlow) Add(node []*BiApproveNode) (err error) {
- prevNodes := make([]*BiApproveNode, 0)
- o := global.DmSQL["rddp"].Begin()
- defer func() {
- if err != nil {
- o.Rollback()
- } else {
- o.Commit()
- }
- // 更新每个节点的下一个节点信息, 放在事务中会更新失败
- if e := UpdateNextNodes(prevNodes); e != nil {
- err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
- return
- }
- }()
- err = o.Create(b).Error
- if err != nil {
- err = fmt.Errorf("insert approve err: %v", err)
- return
- }
- nodesLen := len(node)
- if nodesLen == 0 {
- return
- }
- prevId := 0
- prevNode := new(BiApproveNode)
- for k, v := range node {
- v.BiApproveFlowId = b.BiApproveFlowId
- v.PrevNodeId = prevId
- err = o.Create(v).Error
- if err != nil {
- err = fmt.Errorf("insert node err: %v", err)
- return
- }
- prevId = v.BiApproveNodeId
- // 下一个节点
- if prevNode != nil && k > 0 && k < nodesLen {
- prevNode.NextNodeId = v.BiApproveNodeId
- prevNodes = append(prevNodes, prevNode)
- }
- prevNode = v
- }
- return
- }
- func (b *BiApproveFlow) Update(cols []string, node []*BiApproveNode) (err error) {
- prevNodes := make([]*BiApproveNode, 0)
- o := global.DmSQL["rddp"].Begin()
- defer func() {
- if err != nil {
- o.Rollback()
- } else {
- o.Commit()
- }
- // 更新每个节点的下一个节点信息, 放在事务中会更新失败
- if e := UpdateNextNodes(prevNodes); e != nil {
- err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
- return
- }
- }()
- err = o.Model(b).Select(cols).Updates(b).Error
- if err != nil {
- return
- }
- nodesLen := len(node)
- if nodesLen == 0 {
- return
- }
- prevId := 0
- prevNode := new(BiApproveNode)
- for k, v := range node {
- v.BiApproveFlowId = b.BiApproveFlowId
- v.PrevNodeId = prevId
- err = o.Create(v).Error
- if err != nil {
- err = fmt.Errorf("insert node err: %v", err)
- return
- }
- prevId = v.BiApproveNodeId
- // 下一个节点
- if prevNode != nil && k > 0 && k < nodesLen {
- prevNode.NextNodeId = v.BiApproveNodeId
- prevNodes = append(prevNodes, prevNode)
- }
- prevNode = v
- }
- return
- }
- func (b *BiApproveFlow) Delete() error {
- return global.DmSQL["rddp"].Delete(b).Error
- }
- func (m *BiApproveFlow) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BiApproveFlow, err error) {
- order := ``
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s `, m.TableName(), condition, order)
- err = global.DmSQL["rddp"].Raw(sql, pars...).First(&item).Error
- return
- }
- func GetBiApproveFlowById(biApproveFlowId int) (item *BiApproveFlow, err error) {
- err = global.DmSQL["rddp"].Where("bi_approve_flow_id = ?", biApproveFlowId).First(&item).Error
- return
- }
- func GetBiApproveFlowByClassifyId(classifyId int) (item *BiApproveFlow, err error) {
- err = global.DmSQL["rddp"].Where("classify_id = ?", classifyId).First(&item).Error
- return
- }
- func GetBiApproveFlowByCondition(condition string, pars []interface{}, startSize, pageSize int) (items []*BiApproveFlow, err error) {
- o := global.DmSQL["rddp"]
- sql := "SELECT * FROM bi_approve_flow WHERE 1=1 "
- if condition != "" {
- sql += condition
- }
- sql += " LIMIT ?,?"
- pars = append(pars, startSize, pageSize)
- err = o.Raw(sql, pars...).Scan(&items).Error
- return
- }
- func GetBiApproveFlowCountByCondition(condition string, pars []interface{}) (count int, err error) {
- o := global.DmSQL["rddp"]
- sql := "SELECT COUNT(*) AS count FROM bi_approve_flow WHERE 1=1 "
- if condition != "" {
- sql += condition
- }
- err = o.Raw(sql, pars...).Scan(&count).Error
- return
- }
- func UpdateFlowClassifyName(classifyId int, classifyName string) (err error) {
- o := global.DmSQL["rddp"]
- err = o.Model(&BiApproveFlow{}).Where("classify_id = ?", classifyId).Update("classify_name", classifyName).Error
- return
- }
|