report_approve_flow.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. package report_approve
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "strings"
  8. "time"
  9. )
  10. // ReportApproveFlow 报告审批流表
  11. type ReportApproveFlow struct {
  12. ReportApproveFlowId int `orm:"column(report_approve_flow_id);pk" description:"审批流ID"`
  13. FlowName string `description:"审批流名称"`
  14. ReportType int `description:"报告类型:1-中文研报;2-英文研报;3-智能研报"`
  15. ClassifyFirstId int `description:"一级分类ID"`
  16. ClassifySecondId int `description:"二级分类ID"`
  17. ClassifyThirdId int `description:"三级分类ID"`
  18. CurrVersion int `description:"当前版本号"`
  19. CreateTime time.Time `description:"创建时间"`
  20. ModifyTime time.Time `description:"修改时间"`
  21. Enabled int `description:"1:有效,0:禁用"`
  22. }
  23. var ReportApproveFlowCols = struct {
  24. ReportApproveFlowId string
  25. FlowName string
  26. ReportType string
  27. ClassifyFirstId string
  28. ClassifySecondId string
  29. ClassifyThirdId string
  30. CurrVersion string
  31. CreateTime string
  32. ModifyTime string
  33. Enabled string
  34. }{
  35. ReportApproveFlowId: "report_approve_flow_id",
  36. FlowName: "flow_name",
  37. ReportType: "report_type",
  38. ClassifyFirstId: "classify_first_id",
  39. ClassifySecondId: "classify_second_id",
  40. ClassifyThirdId: "classify_third_id",
  41. CurrVersion: "curr_version",
  42. CreateTime: "create_time",
  43. ModifyTime: "modify_time",
  44. Enabled: "enabled",
  45. }
  46. func (m *ReportApproveFlow) TableName() string {
  47. return "report_approve_flow"
  48. }
  49. func (m *ReportApproveFlow) PrimaryId() string {
  50. return ReportApproveFlowCols.ReportApproveFlowId
  51. }
  52. func (m *ReportApproveFlow) Create() (err error) {
  53. o := orm.NewOrmUsingDB("rddp")
  54. id, err := o.Insert(m)
  55. if err != nil {
  56. return
  57. }
  58. m.ReportApproveFlowId = int(id)
  59. return
  60. }
  61. func (m *ReportApproveFlow) CreateMulti(items []*ReportApproveFlow) (err error) {
  62. if len(items) == 0 {
  63. return
  64. }
  65. o := orm.NewOrmUsingDB("rddp")
  66. _, err = o.InsertMulti(len(items), items)
  67. return
  68. }
  69. func (m *ReportApproveFlow) Update(cols []string) (err error) {
  70. o := orm.NewOrmUsingDB("rddp")
  71. _, err = o.Update(m, cols...)
  72. return
  73. }
  74. func (m *ReportApproveFlow) Del() (err error) {
  75. o := orm.NewOrmUsingDB("rddp")
  76. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  77. _, err = o.Raw(sql, m.ReportApproveFlowId).Exec()
  78. return
  79. }
  80. func (m *ReportApproveFlow) MultiDel(menuIds []int) (err error) {
  81. if len(menuIds) == 0 {
  82. return
  83. }
  84. o := orm.NewOrmUsingDB("rddp")
  85. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.PrimaryId(), utils.GetOrmInReplace(len(menuIds)))
  86. _, err = o.Raw(sql, menuIds).Exec()
  87. return
  88. }
  89. func (m *ReportApproveFlow) GetItemById(id int) (item *ReportApproveFlow, err error) {
  90. o := orm.NewOrmUsingDB("rddp")
  91. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  92. err = o.Raw(sql, id).QueryRow(&item)
  93. return
  94. }
  95. func (m *ReportApproveFlow) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ReportApproveFlow, err error) {
  96. o := orm.NewOrmUsingDB("rddp")
  97. order := ``
  98. if orderRule != "" {
  99. order = ` ORDER BY ` + orderRule
  100. }
  101. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  102. err = o.Raw(sql, pars).QueryRow(&item)
  103. return
  104. }
  105. func (m *ReportApproveFlow) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  106. o := orm.NewOrmUsingDB("rddp")
  107. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  108. err = o.Raw(sql, pars).QueryRow(&count)
  109. return
  110. }
  111. func (m *ReportApproveFlow) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ReportApproveFlow, err error) {
  112. o := orm.NewOrmUsingDB("rddp")
  113. fields := strings.Join(fieldArr, ",")
  114. if len(fieldArr) == 0 {
  115. fields = `*`
  116. }
  117. order := `ORDER BY create_time DESC`
  118. if orderRule != "" {
  119. order = ` ORDER BY ` + orderRule
  120. }
  121. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  122. _, err = o.Raw(sql, pars).QueryRows(&items)
  123. return
  124. }
  125. func (m *ReportApproveFlow) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ReportApproveFlow, err error) {
  126. o := orm.NewOrmUsingDB("rddp")
  127. fields := strings.Join(fieldArr, ",")
  128. if len(fieldArr) == 0 {
  129. fields = `*`
  130. }
  131. order := `ORDER BY create_time DESC`
  132. if orderRule != "" {
  133. order = ` ORDER BY ` + orderRule
  134. }
  135. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  136. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  137. return
  138. }
  139. // ReportApproveFlowItem 报告审批流信息
  140. type ReportApproveFlowItem struct {
  141. ReportApproveFlowId int `description:"审批流ID"`
  142. FlowName string `description:"审批流名称"`
  143. ReportType int `description:"报告类型:1-中文研报;2-英文研报;3-智能研报"`
  144. ClassifyFirstId int `description:"一级分类ID"`
  145. ClassifySecondId int `description:"二级分类ID"`
  146. ClassifyThirdId int `description:"三级分类ID"`
  147. ReportClassify string `description:"报告类型: XX研报/一级分类/二级分类"`
  148. CreateTime string `description:"创建时间"`
  149. ModifyTime string `description:"修改时间"`
  150. }
  151. // FormatReportApproveFlow2Item 格式化报告审批流
  152. func FormatReportApproveFlow2Item(origin *ReportApproveFlow) (item *ReportApproveFlowItem) {
  153. item = new(ReportApproveFlowItem)
  154. if origin == nil {
  155. return
  156. }
  157. item.ReportApproveFlowId = origin.ReportApproveFlowId
  158. item.FlowName = origin.FlowName
  159. item.ReportType = origin.ReportType
  160. item.ClassifyFirstId = origin.ClassifyFirstId
  161. item.ClassifySecondId = origin.ClassifySecondId
  162. item.ClassifyThirdId = origin.ClassifyThirdId
  163. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
  164. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
  165. return
  166. }
  167. // ReportApproveFlowAddReq 新增报告审批流请求体
  168. type ReportApproveFlowAddReq struct {
  169. FlowName string `description:"审批流名称"`
  170. ReportType int `description:"报告类型:1-中文研报;2-英文研报;3-智能研报"`
  171. ClassifyFirstId int `description:"一级分类ID"`
  172. ClassifySecondId int `description:"二级分类ID"`
  173. ClassifyThirdId int `description:"三级分类ID"`
  174. Nodes []ReportApproveNodeSaveReq `description:"审批节点信息"`
  175. }
  176. // ReportApproveFlowEditReq 编辑报告审批流请求体
  177. type ReportApproveFlowEditReq struct {
  178. ReportApproveFlowId int `description:"审批流ID"`
  179. ReportApproveFlowAddReq
  180. }
  181. // ReportApproveFlowRemoveReq 删除报告审批流请求体
  182. type ReportApproveFlowRemoveReq struct {
  183. ReportApproveFlowId int `description:"报告审批流ID"`
  184. }
  185. type ReportClassifyTreeItem struct {
  186. ClassifyId int `description:"分类ID"`
  187. ClassifyName string `description:"分类名称"`
  188. ParentId int `description:"父级ID"`
  189. HasFlow bool `description:"是否已配置审批流"`
  190. Children []*ReportClassifyTreeItem `description:"子分类"`
  191. }
  192. // CreateFlowAndNodes 新增审批流和节点
  193. func (m *ReportApproveFlow) CreateFlowAndNodes(flowItem *ReportApproveFlow, nodeItems []*ReportApproveNode) (err error) {
  194. if flowItem == nil {
  195. err = fmt.Errorf("flow is nil")
  196. return
  197. }
  198. o := orm.NewOrmUsingDB("rddp")
  199. tx, e := o.Begin()
  200. if e != nil {
  201. err = fmt.Errorf("orm begin err: %s", e.Error())
  202. return
  203. }
  204. prevNodes := make([]*ReportApproveNode, 0)
  205. defer func() {
  206. if err != nil {
  207. _ = tx.Rollback()
  208. return
  209. }
  210. _ = tx.Commit()
  211. // 更新每个节点的下一个节点信息, 放在事务中会更新失败
  212. if e = m.UpdateNextNodes(prevNodes); e != nil {
  213. err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
  214. return
  215. }
  216. }()
  217. lastId, e := tx.Insert(flowItem)
  218. if e != nil {
  219. err = fmt.Errorf("insert flow err: %s", e.Error())
  220. return
  221. }
  222. flowItem.ReportApproveFlowId = int(lastId)
  223. nodesLen := len(nodeItems)
  224. if nodesLen == 0 {
  225. return
  226. }
  227. prevId := 0
  228. prevNode := new(ReportApproveNode)
  229. for k, v := range nodeItems {
  230. v.ReportApproveFlowId = flowItem.ReportApproveFlowId
  231. v.PrevNodeId = prevId
  232. id, e := tx.Insert(v)
  233. if e != nil {
  234. err = fmt.Errorf("insert node err: %s", e.Error())
  235. return
  236. }
  237. v.ReportApproveNodeId = int(id)
  238. prevId = v.ReportApproveNodeId
  239. // 下一个节点
  240. if prevNode != nil && k > 0 && k < nodesLen {
  241. prevNode.NextNodeId = int(id)
  242. prevNodes = append(prevNodes, prevNode)
  243. }
  244. prevNode = v
  245. }
  246. return
  247. }
  248. func (m *ReportApproveFlow) UpdateNextNodes(nodes []*ReportApproveNode) (err error) {
  249. if len(nodes) == 0 {
  250. return
  251. }
  252. updateCols := []string{"NextNodeId"}
  253. for _, v := range nodes {
  254. e := v.Update(updateCols)
  255. if e != nil {
  256. err = fmt.Errorf("prev node update err: %s", e.Error())
  257. return
  258. }
  259. }
  260. return
  261. }
  262. // UpdateFlowAndNodes 更新审批流和节点
  263. func (m *ReportApproveFlow) UpdateFlowAndNodes(flowItem *ReportApproveFlow, nodeItems []*ReportApproveNode) (err error) {
  264. if flowItem == nil {
  265. err = fmt.Errorf("flow is nil")
  266. return
  267. }
  268. o := orm.NewOrmUsingDB("rddp")
  269. tx, e := o.Begin()
  270. if e != nil {
  271. err = fmt.Errorf("orm begin err: %s", e.Error())
  272. return
  273. }
  274. prevNodes := make([]*ReportApproveNode, 0)
  275. defer func() {
  276. if err != nil {
  277. _ = tx.Rollback()
  278. return
  279. }
  280. _ = tx.Commit()
  281. // 更新每个节点的下一个节点信息, 放在事务中会更新失败
  282. if e = m.UpdateNextNodes(prevNodes); e != nil {
  283. err = fmt.Errorf("UpdatePrevNodes err: %s", e.Error())
  284. return
  285. }
  286. }()
  287. // 更新审批流
  288. updateCols := []string{"FlowName", "ReportType", "ClassifyFirstId", "ClassifySecondId", "ClassifyThirdId", "CurrVersion", "ModifyTime"}
  289. if e = flowItem.Update(updateCols); e != nil {
  290. err = fmt.Errorf("update flow err: %s", e.Error())
  291. return
  292. }
  293. // 新增节点(旧版的节点不删除, 根据版本号区分)
  294. nodesLen := len(nodeItems)
  295. if nodesLen == 0 {
  296. return
  297. }
  298. prevId := 0
  299. prevNode := new(ReportApproveNode)
  300. for k, v := range nodeItems {
  301. v.ReportApproveFlowId = flowItem.ReportApproveFlowId
  302. v.PrevNodeId = prevId
  303. id, e := tx.Insert(v)
  304. if e != nil {
  305. err = fmt.Errorf("insert node err: %s", e.Error())
  306. return
  307. }
  308. v.ReportApproveNodeId = int(id)
  309. prevId = v.ReportApproveNodeId
  310. // 下一个节点
  311. if prevNode != nil && k > 0 && k < nodesLen {
  312. prevNode.NextNodeId = int(id)
  313. prevNodes = append(prevNodes, prevNode)
  314. }
  315. prevNode = v
  316. }
  317. return
  318. }
  319. // ReportApproveFlowDetailItem 报告审批流详情信息
  320. type ReportApproveFlowDetailItem struct {
  321. ReportApproveFlowItem `description:"审批流信息"`
  322. Nodes []*ReportApproveNodeItem `description:"节点信息"`
  323. }
  324. // FormatFlowAndNodesItem2Detail 格式化审批流详情
  325. func FormatFlowAndNodesItem2Detail(flowItem *ReportApproveFlow, nodeItems []*ReportApproveNode) (detail *ReportApproveFlowDetailItem, err error) {
  326. if flowItem == nil {
  327. return
  328. }
  329. detail = new(ReportApproveFlowDetailItem)
  330. detail.ReportApproveFlowId = flowItem.ReportApproveFlowId
  331. detail.FlowName = flowItem.FlowName
  332. detail.ReportType = flowItem.ReportType
  333. detail.ClassifyFirstId = flowItem.ClassifyFirstId
  334. detail.ClassifySecondId = flowItem.ClassifySecondId
  335. detail.ClassifyThirdId = flowItem.ClassifyThirdId
  336. detail.CreateTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.CreateTime)
  337. detail.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, flowItem.ModifyTime)
  338. detail.Nodes = make([]*ReportApproveNodeItem, 0)
  339. for _, v := range nodeItems {
  340. t, e := FormatReportApproveNode2Item(v)
  341. if e != nil {
  342. err = fmt.Errorf("format node err: %s", e.Error())
  343. return
  344. }
  345. detail.Nodes = append(detail.Nodes, t)
  346. }
  347. return
  348. }
  349. // ReportApproveFlowListReq 审批流列表请求体
  350. type ReportApproveFlowListReq struct {
  351. PageSize int `form:"PageSize"`
  352. CurrentIndex int `form:"CurrentIndex"`
  353. ReportType int `form:"ReportType" description:"报告类型:1-中文研报;2-英文研报;3-智能研报"`
  354. ClassifyFirstId int `form:"ClassifyFirstId" description:"一级分类ID"`
  355. ClassifySecondId int `form:"ClassifySecondId" description:"二级分类ID"`
  356. ClassifyThirdId int `form:"ClassifyThirdId" description:"三级级分类ID"`
  357. Keyword string `form:"Keyword" description:"关键词"`
  358. SortRule int `form:"SortRule" description:"排序方式: 1-正序; 2-倒序(默认)"`
  359. }
  360. // ReportApproveFlowListResp 审批流列表响应体
  361. type ReportApproveFlowListResp struct {
  362. List []*ReportApproveFlowItem
  363. Paging *paging.PagingItem `description:"分页数据"`
  364. }