admin_article.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package advisory
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "time"
  7. )
  8. type AdvisoryArticle struct {
  9. ArticleId int `orm:"column(advisory_article_id);pk"`
  10. Title string `description:"标题"`
  11. Body string `description:"内容"`
  12. Abstract string `description:"简介"`
  13. PublishStatus int `description:"发布状态1已发布、0未发布"`
  14. ChartPermissionId int `description:"所属分类ID"`
  15. ChartPermissionName string `description:"所属分类名称"`
  16. ActualPublishTime time.Time `description:"实际发布时间"`
  17. TimingPublishTime time.Time `description:"定时发布时间"`
  18. LinkName string `description:"链接名称"`
  19. LinkAddress string `description:"链接地址"`
  20. CreateTime time.Time `description:"创建时间"`
  21. LastUpdateTime time.Time `description:"修改时间"`
  22. AdminId int `description:"发布者的用户ID"`
  23. IsTiming int `description:"是否定时发布,1,定时发布,0不定时"`
  24. Code string `description:"分享字符串"`
  25. }
  26. type AdvisoryArticleLog struct {
  27. ArticleId int `orm:"column(advisory_article_id);pk"`
  28. Title string `description:"标题"`
  29. Body string `description:"内容"`
  30. Abstract string `description:"简介"`
  31. PublishStatus int `description:"发布状态1已发布、0未发布"`
  32. ChartPermissionId int `description:"所属分类ID"`
  33. ChartPermissionName string `description:"所属分类名称"`
  34. ActualPublishTime time.Time `description:"实际发布时间"`
  35. TimingPublishTime time.Time `description:"定时发布时间"`
  36. LinkName string `description:"链接名称"`
  37. LinkAddress string `description:"链接地址"`
  38. CreateTime time.Time `description:"创建时间"`
  39. LastUpdateTime time.Time `description:"修改时间"`
  40. AdminId int `description:"发布者的用户ID"`
  41. }
  42. //文章详情
  43. type AdvisoryArticleDetail struct {
  44. ArticleId int `orm:"column(advisory_article_id);pk"`
  45. Title string `description:"标题"`
  46. Body string `description:"内容"`
  47. Abstract string `description:"简介"`
  48. PublishStatus int `description:"发布状态1已发布、0未发布"`
  49. ChartPermissionId int `description:"所属分类ID"`
  50. ChartPermissionName string `description:"所属分类名称"`
  51. ActualPublishTime string `description:"实际发布时间"`
  52. TimingPublishTime string `description:"定时发布时间"`
  53. LinkName string `description:"链接名称"`
  54. LinkAddress string `description:"链接地址"`
  55. CreateTime string `description:"创建时间"`
  56. LastUpdateTime string `description:"修改时间"`
  57. AdminId int `description:"发布者的用户ID"`
  58. Code string `description:"分享字符串"`
  59. ArtCode string `orm:"column(code);"`
  60. }
  61. //预览文章详情带文章所属分类
  62. type PreviewArticleDetail struct {
  63. WhatWeek string `description:"周几"`
  64. ArticleId int `orm:"column(advisory_article_id);pk"`
  65. Title string `description:"标题"`
  66. Body string `description:"内容"`
  67. ChartPermissionId int `description:"所属分类ID"`
  68. ChartPermissionName string `description:"所属分类名称"`
  69. LinkName string `description:"链接名称"`
  70. LinkAddress string `description:"链接地址"`
  71. TimingPublishTime string `description:"定时发布时间"`
  72. PublishStatus int `description:"发布状态1已发布、0未发布"`
  73. //ClassifyChartPermissionName string `description:"所属一级分类名称"`
  74. ActualPublishTime string `description:"实际发布时间"`
  75. LastUpdateTime string `description:"上次编辑的时间"`
  76. Pv int `description:"PV"`
  77. Uv int `description:"UV"`
  78. }
  79. type PreviewArticleDetailList struct {
  80. List []*PreviewArticleDetail
  81. }
  82. type ArticleAddReq struct {
  83. ArticleId int `orm:"column(advisory_article_id);pk"`
  84. Title string `description:"标题"`
  85. Body string `description:"内容"`
  86. Abstract string `description:"文章个简介"`
  87. ChartPermissionId int `description:"所属分类ID"`
  88. ChartPermissionName string `description:"所属分类名称"`
  89. TimingPublishTime string `description:"定时发布时间"`
  90. LinkName string `description:"链接名称"`
  91. LinkAddress string `description:"链接地址"`
  92. }
  93. //保存文章
  94. type PreservationArticle struct {
  95. ArticleId int `orm:"column(advisory_article_id);pk"`
  96. Title string `description:"标题"`
  97. Body string `description:"内容"`
  98. Abstract string `description:"文章个简介"`
  99. ChartPermissionId int `description:"所属分类ID"`
  100. ChartPermissionName string `description:"所属分类名称"`
  101. TimingPublishTime string `description:"定时发布时间"`
  102. LinkName string `description:"链接名称"`
  103. LinkAddress string `description:"链接地址"`
  104. }
  105. type ArticleDeleteReq struct {
  106. ArticleId int64 `description:"ArticleId"`
  107. }
  108. //添加文章
  109. func AddArticle(item *AdvisoryArticle) (newId int64, err error) {
  110. fmt.Println(item)
  111. o := orm.NewOrm()
  112. newId, err = o.Insert(item)
  113. fmt.Println(newId)
  114. return
  115. }
  116. //添加文章日志
  117. func AddArticleLog(item *AdvisoryArticleLog) (newId int64, err error) {
  118. o := orm.NewOrm()
  119. newId, err = o.Insert(item)
  120. return
  121. }
  122. //编辑文章
  123. func EditArticle(item *AdvisoryArticle) (err error) {
  124. o := orm.NewOrm()
  125. sql := " UPDATE advisory_article SET title = ?, body = ?, chart_permission_id = ?,chart_permission_name=?,timing_publish_time=?,publish_status=?, link_name=?, link_address=?, actual_publish_time=? ,is_timing=? ,last_update_time = NOW() WHERE advisory_article_id = ? "
  126. o.Raw(sql, item.Title, item.Body, item.ChartPermissionId, item.ChartPermissionName, item.TimingPublishTime, item.PublishStatus, item.LinkName, item.LinkAddress, item.ActualPublishTime, item.IsTiming, item.ArticleId).Exec()
  127. return
  128. }
  129. //获取文章详情
  130. func GetUserArticleInfo(Uid int, ArticleId int64) (items *AdvisoryArticleDetail, err error) {
  131. o := orm.NewOrm()
  132. sql := `SELECT * FROM advisory_article WHERE advisory_article_id = ? LIMIT 1`
  133. err = o.Raw(sql, ArticleId).QueryRow(&items)
  134. return
  135. }
  136. //获取文章详情
  137. func GetUserArticleInfoByCode(Code string) (items *AdvisoryArticleDetail, err error) {
  138. o := orm.NewOrm()
  139. sql := `SELECT * FROM advisory_article WHERE publish_status = 1 AND code = ?`
  140. err = o.Raw(sql, Code).QueryRow(&items)
  141. return
  142. }
  143. //获取管理员今天是否在这个类目下发文章
  144. func GetUserTodayRticleInfo(Uid int, ChartPermissionId int) (items *AdvisoryArticleDetail, err error) {
  145. o := orm.NewOrm()
  146. sql := `SELECT * FROM advisory_article WHERE admin_id = ? AND chart_permission_id = ? AND publish_status = 1 AND last_update_time>=date(now()) and last_update_time<DATE_ADD(date(now()),INTERVAL 1 DAY) LIMIT 1`
  147. err = o.Raw(sql, Uid, ChartPermissionId).QueryRow(&items)
  148. return
  149. }
  150. type PreviewRsp struct {
  151. List []*PreviewArticleDetail
  152. Item *PreviewArticleDetail
  153. }
  154. //获取用户最新编辑的文章
  155. func GetUserNewArticleInfo(Uid int) (items *PreviewArticleDetail, err error) {
  156. o := orm.NewOrm()
  157. sql := `SELECT * FROM advisory_article WHERE admin_id = ? AND publish_status = 0 ORDER BY last_update_time DESC LIMIT 1`
  158. err = o.Raw(sql, Uid).QueryRow(&items)
  159. return
  160. }
  161. //文章删除
  162. func DeleteArticle(ArticleId int64) (err error) {
  163. sql := `DELETE FROM advisory_article WHERE advisory_article_id=? `
  164. o := orm.NewOrm()
  165. _, err = o.Raw(sql, ArticleId).Exec()
  166. return
  167. }
  168. //文章取消发布
  169. func UnpublishArticle(ArticleId int) (err error) {
  170. sql := `UPDATE advisory_article SET publish_status = 0 WHERE advisory_article_id=? `
  171. o := orm.NewOrm()
  172. _, err = o.Raw(sql, ArticleId).Exec()
  173. return
  174. }
  175. //校验用户是否关注了添加文章的分类
  176. func CheckIsFllow(fllowList []string, ChartPermissionId string) bool {
  177. for _, v := range fllowList {
  178. if ChartPermissionId == v {
  179. return true
  180. }
  181. }
  182. return false
  183. }
  184. //获取预览列表
  185. func GetPreviewArticleList(condition string, pars []interface{}, startSize, pageSize int) (items []*PreviewArticleDetail, err error) {
  186. sql := ` SELECT * FROM advisory_article WHERE 1=1 `
  187. if condition != "" {
  188. sql += condition
  189. }
  190. sql += ` ORDER BY create_time DESC LIMIT ?,? `
  191. o := orm.NewOrm()
  192. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  193. return
  194. }
  195. //获取预览数量
  196. func GetPreviewArticleListCount(condition string, pars []interface{}) (count int, err error) {
  197. sqlCount := ` SELECT COUNT(1) AS count FROM advisory_article WHERE 1=1 `
  198. if condition != "" {
  199. sqlCount += condition
  200. }
  201. o := orm.NewOrm()
  202. err = o.Raw(sqlCount, pars).QueryRow(&count)
  203. return
  204. }
  205. //获取文章列表
  206. func GetArticlerList(condition string, pars []interface{}, startSize, pageSize int) (items []*PreviewArticleDetail, err error) {
  207. sql := ` SELECT * ,
  208. (SELECT COUNT(1) FROM advisory_article_view_record AS avr WHERE avr.article_id=advisory_article.advisory_article_id) AS pv,
  209. (SELECT COUNT(DISTINCT user_id) FROM advisory_article_view_record AS avr WHERE avr.article_id=advisory_article.advisory_article_id) AS uv
  210. FROM advisory_article WHERE 1=1 `
  211. if condition != "" {
  212. sql += condition
  213. }
  214. sql += ` LIMIT ?,? `
  215. o := orm.NewOrm()
  216. //o.Using("rddp")
  217. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  218. return
  219. }
  220. //获取文章数量
  221. func GetArticleListCount(condition string, pars []interface{}) (count int, err error) {
  222. sqlCount := ` SELECT COUNT(1) AS count FROM advisory_article WHERE 1=1 `
  223. if condition != "" {
  224. sqlCount += condition
  225. }
  226. o := orm.NewOrm()
  227. //o.Using("rddp")
  228. err = o.Raw(sqlCount, pars).QueryRow(&count)
  229. return
  230. }
  231. type PreviewArticleListResp struct {
  232. CharList []*ArtChartPermission
  233. Preview *PreviewArticleDetail
  234. List []*PreviewArticleDetail
  235. Paging *paging.PagingItem `description:"分页数据"`
  236. }
  237. type ArticleListResp struct {
  238. List []*PreviewArticleDetail
  239. Paging *paging.PagingItem `description:"分页数据"`
  240. }
  241. //文章预览分类
  242. type ArtChartPermission struct {
  243. ChartPermissionId int `description:"权限id"`
  244. ClassifyName string `description:"分类名称"`
  245. PermissionName string `description:"名称"`
  246. ImageUrl string `description:"图片地址"`
  247. IsBlongChart bool `description:"是否属于这个分类"`
  248. ChildPermission []*ArtChartPermission
  249. }
  250. //获取一级分类名称
  251. func GetFirstChartPermissionAll() (items []*ArtChartPermission, err error) {
  252. o := orm.NewOrm()
  253. sql := `SELECT * FROM chart_permission as ch WHERE product_id = 1 AND product_name = 'ficc' GROUP BY ch.classify_name ORDER BY sort ASC; `
  254. _, err = o.Raw(sql).QueryRows(&items)
  255. return
  256. }
  257. func GetChartToClassifyName(ClassifyName string) (items []*ArtChartPermission, err error) {
  258. o := orm.NewOrm()
  259. sql := `SELECT * FROM chart_permission WHERE product_id = 1 AND product_name = 'ficc' AND classify_name = ? ORDER BY sort ASC `
  260. _, err = o.Raw(sql, ClassifyName).QueryRows(&items)
  261. return
  262. }
  263. //end
  264. //定时发布任务
  265. func ArticleTask() (err error) {
  266. o := orm.NewOrm()
  267. sql := " UPDATE advisory_article SET publish_status = 1, actual_publish_time=NOW() ,last_update_time = NOW() WHERE publish_status = 0 AND is_timing = 1 "
  268. _, err = o.Raw(sql).Exec()
  269. //fmt.Println(err)
  270. return
  271. }
  272. //获取文章数量
  273. func GetThisDayTimingPublishTimeCount(condition string, pars []interface{}) (count int, err error) {
  274. sqlCount := ` SELECT COUNT(1) AS count FROM advisory_article WHERE 1=1 `
  275. if condition != "" {
  276. sqlCount += condition
  277. }
  278. fmt.Println(sqlCount)
  279. o := orm.NewOrm()
  280. err = o.Raw(sqlCount, pars).QueryRow(&count)
  281. return
  282. }
  283. type PvAndUvResp struct {
  284. Pv int `description:"PV"`
  285. Uv int `description:"UV"`
  286. }
  287. //获取PV和Uv数量
  288. func GetPVAndUv() (items []*PvAndUvResp, err error) {
  289. o := orm.NewOrm()
  290. sql := `SELECT id,
  291. (SELECT COUNT( id ) FROM advisory_user_chart_article_record) AS pv,
  292. (SELECT COUNT(DISTINCT user_id) AS uv FROM advisory_user_chart_article_record) AS uv
  293. FROM advisory_user_chart_article_record LIMIT 0,1 `
  294. _, err = o.Raw(sql).QueryRows(&items)
  295. return
  296. }