chart_permission.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. package models
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. const (
  9. FiccProductId = 1
  10. )
  11. // ChartPermission 报告权限表
  12. type ChartPermission struct {
  13. ChartPermissionId int `orm:"column(chart_permission_id);pk" description:"问题ID" json:"chart_permission_id"`
  14. ChartPermissionName string `description:"名称" json:"chart_permission_name"`
  15. PermissionName string `description:"权限名" json:"permission_name"`
  16. Sort int `description:"排序" json:"sort"`
  17. Enabled int `description:"是否可用" json:"enabled"`
  18. CreatedTime time.Time `description:"创建时间" json:"created_time"`
  19. LastUpdatedTime time.Time `description:"更新时间" json:"last_updated_time"`
  20. TeleconferenceSort int `description:"电话会类型排序" json:"teleconference_sort"`
  21. Remark string `description:"备注" json:"remark"`
  22. ClassifyName string `description:"分类名称" json:"classify_name"`
  23. ProductName string `description:"产品名称" json:"product_name"`
  24. ProductId int `description:"产品ID" json:"product_id"`
  25. ImageURL string `orm:"column(image_url);" description:"图片地址" json:"image_url"`
  26. ShowType int `description:"1:查研观向小程序展示" json:"show_type"`
  27. IsOther int `description:"是否是其他,用于查研观向小程序后台展示" json:"is_other"`
  28. IsReport int `description:"是否是报告,用于查研观向小程序前台报告展示" json:"is_report"`
  29. CygxAuth int `description:"是否是权限,用于查研观向小程序前台权限校验" json:"cygx_auth"`
  30. PermissionType int `description:"1主观,2客观" json:"permission_type"`
  31. YbImgUrl string `description:"研报小程序报告列表icon" json:"yb_img_url"`
  32. ProductPermissionName string `description:"种类权限名称" json:"product_permission_name"`
  33. PriceDrivenState int `description:"品种价格驱动开启状态 0-关闭 1-开启" json:"price_driven_state"`
  34. ImageUrlM string `description:"图片地址(查研观向移动端)" json:"image_url_m"`
  35. ParentId int `description:"父级权限id" json:"parent_id"`
  36. IsPublic int `description:"是否是公有权限1:公有权限,0私有权限" json:"is_public"`
  37. }
  38. type ChartPermissionItem struct {
  39. PermissionId int `description:"品种权限ID"`
  40. PermissionName string `description:"品种权限名称"`
  41. ParentId int `description:"父级ID"`
  42. IsPublic int `description:"是否是公有权限1:公有权限,0私有权限" `
  43. Enabled int `description:"是否可用:1可用,0不可用" `
  44. Sort int `description:"排序"`
  45. CreateTime string `description:"创建时间"`
  46. Child []*ChartPermissionItem
  47. }
  48. // Update 更新
  49. func (c *ChartPermission) Update(cols []string) (err error) {
  50. o := orm.NewOrmUsingDB("rddp")
  51. _, err = o.Update(c, cols...)
  52. return
  53. }
  54. type PermissionAddReq struct {
  55. PermissionName string `description:"品种权限名称"`
  56. ParentId int `description:"父级ID"`
  57. Enabled int `description:"是否可用 0禁用, 1启用"` //启用,禁用操作会关联二级品种
  58. IsPublic int `description:"是否是公有权限1:公有权限,0私有权限"`
  59. }
  60. type PermissionEditReq struct {
  61. PermissionId int `description:"品种权限Id"` // 如果ID存在,则是更新操作,否则是新增操作
  62. PermissionName string `description:"品种权限名称"`
  63. ParentId int `description:"父级ID"`
  64. Enabled int `description:"是否可用 0禁用, 1启用"` //启用,禁用操作会关联二级品种
  65. IsPublic int `description:"是否是公有权限1:公有权限,0私有权限"`
  66. PublicPermissionIds []int `description:"公有权限的ID列表"` //一级品种没有公有私有属性
  67. }
  68. type PermissionEnabledReq struct {
  69. PermissionId int `description:"品种权限Id"` // 如果ID存在,则是更新操作,否则是新增操作
  70. Enabled int `description:"是否可用 0禁用, 1启用"` //启用,禁用操作会关联二级品种
  71. }
  72. type PermissionMoveReq struct {
  73. PermissionId int `description:"品种id"`
  74. // ParentChartPermissionId int `description:"父级品种id"`
  75. PrevPermissionId int `description:"上一个兄弟节点品种id"`
  76. NextPermissionId int `description:"下一个兄弟节点品种id"`
  77. }
  78. func (c *ChartPermission) SetEnabled(id, enabled int) (err error) {
  79. o := orm.NewOrmUsingDB("rddp")
  80. to, err := o.Begin()
  81. if err != nil {
  82. return
  83. }
  84. defer func() {
  85. if err != nil {
  86. _ = to.Rollback()
  87. } else {
  88. _ = to.Commit()
  89. }
  90. }()
  91. sql := ` UPDATE chart_permission SET enabled =? WHERE id = ?`
  92. _, err = to.Raw(sql, enabled, id).Exec()
  93. if err != nil {
  94. return
  95. }
  96. sql = ` UPDATE chart_permission SET enabled =? WHERE parent_id = ?`
  97. _, err = to.Raw(sql, enabled, id).Exec()
  98. if err != nil {
  99. return
  100. }
  101. return
  102. }
  103. // Create 新增权限
  104. func (c *ChartPermission) Create() (err error) {
  105. o := orm.NewOrmUsingDB("rddp")
  106. id, err := o.Insert(c)
  107. if err != nil {
  108. return
  109. }
  110. c.ChartPermissionId = int(id)
  111. return
  112. }
  113. // SetIsPublic 更新公有私有权限
  114. func (c *ChartPermission) SetIsPublic(ids []int, parentId, isPublic int) (err error) {
  115. o := orm.NewOrmUsingDB("rddp")
  116. sql := `update chart_permission set is_public=? WHERE parent_id = ? and chart_permission_id IN (` + utils.GetOrmInReplace(len(ids)) + `)`
  117. _, err = o.Raw(sql, isPublic, parentId, ids).Exec()
  118. return
  119. }
  120. // UpdatesByParentId 更新启动禁用
  121. func (c *ChartPermission) UpdateClassifyNameByParentId(parentId int, classifyName string) (err error) {
  122. o := orm.NewOrmUsingDB("rddp")
  123. sql := `update chart_permission set classify_name=? WHERE parent_id = ?`
  124. _, err = o.Raw(sql, classifyName, parentId).Exec()
  125. return
  126. }
  127. // SetEnabledByParentId 更新启动禁用
  128. func (c *ChartPermission) SetEnabledByParentId(parentId, enabled int) (err error) {
  129. o := orm.NewOrmUsingDB("rddp")
  130. sql := ` UPDATE chart_permission SET enabled =? WHERE parent_id = ?`
  131. _, err = o.Raw(sql, enabled, parentId).Exec()
  132. return
  133. }
  134. // SetEnabledByChartPermissionId 更新启动禁用
  135. func (c *ChartPermission) SetEnabledByChartPermissionId(chartPermissionId, enabled int) (err error) {
  136. o := orm.NewOrmUsingDB("rddp")
  137. sql := ` UPDATE chart_permission SET enabled =? WHERE chart_permission_id = ?`
  138. _, err = o.Raw(sql, enabled, chartPermissionId).Exec()
  139. return
  140. }
  141. // GetItemById 查询品种
  142. func (c *ChartPermission) GetItemById(chartPermissionId int) (item *ChartPermission, err error) {
  143. o := orm.NewOrmUsingDB("rddp")
  144. sql := `select * from chart_permission WHERE chart_permission_id = ?`
  145. err = o.Raw(sql, chartPermissionId).QueryRow(&item)
  146. return
  147. }
  148. // GetItemsByCondition 查询列表
  149. func (c *ChartPermission) GetItemsByCondition(condition string, pars []interface{}) (items []*ChartPermission, err error) {
  150. o := orm.NewOrmUsingDB("rddp")
  151. sql := `select * from chart_permission WHERE 1=1 ` + condition + ` order by sort asc, chart_permission_id asc`
  152. _, err = o.Raw(sql, pars).QueryRows(&items)
  153. return
  154. }
  155. // GetItemByCondition 查询列表
  156. func (c *ChartPermission) GetItemByCondition(condition string, pars []interface{}) (item *ChartPermission, err error) {
  157. o := orm.NewOrmUsingDB("rddp")
  158. sql := `select * from chart_permission WHERE 1=1 ` + condition
  159. err = o.Raw(sql, pars).QueryRow(&item)
  160. return
  161. }
  162. // UpdateChartPermissionSortByParentId 根据父类id更新排序
  163. func UpdateChartPermissionSortByParentId(parentId, chartPermissionId, nowSort int, updateSort string, productId int) (err error) {
  164. o := orm.NewOrmUsingDB("rddp")
  165. sql := ` update chart_permission set sort = ` + updateSort + ` WHERE parent_id=? AND product_id = ? AND (sort > ? `
  166. if chartPermissionId > 0 {
  167. sql += ` or ( chart_permission_id > ` + fmt.Sprint(chartPermissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  168. } else {
  169. sql += `)`
  170. }
  171. _, err = o.Raw(sql, parentId, productId, nowSort).Exec()
  172. return
  173. }
  174. // GetMaxSort 获取最大的排序值
  175. func (c *ChartPermission) GetMaxSort() (maxSort int, err error) {
  176. o := orm.NewOrmUsingDB("rddp")
  177. sql := `select max(sort) from chart_permission `
  178. err = o.Raw(sql).QueryRow(&maxSort)
  179. return
  180. }
  181. // GetMaxSortByParentId 获取最大的排序值
  182. func (c *ChartPermission) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  183. o := orm.NewOrmUsingDB("rddp")
  184. sql := `select max(sort) from chart_permission WHERE parent_id=? `
  185. err = o.Raw(sql, parentId).QueryRow(&maxSort)
  186. return
  187. }
  188. // GetFirstChartPermissionByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  189. func (c *ChartPermission) GetFirstChartPermissionByParentId(parentId int) (item *ChartPermission, err error) {
  190. o := orm.NewOrmUsingDB("rddp")
  191. sql := `select * from chart_permission WHERE parent_id=? ORDER BY sort ASC, chart_permission_id ASC LIMIT 1`
  192. err = o.Raw(sql, parentId).QueryRow(&item)
  193. return
  194. }
  195. // GetChartPermissionById 主键获取品种
  196. func GetChartPermissionById(permissionId int) (item *ChartPermission, err error) {
  197. o := orm.NewOrmUsingDB("rddp")
  198. sql := `SELECT * FROM chart_permission WHERE chart_permission_id = ?`
  199. err = o.Raw(sql, permissionId).QueryRow(&item)
  200. return
  201. }
  202. // GetSecondaryChartPermissions 获取二级权限列表
  203. func GetSecondaryChartPermissions() (list []*ChartPermission, err error) {
  204. o := orm.NewOrmUsingDB("rddp")
  205. sql := `SELECT * FROM chart_permission WHERE product_id = ? AND parent_id > 0 AND enabled = 1 ORDER BY parent_id ASC, sort ASC, created_time ASC`
  206. _, err = o.Raw(sql, FiccProductId).QueryRows(&list)
  207. return
  208. }
  209. type SimpleChartPermission struct {
  210. ChartPermissionId int `description:"品种ID"`
  211. ChartPermissionName string `description:"品种名称"`
  212. Sort int `description:"排序"`
  213. Children []*SimpleChartPermission `description:"子分类"`
  214. }
  215. func FormatChartPermission2Simple(origin *ChartPermission) (item *SimpleChartPermission) {
  216. if origin == nil {
  217. return
  218. }
  219. item = new(SimpleChartPermission)
  220. item.ChartPermissionId = origin.ChartPermissionId
  221. item.ChartPermissionName = origin.PermissionName
  222. item.Sort = origin.Sort
  223. return
  224. }
  225. // GetChartPermissionsByProductId 获取权限列表
  226. func GetChartPermissionsByProductId() (list []*ChartPermission, err error) {
  227. o := orm.NewOrmUsingDB("rddp")
  228. sql := `SELECT * FROM chart_permission WHERE product_id = ? AND enabled = 1 ORDER BY parent_id ASC, sort ASC, created_time ASC`
  229. _, err = o.Raw(sql, FiccProductId).QueryRows(&list)
  230. return
  231. }
  232. // GetChartPermissionByIdList
  233. // @Description: 根据品种ID列表获取权限列表
  234. // @author: Roc
  235. // @datetime 2024-06-07 10:32:29
  236. // @param permissionIdList []int
  237. // @return items []*ChartPermission
  238. // @return err error
  239. func GetChartPermissionByIdList(permissionIdList []int) (items []*ChartPermission, err error) {
  240. num := len(permissionIdList)
  241. if num <= 0 {
  242. return
  243. }
  244. o := orm.NewOrmUsingDB("rddp")
  245. sql := `SELECT * FROM chart_permission WHERE chart_permission_id in (` + utils.GetOrmInReplace(num) + `) `
  246. _, err = o.Raw(sql, permissionIdList).QueryRows(&items)
  247. return
  248. }