Bladeren bron

公告管理

huangyan 1 maand geleden
bovenliggende
commit
eed7b375f2
9 gewijzigde bestanden met toevoegingen van 425 en 17 verwijderingen
  1. 0 8
      .idea/.gitignore
  2. 2 1
      .idea/Cold_Api.iml
  3. 0 8
      .idea/modules.xml
  4. 3 0
      Makefile
  5. 186 0
      controllers/bulletin.go
  6. 1 0
      go.mod
  7. 2 0
      go.sum
  8. 212 0
      models/Announcement/bulletin.go
  9. 19 0
      routers/bulletin.go

+ 0 - 8
.idea/.gitignore

@@ -1,8 +0,0 @@
-# 默认忽略的文件
-/shelf/
-/workspace.xml
-# Datasource local storage ignored files
-/dataSources/
-/dataSources.local.xml
-# 基于编辑器的 HTTP 客户端请求
-/httpRequests/

+ 2 - 1
.idea/Cold_Api.iml

@@ -1,8 +1,9 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<module version="4">
+<module type="WEB_MODULE" version="4">
   <component name="Go" enabled="true" />
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
 </module>

+ 0 - 8
.idea/modules.xml

@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="ProjectModuleManager">
-    <modules>
-      <module fileurl="file://$PROJECT_DIR$/.idea/Cold_Api.iml" filepath="$PROJECT_DIR$/.idea/Cold_Api.iml" />
-    </modules>
-  </component>
-</project>

+ 3 - 0
Makefile

@@ -4,3 +4,6 @@ build:
 build-linux:
 	@echo "+ build linux"
 	GOOS=linux GOARCH=amd64 go build -o Cold_Api6200 main.go
+run:
+	@echo "+ run"
+	go run main.go

+ 186 - 0
controllers/bulletin.go

