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 }