classify.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. LevelPath string `gorm:"column:level_path" json:"level_path"` //`description:"分类的层级路径,英文逗号分隔"`
  149. }
  150. type ClassifyItem struct {
  151. Classify
  152. ClassifyMenuId int `description:"二级分类-子目录ID"`
  153. ClassifyMenuList []*ClassifyMenu
  154. ChartPermissionIdList []int `description:"绑定的权限ID"`
  155. Child []*ClassifyItem
  156. }
  157. type ClassifyListResp struct {
  158. List []*ClassifyList
  159. }
  160. type ClassifyPermissionListResp struct {
  161. List []*ClassifyList
  162. Paging *paging.PagingItem `description:"分页数据"`
  163. }
  164. type CheckDeleteClassifyReq struct {
  165. ClassifyId int `description:"分类ID"`
  166. }
  167. type CheckDeleteClassifyResp struct {
  168. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  169. Msg string `description:"描述信息"`
  170. }
  171. type DeleteClassifyReq struct {
  172. ClassifyId int `description:"分类ID"`
  173. }
  174. type EditClassifyReq struct {
  175. ClassifyId int `description:"分类ID"`
  176. ClassifyAddReq
  177. }
  178. type FindByIdClassifyReq struct {
  179. ClassifyId int `description:"分类ID"`
  180. }
  181. func GetClassifyChild(parentId int, keyWord string) (items []*Classify, err error) {
  182. sql := ``
  183. pars := make([]interface{}, 0)
  184. if keyWord != "" {
  185. sql = `SELECT * FROM classify WHERE classify_name LIKE ? AND parent_id=? ORDER BY create_time ASC `
  186. pars = append(pars, utils.GetLikeKeyword(keyWord))
  187. } else {
  188. sql = `SELECT * FROM classify WHERE parent_id=? ORDER BY create_time ASC `
  189. }
  190. pars = append(pars, parentId)
  191. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  192. return
  193. }
  194. // EditClassifyPermissionReq 编辑分类权限请求
  195. type EditClassifyPermissionReq struct {
  196. ClassifyId int `description:"分类ID"`
  197. ChartPermissionIdList []int `description:"权限id数组"`
  198. }
  199. // GetAllClassify 获取所有分类
  200. func GetAllClassify() (list []*Classify, err error) {
  201. sql := ` SELECT * FROM classify `
  202. err = global.DmSQL["rddp"].Raw(sql).Find(&list).Error
  203. return
  204. }
  205. // UpdateClassify 更新分类
  206. func (classifyInfo *Classify) UpdateClassify(cols []string) (err error) {
  207. err = global.DmSQL["rddp"].Select(cols).Updates(classifyInfo).Error
  208. return
  209. }
  210. // SimpleClassifyList 简版分类列表
  211. type SimpleClassifyList struct {
  212. Id int `description:"分类ID"`
  213. ClassifyName string `description:"分类名称"`
  214. ParentId int `description:"父级ID"`
  215. Sort int `description:"排序"`
  216. Child []*SimpleClassifyList
  217. }
  218. // GetClassifyByCondition 获取分类列表
  219. func GetClassifyByCondition(condition, orderRule string, pars []interface{}) (items []*SimpleClassifyList, err error) {
  220. sql := `SELECT * FROM classify WHERE 1 = 1 `
  221. if condition != `` {
  222. sql += condition
  223. }
  224. order := `sort ASC, create_time ASC`
  225. if orderRule != `` {
  226. order = orderRule
  227. }
  228. sql += ` ORDER BY ` + order
  229. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  230. return
  231. }
  232. // RelateTelSecClassifyWithPermissions 关联了电话会的二级分类及权限
  233. type RelateTelSecClassifyWithPermissions struct {
  234. Id int `description:"分类ID"`
  235. ClassifyName string `description:"分类名称"`
  236. ChartPermissionIds string `description:"权限IDs"`
  237. }
  238. // UpdateClassifySortByParentId 根据父类id更新排序
  239. func UpdateClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string) (err error) {
  240. sql := ` update classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  241. if permissionId > 0 {
  242. sql += ` or ( id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  243. }
  244. err = global.DmSQL["rddp"].Exec(sql, parentId, nowSort).Error
  245. return
  246. }
  247. // GetMaxSortByParentId 获取最大的排序值
  248. func (classifyInfo *Classify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  249. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM classify WHERE parent_id = ? `
  250. err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&maxSort).Error
  251. return
  252. }
  253. // GetMaxSort 获取最大的排序值
  254. func (classifyInfo *Classify) GetMaxSort() (maxSort int, err error) {
  255. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM classify`
  256. err = global.DmSQL["rddp"].Raw(sql).Scan(&maxSort).Error
  257. return
  258. }
  259. // GetFirstClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  260. func (classifyInfo *Classify) GetFirstClassifyByParentId(parentId int) (item *Classify, err error) {
  261. sql := `SELECT * FROM classify WHERE parent_id = ? order by sort asc, id asc limit 1`
  262. err = global.DmSQL["rddp"].Raw(sql, parentId).First(&item).Error
  263. return
  264. }
  265. type ClassifyMoveReq struct {
  266. ClassifyId int `description:"分类ID"`
  267. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  268. NextClassifyId int `description:"下一个兄弟节点分类id"`
  269. }
  270. type ClassifySetEnabledReq struct {
  271. ClassifyId int `description:"分类ID"`
  272. Enabled int `description:"是否可用,1可用,0禁用"`
  273. }
  274. func (classifyInfo *Classify) SetEnabled(id, enabled int) (err error) {
  275. to := global.DmSQL["rddp"].Begin()
  276. defer func() {
  277. if err != nil {
  278. _ = to.Rollback()
  279. } else {
  280. _ = to.Commit()
  281. }
  282. }()
  283. sql := ` UPDATE classify SET enabled =? WHERE id = ?`
  284. err = to.Exec(sql, enabled, id).Error
  285. if err != nil {
  286. return
  287. }
  288. sql = ` UPDATE classify SET enabled =? WHERE parent_id = ?`
  289. err = to.Exec(sql, enabled, id).Error
  290. if err != nil {
  291. return
  292. }
  293. return
  294. }
  295. // GetCountClassifyChildByParentId
  296. // @Description: 获取父级分类下子分类数量
  297. // @author: Roc
  298. // @datetime 2024-06-17 10:58:46
  299. // @param parentId int
  300. // @return total int
  301. // @return err error
  302. func GetCountClassifyChildByParentId(parentId int) (total int, err error) {
  303. sql := `SELECT count(1) AS total FROM classify WHERE parent_id = ? `
  304. err = global.DmSQL["rddp"].Raw(sql, parentId).Scan(&total).Error
  305. return
  306. }
  307. // GetClassifyListByKeyword
  308. // @Description: 获取分类列表
  309. // @author: Roc
  310. // @datetime 2024-06-19 09:49:33
  311. // @param keyWord string
  312. // @param enabled int
  313. // @return items []*ClassifyList
  314. // @return err error
  315. func GetClassifyListByKeyword(keyWord string, enabled, classifyType int) (items []*ClassifyList, err error) {
  316. sql := ``
  317. pars := make([]interface{}, 0)
  318. sql = `SELECT * FROM classify WHERE 1=1 `
  319. if enabled == 1 {
  320. sql += ` AND enabled = 1 `
  321. }
  322. if classifyType > 0 {
  323. sql += fmt.Sprintf(` AND classify_type = %d `, classifyType)
  324. }
  325. if keyWord != `` {
  326. sql += ` AND classify_name LIKE ? `
  327. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  328. }
  329. sql += ` ORDER BY sort ASC, create_time ASC`
  330. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  331. return
  332. }
  333. // GetClassifyListByParentIdList
  334. // @Description: 获取分类列表
  335. // @author: Roc
  336. // @datetime 2024-06-19 09:49:33
  337. // @param keyWord string
  338. // @param enabled int
  339. // @return items []*ClassifyList
  340. // @return err error
  341. func GetClassifyListByParentIdList(parentClassifyIdList []int) (items []*ClassifyList, err error) {
  342. num := len(parentClassifyIdList)
  343. if num <= 0 {
  344. return
  345. }
  346. sql := `SELECT * FROM classify WHERE id in (` + utils.GetOrmInReplace(num) + `) ORDER BY sort ASC, create_time ASC`
  347. err = global.DmSQL["rddp"].Raw(sql, parentClassifyIdList).Find(&items).Error
  348. return
  349. }
  350. // GetClassifyListByIdList
  351. // @Description: 根据指标ID列表,获取分类列表
  352. // @author: Roc
  353. // @datetime 2024-06-27 15:23:57
  354. // @param classifyIdList []int
  355. // @return items []*Classify
  356. // @return err error
  357. func GetClassifyListByIdList(classifyIdList []int) (items []*Classify, err error) {
  358. num := len(classifyIdList)
  359. if num <= 0 {
  360. return
  361. }
  362. sql := `SELECT * FROM classify WHERE id IN (` + utils.GetOrmInReplace(num) + `) `
  363. err = global.DmSQL["rddp"].Raw(sql, classifyIdList).Find(&items).Error
  364. return
  365. }
  366. func (m *Classify) TableName() string {
  367. return "classify"
  368. }
  369. func (m *Classify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Classify, err error) {
  370. fields := strings.Join(fieldArr, ",")
  371. if len(fieldArr) == 0 {
  372. fields = `*`
  373. }
  374. order := `ORDER BY create_time DESC`
  375. if orderRule != "" {
  376. order = ` ORDER BY ` + orderRule
  377. }
  378. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  379. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  380. return
  381. }