scrm-go

article_category_service.go 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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(category models.ArticleCategory) error {
  48. err := service.PatientWriteDB().Create(&category).Error
  49. return err
  50. }
  51. func GetCategorys(page int64,limit int64,orgID int64) ( categorys []*models.ArticleCategory,total int64,err error) {
  52. db := service.PatientReadDB().Table("sgj_patient_articles_menu as a").Where("a.status=1")
  53. if(orgID > 0){
  54. db = db.Where("a.user_org_id=?" ,orgID)
  55. }
  56. offset := (page - 1) * limit
  57. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  58. 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
  59. if err != nil {
  60. return
  61. }
  62. return
  63. }
  64. func GetCategorysByID(orgID int64,id int64)(categorys []*models.ArticleCategory,err error){
  65. err = service.PatientReadDB().Where("user_org_id = ? AND id =? AND status =1", orgID, id).Find(&categorys).Error
  66. if err == gorm.ErrRecordNotFound {
  67. return nil, nil
  68. }
  69. if err != nil {
  70. return nil, err
  71. }
  72. return categorys,err
  73. }
  74. func EditCategory(cat *models.ArticleCategory,orgID int64,id int64){
  75. 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})
  76. }
  77. func DeleteCategorys(orgID int64,ids[] int64)(err error){
  78. if len(ids) ==1{
  79. 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
  80. }else {
  81. 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
  82. }
  83. return
  84. }
  85. func AddVido(articles models.Articles) error {
  86. err := service.PatientWriteDB().Create(&articles).Error
  87. return err
  88. }
  89. func AddPrviewArticle(articles models.Articles)(err error) {
  90. err = service.PatientWriteDB().Create(&articles).Error
  91. return err
  92. }
  93. func GetArtilcePreview(OrgID int64)(models.Articles,error) {
  94. article := models.Articles{}
  95. err := service.PatientReadDB().Where("user_org_id = ? AND article_status = ? AND type = ?", OrgID, 3, 1).Last(&article).Error
  96. return article, err
  97. }
  98. func AddDraft(articles models.Articles) error {
  99. err := service.PatientReadDB().Create(&articles).Error
  100. return err
  101. }
  102. func GetPublished(orgID int64,page int64,limit int64, searchKey string)(articles []*models.Articles,total int64, err error) {
  103. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  104. if (orgID > 0) {
  105. db = db.Where("a.user_org_id=? AND article_status = ?", orgID,1)
  106. }
  107. if len(searchKey) > 0 {
  108. searchKey = "%" + searchKey + "%"
  109. db = db.Where("a.title LIKE ?", searchKey)
  110. }
  111. offset := (page - 1) * limit
  112. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  113. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  114. return db.Where(" status = ?",1)
  115. }).Where("status = ?",1).
  116. 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
  117. fmt.Println("err是啥呢?",err)
  118. if err != nil {
  119. return
  120. }
  121. return
  122. }
  123. func GetDraftbox(orgID int64,page int64,limit int64, searchKey string)(articles []*models.Articles,total int64, err error) {
  124. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  125. if (orgID > 0) {
  126. db = db.Where("a.user_org_id=? AND article_status = ?", orgID,2)
  127. }
  128. if len(searchKey) > 0 {
  129. searchKey = "%" + searchKey + "%"
  130. db = db.Where("a.title LIKE ?", searchKey)
  131. }
  132. offset := (page - 1) * limit
  133. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  134. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  135. return db.Where(" status = ?",1)
  136. }).Where("article_status = ? AND status = ?",2,1).
  137. 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
  138. fmt.Println("err是啥呢?",err)
  139. if err != nil {
  140. return
  141. }
  142. return
  143. }
  144. func GetNoPass(orgID int64,page int64,limit int64, searchKey string)(articles []*models.Articles,total int64, err error) {
  145. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  146. if (orgID > 0) {
  147. db = db.Where("a.user_org_id=? AND article_status = ?", orgID,4)
  148. }
  149. if len(searchKey) > 0 {
  150. searchKey = "%" + searchKey + "%"
  151. db = db.Where("a.title LIKE ?", searchKey)
  152. }
  153. offset := (page - 1) * limit
  154. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  155. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  156. return db.Where(" status = ?",1)
  157. }).Where("article_status = ? AND status = ?",4,1).
  158. 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
  159. fmt.Println("err是啥呢?",err)
  160. if err != nil {
  161. return
  162. }
  163. return
  164. }
  165. func GetArticleInfo(orgID int64,id int64)(articles models.Articles,err error){
  166. err = service.PatientReadDB().Where("user_org_id = ? AND id = ? AND status = ?", orgID, id, 1).Find(&articles).Error
  167. return articles,err
  168. }
  169. func DeleteArticle(ids[] int64,orgID int64)(err error) {
  170. if len(ids) ==1{
  171. 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
  172. }else {
  173. 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
  174. }
  175. return
  176. }
  177. func GetMenus(OrgID int64)(categorys []*models.ArticleCategory,err error) {
  178. err = service.PatientReadDB().Where("user_org_id = ?", OrgID).Find(&categorys).Error
  179. return categorys ,err
  180. }
  181. func UpdataArticleInfo(art *models.Articles,orgID int64,id int64){
  182. 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})
  183. }
  184. func PreviewEditArticle(art models.Articles,orgID int64,id int64) {
  185. 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})
  186. }
  187. func GetPreviewInfo(orgID int64)(models.Articles,error) {
  188. article := models.Articles{}
  189. err := service.PatientReadDB().Where("user_org_id = ? AND article_status = ? AND type = ?", orgID, 3, 1).Last(&article).Error
  190. return article, err
  191. }
  192. func GetAllComment(page int64,limit int64,orgID int64)(articles []*models.Articles,total int64, err error) {
  193. db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
  194. if (orgID > 0) {
  195. db = db.Where("a.user_org_id=? AND article_status = ?", orgID,1)
  196. }
  197. offset := (page - 1) * limit
  198. err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
  199. Preload("Comments", func(db *gorm.DB) *gorm.DB {
  200. return db.Where(" status = ?",1)
  201. }).
  202. 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
  203. fmt.Println("err是啥呢?",err)
  204. if err != nil {
  205. return
  206. }
  207. return
  208. }
  209. func GetArticleCommentDetail(articleId int64,orgID int64)(*models.Articles, error){
  210. article := &models.Articles{}
  211. err :=service.PatientReadDB().Where("id = ? AND user_org_id = ? AND status = ?", articleId, orgID,1).Find(article).Error
  212. tm := time.Unix(article.Ctime, 0)
  213. article.PublicTime = tm.Format("2006-01-02 15:04:05")
  214. return article, err
  215. }