data_approve.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package data_approve
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "time"
  7. )
  8. type DataApprove struct {
  9. DataApproveId int `gorm:"column:data_approve_id;primary_key"`
  10. Title string `gorm:"column:title"`
  11. DataType int `gorm:"column:data_type"`
  12. State int `gorm:"column:state"` // '审批状态:1-待审批;2-已审批;3-已驳回;4-已撤回'
  13. FlowId int `gorm:"column:flow_id"`
  14. FlowVersion int `gorm:"column:flow_version"`
  15. StartNodeId int `gorm:"column:start_node_id"`
  16. CurrNodeId int `gorm:"column:curr_node_id"`
  17. ApplyUserId int `gorm:"column:apply_user_id"`
  18. ApplyUserName string `gorm:"column:apply_user_name"`
  19. ApproveRemark string `gorm:"column:approve_remark"`
  20. ApproveTime time.Time `gorm:"column:approve_time"`
  21. CreateTime time.Time `gorm:"column:create_time"`
  22. ModifyTime time.Time `gorm:"column:update_time"`
  23. }
  24. var DataApproveCols = struct {
  25. DataApproveId string
  26. Title string
  27. DataType string // 资产类型:1:指标、2:图表
  28. State string // '审批状态:1-待审批;2-已审批;3-已驳回;4-已撤回'
  29. FlowId string
  30. FlowVersion string
  31. StartNodeId string
  32. CurrNodeId string
  33. ApplyUserId string
  34. ApplyUserName string
  35. ApproveRemark string
  36. ApproveTime string
  37. CreateTime string
  38. ModifyTime string
  39. }{
  40. DataApproveId: "data_approve_id",
  41. Title: "title",
  42. DataType: "data_type",
  43. State: "state",
  44. FlowId: "flow_id",
  45. FlowVersion: "flow_version",
  46. StartNodeId: "start_node_id",
  47. CurrNodeId: "curr_node_id",
  48. ApplyUserId: "apply_user_id",
  49. ApplyUserName: "apply_user_name",
  50. ApproveRemark: "approve_remark",
  51. ApproveTime: "approve_time",
  52. CreateTime: "create_time",
  53. ModifyTime: "modify_time",
  54. }
  55. type DataApproveItemOrm struct {
  56. DataApproveId int `description:"审批ID"`
  57. DataApproveRecordId int `description:"审批记录ID"`
  58. Title string `description:"审批标题"`
  59. DataType int `description:"资产类型"`
  60. State int `description:"审批状态:1-待审批;2-已审批;3-已驳回;4-已撤回"`
  61. RecordState int `description:"审批记录状态:1-待审批;2-已通过;3-已驳回"`
  62. FlowId int `description:"审批流ID"`
  63. FlowVersion int `description:"审批流版本"`
  64. StartNodeId int `description:"开始节点ID"`
  65. CurrNodeId int `description:"当前节点ID"`
  66. ApplyUserId int `description:"申请人ID"`
  67. ApplyUserName string `description:"申请人姓名"`
  68. ApproveRemark string `description:"审批备注"`
  69. ApproveTime time.Time `description:"审批时间"`
  70. HandleTime time.Time `description:"处理时间"`
  71. CreateTime time.Time `description:"创建时间"`
  72. ModifyTime time.Time `description:"修改时间"`
  73. NodeState int `description:"当前节点审批状态:1-待审批;2-已审批;3-已驳回;4-已撤回" json:"-"`
  74. NodeApproveTime time.Time `description:"当前节点审批时间" json:"-"`
  75. }
  76. func (b *DataApprove) TableName() string {
  77. return "data_approve"
  78. }
  79. func (b *DataApprove) Update(cols []string) (err error) {
  80. db := global.DmSQL["data"]
  81. err = db.Model(b).Select(cols).Updates(b).Error
  82. return
  83. }
  84. func (b *DataApprove) Create() (err error) {
  85. db := global.DmSQL["data"]
  86. err = db.Create(b).Error
  87. return
  88. }
  89. // AddDataApprove
  90. // @Description: 添加审批单
  91. // @author: Roc
  92. // @datetime 2024-12-06 09:46:04
  93. // @param dataApprove *DataApprove
  94. // @param relationList []*DataApproveRelation
  95. // @return err error
  96. func AddDataApprove(dataApprove *DataApprove, relationList []*DataApproveRelation) (err error) {
  97. db := global.DmSQL["data"].Begin()
  98. defer func() {
  99. if err != nil {
  100. db.Rollback()
  101. } else {
  102. db.Commit()
  103. }
  104. }()
  105. err = db.Create(dataApprove).Error
  106. if err != nil {
  107. return
  108. }
  109. relationIdList := make([]int, 0)
  110. // 关联指标
  111. if len(relationList) > 0 {
  112. for _, v := range relationList {
  113. v.DataApproveId = dataApprove.DataApproveId
  114. relationIdList = append(relationIdList, v.DataId)
  115. }
  116. err = db.CreateInBatches(relationList, utils.MultiAddNum).Error
  117. if err != nil {
  118. return
  119. }
  120. }
  121. // 更新资产公开状态
  122. switch dataApprove.DataType {
  123. case utils.DataApproveTypeEdb:
  124. sql := `UPDATE edb_info set public_status = ?,modify_time=now() WHERE edb_info_id in (?) `
  125. err = global.DmSQL["data"].Exec(sql, utils.DataPublicCommit, relationIdList).Error
  126. case utils.DataApproveTypeChart:
  127. sql := `UPDATE chart_info set public_status = ?,modify_time=now() WHERE chart_info_id in (?) `
  128. err = global.DmSQL["data"].Exec(sql, utils.DataPublicCommit, relationIdList).Error
  129. }
  130. return
  131. }
  132. func GetDataApproveByFlowIdAndVersionId(dataFlowId int, flowVersion int) (item []*DataApprove, err error) {
  133. db := global.DmSQL["data"]
  134. err = db.Where("flow_id = ? AND flow_version = ?", dataFlowId, flowVersion).Find(&item).Error
  135. return
  136. }
  137. func GetDataApproveById(DataApproveId int) (item *DataApprove, err error) {
  138. db := global.DmSQL["data"]
  139. err = db.Where("data_approve_id = ?", DataApproveId).First(&item).Error
  140. return
  141. }
  142. // GetApprovingDataApproveCount 获取待处理的审批分页列表总数
  143. func GetApprovingDataApproveCount(cond string, pars []interface{}) (count int, err error) {
  144. base := fmt.Sprintf(`SELECT a.data_approve_record_id
  145. FROM data_approve_record AS a
  146. JOIN data_approve AS b ON a.data_approve_id = b.data_approve_id AND a.node_id = b.curr_node_id
  147. WHERE 1 = 1 %s`, cond)
  148. sql := fmt.Sprintf(`SELECT COUNT(1) FROM (%s) t`, base)
  149. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  150. return
  151. }
  152. // GetApprovingDataApprovePageList 获取待处理的审批列表-分页
  153. func GetApprovingDataApprovePageList(cond string, pars []interface{}, orderRule string, startSize, pageSize int) (items []*DataApproveItemOrm, err error) {
  154. order := `ORDER BY a.create_time DESC`
  155. if orderRule != "" {
  156. order = ` ORDER BY ` + orderRule
  157. }
  158. sql := fmt.Sprintf(`SELECT a.data_approve_record_id, a.state AS record_state, b.*
  159. FROM data_approve_record AS a
  160. JOIN data_approve AS b ON a.data_approve_id = b.data_approve_id AND a.node_id = b.curr_node_id
  161. WHERE 1 = 1 %s %s
  162. LIMIT ?,?`, cond, order)
  163. pars = append(pars, startSize, pageSize)
  164. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  165. return
  166. }
  167. // GetApprovedDataApproveCount 获取已处理的审批分页列表总数
  168. func GetApprovedDataApproveCount(cond string, pars []interface{}) (count int, err error) {
  169. base := fmt.Sprintf(`SELECT a.data_approve_record_id
  170. FROM data_approve_record AS a
  171. JOIN data_approve AS b ON a.data_approve_id = b.data_approve_id
  172. WHERE 1 = 1 %s`, cond)
  173. sql := fmt.Sprintf(`SELECT COUNT(1) FROM (%s) t`, base)
  174. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  175. return
  176. }
  177. // GetApprovedDataApprovePageList 获取已处理的审批列表-分页
  178. func GetApprovedDataApprovePageList(cond string, pars []interface{}, orderRule string, startSize, pageSize int) (items []*DataApproveItemOrm, err error) {
  179. order := `ORDER BY a.create_time DESC`
  180. if orderRule != "" {
  181. order = ` ORDER BY ` + orderRule
  182. }
  183. sql := fmt.Sprintf(`SELECT a.data_approve_record_id, a.node_state AS record_state,a.node_state,a.node_approve_time, a.node_approve_time AS handle_time, b.*
  184. FROM data_approve_record AS a
  185. JOIN data_approve AS b ON a.data_approve_id = b.data_approve_id
  186. WHERE 1 = 1 %s %s
  187. LIMIT ?,?`, cond, order)
  188. pars = append(pars, startSize, pageSize)
  189. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  190. return
  191. }
  192. // GetApplyDataApproveCount 获取我发起的审批分页列表总数
  193. func GetApplyDataApproveCount(cond string, pars []interface{}) (count int, err error) {
  194. base := fmt.Sprintf(`SELECT a.* FROM data_approve AS a WHERE 1 = 1 %s`, cond)
  195. sql := fmt.Sprintf(`SELECT COUNT(1) FROM (%s) t`, base)
  196. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  197. return
  198. }
  199. // GetApplyDataApprovePageList 获取我发起的审批列表-分页
  200. func GetApplyDataApprovePageList(cond string, pars []interface{}, orderRule string, startSize, pageSize int) (items []*DataApproveItemOrm, err error) {
  201. order := `ORDER BY a.create_time DESC`
  202. if orderRule != "" {
  203. order = ` ORDER BY ` + orderRule
  204. }
  205. sql := fmt.Sprintf(`SELECT a.*,a.approve_time as handle_time,a.state as record_state FROM data_approve AS a WHERE 1 = 1 %s %s LIMIT ?,?`, cond, order)
  206. pars = append(pars, startSize, pageSize)
  207. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  208. return
  209. }
  210. func GetDataApproveCountByState(state int) (count int, err error) {
  211. db := global.DmSQL["data"]
  212. sql := `SELECT COUNT(*) FROM data_approve WHERE state = ?`
  213. err = db.Raw(sql, state).Scan(&count).Error
  214. return
  215. }