@@ -0,0 +1,186 @@
+package controllers
+
+import (
+	"Cold_Api/conf"
+	"Cold_Api/controllers/lib"
+	"Cold_Api/models/Account"
+	"Cold_Api/models/Announcement"
+	"encoding/json"
+	beego "github.com/beego/beego/v2/server/web"
+	"github.com/google/uuid"
+	"math"
+	"time"
+)
+
+type BulletinController struct {
+	beego.Controller
+	Admin_r Account.Admin // 登陆的用户
+	T_pid   int           // 公司id
+}
+
+func (c *BulletinController) Prepare() {
+	GetCookie := c.Ctx.GetCookie("User_tokey")
+	GetString := c.GetString("User_tokey")
+	User_tokey := GetCookie
+	if len(User_tokey) == 0 {
+		User_tokey = GetString
+	}
+	if Account.Admin_r == nil {
+		return
+	}
+	c.Admin_r = *Account.Admin_r
+	T_pid := c.Admin_r.T_pid
+	EntryPid, _ := Account.Redis_Tokey_T_pid_Get(User_tokey)
+	if EntryPid > 0 {
+		T_pid = EntryPid
+	}
+	c.T_pid = T_pid
+}
+
+// AddBulletin 添加公告
+func (b *BulletinController) AddBulletin() {
+	title := b.GetString("title")
+	content := b.GetString("content")
+	if len(title) == 0 {
+		b.Data["json"] = lib.JSONS{Code: 310, Msg: "标题不能为空"}
+		b.ServeJSON()
+		return
+	}
+	if len(content) == 0 {
+		b.Data["json"] = lib.JSONS{Code: 310, Msg: "内容不能为空"}
+		b.ServeJSON()
+		return
+	}
+	list := Account.Read_User_List_All()
+	var err error
+	uid, _ := uuid.NewRandom()
+	for _, v := range list {
+		notice := Announcement.Bulletin{
+			BulletinId: uid.String(),
+			T_uuid:     v.T_uuid,
+			Title:      title,
+			Content:    content,
+			State:      1,
+			CreateTime: time.Time{},
+			UpdateTime: time.Time{},
+		}
+		_, err = Announcement.AddBulletin(notice)
+	}
+	if err != nil {
+		b.Data["json"] = lib.JSONS{Code: 301, Msg: "添加失败!"}
+		b.ServeJSON()
+		return
+	}
+	b.Data["json"] = lib.JSONS{Code: 200, Msg: "添加成功!"}
+	b.ServeJSON()
+	return
+}
+
+// GetBulletinById  根据id查询公告详情
+func (b *BulletinController) GetBulletinById() {
+	id := b.GetString("id")
+	if len(id) == 0 {
+		b.Data["json"] = lib.JSONS{Code: 310, Msg: "id不能为空"}
+		b.ServeJSON()
+		return
+	}
+	bulletin, err := Announcement.GetBulletinById(id, b.Admin_r.T_uuid)
+	if err != nil {
+		b.Data["json"] = lib.JSONS{Code: 301, Msg: "查询失败"}
+		b.ServeJSON()
+		return
+	}
+	b.Data["json"] = lib.JSONS{Code: 200, Msg: "查询成功!", Data: bulletin}
+	b.ServeJSON()
+	return
+}
+
+// DeleteBulletin 删除公告
+func (b *BulletinController) DeleteBulletin() {
+	id := b.GetString("bulletin_id")
+	if len(id) == 0 {
+		b.Data["json"] = lib.JSONS{Code: 310, Msg: "id不能为空"}
+		b.ServeJSON()
+		return
+	}
+	err := Announcement.DeleteBulletinById(id)
+	if err != nil {
+		b.Data["json"] = lib.JSONS{Code: 301, Msg: "删除失败"}
+		b.ServeJSON()
+		return
+	}
+	b.Data["json"] = lib.JSONS{Code: 200}
+	b.ServeJSON()
+	return
+}
+
+// UpdateBulletin 修改公告
+func (b *BulletinController) UpdateBulletin() {
+	var bulletin Announcement.Bulletin
+	err := json.Unmarshal(b.Ctx.Input.RequestBody, &bulletin)
+	if err != nil {
+		b.Data["json"] = lib.JSONS{Code: 302, Msg: "json序列化失败"}
+		b.ServeJSON()
+		return
+	}
+	bulletin.T_uuid = b.Admin_r.T_uuid
+	bulletin.UpdateTime = time.Now()
+	err = Announcement.UpdateBulletin(bulletin)
+	if err != nil {
+		b.Data["json"] = lib.JSONS{Code: 301, Msg: "修改失败"}
+		b.ServeJSON()
+		return
+	}
+	b.Data["json"] = lib.JSONS{Code: 200, Msg: "修改成功"}
+	b.ServeJSON()
+	return
+}
+
+// GetBulletinsList 返回用户公告列表
+func (b *BulletinController) GetBulletinsList() {
+	type R_JSONS struct {
+		//必须的大写开头
+		Data      []Announcement.Bulletin
+		Num       int64
+		Page      int
+		Page_size int
+	}
+
+	var r_jsons R_JSONS
+
+	page, _ := b.GetInt("page")
+
+	if page < 1 {
+		page = 1
+	}
+	page_z, _ := b.GetInt("page_z")
+	if page_z < 1 {
+		page_z = conf.Page_size
+	}
+	r_jsons.Data, r_jsons.Num = Announcement.Read_Bulletin_List(b.Admin_r.T_uuid, page, page_z)
+	r_jsons.Page = page
+	r_jsons.Page_size = int(math.Ceil(float64(r_jsons.Num) / float64(page_z)))
+	b.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
+	b.ServeJSON()
+	return
+}
+
+// IsReadBulletin 公告是否已读
+func (b *BulletinController) IsReadBulletin() {
+	is_show := b.GetString("is_show")
+	id := b.GetString("id")
+	if len(is_show) == 0 {
+		b.Data["json"] = lib.JSONS{Code: 310, Msg: "缺少必要参数"}
+		b.ServeJSON()
+		return
+	}
+	err := Announcement.IsReadBulletin(id, b.Admin_r.T_uuid, is_show)
+	if err != nil {
+		b.Data["json"] = lib.JSONS{Code: 301, Msg: "修改失败"}
+		b.ServeJSON()
+		return
+	}
+	b.Data["json"] = lib.JSONS{Code: 200, Msg: "修改成功"}
+	b.ServeJSON()
+	return
+}

+ 1 - 0
go.mod

