scrm-go

article_category_service.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.Joins("JOIN sgj_patient_articles_menu as s ON s.id = a.class_id AND s.status=1 AND user_org_id=?",orgID)
  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. }).
  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. }