article_category_service.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. package article_service
  2. import (
  3. "SCRM/models"
  4. "SCRM/service"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "time"
  8. )
  9. func FindArticleCategoryType(userOrgId int64) (*[]models.ArticleCategory, error) {
  10. cartegory := &[]models.ArticleCategory{}
  11. err := service.PatientReadDB().Where("status = ? AND user_org_id = ? ", 1, userOrgId).Find(cartegory).Error
  12. return cartegory, err
  13. }
  14. func FindCategoryList(orgID int64) (cate []*models.ArticleCategory, err error) {
  15. err = service.PatientReadDB().Model(&models.ArticleCategory{}).Where("user_org_id =? AND status =?", orgID, 1).Order("id asc").Find(&cate).Error
  16. return
  17. }
  18. func AddAritcle(articles models.Articles) error {
  19. err := service.PatientWriteDB().Create(&articles).Error
  20. return err
  21. }
  22. func FindAllArticle(orgID int64, page int64, limit int64, searchKey string, classId int64) (articles []*models.Articles, total int64, err error) {
  23. fmt.Println("机构ID", orgID, "搜搜索", searchKey)
  24. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  25. if orgID > 0 {
  26. db = db.Where("a.user_org_id=?", orgID)
  27. }
  28. if classId > 0 {
  29. db = db.Where("class_id = ?", classId)
  30. }
  31. if len(searchKey) > 0 {
  32. searchKey = "%" + searchKey + "%"
  33. db = db.Where("a.title LIKE ?", searchKey)
  34. }
  35. offset := (page - 1) * limit
  36. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  37. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  38. return db.Where(" status = ?", 1)
  39. }).Where("article_status <> ?", 3).
  40. Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error
  41. fmt.Println("err是啥呢?", err)
  42. if err != nil {
  43. return
  44. }
  45. return
  46. }
  47. func AddCategory(name string) (*models.ArticleCategory, error) {
  48. var art models.ArticleCategory
  49. var err error
  50. err = service.PatientReadDB().Model(&models.ArticleCategory{}).Where("name = ? AND status = ?", name, 1).Find(&art).Error
  51. fmt.Println("错误", err)
  52. if err == gorm.ErrRecordNotFound {
  53. return nil, err
  54. }
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &art, err
  59. }
  60. func SaveCategory(category models.ArticleCategory) error {
  61. err := service.PatientWriteDB().Create(&category).Error
  62. return err
  63. }
  64. func GetCategorys(page int64, limit int64, orgID int64) (categorys []*models.ArticleCategory, total int64, err error) {
  65. db := service.PatientReadDB().Table("sgj_patient_articles_menu as a").Where("a.status=1")
  66. if orgID > 0 {
  67. db = db.Where("a.user_org_id=?", orgID)
  68. }
  69. offset := (page - 1) * limit
  70. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  71. Select("a.id,a.name,a.order,a.status,a.summary,a.type,a.user_org_id,a.mtime,a.ctime,a.num").Find(&categorys).Error
  72. if err != nil {
  73. return
  74. }
  75. //rows, err := db.Raw("SELECT t.name,(SELECT count(id) from sgj_patient_articles as a WHERE a.class_id = t.id) as count from sgj_patient_articles_menu as t").Rows()
  76. //fmt.Println("错误",err)
  77. //fmt.Println("rows是谁很忙",rows)
  78. return
  79. }
  80. func GetCategorysByID(orgID int64, id int64) (categorys []*models.ArticleCategory, err error) {
  81. err = service.PatientReadDB().Where("user_org_id = ? AND id =? AND status =1", orgID, id).Find(&categorys).Error
  82. if err == gorm.ErrRecordNotFound {
  83. return nil, nil
  84. }
  85. if err != nil {
  86. return nil, err
  87. }
  88. return categorys, err
  89. }
  90. func EditCategory(cat *models.ArticleCategory, orgID int64, id int64) {
  91. service.PatientWriteDB().Model(cat).Where("user_org_id =? AND id =? AND status = ?", orgID, id, 1).Update(map[string]interface{}{"Name": cat.Name, "Summary": cat.Summary, "Order": cat.Order, "mtime": cat.Mtime})
  92. }
  93. func DeleteCategorys(orgID int64, ids []int64) (err error) {
  94. if len(ids) == 1 {
  95. err = service.PatientWriteDB().Model(&models.ArticleCategory{}).Where("id=? and user_org_id = ?", ids[0], orgID).Update(map[string]interface{}{"Status": 0, "mtime": time.Now().Unix()}).Error
  96. } else {
  97. err = service.PatientWriteDB().Model(&models.ArticleCategory{}).Where("id IN (?) and user_org_id = ?", ids, orgID).Update(map[string]interface{}{"Status": 0, "mtime": time.Now().Unix()}).Error
  98. }
  99. return
  100. }
  101. func AddVido(articles models.Articles) error {
  102. err := service.PatientWriteDB().Create(&articles).Error
  103. return err
  104. }
  105. func AddPrviewArticle(articles models.Articles) (err error) {
  106. err = service.PatientWriteDB().Create(&articles).Error
  107. return err
  108. }
  109. func GetArtilcePreview(OrgID int64) (models.Articles, error) {
  110. article := models.Articles{}
  111. err := service.PatientReadDB().Where("user_org_id = ? AND article_status = ? AND type = ?", OrgID, 3, 1).Last(&article).Error
  112. return article, err
  113. }
  114. func AddDraft(articles models.Articles) error {
  115. err := service.PatientReadDB().Create(&articles).Error
  116. return err
  117. }
  118. func GetPublished(orgID int64, page int64, limit int64, searchKey string) (articles []*models.Articles, total int64, err error) {
  119. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  120. if orgID > 0 {
  121. db = db.Where("a.user_org_id=? AND article_status = ?", orgID, 1)
  122. }
  123. if len(searchKey) > 0 {
  124. searchKey = "%" + searchKey + "%"
  125. db = db.Where("a.title LIKE ?", searchKey)
  126. }
  127. offset := (page - 1) * limit
  128. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  129. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  130. return db.Where(" status = ?", 1)
  131. }).Where("status = ?", 1).
  132. Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error
  133. fmt.Println("err是啥呢?", err)
  134. if err != nil {
  135. return
  136. }
  137. return
  138. }
  139. //func GetAllartic(page int64,limit int64,orgID int64)(articles []*models.Articles,total int64, err error) {
  140. // db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  141. // if (orgID > 0) {
  142. // db = db.Where("a.user_org_id=? AND article_status = ?", orgID,1)
  143. // }
  144. // offset := (page - 1) * limit
  145. // err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  146. // Preload("Comments", func(db *gorm.DB) *gorm.DB {
  147. // return db.Where(" status = ?",1)
  148. // }).Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error
  149. // fmt.Println("err是啥呢?",err)
  150. // if err != nil {
  151. // return
  152. // }
  153. // return
  154. //}
  155. func GetDraftbox(orgID int64, page int64, limit int64, searchKey string) (articles []*models.Articles, total int64, err error) {
  156. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  157. if orgID > 0 {
  158. db = db.Where("a.user_org_id=? AND article_status = ?", orgID, 2)
  159. }
  160. if len(searchKey) > 0 {
  161. searchKey = "%" + searchKey + "%"
  162. db = db.Where("a.title LIKE ?", searchKey)
  163. }
  164. offset := (page - 1) * limit
  165. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  166. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  167. return db.Where(" status = ?", 1)
  168. }).Where("article_status = ? AND status = ?", 2, 1).
  169. Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error
  170. fmt.Println("err是啥呢?", err)
  171. if err != nil {
  172. return
  173. }
  174. return
  175. }
  176. func GetNoPass(orgID int64, page int64, limit int64, searchKey string) (articles []*models.Articles, total int64, err error) {
  177. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  178. if orgID > 0 {
  179. db = db.Where("a.user_org_id=? AND article_status = ?", orgID, 4)
  180. }
  181. if len(searchKey) > 0 {
  182. searchKey = "%" + searchKey + "%"
  183. db = db.Where("a.title LIKE ?", searchKey)
  184. }
  185. offset := (page - 1) * limit
  186. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  187. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  188. return db.Where(" status = ?", 1)
  189. }).Where("article_status = ? AND status = ?", 4, 1).
  190. Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error
  191. fmt.Println("err是啥呢?", err)
  192. if err != nil {
  193. return
  194. }
  195. return
  196. }
  197. func GetArticleInfo(orgID int64, id int64) (articles models.Articles, err error) {
  198. err = service.PatientReadDB().Where("user_org_id = ? AND id = ? AND status = ?", orgID, id, 1).Find(&articles).Error
  199. return articles, err
  200. }
  201. func DeleteArticle(ids []int64, orgID int64) (err error) {
  202. if len(ids) == 1 {
  203. err = service.PatientWriteDB().Model(&models.Articles{}).Where("id=? and user_org_id = ?", ids[0], orgID).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  204. } else {
  205. err = service.PatientWriteDB().Model(&models.Articles{}).Where("id IN (?) and user_org_id = ?", ids, orgID).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  206. }
  207. return
  208. }
  209. func DeleteArticles(id int64, orgID int64) (err error) {
  210. err = service.PatientWriteDB().Model(&models.Articles{}).Where("id = ? and user_org_id = ?", id, orgID).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  211. return
  212. }
  213. func GetMenus(OrgID int64) (categorys []*models.ArticleCategory, err error) {
  214. err = service.PatientReadDB().Where("user_org_id = ? AND status = ?", OrgID, 1).Find(&categorys).Error
  215. return categorys, err
  216. }
  217. func UpdataArticleInfo(art *models.Articles, orgID int64, id int64) {
  218. service.PatientWriteDB().Model(art).Where("user_org_id = ? AND id = ? AND status = ?", orgID, id, 1).Update(map[string]interface{}{"title": art.Title, "content": art.Content, "imgs": art.Imgs, "video_url": art.VideoUrl, "class_id": art.ClassId, "mtime": art.Mtime, "article_status": art.ArticleStatus})
  219. }
  220. func SaveVidoDraft(art *models.Articles, orgID int64, id int64) {
  221. service.PatientWriteDB().Model(art).Where("user_org_id =? AND id = ? AND status = ?", orgID, id, 1).Update(map[string]interface{}{"title": art.Title, "imgs": art.Imgs, "class_id": art.ClassId, "mtime": art.Mtime, "article_status": art.ArticleStatus})
  222. }
  223. func PreviewEditArticle(art models.Articles, orgID int64, id int64) {
  224. service.PatientWriteDB().Model(art).Where("user_org_id = ? AND id = ? AND status = ?", orgID, id, 1).Update(map[string]interface{}{"title": art.Title, "content": art.Content, "imgs": art.Imgs, "class_id": art.ClassId, "mtime": art.Mtime, "article_status": art.ArticleStatus})
  225. }
  226. //func GetPreviewInfo(orgID int64)(models.Articles,error) {
  227. // article := models.Articles{}
  228. // err := service.PatientReadDB().Where("user_org_id = ? AND article_status = ? AND type = ?", orgID, 3, 1).Last(&article).Error
  229. // return article, err
  230. //}
  231. func GetPreviewInfoById(id int64, orgid int64) (models.Articles, error) {
  232. articles := models.Articles{}
  233. err := service.PatientReadDB().Model(articles).Where("id = ? AND user_org_id = ?", id, orgid).Find(&articles).Error
  234. return articles, err
  235. }
  236. func GetAllComment(page int64, limit int64, orgID int64) (articles []*models.Articles, total int64, err error) {
  237. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  238. if orgID > 0 {
  239. db = db.Where("a.user_org_id=? AND article_status = ?", orgID, 1)
  240. }
  241. offset := (page - 1) * limit
  242. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  243. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  244. return db.Where(" status = ?", 1)
  245. }).
  246. Select("a.id, a.title, a.summary, a.content, a.type, a.num, a.mtime, a.real_read_num, a.ctime, a.class_id, a.author, a.status, a.reason,a.star_num, a.comment_num,a.user_org_id, a.article_status, a.imgs, a.video_url, a.source, a.category_id, a.ttid, a.ttype, a.toid").Find(&articles).Error
  247. fmt.Println("err是啥呢?", err)
  248. if err != nil {
  249. return
  250. }
  251. return
  252. }
  253. func GetArticleCommentDetail(articleId int64, orgID int64) (*models.Articles, error) {
  254. article := &models.Articles{}
  255. err := service.PatientReadDB().Where("id = ? AND user_org_id = ? AND status = ?", articleId, orgID, 1).Find(article).Error
  256. tm := time.Unix(article.Ctime, 0)
  257. article.PublicTime = tm.Format("2006-01-02 15:04:05")
  258. return article, err
  259. }
  260. func FindAllComments(page int64, limit int64, articleId int64) (comment []*models.Comment, total int64, err error) {
  261. db := service.PatientReadDB().Table("sgj_patient_articles_comment as a").Where("a.status = 1")
  262. offset := (page - 1) * limit
  263. err = db.Where("article_id = ? AND parent_id = ?", articleId, 0).Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  264. Select("a.id,a.article_id,a.comment_user_id,a.parent_id,a.content,a.ctime,a.status,a.star_num,a.comment_num,a.comment_id,a.comment_user_name,a.comment_user_avater,a.source,a.ttid,a.tuser_id,a.taid,a.tpid,a.tcid").Find(&comment).Error
  265. fmt.Println("err是啥呢?", err)
  266. if err != nil {
  267. return
  268. }
  269. return
  270. }
  271. func AddComment(comment *models.Comment) error {
  272. err := service.PatientReadDB().Create(comment).Error
  273. return err
  274. }
  275. func GetReplyAllComents(orgid int64, page int64, limit int64) (comment []*models.Comment, total int64, err error) {
  276. db := service.PatientReadDB().Table("sgj_patient_articles_comment as a").Where("a.status = ? AND a.user_org_id = ?", 1, orgid)
  277. offset := (page - 1) * limit
  278. err = db.Where("parent_id = ?", 0).Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  279. Select("a.id,a.article_id,a.comment_user_id,a.parent_id,a.content,a.ctime,a.status,a.star_num,a.comment_num,a.comment_id,a.comment_user_name,a.comment_user_avater,a.source,a.ttid,a.tuser_id,a.taid,a.tpid,a.tcid").Find(&comment).Error
  280. fmt.Println("err是啥呢?", err)
  281. if err != nil {
  282. return
  283. }
  284. return
  285. }
  286. func GetAllReplyFormId(articleID int64, commentId int64) ([]models.Comment, error) {
  287. var comments []models.Comment
  288. errs := service.PatientReadDB().Model(&models.Comment{}).
  289. Where("article_id = ? AND parent_id = ? AND status = ?", articleID, commentId, 1).
  290. Find(&comments).Error
  291. return comments, errs
  292. }
  293. func ClearReplyInfo(id int64) error {
  294. err := service.PatientWriteDB().Model(&models.Comment{}).Where("id = ?", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  295. return err
  296. }
  297. func DeleteReply(id int64) error {
  298. err := service.PatientWriteDB().Model(&models.Comment{}).Where("id = ?", id).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  299. return err
  300. }
  301. func GetQueryReplyInfo(parentid int64, articleID int64) ([]models.Comment, error) {
  302. var comments []models.Comment
  303. errs := service.PatientWriteDB().Model(&models.Comment{}).
  304. Where("article_id = ? AND parent_id = ? AND status = ?", articleID, parentid, 1).Find(&comments).Error
  305. return comments, errs
  306. }
  307. func DeleteAllReply(ids []int64) (err error) {
  308. if len(ids) == 1 {
  309. err = service.PatientWriteDB().Model(&models.Comment{}).Where("id=?", ids[0]).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  310. } else {
  311. err = service.PatientWriteDB().Model(&models.Comment{}).Where("id IN (?)", ids).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  312. }
  313. return
  314. }
  315. func DeleteAllArticles(ids []int64) (err error) {
  316. if len(ids) == 1 {
  317. err = service.PatientWriteDB().Model(&models.Articles{}).Where("id=?", ids[0]).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  318. } else {
  319. err = service.PatientWriteDB().Model(&models.Articles{}).Where("id IN (?)", ids).Update(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  320. }
  321. return
  322. }
  323. func GetArticleDetailById(id int64, orgid int64) (models.Articles, error) {
  324. articles := models.Articles{}
  325. err := service.PatientReadDB().Model(articles).Where("id = ? AND user_org_id = ? AND status = ?", id, orgid, 1).Find(&articles).Error
  326. return articles, err
  327. }