classify.go 20 KB

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