Sfoglia il codice sorgente

文章分类模块

xiaoming_global 5 anni fa
parent
commit
6f53b3fadd

+ 175 - 2
controllers/article/article_controller.go Vedi File

@@ -7,6 +7,10 @@ import (
7 7
 	"SCRM/service/article_service"
8 8
 	"SCRM/enums"
9 9
 	"SCRM/models"
10
+	"time"
11
+	"SCRM/utils"
12
+	"encoding/json"
13
+	"strconv"
10 14
 )
11 15
 
12 16
 func ArticleRouters() {
@@ -14,6 +18,9 @@ func ArticleRouters() {
14 18
 	beego.Router("/api/acticle/createacticle",&ArticleManage{},"Get:CreateArticle")
15 19
 	beego.Router("/api/acticle/getArticleType",&ArticleManage{},"Get:GetArticleType")
16 20
 	beego.Router("/api/acticle/getAllArticles",&ArticleManage{},"Get:GetAllArticles")
21
+	beego.Router("/api/article/addCategory",&ArticleManage{},"Post:AddCategory")
22
+	beego.Router("/api/acticle/getCategorys",&ArticleManage{},"Get:GetCategorys")
23
+	beego.Router("/api/acticle/edit",&ArticleManage{},"Put:EditCategorys")
17 24
 }
18 25
 
19 26
 type ArticleManage struct {
@@ -28,6 +35,7 @@ func (this *ArticleManage) CreateArticle(){
28 35
 	actcontent := this.GetString("act_content")
29 36
 	orglogo := this.GetString("org_logo")
30 37
 	acttype, _ := this.GetInt64("act_type")
38
+	timenow := time.Now().Unix()
31 39
 	fmt.Println("姓名:",actname,"文章内容",actcontent,"图片",orglogo,"文章类型",acttype,userOrgID)
32 40
 	articles := models.Articles{
33 41
 				Title: actname,
@@ -35,6 +43,8 @@ func (this *ArticleManage) CreateArticle(){
35 43
 				Imgs: orglogo,
36 44
 				ClassId: acttype,
37 45
 				UserOrgId:userOrgID,
46
+				Ctime:timenow,
47
+				Status:1,
38 48
 		}
39 49
 		err := article_service.AddAritcle(articles)
40 50
 		if err !=nil{
@@ -59,7 +69,7 @@ func (this *ArticleManage) GetArticleType(){
59 69
 }
60 70
 
61 71
 func (this *ArticleManage) GetAllArticles()  {
62
-	page, _ := this.GetInt("page", 1)
72
+	page, _ := this.GetInt64("page", 1)
63 73
 	limit, _ := this.GetInt64("limit", 10)
64 74
 	searchKey := this.GetString("search", "")
65 75
 	classId,_ := this.GetInt64("classid",0)
@@ -74,16 +84,179 @@ func (this *ArticleManage) GetAllArticles()  {
74 84
 	adminUserInfo := this.GetAdminUserInfo()
75 85
 	userOrgID := adminUserInfo.CurrentOrgId
76 86
 
77
-	articles, total, err := article_service.FindAllArticle(userOrgID, page,10, searchKey, classId)
87
+	articles, total, err := article_service.FindAllArticle(userOrgID, page,limit, searchKey, classId)
78 88
 	fmt.Println("文章内容是是么",articles)
89
+	fmt.Println("total",total)
90
+	fmt.Println("err",err)
79 91
 	if err !=nil{
80 92
 		this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章列表失败")
81 93
 		return
82 94
 	}
95
+	category, err := article_service.FindCategoryList(userOrgID)
96
+	fmt.Println("category是傻",category,err)
97
+	if err !=nil{
98
+		this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章分类列表失败")
99
+		return
100
+	}
83 101
 	this.ServeSuccessJSON(map[string]interface{}{
84 102
 		"articles":articles,
85 103
 		"total":total,
104
+		"category":category,
105
+	})
106
+	return
107
+}
108
+
109
+func (this *ArticleManage) AddCategory(){
110
+
111
+	dataBody := make(map[string]interface{}, 0)
112
+	err := json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
113
+	fmt.Println("err是什么呢",err)
114
+	if err != nil {
115
+		utils.ErrorLog(err.Error())
116
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
117
+		return
118
+	}
119
+
120
+	name:= dataBody["name"].(string)
121
+	fmt.Println("name是谁?",name)
122
+	if len(name) == 0 {
123
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "姓名不能为空")
124
+		return
125
+	}
126
+
127
+	content := dataBody["content"].(string)
128
+	fmt.Println("content是谁?",content)
129
+	if len(content) == 0 {
130
+    	this.ServeFailJsonSend(enums.ErrorCodeParamWrong,"内容不能为空")
131
+    	return
132
+	}
133
+
134
+	sort := dataBody["sort"].(string)
135
+	sors, _ := strconv.ParseInt(sort, 10, 64)
136
+	if sors  <=0 {
137
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "排序不能为空")
138
+		return
139
+	}
140
+	fmt.Println("soert是谁?",sort)
141
+	fmt.Println("姓名:",name,"内容",content,"排序",sort)
142
+	timenow := time.Now().Unix()
143
+	adminUserInfo := this.GetAdminUserInfo()
144
+	userOrgID := adminUserInfo.CurrentOrgId
145
+	category := models.ArticleCategory{
146
+		Name:    name,
147
+		Summary: content,
148
+		Order:   sors,
149
+		UserOrgId:userOrgID,
150
+		Status: 1,
151
+		Ctime: timenow,
152
+	}
153
+   fmt.Println(category)
154
+	err = article_service.AddCategory(category)
155
+	fmt.Println(err)
156
+	if err !=nil{
157
+		this.ServeFailJsonSend(enums.ErrorCodeDataException, "添加分类失败")
158
+		return
159
+	}
160
+
161
+	this.ServeSuccessJSON(map[string]interface{}{
162
+		"category":category,
163
+	})
164
+}
165
+
166
+func  (this *ArticleManage) GetCategorys()  {
167
+	adminUserInfo := this.GetAdminUserInfo()
168
+	userOrgID := adminUserInfo.CurrentOrgId
169
+	page, _ := this.GetInt64("page", 1)
170
+	limit, _ := this.GetInt64("limit", 10)
171
+	if page <= 0 {
172
+		page = 1
173
+	}
174
+	if limit <= 0 {
175
+		limit = 10
176
+	}
177
+	fmt.Println("机构ID",userOrgID)
178
+	categorys, total, err := article_service.GetCategorys(page, limit, userOrgID)
179
+	fmt.Println("hhe",categorys,total,err)
180
+	if err !=nil{
181
+		this.ServeFailJsonSend(enums.ErrorCodeDataException, "获取文章分类列表失败")
182
+		return
183
+	}
184
+	this.ServeSuccessJSON(map[string]interface{}{
185
+		"category":categorys,
186
+		"total":total,
86 187
 	})
87 188
 	return
88 189
 }
89 190
 
191
+func (this *ArticleManage) EditCategorys()  {
192
+	id, _ := this.GetInt64("MenuId", 0)
193
+	fmt.Println("ID是多少",id)
194
+	if id <= 0 {
195
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误:id")
196
+		return
197
+	}
198
+	adminUserInfo := this.GetAdminUserInfo()
199
+	userOrgID := adminUserInfo.CurrentOrgId
200
+	categorys, err := article_service.GetCategorysByID(userOrgID, id)
201
+	fmt.Println("err是设么",err)
202
+	if err != nil {
203
+		this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:("+err.Error()+")")
204
+		return
205
+	}
206
+	if categorys == nil {
207
+		this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:(会员记录不存在)")
208
+		return
209
+	}
210
+
211
+	timenow := time.Now().Unix()
212
+
213
+	dataBody := make(map[string]interface{}, 0)
214
+	err = json.Unmarshal(this.Ctx.Input.RequestBody, &dataBody)
215
+	if err != nil {
216
+		utils.ErrorLog(err.Error())
217
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "参数错误")
218
+		return
219
+	}
220
+
221
+	name:= dataBody["Name"].(string)
222
+	fmt.Println("name是谁?",name)
223
+	if len(name) == 0 {
224
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "姓名不能为空")
225
+		return
226
+	}
227
+
228
+	content := dataBody["Summary"].(string)
229
+	fmt.Println("content是谁?",content)
230
+	if len(content) == 0 {
231
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong,"内容不能为空")
232
+		return
233
+	}
234
+
235
+	sort := dataBody["Order"].(string)
236
+	sors, _ := strconv.ParseInt(sort, 10, 64)
237
+	if sors  <=0 {
238
+		this.ServeFailJsonSend(enums.ErrorCodeParamWrong, "排序不能为空")
239
+		return
240
+	}
241
+	fmt.Println("soert是谁?",sort)
242
+	fmt.Println("姓名:",name,"内容",content,"排序",sort)
243
+
244
+	category := models.ArticleCategory{
245
+		Name:    name,
246
+		Summary: content,
247
+		Order:   sors,
248
+		UserOrgId:userOrgID,
249
+		Status: 1,
250
+		Mtime: timenow,
251
+	}
252
+	err = article_service.EditCategory(category)
253
+	fmt.Println("err错误",err)
254
+	if err != nil {
255
+		this.ServeFailJsonSend(enums.ErrorCodeDBUpdate, "编辑类别失败:("+err.Error()+")")
256
+		return
257
+	}
258
+	fmt.Println(category)
259
+	this.ServeSuccessJSON(map[string]interface{}{
260
+		"category":categorys,
261
+	})
262
+}

+ 56 - 6
service/article_service/article_category_service.go Vedi File

@@ -3,6 +3,9 @@ package article_service
3 3
 import (
4 4
 	"SCRM/models"
5 5
 	"SCRM/service"
6
+	"fmt"
7
+	"github.com/jinzhu/gorm"
8
+
6 9
 )
7 10
 
8 11
 func FindArticleCategoryType (userOrgId int64)(*[]models.ArticleCategory,error){
@@ -11,20 +14,25 @@ func FindArticleCategoryType (userOrgId int64)(*[]models.ArticleCategory,error){
11 14
 	return  cartegory,err
12 15
 }
13 16
 
17
+func FindCategoryList(orgID int64)(cate []*models.ArticleCategory,err error)  {
18
+	err = service.PatientReadDB().Model(&models.ArticleCategory{}).Where("user_org_id =? AND status =?", orgID, 1).Order("id asc").Find(&cate).Error
19
+	return
20
+}
21
+
14 22
 func AddAritcle(articles models.Articles) error {
15 23
 	err := service.PatientWriteDB().Create(&articles).Error
16 24
 	return  err
17 25
 }
18 26
 
19
-func FindAllArticle(orgID int64, page int, count int, searchKey string, classId int64) (articles *[]models.Articles,total int64, err error) {
20
-
27
+func FindAllArticle(orgID int64, page int64,limit int64, searchKey string, classId int64) (articles []*models.Articles,total int64, err error) {
28
+    fmt.Println("机构ID",orgID,"搜搜索",searchKey)
21 29
 	db := service.PatientReadDB().Table("sgj_patient_articles as a ").Where("a.status=1")
22 30
 	if(orgID>0){
23
-		db.Where("a.user_org_id = ?" ,orgID)
31
+	  db = db.Where("a.user_org_id=?" ,orgID)
24 32
 	}
25 33
 
26 34
 	if(classId>0){
27
-	  db = db.Joins("JOIN sgj_patient_articles_menu as s ON s.id = a.class_id AND s.status=1 AND user_org_id = ?")
35
+	  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)
28 36
 	}
29 37
 
30 38
 	if len(searchKey) > 0 {
@@ -32,8 +40,50 @@ func FindAllArticle(orgID int64, page int, count int, searchKey string, classId
32 40
 		db = db.Where("a.title LIKE ?", searchKey)
33 41
 	}
34 42
 
35
-	//err = db.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" +
36
-	//	"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").Count(&total).Find(&articles).Error
43
+	    offset := (page - 1) * limit
44
+		err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
45
+		Preload("Comments", func(db *gorm.DB) *gorm.DB {
46
+			return  db.Where(" status = ?",1)
47
+		}).
48
+		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
49
+		fmt.Println("err是啥呢?",err)
50
+		 	if err != nil {
51
+			return
52
+		}
53
+		return
54
+}
55
+
56
+func AddCategory(category models.ArticleCategory) error {
57
+	err := service.PatientWriteDB().Create(&category).Error
58
+	return err
59
+}
37 60
 
61
+func GetCategorys(page int64,limit int64,orgID int64) ( categorys []*models.ArticleCategory,total int64,err error) {
62
+	db := service.PatientReadDB().Table("sgj_patient_articles_menu as a").Where("a.status=1")
63
+	if(orgID > 0){
64
+		db = db.Where("a.user_org_id=?" ,orgID)
65
+	}
66
+	offset := (page - 1) * limit
67
+	err = db.Count(&total).Order("a.ctime desc").Offset(offset).Limit(limit).
68
+		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
69
+	if err != nil {
38 70
 		return
71
+	}
72
+	return
73
+}
74
+
75
+func GetCategorysByID(orgID int64,id int64)(categorys []*models.ArticleCategory,err error){
76
+	err = service.PatientReadDB().Where("user_org_id = ? AND id =? AND status =1", orgID, id).Find(&categorys).Error
77
+	if err == gorm.ErrRecordNotFound {
78
+		return nil, nil
79
+	}
80
+	if err != nil {
81
+		return nil, err
82
+	}
83
+	return categorys,err
84
+}
85
+
86
+func EditCategory(category models.ArticleCategory)(err error)  {
87
+	err = service.PatientWriteDB().Save(&category).Error
88
+	return err
39 89
 }

+ 12 - 0
service/article_service/models.go Vedi File

@@ -0,0 +1,12 @@
1
+package article_service
2
+
3
+import (
4
+	"SCRM/models"
5
+)
6
+
7
+type Articles struct{
8
+
9
+	models.Articles
10
+	ArticleCategory []*models.ArticleCategory  `gorm:"foreignkey:class_id" json:"id"`
11
+ }
12
+