classify.go 28 KB

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