report_approve_flow.go 13 KB

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