123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- package report_approve
- import (
- "eta/eta_api/utils"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/rdlucklib/rdluck_tools/paging"
- "strings"
- "time"
- )
- // ReportApproveFlow 报告审批流表
- type ReportApproveFlow struct {
- ReportApproveFlowId int `orm:"column(report_approve_flow_id);pk" description:"审批流ID"`
- FlowName string `description:"审批流名称"`
- ReportType int `description:"报告类型:1-中文研报;2-英文研报;3-智能研报"`
- ClassifyFirstId int `description:"一级分类ID"`
- ClassifySecondId int `description:"二级分类ID"`
- CurrVersion int `description:"当前版本号"`
- CreateTime time.Time `description:"创建时间"`
- ModifyTime time.Time `description:"修改时间"`
- }
- var ReportApproveFlowCols = struct {
- ReportApproveFlowId string
- FlowName string
- ReportType string
- ClassifyFirstId string
- ClassifySecondId string
- CurrVersion string
- CreateTime string
- ModifyTime string
- }{
- ReportApproveFlowId: "report_approve_flow_id",
- FlowName: "flow_name",
- ReportType: "report_type",
- ClassifyFirstId: "classify_first_id",
- ClassifySecondId: "classify_second_id",
- CurrVersion: "curr_version",
- CreateTime: "create_time",
- ModifyTime: "modify_time",
- }
- func (m *ReportApproveFlow) TableName() string {
- return "report_approve_flow"
- }
- func (m *ReportApproveFlow) PrimaryId() string {
- return ReportApproveFlowCols.ReportApproveFlowId
- }
- func (m *ReportApproveFlow) Create() (err error) {
- o := orm.NewOrmUsingDB("rddp")
- id, err := o.Insert(m)
- if err != nil {
- return
- }
- m.ReportApproveFlowId = int(id)
- return
- }
- func (m *ReportApproveFlow) CreateMulti(items []*ReportApproveFlow) (err error) {
- if len(items) == 0 {
- return
- }
- o := orm.NewOrmUsingDB("rddp")
- _, err = o.InsertMulti(len(items), items)
- return
- }
- func (m *ReportApproveFlow) Update(cols []string) (err error) {
- o := orm.NewOrmUsingDB("rddp")
- _, err = o.Update(m, cols...)
- return
- }
- func (m *ReportApproveFlow) Del() (err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
- _, err = o.Raw(sql, m.ReportApproveFlowId).Exec()
- return
- }
- func (m *ReportApproveFlow) MultiDel(menuIds []int) (err error) {
- if len(menuIds) == 0 {
- return
- }
- o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
- _, err = o.Raw(sql, menuIds).Exec()
- return
- }
- func (m *ReportApproveFlow) GetItemById(id int) (item *ReportApproveFlow, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
- err = o.Raw(sql, id).QueryRow(&item)
- return
- }
- func (m *ReportApproveFlow) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ReportApproveFlow, err error) {
- o := orm.NewOrmUsingDB("rddp")
- order := ``
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
- err = o.Raw(sql, pars).QueryRow(&item)
- return
- }
- func (m *ReportApproveFlow) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrmUsingDB("rddp")
- sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- func (m *ReportApproveFlow) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportApproveFlow, err error) {
- o := orm.NewOrmUsingDB("rddp")
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := `ORDER BY create_time DESC`
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
- _, err = o.Raw(sql, pars).QueryRows(&items)
- return
- }
- func (m *ReportApproveFlow) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ReportApproveFlow, err error) {
- o := orm.NewOrmUsingDB("rddp")
- fields := strings.Join(fieldArr, ",")
- if len(fieldArr) == 0 {
- fields = `*`
- }
- order := `ORDER BY create_time DESC`
- if orderRule != "" {
- order = ` ORDER BY ` + orderRule
- }
- sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- return
- }
- // ReportApproveFlowItem 报告审批流信息
- type ReportApproveFlowItem struct {
- ReportApproveFlowId int `description:"审批流ID"`
- FlowName string `description:"审批流名称"`
- ReportType int `description:"报告类型:1-中文研报;2-英文研报;3-智能研报"`
- ClassifyFirstId int `description:"一级分类ID"`
- ClassifySecondId int `description:"二级分类ID"`
- ReportClassify string `description:"报告类型: XX研报/一级分类/二级分类"`
- CreateTime string `description:"创建时间"`
- ModifyTime string `description:"修改时间"`
- }
- // FormatReportApproveFlow2Item 格式化报告审批流
- func FormatReportApproveFlow2Item(origin *ReportApproveFlow) (item *ReportApproveFlowItem) {
- item = new(ReportApproveFlowItem)
- if origin == nil {
- return
- }
- item.ReportApproveFlowId = origin.ReportApproveFlowId
- item.FlowName = origin.FlowName
- item.ReportType = origin.ReportType
- item.ClassifyFirstId = origin.ClassifyFirstId
- item.ClassifySecondId = origin.ClassifySecondId
- item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
- item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
- return
- }
- // ReportApproveFlowAddReq 新增报告审批流请求体
- type ReportApproveFlowAddReq struct {
- FlowName string `description:"审批流名称"`
- ReportType int `description:"报告类型:1-中文研报;2-英文研报;3-智能研报"`
- ClassifyFirstId int `description:"一级分类ID"`
- ClassifySecondId int `description:"二级分类ID"`
- Nodes []ReportApproveNodeSaveReq `description:"审批节点信息"`
- }
- // ReportApproveFlowEditReq 编辑报告审批流请求体
- type ReportApproveFlowEditReq struct {
- ReportApproveFlowId int `description:"审批流ID"`
- ReportApproveFlowAddReq
- }
- // ReportApproveFlowRemoveReq 删除报告审批流请求体
- type ReportApproveFlowRemoveReq struct {
- ReportApproveFlowId int `description:"报告审批流ID"`
- }
- type ReportClassifyTreeItem struct {
- ClassifyId int `description:"分类ID"`
- ClassifyName string `description:"分类名称"`
- ParentId int `description:"父级ID"`
- HasFlow bool `description:"是否已配置审批流"`
- Children []*ReportClassifyTreeItem `description:"子分类"`
- }
- // CreateFlowAndNodes 新增审批流和节点
- func (m *ReportApproveFlow) CreateFlowAndNodes(flowItem *ReportApproveFlow, nodeItems []*ReportApproveNode) (err error) {
- if flowItem == nil {
- err = fmt.Errorf("flow is nil")
- return
- }
- o := orm.NewOrmUsingDB("rddp")
- tx, e := o.Begin()
- if e != nil {
- err = fmt.Errorf("orm begin err: %s", e.Error())
- return
- }
- prevNodes := make([]*ReportApproveNode, 0)
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- // 更新每个节点的下一个节点信息, 放在事务中会更新失败
- if e = m.UpdateNextNodes(prevNodes); e != nil {
- err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
- return
- }
- }()
- lastId, e := tx.Insert(flowItem)
- if e != nil {
- err = fmt.Errorf("insert flow err: %s", e.Error())
- return
- }
- flowItem.ReportApproveFlowId = int(lastId)
- nodesLen := len(nodeItems)
- if nodesLen == 0 {
- return
- }
- prevId := 0
- prevNode := new(ReportApproveNode)
- for k, v := range nodeItems {
- v.ReportApproveFlowId = flowItem.ReportApproveFlowId
- v.PrevNodeId = prevId
- id, e := tx.Insert(v)
- if e != nil {
- err = fmt.Errorf("insert node err: %s", e.Error())
- return
- }
- v.ReportApproveNodeId = int(id)
- prevId = v.ReportApproveNodeId
- // 下一个节点
- if prevNode != nil && k > 0 && k < nodesLen {
- prevNode.NextNodeId = int(id)
- prevNodes = append(prevNodes, prevNode)
- }
- prevNode = v
- }
- return
- }
- func (m *ReportApproveFlow) UpdateNextNodes(nodes []*ReportApproveNode) (err error) {
- if len(nodes) == 0 {
- return
- }
- updateCols := []string{"NextNodeId"}
- for _, v := range nodes {
- e := v.Update(updateCols)
- if e != nil {
- err = fmt.Errorf("prev node update err: %s", e.Error())
- return
- }
- }
- return
- }
- // UpdateFlowAndNodes 更新审批流和节点
- func (m *ReportApproveFlow) UpdateFlowAndNodes(flowItem *ReportApproveFlow, nodeItems []*ReportApproveNode) (err error) {
- if flowItem == nil {
- err = fmt.Errorf("flow is nil")
- return
- }
- o := orm.NewOrmUsingDB("rddp")
- tx, e := o.Begin()
- if e != nil {
- err = fmt.Errorf("orm begin err: %s", e.Error())
- return
- }
- prevNodes := make([]*ReportApproveNode, 0)
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- return
- }
- _ = tx.Commit()
- // 更新每个节点的下一个节点信息, 放在事务中会更新失败
- if e = m.UpdateNextNodes(prevNodes); e != nil {
- err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
- return
- }
- }()
- // 更新审批流
- updateCols := []string{"FlowName", "ReportType", "ClassifyFirstId", "ClassifySecondId", "CurrVersion", "ModifyTime"}
- if e = flowItem.Update(updateCols); e != nil {
- err = fmt.Errorf("update flow err: %s", e.Error())
- return
- }
- // 新增节点(旧版的节点不删除, 根据版本号区分)
- nodesLen := len(nodeItems)
- if nodesLen == 0 {
- return
- }
- prevId := 0
- prevNode := new(ReportApproveNode)
- for k, v := range nodeItems {
- v.ReportApproveFlowId = flowItem.ReportApproveFlowId
- v.PrevNodeId = prevId
- id, e := tx.Insert(v)
- if e != nil {
- err = fmt.Errorf("insert node err: %s", e.Error())
- return
- }
- v.ReportApproveNodeId = int(id)
- prevId = v.ReportApproveNodeId
- // 下一个节点
- if prevNode != nil && k > 0 && k < nodesLen {
- prevNode.NextNodeId = int(id)
- prevNodes = append(prevNodes, prevNode)
- }
- prevNode = v
- }
- return
- }
- // ReportApproveFlowDetailItem 报告审批流详情信息
- type ReportApproveFlowDetailItem struct {
- ReportApproveFlowItem `description:"审批流信息"`
- Nodes []*ReportApproveNodeItem `description:"节点信息"`
- }
- // FormatFlowAndNodesItem2Detail 格式化审批流详情
- func FormatFlowAndNodesItem2Detail(flowItem *ReportApproveFlow, nodeItems []*ReportApproveNode) (detail *ReportApproveFlowDetailItem, err error) {
- if flowItem == nil {
- return
- }
- detail = new(ReportApproveFlowDetailItem)
- detail.ReportApproveFlowId = flowItem.ReportApproveFlowId
- detail.FlowName = flowItem.FlowName
- detail.ReportType = flowItem.ReportType
- detail.ClassifyFirstId = flowItem.ClassifyFirstId
- detail.ClassifySecondId = flowItem.ClassifySecondId
- detail.CreateTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.CreateTime)
- detail.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.ModifyTime)
- detail.Nodes = make([]*ReportApproveNodeItem, 0)
- for _, v := range nodeItems {
- t, e := FormatReportApproveNode2Item(v)
- if e != nil {
- err = fmt.Errorf("format node err: %s", e.Error())
- return
- }
- detail.Nodes = append(detail.Nodes, t)
- }
- return
- }
- // ReportApproveFlowListReq 审批流列表请求体
- type ReportApproveFlowListReq struct {
- PageSize int `form:"PageSize"`
- CurrentIndex int `form:"CurrentIndex"`
- ReportType int `form:"ReportType" description:"报告类型:1-中文研报;2-英文研报;3-智能研报"`
- ClassifyFirstId int `form:"ClassifyFirstId" description:"一级分类ID"`
- ClassifySecondId int `form:"ClassifySecondId" description:"二级分类ID"`
- Keyword string `form:"Keyword" description:"关键词"`
- SortRule int `form:"SortRule" description:"排序方式: 1-正序; 2-倒序(默认)"`
- }
- // ReportApproveFlowListResp 审批流列表响应体
- type ReportApproveFlowListResp struct {
- List []*ReportApproveFlowItem
- Paging *paging.PagingItem `description:"分页数据"`
- }
|