report_approve_flow.go 12 KB

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