classify.go 20 KB

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