classify.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package models
  2. import (
  3. "eta_gn/eta_task/global"
  4. "fmt"
  5. "strings"
  6. "time"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. )
  9. type Classify struct {
  10. Id int `gorm:"column:id;primaryKey"` // `orm:"column(id);pk"`
  11. ClassifyName string `description:"分类名称"`
  12. Sort int `json:"-"`
  13. Level int `gorm:"column:level"` //`description:"分类级别"`
  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. IsMassSend int `description:"1:群发,0:非群发"`
  28. ClassifyType int `gorm:"column:classify_type"` //`description:"分类类型:1-研报;
  29. IsRemind int `gorm:"column:is_remind"` //`description:"是否开启提醒:0-关闭;1-开启"`
  30. RemindTime string `gorm:"column:remind_time"` //`description:"提醒时间:可选00:00-23:59"`
  31. ReportNum int `gorm:"column:report_num"` //`description:"分类下的报告数"`
  32. LevelPath string `gorm:"column:level_path"` //`description:"分类的层级路径,英文逗号分隔"`
  33. }
  34. type ClassifyAddReq struct {
  35. ClassifyName string `description:"分类名称"`
  36. ParentId int `description:"父级分类id,没有父级分类传0"`
  37. Abstract string `description:"栏目简介"`
  38. Descript string `description:"分享描述"`
  39. ReportAuthor string `description:"栏目作者"`
  40. AuthorDescript string `description:"作者简介"`
  41. ColumnImgUrl string `description:"栏目配图"`
  42. ReportImgUrl string `description:"报告配图"`
  43. HeadImgUrl string `description:"头部banner"`
  44. AvatarImgUrl string `description:"头像"`
  45. HomeImgUrl string `description:"首页配图"`
  46. ClassifyLabel string `description:"分类标签"`
  47. }
  48. //func GetClassifyByName(classifyName string, parentId int) (item *Classify, err error) {
  49. // sql := `SELECT * FROM classify WHERE classify_name=? AND parent_id=? `
  50. // o := orm.NewOrmUsingDB("rddp")
  51. // err = o.Raw(sql, classifyName, parentId).QueryRow(&item)
  52. // return
  53. //}
  54. func GetClassifyById(classifyId int) (item *Classify, err error) {
  55. //sql := `SELECT * FROM classify WHERE id=? `
  56. //o := orm.NewOrmUsingDB("rddp")
  57. //err = o.Raw(sql, classifyId).QueryRow(&item)
  58. sql := `SELECT * FROM classify WHERE id=? `
  59. err = global.DmSQL["data"].Raw(sql, classifyId).Find(&item).Error
  60. return
  61. }
  62. //
  63. //// 添加分类
  64. //func AddClassify(item *Classify) (err error) {
  65. // o := orm.NewOrmUsingDB("rddp")
  66. // _, err = o.Insert(item)
  67. // return
  68. //}
  69. //
  70. //func GetReportCountByClassifyId(classifyId int) (count int, err error) {
  71. // o := orm.NewOrmUsingDB("rddp")
  72. // sql := `SELECT COUNT(1) AS count FROM report WHERE classify_id_second=? `
  73. // err = o.Raw(sql, classifyId).QueryRow(&count)
  74. // return
  75. //}
  76. //
  77. //func GetClassifySubCountByClassifyId(classifyId int) (count int, err error) {
  78. // o := orm.NewOrmUsingDB("rddp")
  79. // sql := `SELECT COUNT(1) as num FROM classify AS a
  80. // INNER JOIN report AS b ON a.id=b.classify_id_second
  81. // WHERE a.parent_id=? `
  82. // err = o.Raw(sql, classifyId).QueryRow(&count)
  83. // return
  84. //}
  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. // o := orm.NewOrmUsingDB("rddp")
  91. // err = o.Raw(sqlCount, classifyId).QueryRow(&count)
  92. // return
  93. //}
  94. //
  95. //// 删除分类
  96. //func DeleteClassify(classifyId int) (err error) {
  97. // sql := `DELETE FROM classify WHERE id=? `
  98. // o := orm.NewOrmUsingDB("rddp")
  99. // _, err = o.Raw(sql, classifyId).Exec()
  100. // if err != nil {
  101. // return
  102. // }
  103. // deleteImgSql := `DELETE FROM banner WHERE classify_id=? `
  104. // _, err = o.Raw(deleteImgSql, classifyId).Exec()
  105. // return
  106. //}
  107. //
  108. //// classifyName, abstract, descript string, parentId, classifyId int
  109. //// 修改分类
  110. //func EditClassify(req *EditClassifyReq) (err error) {
  111. // o := orm.NewOrmUsingDB("rddp")
  112. // 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=?, modify_time= NOW() WHERE id = ? `
  113. // _, 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.ClassifyId).Exec()
  114. // return
  115. //}
  116. //
  117. ////获取父级分类
  118. //
  119. //func ParentClassify() (items []*Classify, err error) {
  120. // sql := `SELECT * FROM classify WHERE parent_id=0 order by id desc `
  121. // o := orm.NewOrmUsingDB("rddp")
  122. // _, err = o.Raw(sql).QueryRows(&items)
  123. // return
  124. //}
  125. //
  126. //// 根据id获取分类详情
  127. //func FindByIdClassify(classifyId int) (item *Classify, err error) {
  128. // sql := `SELECT * FROM classify WHERE id=? `
  129. // o := orm.NewOrmUsingDB("rddp")
  130. // err = o.Raw(sql, classifyId).QueryRow(&item)
  131. // return
  132. //}
  133. type ClassifyList struct {
  134. Id int `gorm:"column:id;primaryKey"` // `orm:"column(id);pk"`
  135. ClassifyName string `description:"分类名称"`
  136. Sort int `json:"-"`
  137. ParentId int `description:"父级分类id"`
  138. CreateTime time.Time `description:"创建时间"`
  139. ModifyTime time.Time `description:"修改时间"`
  140. Abstract string `description:"简介"`
  141. Descript string `description:"描述"`
  142. ClassifyLabel string `description:"分类标签"`
  143. Child []*Classify `gorm:"-"`
  144. }
  145. type ClassifyListResp struct {
  146. List []*ClassifyList `gorm:"-"`
  147. Paging *paging.PagingItem `description:"分页数据"`
  148. }
  149. //
  150. //// 获取分类列表
  151. //func GetClassifyList(startSize, pageSize int, keyWord, companyType string) (items []*ClassifyList, err error) {
  152. // sql := ``
  153. // companyTypeSqlStr := ``
  154. // if companyType == "ficc" {
  155. // companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
  156. // } else if companyType == "权益" {
  157. // companyTypeSqlStr = " AND (id = 40 or parent_id = 40) "
  158. // }
  159. // if keyWord != "" {
  160. // sql = `SELECT * FROM (
  161. // SELECT * FROM classify
  162. // WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE '%` + keyWord + `%'
  163. // UNION
  164. // SELECT * FROM classify
  165. // WHERE id IN(SELECT parent_id FROM classify
  166. // WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE '%` + keyWord + `%')
  167. // )AS t
  168. // ORDER BY create_time ASC
  169. // LIMIT ?,? `
  170. // } else {
  171. // sql = `SELECT * FROM classify WHERE parent_id=0 ` + companyTypeSqlStr + ` ORDER BY create_time ASC LIMIT ?,? `
  172. // }
  173. // o := orm.NewOrmUsingDB("rddp")
  174. // _, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
  175. // return
  176. //}
  177. //
  178. //func GetClassifyListCount(keyWord, companyType string) (count int, err error) {
  179. // sqlCount := ``
  180. //
  181. // companyTypeSqlStr := ``
  182. // if companyType == "ficc" {
  183. // companyTypeSqlStr = " AND id != 40 AND parent_id != 40 "
  184. // } else if companyType == "权益" {
  185. // companyTypeSqlStr = " AND (id = 40 or parent_id = 40) "
  186. // }
  187. // if keyWord != "" {
  188. // sqlCount = `SELECT COUNT(1) AS count FROM (
  189. // SELECT * FROM classify
  190. // WHERE parent_id=0 ` + companyTypeSqlStr + ` AND classify_name LIKE '%` + keyWord + `%'
  191. // UNION
  192. // SELECT * FROM classify
  193. // WHERE id IN(SELECT parent_id FROM classify
  194. // WHERE parent_id>0 ` + companyTypeSqlStr + ` AND classify_name LIKE '%` + keyWord + `%')
  195. // )AS t `
  196. //
  197. // } else {
  198. // sqlCount = `SELECT COUNT(1) AS count FROM classify WHERE parent_id=0 ` + companyTypeSqlStr
  199. // }
  200. // o := orm.NewOrmUsingDB("rddp")
  201. // err = o.Raw(sqlCount).QueryRow(&count)
  202. // return
  203. //}
  204. type CheckDeleteClassifyReq struct {
  205. ClassifyId int `description:"分类ID"`
  206. }
  207. type CheckDeleteClassifyResp struct {
  208. Code int `description:"编码:0:检测成功,可进行删除,1:分类不存在,2:该分类有关联报告,不允许删除,3:二级分类有关联报告,不允许删除,4:该分类下有关联分类,是否确认全部删除"`
  209. Msg string `description:"描述信息"`
  210. }
  211. type DeleteClassifyReq struct {
  212. ClassifyId int `description:"分类ID"`
  213. }
  214. type EditClassifyReq struct {
  215. ClassifyId int `description:"分类ID"`
  216. ClassifyName string `description:"分类名称"`
  217. ParentId int `description:"父级分类id"`
  218. Abstract string `description:"栏目简介"`
  219. Descript string `description:"分享描述"`
  220. ReportAuthor string `description:"栏目作者"`
  221. AuthorDescript string `description:"作者简介"`
  222. ColumnImgUrl string `description:"栏目配图"`
  223. HeadImgUrl string `description:"头部banner"`
  224. AvatarImgUrl string `description:"头像"`
  225. ReportImgUrl string `description:"报告配图"`
  226. HomeImgUrl string `description:"首页配图"`
  227. ClassifyLabel string `description:"分类标签"`
  228. }
  229. type FindByIdClassifyReq struct {
  230. ClassifyId int `description:"分类ID"`
  231. }
  232. //func GetClassifyChild(parentId int, keyWord string) (items []*Classify, err error) {
  233. // o := orm.NewOrmUsingDB("rddp")
  234. // sql := ``
  235. // if keyWord != "" {
  236. // sql = `SELECT * FROM classify WHERE parent_id=? AND classify_name LIKE '%` + keyWord + `%' ORDER BY create_time ASC `
  237. // } else {
  238. // sql = `SELECT * FROM classify WHERE parent_id=? ORDER BY create_time ASC `
  239. // }
  240. // _, err = o.Raw(sql, parentId).QueryRows(&items)
  241. // return
  242. //}
  243. func (m *Classify) TableName() string {
  244. return "classify"
  245. }
  246. func (m *Classify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*Classify, err error) {
  247. fields := strings.Join(fieldArr, ",")
  248. if len(fieldArr) == 0 {
  249. fields = `*`
  250. }
  251. order := `ORDER BY create_time DESC`
  252. if orderRule != "" {
  253. order = ` ORDER BY ` + orderRule
  254. }
  255. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  256. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  257. return
  258. }
  259. func (m *Classify) GetItemById(id int) (item *Classify, err error) {
  260. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? `, m.TableName(), "id")
  261. err = global.DmSQL["rddp"].Raw(sql, id).First(&item).Error
  262. return
  263. }
  264. func (m *Classify) GetItemsByIds(ids []int) (items []*Classify, err error) {
  265. if len(ids) == 0 {
  266. return
  267. }
  268. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s IN (?) ORDER BY level ASC`, m.TableName(), "id")
  269. err = global.DEFAULT_DmSQL.Raw(sql, ids).Find(&items).Error
  270. return
  271. }