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 }