chart_permission.go 11 KB

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