@@ -8,6 +8,7 @@ require (
 	github.com/go-resty/resty/v2 v2.7.0
 	github.com/go-sql-driver/mysql v1.7.0
 	github.com/gomodule/redigo v2.0.0+incompatible
+	github.com/google/uuid v1.2.0
 	github.com/mssola/user_agent v0.6.0
 	github.com/nats-io/nats.go v1.22.1
 	github.com/qiniu/go-sdk/v7 v7.14.0

+ 2 - 0
go.sum

@@ -161,6 +161,8 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf
 github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
 github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
 github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
+github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
+github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
 github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
 github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=

+ 212 - 0
models/Announcement/bulletin.go

@@ -0,0 +1,212 @@
+package Announcement
+
+import (
+	"Cold_Api/conf"
+	"Cold_Api/controllers/lib"
+	"encoding/json"
+	"fmt"
+	"github.com/astaxie/beego/cache"
+	"github.com/beego/beego/v2/adapter/orm"
+	orm2 "github.com/beego/beego/v2/client/orm"
+	"github.com/beego/beego/v2/core/logs"
+	"sync"
+	"time"
+)
+
+type Bulletin struct {
+	Id         int       `orm:"column(ID);size(11);auto;pk" json:"id"`
+	T_uuid     string    `orm:"size(256);null"`
+	BulletinId string    `orm:"size(256);null" json:"bulletinId"`                      // 公告id
+	Title      string    `orm:"size(256);null" json:"title"`                           // 标题
+	Content    string    `orm:"size(256);null" json:"content"`                         // 内容
+	State      int       `orm:"size(2);default(0)" json:"state"`                       // 状态 0 可查看 1 不可查看
+	IsShow     int       `orm:"size(2);default(0)" json:"IsShow"`                      // 是否查看 0 未查看 1 已查看
+	CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` // auto_now 每次 model 保存时都会对时间自动更新
+	UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"`     // auto_now_add 第一次保存时才设置时间
+}
+
+func (n *Bulletin) TableName() string {
+	return "bulletin"
+}
+
+var redisCache_bulletin cache.Cache
+var Notice_map *sync.Map
+
+func init() {
+	//注册模型
+	Notice_map = new(sync.Map)
+	//注册模型
+	orm.RegisterModel(new(Bulletin))
+	config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
+		"redis_Notice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
+	fmt.Println(config)
+	var err error
+	redisCache_bulletin, err = cache.NewCache("redis", config)
+	if err != nil || redisCache_bulletin == nil {
+		errMsg := "failed to init redis"
+		logs.Error(errMsg, err)
+		panic(errMsg)
+	}
+}
+
+// ---------------- Redis -------------------
+func Redis_Notice_Set(r Bulletin) (err error) {
+	//json序列化
+	str, err := json.Marshal(r)
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return
+	}
+	uptime := r.UpdateTime.Format("2006-01-02 15:04:05")
+	key := uptime + r.Title
+	err = redisCache_bulletin.Put(key, str, 24*time.Hour)
+	if err != nil {
+		logs.Error("set key:", key, ",value:", str, err)
+	}
+	return
+}
+func Redis_Notice_Get(b Bulletin) (r Bulletin, is bool) {
+	uptime := b.UpdateTime.Format("2006-01-02 15:04:05")
+	key := uptime + b.Title
+	if redisCache_bulletin.IsExist(key) {
+		//println("找到key:",key)
+		v := redisCache_bulletin.Get(key)
+
+		err := json.Unmarshal(v.([]byte), &r)
+		if err != nil {
+			logs.Error(lib.FuncName(), err)
+			return Bulletin{}, false
+		}
+		return r, true
+	}
+	//println("没有 找到key:",key)
+	return Bulletin{}, false
+}
+func Redis_Company_DelK(b Bulletin) (err error) {
+	uptime := b.UpdateTime.Format("2006-01-02 15:04:05")
+	key := uptime + b.Title
+	err = redisCache_bulletin.Delete(key)
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+	}
+	return
+}
+
+// ---------------- 特殊方法 -------------------
+// 添加公告
+func AddBulletin(n Bulletin) (id int64, err error) {
+	o := orm.NewOrm()
+	id, err = o.Insert(&n)
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return
+	}
+	n.Id = int(id)
+	Redis_Notice_Set(n)
+	return id, err
+}
+
+// GetBulletinById 根据id查询公告
+func GetBulletinById(id, t_uuid string) (bulletin Bulletin, err error) {
+	o := orm.NewOrm()
+	query := o.QueryTable(&bulletin)
+	err = query.Filter("ID", id).Filter("State", 0).Filter("t_uuid", t_uuid).One(&bulletin)
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return
+	}
+	return bulletin, err
+}
+
+// DeleteBulletinById 删除公告
+func DeleteBulletinById(bulletin_id string) (err error) {
+	o := orm.NewOrm()
+	query := o.QueryTable(new(Bulletin))
+	_, err = query.Filter("bulletin_id", bulletin_id).Delete()
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return
+	}
+	//Redis_Company_DelK(bulletin)
+	return err
+}
+
+// UpdateBulletin 修改公告
+func UpdateBulletin(b Bulletin) (err error) {
+	o := orm.NewOrm()
+	query := o.QueryTable(new(Bulletin))
+	params := orm.Params{
+		"UpdateTime": b.UpdateTime,
+	}
+	if b.Title != "" {
+		params["Title"] = b.Title
+	}
+	if b.Content != "" {
+		params["Content"] = b.Content
+	}
+	if b.IsShow != 0 {
+		params["IsShow"] = b.IsShow
+	}
+	_, err = query.Filter("bulletin_id", b.BulletinId).Update(orm2.Params(params))
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return
+	}
+	return err
+}
+
+// Read_Bulletin_List 获取公告列表
+func Read_Bulletin_List(t_uuid string, page int, page_z int) (BulletinList []Bulletin, cnt int64) {
+
+	o := orm.NewOrm()
+	// 也可以直接使用 Model 结构体作为表名
+
+	qs := o.QueryTable(new(Bulletin))
+	var maps []Bulletin
+
+	var offset int64
+	if page <= 1 {
+		offset = 0
+	} else {
+		offset = int64((page - 1) * page_z)
+	}
+	cond := orm.NewCondition()
+	cond1 := cond.And("State", 0).And("T_uuid", t_uuid)
+
+	//if len(T_name) > 0 {
+	//	cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_user__icontains", T_name))
+	//}
+	var err error
+	if page_z == 9999 {
+		_, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps)
+	} else {
+		_, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-update_time").All(&maps)
+	}
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+	}
+	cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+	}
+
+	for _, v := range maps {
+		BulletinList = append(BulletinList, v)
+	}
+	return BulletinList, cnt
+}
+
+// IsReadBulletin 已读状态
+func IsReadBulletin(id, t_uuid, bulletin_id string) (err error) {
+	o := orm.NewOrm()
+	query := o.QueryTable(new(Bulletin))
+	params := orm.Params{
+		"is_show": bulletin_id,
+	}
+	_, err = query.Filter("id", id).Filter("t_uuid", t_uuid).Update(orm2.Params(params))
+	if err != nil {
+		logs.Error(lib.FuncName(), err)
+		return
+	}
+	return err
+}

+ 19 - 0
routers/bulletin.go

@@ -0,0 +1,19 @@
+package routers
+
+import (
+	"Cold_Api/conf"
+	"Cold_Api/controllers"
+	beego "github.com/beego/beego/v2/server/web"
+)
+
+func init() {
+	ns := beego.NewNamespace(conf.Version,
+		beego.NSRouter("/Bulletin/AddBulletin", &controllers.BulletinController{}, "*:AddBulletin"),
+		beego.NSRouter("/Bulletin/GetBulletinById", &controllers.BulletinController{}, "*:GetBulletinById"),
+		beego.NSRouter("/Bulletin/DeleteBulletin", &controllers.BulletinController{}, "*:DeleteBulletin"),
+		beego.NSRouter("/Bulletin/UpdateBulletin", &controllers.BulletinController{}, "*:UpdateBulletin"),
+		beego.NSRouter("/Bulletin/GetBulletinList", &controllers.BulletinController{}, "*:GetBulletinsList"),
+		beego.NSRouter("/Bulletin/IsReadBulletin", &controllers.BulletinController{}, "*:IsReadBulletin"),
+	)
+	beego.AddNamespace(ns)
+}