refactor: service module and dao module
This commit is contained in:
+19
-31
@@ -1,29 +1,24 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crm/global"
|
||||
"crm/dao"
|
||||
"crm/models"
|
||||
"crm/response"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
Read = 1 // 已读
|
||||
UnRead = 2 // 未读
|
||||
)
|
||||
|
||||
type NoticeService struct {
|
||||
noticeDao *dao.NoticeDao
|
||||
}
|
||||
|
||||
func NewNoticeService() *NoticeService {
|
||||
return &NoticeService{
|
||||
noticeDao: dao.NewNoticeDao(),
|
||||
}
|
||||
}
|
||||
|
||||
// 新建通知
|
||||
func (n *NoticeService) Create(param *models.NoticeParam) int {
|
||||
notice := models.Notice{
|
||||
Content: param.Content,
|
||||
Status: UnRead,
|
||||
Creator: param.Creator,
|
||||
Created: time.Now().Unix(),
|
||||
}
|
||||
if err := global.Db.Create(¬ice).Error; err != nil {
|
||||
func (n *NoticeService) Create(param *models.NoticeCreateParam) int {
|
||||
if err := n.noticeDao.Create(param); err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
@@ -31,31 +26,24 @@ func (n *NoticeService) Create(param *models.NoticeParam) int {
|
||||
|
||||
// 更新通知
|
||||
func (n *NoticeService) Update(param *models.NoticeUpdateParam) int {
|
||||
notice := models.Notice{
|
||||
Id: param.Id,
|
||||
Status: Read,
|
||||
Updated: time.Now().Unix(),
|
||||
}
|
||||
if err := global.Db.Model(¬ice).Updates(¬ice).Error; err != nil {
|
||||
if err := n.noticeDao.Update(param); err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 获取未读通知数量
|
||||
func (n *NoticeService) UnReadCount(uid int64) (models.UnReadNotice, int) {
|
||||
var unRead models.UnReadNotice
|
||||
raw := "select count(*) from notice where status = 2 and creator = ?"
|
||||
if err := global.Db.Raw(raw, uid).Scan(&unRead.Count).Error; err != nil {
|
||||
return unRead, response.ErrCodeFailed
|
||||
func (n *NoticeService) GetUnReadCount(uid int64) (models.UnReadNotice, int) {
|
||||
unReadCount, err := n.noticeDao.GetUnReadCountByUid(uid)
|
||||
if err != nil {
|
||||
return unReadCount, response.ErrCodeFailed
|
||||
}
|
||||
return unRead, response.ErrCodeSuccess
|
||||
return unReadCount, response.ErrCodeSuccess
|
||||
}
|
||||
|
||||
// 删除通知
|
||||
func (n *NoticeService) Delete(param *models.NoticeDeleteParam) int {
|
||||
err := global.Db.Delete(&models.Notice{}, param.Ids).Error
|
||||
if err != nil {
|
||||
if err := n.noticeDao.Delete(param); err != nil {
|
||||
return response.ErrCodeFailed
|
||||
}
|
||||
return response.ErrCodeSuccess
|
||||
@@ -63,8 +51,8 @@ func (n *NoticeService) Delete(param *models.NoticeDeleteParam) int {
|
||||
|
||||
// 获取通知列表
|
||||
func (n *NoticeService) GetList(uid int64) ([]*models.NoticeList, int) {
|
||||
noticeList := make([]*models.NoticeList, 0)
|
||||
if err := global.Db.Table(NOTICE).Where("creator = ?", uid).Order("status desc").Order("created desc").Find(¬iceList).Error; err != nil {
|
||||
noticeList, err := n.noticeDao.GetList(uid)
|
||||
if err != nil {
|
||||
return nil, response.ErrCodeFailed
|
||||
}
|
||||
return noticeList, response.ErrCodeSuccess
|
||||
|
||||
Reference in New Issue
Block a user