classify.go 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "time"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. //type Classify struct {
  11. // Id int `orm:"column(id);pk"`
  12. // ClassifyName string `description:"分类名称"`
  13. // Sort int `json:"-"`
  14. // ParentId int `description:"父级分类id"`
  15. // CreateTime time.Time `description:"创建时间"`
  16. // ModifyTime time.Time `description:"修改时间"`
  17. // Abstract string `description:"栏目简介"`
  18. // Descript string `description:"分享描述"`
  19. // ReportAuthor string `description:"栏目作者"`
  20. // AuthorDescript string `description:"作者简介"`
  21. // ColumnImgUrl string `description:"栏目配图"`
  22. // HeadImgUrl string `description:"头部banner"`
  23. // AvatarImgUrl string `description:"头像"`
  24. // ReportImgUrl string `description:"报告配图"`
  25. // HomeImgUrl string `description:"首页配图"`
  26. // ClassifyLabel string `description:"分类标签"`
  27. // ShowType int `description:"展示类型:1-列表 2-专栏"`
  28. // HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  29. // VipTitle string `description:"研究员头衔"`
  30. // IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  31. // YbFiccSort int `description:"小程序FICC页排序"`
  32. // YbFiccIcon string `description:"小程序FICC页icon"`
  33. // YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  34. // YbIconUrl string `description:"小程序已购页icon"`
  35. // YbBgUrl string `description:"小程序已购详情背景图"`
  36. // YbListImg string `description:"小程序研报列表封面图"`
  37. // YbShareBgImg string `description:"小程序研报详情分享背景图"`
  38. // YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  39. // RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  40. // RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`
  41. // IsMassSend int `description:"1:群发,0:非群发"`
  42. // Enabled int `description:"是否可用,1可用,0禁用"`
  43. // Level int `description:"层级"`
  44. // HasChild int `description:"是否有子级别,0:下面没有子分类,1:下面有子分类;默认:0"`
  45. // ReportDetailShowType int `description:"报告详情的展示类型:1-拼接;2:目录"`
  46. //}
  47. type Classify struct {
  48. Id int `gorm:"column:id;primaryKey" json:"id"` //`orm:"column(id);pk" gorm:"primaryKey" `
  49. ClassifyName string `gorm:"column:classify_name" json:"classify_name"` //`description:"分类名称"`
  50. Sort int `gorm:"column:sort" json:"sort"` //`json:"-"`
  51. ParentId int `gorm:"column:parent_id" json:"parent_id"` //`description:"父级分类id"`
  52. CreateTime time.Time `gorm:"column:create_time" json:"create_time"` //`description:"创建时间"`
  53. ModifyTime time.Time `gorm:"column:modify_time" json:"modify_time"` //`description:"修改时间"`
  54. Abstract string `gorm:"column:abstract" json:"abstract"` //`description:"栏目简介"`
  55. Descript string `gorm:"column:descript" json:"descript"` //`description:"分享描述"`
  56. ReportAuthor string `gorm:"column:report_author" json:"report_author"` //`description:"栏目作者"`
  57. AuthorDescript string `gorm:"column:author_descript" json:"author_descript"` //`description:"作者简介"`
  58. ColumnImgUrl string `gorm:"column:column_img_url" json:"column_img_url"` //`description:"栏目配图"`
  59. HeadImgUrl string `gorm:"column:head_img_url" json:"head_img_url"` //`description:"头部banner"`
  60. AvatarImgUrl string `gorm:"column:avatar_img_url" json:"avatar_img_url"` //`description:"头像"`
  61. ReportImgUrl string `gorm:"column:report_img_url" json:"report_img_url"` //`description:"报告配图"`
  62. HomeImgUrl string `gorm:"column:home_img_url" json:"home_img_url"` //`description:"首页配图"`
  63. ClassifyLabel string `gorm:"column:classify_label" json:"classify_label"` //`description:"分类标签"`
  64. ShowType int `gorm:"column:show_type" json:"show_type"` //`description:"展示类型:1-列表 2-专栏"`
  65. HasTeleconference int `gorm:"column:has_teleconference" json:"has_teleconference"` //`description:"是否有电话会:0-否 1-是"`
  66. VipTitle string `gorm:"column:vip_title" json:"vip_title"` //`description:"研究员头衔"`
  67. IsShow int `gorm:"column:is_show" json:"is_show"` //`description:"是否在小程序显示:1-显示 0-隐藏"`
  68. YbFiccSort int `gorm:"column:yb_ficc_sort" json:"yb_ficc_sort"` //`description:"小程序FICC页排序"`
  69. YbFiccIcon string `gorm:"column:yb_ficc_icon" json:"yb_ficc_icon"` // `description:"小程序FICC页icon"`
  70. YbFiccPcIcon string `gorm:"column:yb_ficc_pc_icon" json:"yb_ficc_pc_icon"` //`description:"小程序PC端FICC页背景图"`
  71. YbIconUrl string `gorm:"column:yb_icon_url" json:"yb_icon_url"` //`description:"小程序已购页icon"`
  72. YbBgUrl string `gorm:"column:yb_bg_url" json:"yb_bg_url"` //`description:"小程序已购详情背景图"`
  73. YbListImg string `gorm:"column:yb_list_img" json:"yb_list_img"` //`description:"小程序研报列表封面图"`
  74. YbShareBgImg string `gorm:"column:yb_share_bg_img" json:"yb_share_bg_img"` //`description:"小程序研报详情分享背景图"`
  75. YbRightBanner string `gorm:"column:yb_right_banner" json:"yb_right_banner"` //`description:"Pc端详情页,右侧,报告合集背景图"`
  76. RelateTel int `gorm:"column:relate_tel" json:"relate_tel"` //`description:"是否在电话会中可选: 0-否; 1-是"`
  77. RelateVideo int `gorm:"column:relate_video" json:"relate_video"` //`description:"是否在路演视频中可选: 0-否; 1-是"`
  78. IsMassSend int `gorm:"column:is_mass_send" json:"is_mass_send"` //`description:"1:群发,0:非群发"`
  79. Enabled int `gorm:"column:enabled" json:"enabled"` //`description:"是否可用,1可用,0禁用"`
  80. Level int `gorm:"column:level" json:"level"` //`description:"层级"`
  81. HasChild int `gorm:"column:has_child" json:"has_child"` //`description:"是否有子级别,0:下面没有子分类,1:下面有子分类;默认:0"`
  82. ReportDetailShowType int `gorm:"column:report_detail_show_type" json:"report_detail_show_type"` //`description:"报告详情的展示类型:1-拼接;2:目录"`
  83. }
  84. //type ClassifyVO struct {
  85. // Id int `orm:"column(id);pk"`
  86. // ClassifyName string `description:"分类名称"`
  87. // Sort int `json:"-"`
  88. // ParentId int `description:"父级分类id"`
  89. // ClassifyLabel string `description:"分类标签"`
  90. // Enabled int `description:"是否可用,1可用,0禁用"`
  91. // Level int `description:"层级"`
  92. // Children *[]ClassifyVO
  93. //}
  94. type ClassifyVO struct {
  95. Id int `gorm:"column:id;primaryKey"`
  96. ClassifyName string `description:"分类名称"`
  97. Sort int `json:"-"`
  98. ParentId int `description:"父级分类id"`
  99. ClassifyLabel string `description:"分类标签"`
  100. Enabled int `description:"是否可用,1可用,0禁用"`
  101. Level int `description:"层级"`
  102. Children *[]ClassifyVO
  103. }
  104. type ClassifyAddReq struct {
  105. ClassifyName string `description:"分类名称"`
  106. ParentId int `description:"父级分类id,没有父级分类传0"`
  107. ChartPermissionIdList []int `description:"权限id数组"`
  108. /*Abstract string `description:"栏目简介"`
  109. Descript string `description:"分享描述"`
  110. ReportAuthor string `description:"栏目作者"`
  111. AuthorDescript string `description:"作者简介"`
  112. ColumnImgUrl string `description:"栏目配图"`
  113. ReportImgUrl string `description:"报告配图"`
  114. HeadImgUrl string `description:"头部banner"`
  115. AvatarImgUrl string `description:"头像"`
  116. HomeImgUrl string `description:"首页配图"`
  117. ClassifyLabel string `description:"分类标签"`
  118. ShowType int `description:"展示类型:1-列表 2-专栏"`
  119. HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  120. VipTitle string `description:"研究员头衔"`
  121. Sort int `description:"后台排序"`
  122. IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  123. YbFiccSort int `description:"小程序FICC页排序"`
  124. YbFiccIcon string `description:"小程序FICC页icon"`
  125. YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  126. YbIconUrl string `description:"小程序已购页icon"`
  127. YbBgUrl string `description:"小程序已购详情背景图"`
  128. YbListImg string `description:"小程序研报列表封面图"`
  129. YbShareBgImg string `description:"小程序研报详情分享背景图"`
  130. YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  131. MenuList []*ClassifyMenuSaveReq `description:"子目录列表"`
  132. ClassifyMenuId int `description:"二级分类-子目录ID"`
  133. RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  134. RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`*/
  135. }
  136. func (c *Classify) Delete(childIds []int) (err error) {
  137. tx := global.DbMap[utils.DbNameReport].Begin()
  138. defer func() {
  139. if err != nil {
  140. tx.Rollback()
  141. } else {
  142. tx.Commit()
  143. }
  144. }()
  145. // 删除自身
  146. err = tx.Delete(c).Error
  147. if err != nil {
  148. return
  149. }
  150. // 删除子分类
  151. if len(childIds) > 0 {
  152. sql := `DELETE FROM classify WHERE id IN (` + utils.GetOrmInReplace(len(childIds)) + `)`
  153. err = tx.Exec(sql, childIds).Error
  154. if err != nil {
  155. return
  156. }
  157. }
  158. // 检查是否需要更新父类
  159. if c.ParentId > 0 {
  160. var count int
  161. sql := `SELECT COUNT(*) FROM classify WHERE parent_id=? `
  162. var countNull sql2.NullInt64
  163. err = tx.Raw(sql, c.ParentId).Scan(&countNull).Error
  164. if err != nil {
  165. return
  166. }
  167. if countNull.Valid {
  168. count = int(countNull.Int64)
  169. }
  170. if err != nil {
  171. return
  172. }
  173. if count == 0 {
  174. sql = `UPDATE classify SET has_child=0 WHERE id=? `
  175. err = tx.Exec(sql, c.ParentId).Error
  176. if err != nil {
  177. return
  178. }
  179. }
  180. }
  181. deleteImgSql := `DELETE FROM banner WHERE classify_id=? `
  182. err = tx.Exec(deleteImgSql, c.Id).Error
  183. return
  184. }
  185. func GetClassifyByName(classifyName string, parentId int) (item *Classify, err error) {
  186. sql := `SELECT * FROM classify WHERE classify_name=? AND parent_id=? `
  187. err = global.DbMap[utils.DbNameReport].Raw(sql, classifyName, parentId).First(&item).Error
  188. return
  189. }
  190. func GetClassifyById(classifyId int) (item *Classify, err error) {
  191. sql := `SELECT * FROM classify WHERE id=? `
  192. err = global.DbMap[utils.DbNameReport].Raw(sql, classifyId).First(&item).Error
  193. return
  194. }
  195. // 添加分类
  196. func AddClassify(item *Classify) (err error) {
  197. err = global.DbMap[utils.DbNameReport].Create(item).Error
  198. return
  199. }
  200. func GetReportCountByClassifyId(classifyId int) (count int, err error) {
  201. sql := `SELECT COUNT(1) AS count FROM report WHERE classify_id_second=? `
  202. var countNull sql2.NullInt64
  203. err = global.DbMap[utils.DbNameReport].Raw(sql, classifyId).Scan(&countNull).Error
  204. if err != nil {
  205. return
  206. }
  207. if countNull.Valid {
  208. count = int(countNull.Int64)
  209. }
  210. return
  211. }
  212. func GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
  213. sql := `SELECT COUNT(1) as num FROM classify AS a
  214. INNER JOIN report AS b ON a.id=b.classify_id_second
  215. WHERE a.parent_id=? `
  216. var countNull sql2.NullInt64
  217. err = global.DbMap[utils.DbNameReport].Raw(sql, classifyId).Scan(&countNull).Error
  218. if err != nil {
  219. return
  220. }
  221. if countNull.Valid {
  222. count = int(countNull.Int64)
  223. }
  224. return
  225. }
  226. func GetClassifySubCountByParentId(classifyId int) (count int, err error) {
  227. sqlCount := `
  228. SELECT COUNT(1) as num FROM classify AS a
  229. WHERE a.parent_id=? `
  230. var countNull sql2.NullInt64
  231. err = global.DbMap[utils.DbNameReport].Raw(sqlCount, classifyId).Scan(&countNull).Error
  232. if err != nil {
  233. return
  234. }
  235. if countNull.Valid {
  236. count = int(countNull.Int64)
  237. }
  238. return
  239. }
  240. // 删除分类
  241. //func DeleteClassify(classifyId int) (err error) {
  242. // sql := `DELETE FROM classify WHERE id=? `
  243. // o := global.DbMap[utils.DbNameReport]
  244. // _, err = o.Raw(sql, classifyId).Exec()
  245. // if err != nil {
  246. // return
  247. // }
  248. // deleteImgSql := `DELETE FROM banner WHERE classify_id=? `
  249. // _, err = o.Raw(deleteImgSql, classifyId).Exec()
  250. // return
  251. //}
  252. // classifyName, abstract, descript string, parentId, classifyId int
  253. // 修改分类
  254. //func EditClassify(req *EditClassifyReq) (err error) {
  255. // o := global.DbMap[utils.DbNameReport]
  256. // //sql := `UPDATE classify SET classify_name = ?,abstract=?, parent_id= ?,descript=?,report_author=?,author_descript=?,column_img_url=?,head_img_url=?,avatar_img_url=?,report_img_url=?,home_img_url=?,classify_label=?,show_type=?,has_teleconference=?,vip_title=?,modify_time= NOW() WHERE id = ? `
  257. // //_, err = o.Raw(sql, req.ClassifyName, req.Abstract, req.ParentId, req.Descript, req.ReportAuthor, req.AuthorDescript, req.ColumnImgUrl, req.HeadImgUrl, req.AvatarImgUrl, req.ReportImgUrl, req.HomeImgUrl, req.ClassifyLabel, req.ShowType, req.HasTeleconference, req.VipTitle, req.ClassifyId).Exec()
  258. // sql := `UPDATE classify SET classify_name = ?,parent_id= ?,modify_time= NOW() WHERE id = ? `
  259. // _, err = o.Raw(sql, req.ClassifyName, req.ParentId, req.ClassifyId).Exec()
  260. //
  261. // return
  262. //}
  263. // ParentClassify
  264. // @Description: 获取父级分类
  265. // @author: Roc
  266. // @datetime 2024-06-18 15:03:49
  267. // @return items []*Classify
  268. // @return err error
  269. //func ParentClassify() (items []*Classify, err error) {
  270. // sql := `SELECT * FROM classify WHERE parent_id=0 order by id desc `
  271. // o := global.DbMap[utils.DbNameReport]
  272. // _, err = o.Raw(sql).QueryRows(&items)
  273. // return
  274. //}
  275. // 根据id获取分类详情
  276. func FindByIdClassify(classifyId int) (item *Classify, err error) {
  277. sql := `SELECT * FROM classify WHERE id=? `
  278. err = global.DbMap[utils.DbNameReport].Raw(sql, classifyId).First(&item).Error
  279. return
  280. }
  281. //type ClassifyList struct {
  282. // Id int `orm:"column(id);pk"`
  283. // ClassifyName string `description:"分类名称"`
  284. // Sort int `description:"排序"`
  285. // ParentId int `description:"父级分类id"`
  286. // CreateTime time.Time `description:"创建时间"`
  287. // ModifyTime time.Time `description:"修改时间"`
  288. // Abstract string `description:"简介"`
  289. // Descript string `description:"描述"`
  290. // ClassifyLabel string `description:"分类标签"`
  291. // ShowType int `description:"展示类型:1-列表 2-专栏"`
  292. // HasTeleconference int `description:"是否有电话会:0-否 1-是"`
  293. // IsShow int `description:"是否在小程序显示:1-显示 0-隐藏"`
  294. // YbFiccSort int `description:"小程序FICC页排序"`
  295. // YbFiccIcon string `description:"小程序FICC页icon"`
  296. // YbFiccPcIcon string `description:"小程序PC端FICC页背景图"`
  297. // YbIconUrl string `description:"小程序已购页icon"`
  298. // YbBgUrl string `description:"小程序已购详情背景图"`
  299. // YbListImg string `description:"小程序研报列表封面图"`
  300. // YbShareBgImg string `description:"小程序研报详情分享背景图"`
  301. // YbRightBanner string `description:"Pc端详情页,右侧,报告合集背景图"`
  302. // RelateTel int `description:"是否在电话会中可选: 0-否; 1-是"`
  303. // RelateVideo int `description:"是否在路演视频中可选: 0-否; 1-是"`
  304. // Enabled int `description:"是否可用,1可用,0禁用"`
  305. // Child []*ClassifyList
  306. // ClassifyMenuId int `description:"二级分类-子目录ID"`
  307. // ClassifyMenuList []*ClassifyMenu
  308. // ChartPermissionIdList []int `description:"绑定的权限ID"`
  309. // Level int `description:"层级"`
  310. // HasChild int `description:"是否有子级别,0:下面没有子分类,1:下面有子分类;默认:0"`
  311. // IsEnableDelete int `description:"是否允许删除: 0-否; 1-是"`
  312. //}
  313. type ClassifyList struct {
  314. Id int `gorm:"column:id"` //`orm:"column(id);pk" gorm:"primaryKey" `
  315. ClassifyName string `gorm:"column:classify_name"` //`description:"分类名称"`
  316. Sort int `gorm:"column:sort"` //`description:"排序"`
  317. ParentId int `gorm:"column:parent_id"` //`description:"父级分类id"`
  318. CreateTime time.Time `gorm:"column:create_time"` //`description:"创建时间"`
  319. ModifyTime time.Time `gorm:"column:modify_time"` //`description:"修改时间"`
  320. Abstract string `gorm:"column:abstract"` //`description:"简介"`
  321. Descript string `gorm:"column:descript"` //`description:"描述"`
  322. ClassifyLabel string `gorm:"column:classify_label"` //`description:"分类标签"`
  323. ShowType int `gorm:"column:show_type"` //`description:"展示类型:1-列表 2-专栏"`
  324. HasTeleconference int `gorm:"column:has_teleconference"` //`description:"是否有电话会:0-否 1-是"`
  325. IsShow int `gorm:"column:is_show"` //`description:"是否在小程序显示:1-显示 0-隐藏"`
  326. YbFiccSort int `gorm:"column:yb_ficc_sort"` //`description:"小程序FICC页排序"`
  327. YbFiccIcon string `gorm:"column:yb_ficc_icon"` //`description:"小程序FICC页icon"`
  328. YbFiccPcIcon string `gorm:"column:yb_ficc_pc_icon"` //`description:"小程序PC端FICC页背景图"`
  329. YbIconUrl string `gorm:"column:yb_icon_url"` //`description:"小程序已购页icon"`
  330. YbBgUrl string `gorm:"column:yb_bg_url"` //`description:"小程序已购详情背景图"`
  331. YbListImg string `gorm:"column:yb_list_img"` //`description:"小程序研报列表封面图"`
  332. YbShareBgImg string `gorm:"column:yb_share_bg_img"` //`description:"小程序研报详情分享背景图"`
  333. YbRightBanner string `gorm:"column:yb_right_banner"` //`description:"Pc端详情页,右侧,报告合集背景图"`
  334. RelateTel int `gorm:"column:relate_tel"` //`description:"是否在电话会中可选: 0-否; 1-是"`
  335. RelateVideo int `gorm:"column:relate_video"` //`description:"是否在路演视频中可选: 0-否; 1-是"`
  336. Enabled int `gorm:"column:enabled"` //`description:"是否可用,1可用,0禁用"`
  337. Level int `gorm:"column:level"` //`description:"层级"`
  338. HasChild int `gorm:"column:has_child"` //`description:"是否有子级别,0:下面没有子分类,1:下面有子分类;默认:0"`
  339. Child []*ClassifyList `gorm:"-"`
  340. ClassifyMenuId int `gorm:"-"` //`description:"二级分类-子目录ID"`
  341. ClassifyMenuList []*ClassifyMenu `gorm:"-"`
  342. ChartPermissionIdList []int `gorm:"-"` //`description:"绑定的权限ID"`
  343. IsEnableDelete int `gorm:"-" description:"是否允许删除: 0-否; 1-是"`
  344. }
  345. type ClassifyItem struct {
  346. Classify
  347. ClassifyMenuId int `description:"二级分类-子目录ID"`
  348. ClassifyMenuList []*ClassifyMenu
  349. ChartPermissionIdList []int `description:"绑定的权限ID"`
  350. Child []*ClassifyItem
  351. }
  352. type ClassifyListResp struct {
  353. List []*ClassifyList
  354. }
  355. type ClassifyPermissionListResp struct {
  356. List []*ClassifyList
  357. Paging *paging.PagingItem `description:"分页数据"`
  358. }
  359. // 获取分类列表
  360. //func GetClassifyList(keyWord string, enabled int) (items []*ClassifyList, err error) {
  361. // sql := ``
  362. // companyTypeSqlStr := ``
  363. // if enabled == 1 {
  364. // companyTypeSqlStr += ` AND enabled = 1 `
  365. // }
  366. // pars := make([]interface{}, 0)
  367. // if keyWord != "" {
  368. // sql = `SELECT * FROM (
  369. // SELECT * FROM classify
  370. // WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE ?
  371. // UNION
  372. // SELECT * FROM classify
  373. // WHERE id IN( SELECT parent_id FROM classify
  374. // WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE ? )
  375. // )AS t
  376. // ORDER BY sort ASC,create_time ASC`
  377. // pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  378. // } else {
  379. // sql = `SELECT * FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
  380. //
  381. // sql += ` ORDER BY sort ASC, create_time ASC`
  382. // }
  383. // pars = append(pars)
  384. //
  385. // o := global.DbMap[utils.DbNameReport]
  386. // _, err = o.Raw(sql, pars...).QueryRows(&items)
  387. // return
  388. //}
  389. //func GetClassifyListCount(keyWord, companyType string, hideDayWeek int) (count int, err error) {
  390. // sqlCount := ``
  391. //
  392. // companyTypeSqlStr := ``
  393. // if companyType == "ficc" {
  394. // companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
  395. // } else if companyType == "权益" {
  396. // companyTypeSqlStr = " AND (id = 40 or parent_id = 40) "
  397. // }
  398. //
  399. // pars := make([]interface{}, 0)
  400. //
  401. // if keyWord != "" {
  402. // sqlCount = `SELECT COUNT(1) AS count FROM (
  403. // SELECT * FROM classify
  404. // WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE ?
  405. // UNION
  406. // SELECT * FROM classify
  407. // WHERE id IN(SELECT parent_id FROM classify
  408. // WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE ? )
  409. // )AS t `
  410. // pars = utils.GetLikeKeywordPars(pars, keyWord, 2)
  411. // } else {
  412. // sqlCount = `SELECT COUNT(1) AS count FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
  413. // if hideDayWeek == 1 {
  414. // sqlCount += ` AND classify_name <> '晨报' AND classify_name <> '周报' `
  415. // }
  416. // }
  417. // o := global.DbMap[utils.DbNameReport]
  418. // err = o.Raw(sqlCount, pars...).QueryRow(&count)
  419. // return
  420. //}
  421. type CheckDeleteClassifyReq struct {
  422. ClassifyId int `description:"分类ID"`
  423. }
  424. type CheckDeleteClassifyResp struct {
  425. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  426. Msg string `description:"描述信息"`
  427. }
  428. type DeleteClassifyReq struct {
  429. ClassifyId int `description:"分类ID"`
  430. }
  431. type EditClassifyReq struct {
  432. ClassifyId int `description:"分类ID"`
  433. ClassifyAddReq
  434. }
  435. type FindByIdClassifyReq struct {
  436. ClassifyId int `description:"分类ID"`
  437. }
  438. func GetClassifyChild(parentId int, keyWord string) (items []*Classify, err error) {
  439. sql := ``
  440. pars := make([]interface{}, 0)
  441. if keyWord != "" {
  442. sql = `SELECT * FROM classify WHERE classify_name LIKE ? AND parent_id=? ORDER BY create_time ASC `
  443. pars = append(pars, utils.GetLikeKeyword(keyWord))
  444. } else {
  445. sql = `SELECT * FROM classify WHERE parent_id=? ORDER BY create_time ASC `
  446. }
  447. pars = append(pars, parentId)
  448. err = global.DbMap[utils.DbNameReport].Raw(sql, pars...).Find(&items).Error
  449. return
  450. }
  451. //func GetClassifyChildByParentIds(parentId []int, keyWord string, enabled int) (items []*Classify, err error) {
  452. // parentIdLen := len(parentId)
  453. // if parentIdLen == 0 {
  454. // return
  455. // }
  456. // o := global.DbMap[utils.DbNameReport]
  457. // sql := ``
  458. // pars := make([]interface{}, 0)
  459. // pars = append(pars, parentId)
  460. // if keyWord != "" {
  461. // sql = `SELECT * FROM classify WHERE parent_id IN (` + utils.GetOrmInReplace(parentIdLen) + `) AND classify_name LIKE ? `
  462. // pars = append(pars, utils.GetLikeKeyword(keyWord))
  463. // } else {
  464. // sql = `SELECT * FROM classify WHERE parent_id IN (` + utils.GetOrmInReplace(parentIdLen) + `) `
  465. // }
  466. //
  467. // if enabled == 1 {
  468. // sql += ` AND enabled=1 `
  469. // }
  470. // sql += ` ORDER BY create_time ASC `
  471. // _, err = o.Raw(sql, pars...).QueryRows(&items)
  472. //
  473. // return
  474. //}
  475. // EditClassifyPermissionReq 编辑分类权限请求
  476. type EditClassifyPermissionReq struct {
  477. ClassifyId int `description:"分类ID"`
  478. ChartPermissionIdList []int `description:"权限id数组"`
  479. }
  480. // GetAllClassify 获取所有分类
  481. func GetAllClassify() (list []*Classify, err error) {
  482. o := global.DbMap[utils.DbNameReport]
  483. sql := ` SELECT * FROM classify `
  484. err = o.Raw(sql).Find(&list).Error
  485. return
  486. }
  487. // GetAllClassifyWithDesc 获取所有倒序分类
  488. func GetAllClassifyWithDesc() (list []*Classify, err error) {
  489. o := global.DbMap[utils.DbNameReport]
  490. sql := ` SELECT * FROM classify ORDER BY id DESC `
  491. err = o.Raw(sql).Find(&list).Error
  492. return
  493. }
  494. // GetClassifyByKeyword 名称获取分类
  495. //func GetClassifyByKeyword(keyword string) (item Classify, err error) {
  496. // o := global.DbMap[utils.DbNameReport]
  497. // sql := ` SELECT * FROM classify WHERE classify_name = ? LIMIT 1 `
  498. // err = o.Raw(sql, keyword).QueryRow(&item)
  499. // return
  500. //}
  501. // UpdateClassify 更新分类
  502. func (classifyInfo *Classify) UpdateClassify(cols []string) (err error) {
  503. err = global.DbMap[utils.DbNameReport].Select(cols).Updates(classifyInfo).Error
  504. return
  505. }
  506. // SimpleClassifyList 简版分类列表
  507. type SimpleClassifyList struct {
  508. Id int `description:"分类ID"`
  509. ClassifyName string `description:"分类名称"`
  510. ParentId int `description:"父级ID"`
  511. Sort int `description:"排序"`
  512. Child []*SimpleClassifyList
  513. }
  514. // GetClassifyByCondition 获取分类列表
  515. func GetClassifyByCondition(condition, orderRule string, pars []interface{}) (items []*SimpleClassifyList, err error) {
  516. sql := `SELECT * FROM classify WHERE 1 = 1 `
  517. if condition != `` {
  518. sql += condition
  519. }
  520. order := `sort ASC, create_time ASC`
  521. if orderRule != `` {
  522. order = orderRule
  523. }
  524. sql += ` ORDER BY ` + order
  525. err = global.DbMap[utils.DbNameReport].Raw(sql, pars...).Find(&items).Error
  526. return
  527. }
  528. // UpdateChildClassifyRelateSetting 更新子分类关联设置
  529. //func UpdateChildClassifyRelateSetting(parentId, relateTel, relateVideo int) (err error) {
  530. // o := global.DbMap[utils.DbNameReport]
  531. // sql := `UPDATE classify SET relate_tel = ?, relate_video = ? WHERE parent_id = ?`
  532. // _, err = o.Raw(sql, relateTel, relateVideo, parentId).Exec()
  533. // return
  534. //}
  535. // RelateTelSecClassifyWithPermissions 关联了电话会的二级分类及权限
  536. type RelateTelSecClassifyWithPermissions struct {
  537. Id int `description:"分类ID"`
  538. ClassifyName string `description:"分类名称"`
  539. ChartPermissionIds string `description:"权限IDs"`
  540. }
  541. // UpdateClassifySortByParentId 根据父类id更新排序
  542. func UpdateClassifySortByParentId(parentId, permissionId, nowSort int, updateSort string) (err error) {
  543. sql := ` update classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  544. if permissionId > 0 {
  545. sql += ` or ( id > ` + fmt.Sprint(permissionId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  546. }
  547. err = global.DbMap[utils.DbNameReport].Exec(sql, parentId, nowSort).Error
  548. return
  549. }
  550. // GetMaxSortByParentId 获取最大的排序值
  551. func (classifyInfo *Classify) GetMaxSortByParentId(parentId int) (maxSort int, err error) {
  552. sql := `SELECT max(sort) AS sort FROM classify WHERE parent_id = ? `
  553. if utils.DbDriverName == utils.DbDriverByDm {
  554. sql = `SELECT COALESCE(MAX(sort), 0) AS sort FROM classify WHERE parent_id = ? `
  555. }
  556. var maxNull sql2.NullInt64
  557. err = global.DbMap[utils.DbNameReport].Raw(sql, parentId).Scan(&maxNull).Error
  558. if err != nil {
  559. return
  560. }
  561. if maxNull.Valid {
  562. maxSort = int(maxNull.Int64)
  563. }
  564. return
  565. }
  566. // GetMaxSort 获取最大的排序值
  567. func (classifyInfo *Classify) GetMaxSort() (maxSort int, err error) {
  568. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM classify`
  569. var maxNull sql2.NullInt64
  570. err = global.DbMap[utils.DbNameReport].Raw(sql).Scan(&maxNull).Error
  571. if err != nil {
  572. return
  573. }
  574. if maxNull.Valid {
  575. maxSort = int(maxNull.Int64)
  576. }
  577. return
  578. }
  579. // GetFirstClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  580. func (classifyInfo *Classify) GetFirstClassifyByParentId(parentId int) (item *Classify, err error) {
  581. sql := `SELECT * FROM classify WHERE parent_id = ? order by sort asc, id asc limit 1`
  582. err = global.DbMap[utils.DbNameReport].Raw(sql, parentId).First(&item).Error
  583. return
  584. }
  585. type ClassifyMoveReq struct {
  586. ClassifyId int `description:"分类ID"`
  587. PrevClassifyId int `description:"上一个兄弟节点分类id"`
  588. NextClassifyId int `description:"下一个兄弟节点分类id"`
  589. }
  590. type ClassifySetEnabledReq struct {
  591. ClassifyId int `description:"分类ID"`
  592. Enabled int `description:"是否可用,1可用,0禁用"`
  593. }
  594. func (classifyInfo *Classify) SetEnabled(id, enabled int) (err error) {
  595. to := global.DbMap[utils.DbNameReport].Begin()
  596. defer func() {
  597. if err != nil {
  598. _ = to.Rollback()
  599. } else {
  600. _ = to.Commit()
  601. }
  602. }()
  603. sql := ` UPDATE classify SET enabled =? WHERE id = ?`
  604. err = to.Exec(sql, enabled, id).Error
  605. if err != nil {
  606. return
  607. }
  608. sql = ` UPDATE classify SET enabled =? WHERE parent_id = ?`
  609. err = to.Exec(sql, enabled, id).Error
  610. if err != nil {
  611. return
  612. }
  613. return
  614. }
  615. // GetCountClassifyChildByParentId
  616. // @Description: 获取父级分类下子分类数量
  617. // @author: Roc
  618. // @datetime 2024-06-17 10:58:46
  619. // @param parentId int
  620. // @return total int
  621. // @return err error
  622. func GetCountClassifyChildByParentId(parentId int) (total int, err error) {
  623. sql := `SELECT count(1) AS total FROM classify WHERE parent_id = ? `
  624. var countNull sql2.NullInt64
  625. err = global.DbMap[utils.DbNameReport].Raw(sql, parentId).Scan(&countNull).Error
  626. if err != nil {
  627. return
  628. }
  629. if countNull.Valid {
  630. total = int(countNull.Int64)
  631. }
  632. return
  633. }
  634. // GetClassifyListByKeyword
  635. // @Description: 获取分类列表
  636. // @author: Roc
  637. // @datetime 2024-06-19 09:49:33
  638. // @param keyWord string
  639. // @param enabled int
  640. // @return items []*ClassifyList
  641. // @return err error
  642. func GetClassifyListByKeyword(keyWord string, enabled int) (items []*ClassifyList, err error) {
  643. sql := ``
  644. pars := make([]interface{}, 0)
  645. sql = `SELECT * FROM classify WHERE 1=1 `
  646. if enabled == 1 {
  647. sql += ` AND enabled = 1 `
  648. }
  649. if keyWord != `` {
  650. sql += ` AND classify_name LIKE ? `
  651. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  652. }
  653. sql += ` ORDER BY sort ASC, create_time ASC`
  654. err = global.DbMap[utils.DbNameReport].Raw(sql, pars...).Find(&items).Error
  655. return
  656. }
  657. // GetClassifyListByParentIdList
  658. // @Description: 获取分类列表
  659. // @author: Roc
  660. // @datetime 2024-06-19 09:49:33
  661. // @param keyWord string
  662. // @param enabled int
  663. // @return items []*ClassifyList
  664. // @return err error
  665. func GetClassifyListByParentIdList(parentClassifyIdList []int) (items []*ClassifyList, err error) {
  666. num := len(parentClassifyIdList)
  667. if num <= 0 {
  668. return
  669. }
  670. sql := `SELECT * FROM classify WHERE id in (` + utils.GetOrmInReplace(num) + `) ORDER BY sort ASC, create_time ASC`
  671. err = global.DbMap[utils.DbNameReport].Raw(sql, parentClassifyIdList).Find(&items).Error
  672. return
  673. }
  674. // GetClassifyListByIdList
  675. // @Description: 根据指标ID列表,获取分类列表
  676. // @author: Roc
  677. // @datetime 2024-06-27 15:23:57
  678. // @param classifyIdList []int
  679. // @return items []*Classify
  680. // @return err error
  681. func GetClassifyListByIdList(classifyIdList []int) (items []*Classify, err error) {
  682. num := len(classifyIdList)
  683. if num <= 0 {
  684. return
  685. }
  686. sql := `SELECT * FROM classify WHERE id IN (` + utils.GetOrmInReplace(num) + `) `
  687. err = global.DbMap[utils.DbNameReport].Raw(sql, classifyIdList).Find(&items).Error
  688. return
  689. }
  690. // GetClassifyListByParentId 查询父分类下所有的子分类
  691. func GetClassifyListByParentId(parentId int) (items []*Classify, err error) {
  692. sql := `SELECT * FROM classify WHERE parent_id = ? and enabled = 1`
  693. err = global.DbMap[utils.DbNameReport].Raw(sql, parentId).Find(&items).Error
  694. return items, err
  695. }