classify.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package models
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. type Classify struct {
  10. Id int `gorm:"column:id;primaryKey" json:"id"` //`orm:"column(id);pk" gorm:"primaryKey" `
  11. ClassifyName string `gorm:"column:classify_name" json:"classify_name"` //`description:"分类名称"`
  12. Sort int `gorm:"column:sort" json:"sort"` //`json:"-"`
  13. ParentId int `gorm:"column:parent_id" json:"parent_id"` //`description:"父级分类id"`
  14. CreateTime time.Time `gorm:"column:create_time" json:"create_time"` //`description:"创建时间"`
  15. ModifyTime time.Time `gorm:"column:modify_time" json:"modify_time"` //`description:"修改时间"`
  16. Abstract string `gorm:"column:abstract" json:"abstract"` //`description:"栏目简介"`
  17. Descript string `gorm:"column:descript" json:"descript"` //`description:"分享描述"`
  18. ReportAuthor string `gorm:"column:report_author" json:"report_author"` //`description:"栏目作者"`
  19. AuthorDescript string `gorm:"column:author_descript" json:"author_descript"` //`description:"作者简介"`
  20. ColumnImgUrl string `gorm:"column:column_img_url" json:"column_img_url"` //`description:"栏目配图"`
  21. HeadImgUrl string `gorm:"column:head_img_url" json:"head_img_url"` //`description:"头部banner"`
  22. AvatarImgUrl string `gorm:"column:avatar_img_url" json:"avatar_img_url"` //`description:"头像"`
  23. ReportImgUrl string `gorm:"column:report_img_url" json:"report_img_url"` //`description:"报告配图"`
  24. HomeImgUrl string `gorm:"column:home_img_url" json:"home_img_url"` //`description:"首页配图"`
  25. ClassifyLabel string `gorm:"column:classify_label" json:"classify_label"` //`description:"分类标签"`
  26. ShowType int `gorm:"column:show_type" json:"show_type"` //`description:"展示类型:1-列表 2-专栏"`
  27. HasTeleconference int `gorm:"column:has_teleconference" json:"has_teleconference"` //`description:"是否有电话会:0-否 1-是"`
  28. VipTitle string `gorm:"column:vip_title" json:"vip_title"` //`description:"研究员头衔"`
  29. IsShow int `gorm:"column:is_show" json:"is_show"` //`description:"是否在小程序显示:1-显示 0-隐藏"`
  30. YbFiccSort int `gorm:"column:yb_ficc_sort" json:"yb_ficc_sort"` //`description:"小程序FICC页排序"`
  31. YbFiccIcon string `gorm:"column:yb_ficc_icon" json:"yb_ficc_icon"` // `description:"小程序FICC页icon"`
  32. YbFiccPcIcon string `gorm:"column:yb_ficc_pc_icon" json:"yb_ficc_pc_icon"` //`description:"小程序PC端FICC页背景图"`
  33. YbIconUrl string `gorm:"column:yb_icon_url" json:"yb_icon_url"` //`description:"小程序已购页icon"`
  34. YbBgUrl string `gorm:"column:yb_bg_url" json:"yb_bg_url"` //`description:"小程序已购详情背景图"`
  35. YbListImg string `gorm:"column:yb_list_img" json:"yb_list_img"` //`description:"小程序研报列表封面图"`
  36. YbShareBgImg string `gorm:"column:yb_share_bg_img" json:"yb_share_bg_img"` //`description:"小程序研报详情分享背景图"`
  37. YbRightBanner string `gorm:"column:yb_right_banner" json:"yb_right_banner"` //`description:"Pc端详情页,右侧,报告合集背景图"`
  38. RelateTel int `gorm:"column:relate_tel" json:"relate_tel"` //`description:"是否在电话会中可选: 0-否; 1-是"`
  39. RelateVideo int `gorm:"column:relate_video" json:"relate_video"` //`description:"是否在路演视频中可选: 0-否; 1-是"`
  40. IsMassSend int `gorm:"column:is_mass_send" json:"is_mass_send"` //`description:"1:群发,0:非群发"`
  41. Enabled int `gorm:"column:enabled" json:"enabled"` //`description:"是否可用,1可用,0禁用"`
  42. Level int `gorm:"column:level" json:"level"` //`description:"层级"`
  43. HasChild int `gorm:"column:has_child" json:"has_child"` //`description:"是否有子级别,0:下面没有子分类,1:下面有子分类;默认:0"`
  44. ReportDetailShowType int `gorm:"column:report_detail_show_type" json:"report_detail_show_type"` //`description:"报告详情的展示类型:1-拼接;2:目录"`
  45. }
  46. type ClassifyAddReq struct {
  47. ClassifyName string `description:"分类名称"`
  48. ParentId int `description:"父级分类id,没有父级分类传0"`
  49. ChartPermissionIdList []int `description:"权限id数组"`
  50. }
  51. func GetClassifyByName(classifyName string, parentId int) (item *Classify, err error) {
  52. sql := `SELECT * FROM classify WHERE classify_name=? AND parent_id=? `
  53. err = global.DmSQL["rddp"].Raw(sql, classifyName, parentId).First(&item).Error
  54. return
  55. }
  56. func GetClassifyById(classifyId int) (item *Classify, err error) {
  57. sql := `SELECT * FROM classify WHERE id=? `
  58. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&item).Error
  59. return
  60. }
  61. // 添加分类
  62. func AddClassify(item *Classify) (err error) {
  63. err = global.DmSQL["rddp"].Create(item).Error
  64. return
  65. }
  66. func GetReportCountByClassifyId(classifyId int) (count int, err error) {
  67. sql := `SELECT COUNT(1) as num FROM report WHERE classify_id_second=? `
  68. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&count).Error
  69. return
  70. }
  71. func GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
  72. sql := `SELECT COUNT(1) as num FROM classify AS a
  73. INNER JOIN report AS b ON a.id=b.classify_id_second
  74. WHERE a.parent_id=? `
  75. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&count).Error
  76. return
  77. }
  78. func GetClassifySubCountByParentId(classifyId int) (count int, err error) {
  79. sqlCount := `
  80. SELECT COUNT(1) as num FROM classify AS a
  81. WHERE a.parent_id=? `
  82. err = global.DmSQL["rddp"].Raw(sqlCount, classifyId).Scan(&count).Error
  83. return
  84. }
  85. // 删除分类
  86. func DeleteClassify(classifyId int) (err error) {
  87. sql := `DELETE FROM classify WHERE id=? `
  88. err = global.DmSQL["rddp"].Exec(sql, classifyId).Error
  89. if err != nil {
  90. return
  91. }
  92. deleteImgSql := `DELETE FROM banner WHERE classify_id=? `
  93. err = global.DmSQL["rddp"].Exec(deleteImgSql, classifyId).Error
  94. return
  95. }
  96. // 根据id获取分类详情
  97. func FindByIdClassify(classifyId int) (item *Classify, err error) {
  98. sql := `SELECT * FROM classify WHERE id=? `
  99. err = global.DmSQL["rddp"].Raw(sql, classifyId).First(&item).Error
  100. return
  101. }
  102. type ClassifyList struct {
  103. Id int `gorm:"column:id"` //`orm:"column(id);pk" gorm:"primaryKey" `
  104. ClassifyName string `gorm:"column:classify_name"` //`description:"分类名称"`
  105. Sort int `gorm:"column:sort"` //`description:"排序"`
  106. ParentId int `gorm:"column:parent_id"` //`description:"父级分类id"`
  107. CreateTime time.Time `gorm:"column:create_time"` //`description:"创建时间"`
  108. ModifyTime time.Time `gorm:"column:modify_time"` //`description:"修改时间"`
  109. Abstract string `gorm:"column:abstract"` //`description:"简介"`
  110. Descript string `gorm:"column:descript"` //`description:"描述"`
  111. ClassifyLabel string `gorm:"column:classify_label"` //`description:"分类标签"`
  112. ShowType int `gorm:"column:show_type"` //`description:"展示类型:1-列表 2-专栏"`
  113. HasTeleconference int `gorm:"column:has_teleconference"` //`description:"是否有电话会:0-否 1-是"`
  114. IsShow int `gorm:"column:is_show"` //`description:"是否在小程序显示:1-显示 0-隐藏"`
  115. YbFiccSort int `gorm:"column:yb_ficc_sort"` //`description:"小程序FICC页排序"`
  116. YbFiccIcon string `gorm:"column:yb_ficc_icon"` // `description:"小程序FICC页icon"`
  117. YbFiccPcIcon string `gorm:"column:yb_ficc_pc_icon"` //`description:"小程序PC端FICC页背景图"`
  118. YbIconUrl string `gorm:"column:yb_icon_url"` //`description:"小程序已购页icon"`
  119. YbBgUrl string `gorm:"column:yb_bg_url"` //`description:"小程序已购详情背景图"`
  120. YbListImg string `gorm:"column:yb_list_img"` //`description:"小程序研报列表封面图"`
  121. YbShareBgImg string `gorm:"column:yb_share_bg_img"` //`description:"小程序研报详情分享背景图"`
  122. YbRightBanner string `gorm:"column:yb_right_banner"` //`description:"Pc端详情页,右侧,报告合集背景图"`
  123. RelateTel int `gorm:"column:relate_tel"` //`description:"是否在电话会中可选: 0-否; 1-是"`
  124. RelateVideo int `gorm:"column:relate_video"` //`description:"是否在路演视频中可选: 0-否; 1-是"`
  125. Enabled int `gorm:"column:enabled"` //`description:"是否可用,1可用,0禁用"`
  126. Child []*ClassifyList `gorm:"-"`
  127. ClassifyMenuId int `gorm:"-"` //`description:"二级分类-子目录ID"`
  128. ClassifyMenuList []*ClassifyMenu `gorm:"-"`
  129. ChartPermissionIdList []int `gorm:"-"` // `description:"绑定的权限ID"`
  130. Level int `gorm:"column:level"` //`description:"层级"`
  131. HasChild int `gorm:"column:has_child"` //`description:"是否有子级别,0:下面没有子分类,1:下面有子分类;默认:0"`
  132. }
  133. type ClassifyItem struct {
  134. Classify
  135. ClassifyMenuId int `description:"二级分类-子目录ID"`
  136. ClassifyMenuList []*ClassifyMenu
  137. ChartPermissionIdList []int `description:"绑定的权限ID"`
  138. Child []*ClassifyItem
  139. }
  140. type ClassifyListResp struct {
  141. List []*ClassifyList
  142. }
  143. type ClassifyPermissionListResp struct {
  144. List []*ClassifyList
  145. Paging *paging.PagingItem `description:"分页数据"`
  146. }
  147. type CheckDeleteClassifyReq struct {
  148. ClassifyId int `description:"分类ID"`
  149. }
  150. type CheckDeleteClassifyResp struct {
  151. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  152. Msg string `description:"描述信息"`
  153. }
  154. type DeleteClassifyReq struct {
  155. ClassifyId int `description:"分类ID"`
  156. }
  157. type EditClassifyReq struct {
  158. ClassifyId int `description:"分类ID"`
  159. ClassifyAddReq
  160. }
  161. type FindByIdClassifyReq struct {
  162. ClassifyId int `description:"分类ID"`
  163. }
  164. func GetClassifyChild(parentId int, keyWord string) (items []*Classify, err error) {
  165. sql := ``
  166. pars := make([]interface{}, 0)
  167. if keyWord != "" {
  168. sql = `SELECT * FROM classify WHERE classify_name LIKE ? AND parent_id=? ORDER BY create_time ASC `
  169. pars = append(pars, utils.GetLikeKeyword(keyWord))
  170. } else {
  171. sql = `SELECT * FROM classify WHERE parent_id=? ORDER BY create_time ASC `
  172. }
  173. pars = append(pars, parentId)
  174. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  175. return
  176. }
  177. // EditClassifyPermissionReq 编辑分类权限请求
  178. type EditClassifyPermissionReq struct {
  179. ClassifyId int `description:"分类ID"`
  180. ChartPermissionIdList []int `description:"权限id数组"`
  181. }
  182. // GetAllClassify 获取所有分类
  183. func GetAllClassify() (list []*Classify, err error) {
  184. sql := ` SELECT * FROM classify `
  185. err = global.DmSQL["rddp"].Raw(sql).Find(&list).Error
  186. return
  187. }
  188. // UpdateClassify 更新分类
  189. func (classifyInfo *Classify) UpdateClassify(cols []string) (err error) {
  190. err = global.DmSQL["rddp"].Select(cols).Updates(classifyInfo).Error
  191. return
  192. }
  193. // SimpleClassifyList 简版分类列表
  194. type SimpleClassifyList struct {
  195. Id int `description:"分类ID"`
  196. ClassifyName string `description:"分类名称"`
  197. ParentId int `description:"父级ID"`
  198. Sort int `description:"排序"`
  199. Child []*SimpleClassifyList
  200. }
  201. // GetClassifyByCondition 获取分类列表
  202. func GetClassifyByCondition(condition, orderRule string, pars []interface{}) (items []*SimpleClassifyList, err error) {
  203. sql := `SELECT * FROM classify WHERE 1 = 1 `
  204. if condition != `` {
  205. sql += condition
  206. }
  207. order := `sort ASC, create_time ASC`
  208. if orderRule != `` {
  209. order = orderRule
  210. }
  211. sql += ` ORDER BY ` + order
  212. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  213. return
  214. }
  215. // RelateTelSecClassifyWithPermissions 关联了电话会的二级分类及权限
  216. type RelateTelSecClassifyWithPermissions struct {
  217. Id int `description:"分类ID"`
  218. ClassifyName string `description:"分类名称"`
  219. ChartPermissionIds string `description:"权限IDs"`
  220. }
  221. // UpdateClassifySortByParentId 根据父类id更新排序
  222. func UpdateClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string) (err error) {
  223. sql := ` update classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  224. if permissionId > 0 {
  225. sql += ` or ( id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  226. }
  227. err = global.DmSQL["rddp"].Exec(sql, parentId, nowSort).Error
  228. return
  229. }
  230. // GetMaxSortByParentId 获取最大的排序值
  231. func (classifyInfo *Classify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  232. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM classify WHERE parent_id = ? `
  233. err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&maxSort).Error
  234. return
  235. }
  236. // GetMaxSort 获取最大的排序值
  237. func (classifyInfo *Classify) GetMaxSort() (maxSort int, err error) {
  238. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM classify`
  239. err = global.DmSQL["rddp"].Raw(sql).Scan(&maxSort).Error
  240. return
  241. }
  242. // GetFirstClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  243. func (classifyInfo *Classify) GetFirstClassifyByParentId(parentId int) (item *Classify, err error) {
  244. sql := `SELECT * FROM classify WHERE parent_id = ? order by sort asc, id asc limit 1`
  245. err = global.DmSQL["rddp"].Raw(sql, parentId).First(&item).Error
  246. return
  247. }
  248. type ClassifyMoveReq struct {
  249. ClassifyId int `description:"分类ID"`
  250. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  251. NextClassifyId int `description:"下一个兄弟节点分类id"`
  252. }
  253. type ClassifySetEnabledReq struct {
  254. ClassifyId int `description:"分类ID"`
  255. Enabled int `description:"是否可用,1可用,0禁用"`
  256. }
  257. func (classifyInfo *Classify) SetEnabled(id, enabled int) (err error) {
  258. to := global.DmSQL["rddp"].Begin()
  259. defer func() {
  260. if err != nil {
  261. _ = to.Rollback()
  262. } else {
  263. _ = to.Commit()
  264. }
  265. }()
  266. sql := ` UPDATE classify SET enabled =? WHERE id = ?`
  267. err = to.Exec(sql, enabled, id).Error
  268. if err != nil {
  269. return
  270. }
  271. sql = ` UPDATE classify SET enabled =? WHERE parent_id = ?`
  272. err = to.Exec(sql, enabled, id).Error
  273. if err != nil {
  274. return
  275. }
  276. return
  277. }
  278. // GetCountClassifyChildByParentId
  279. // @Description: 获取父级分类下子分类数量
  280. // @author: Roc
  281. // @datetime 2024-06-17 10:58:46
  282. // @param parentId int
  283. // @return total int
  284. // @return err error
  285. func GetCountClassifyChildByParentId(parentId int) (total int, err error) {
  286. sql := `SELECT count(1) AS total FROM classify WHERE parent_id = ? `
  287. err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&total).Error
  288. return
  289. }
  290. // GetClassifyListByKeyword
  291. // @Description: 获取分类列表
  292. // @author: Roc
  293. // @datetime 2024-06-19 09:49:33
  294. // @param keyWord string
  295. // @param enabled int
  296. // @return items []*ClassifyList
  297. // @return err error
  298. func GetClassifyListByKeyword(keyWord string, enabled int) (items []*ClassifyList, err error) {
  299. sql := ``
  300. pars := make([]interface{}, 0)
  301. sql = `SELECT * FROM classify WHERE 1=1 `
  302. if enabled == 1 {
  303. sql += ` AND enabled = 1 `
  304. }
  305. if keyWord != `` {
  306. sql += ` AND classify_name LIKE ? `
  307. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  308. }
  309. sql += ` ORDER BY sort ASC, create_time ASC`
  310. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  311. return
  312. }
  313. // GetClassifyListByParentIdList
  314. // @Description: 获取分类列表
  315. // @author: Roc
  316. // @datetime 2024-06-19 09:49:33
  317. // @param keyWord string
  318. // @param enabled int
  319. // @return items []*ClassifyList
  320. // @return err error
  321. func GetClassifyListByParentIdList(parentClassifyIdList []int) (items []*ClassifyList, err error) {
  322. num := len(parentClassifyIdList)
  323. if num <= 0 {
  324. return
  325. }
  326. sql := `SELECT * FROM classify WHERE id in (` + utils.GetOrmInReplace(num) + `) ORDER BY sort ASC, create_time ASC`
  327. err = global.DmSQL["rddp"].Raw(sql, parentClassifyIdList).Find(&items).Error
  328. return
  329. }
  330. // GetClassifyListByIdList
  331. // @Description: 根据指标ID列表,获取分类列表
  332. // @author: Roc
  333. // @datetime 2024-06-27 15:23:57
  334. // @param classifyIdList []int
  335. // @return items []*Classify
  336. // @return err error
  337. func GetClassifyListByIdList(classifyIdList []int) (items []*Classify, err error) {
  338. num := len(classifyIdList)
  339. if num <= 0 {
  340. return
  341. }
  342. sql := `SELECT * FROM classify WHERE id IN (` + utils.GetOrmInReplace(num) + `) `
  343. err = global.DmSQL["rddp"].Raw(sql, classifyIdList).Find(&items).Error
  344. return
  345. }