作者 linmadan

添加个人消息统计,重构任务查询与标签删除功能

... ... @@ -181,6 +181,23 @@ func (customerValueService *CustomerValueService) RemoveCustomerValue(removeCust
if customerValue == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeCustomerValueCommand.CustomerValueId)))
}
var taskRepository domain.TaskRepository
if value, err := factory.CreateTaskRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
taskRepository = value
}
if count, _, err := taskRepository.Find(map[string]interface{}{
"customerValues": []int{customerValue.CustomerValueId},
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该标签已被使用,不可删除")
}
}
if customerValue, err := customerValueRepository.Remove(customerValue); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ...
... ... @@ -181,6 +181,23 @@ func (projectBelongService *ProjectBelongService) RemoveProjectBelong(removeProj
if projectBelong == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProjectBelongCommand.ProjectBelongId)))
}
var taskRepository domain.TaskRepository
if value, err := factory.CreateTaskRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
taskRepository = value
}
if count, _, err := taskRepository.Find(map[string]interface{}{
"projectBelongs": []int{projectBelong.ProjectBelongId},
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该标签已被使用,不可删除")
}
}
if projectBelong, err := projectBelongRepository.Remove(projectBelong); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ...
package command
import (
"fmt"
"github.com/astaxie/beego/validation"
)
type PersonNotificationStatisticsCommand struct {
// 统一用户UID
Uid int64 `json:"uid" valid:"Required"`
}
func (personNotificationStatisticsCommand *PersonNotificationStatisticsCommand) ValidateCommand() error {
valid := validation.Validation{}
b, err := valid.Valid(personNotificationStatisticsCommand)
if err != nil {
return err
}
if !b {
for _, validErr := range valid.Errors {
return fmt.Errorf("%s %s", validErr.Key, validErr.Message)
}
}
return nil
}
... ...
... ... @@ -12,6 +12,56 @@ import (
type StatisticsService struct {
}
// 获取个人消息通知统计
func (statisticsService *StatisticsService) PersonNotificationStatistics(personNotificationStatisticsCommand *command.PersonNotificationStatisticsCommand) (interface{}, error) {
if err := personNotificationStatisticsCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var employeeDao *dao.EmployeeDao
if value, err := factory.CreateEmployeeDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
employeeDao = value
}
var employeeRepository domain.EmployeeRepository
if value, err := factory.CreateEmployeeRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
employeeRepository = value
}
employee, err := employeeRepository.FindOne(map[string]interface{}{
"uid": personNotificationStatisticsCommand.Uid,
})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if employee == nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "无效的企业员工")
}
if personNotificationStatistics, err := employeeDao.CalculatePersonUnReadNotification(personNotificationStatisticsCommand.Uid); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return personNotificationStatistics, nil
}
}
// 获取系统任务统计
func (statisticsService *StatisticsService) SystemTaskStatistics(systemTaskStatisticsCommand *command.SystemTaskStatisticsCommand) (interface{}, error) {
if err := systemTaskStatisticsCommand.ValidateCommand(); err != nil {
... ...
... ... @@ -15,6 +15,8 @@ type SearchTaskCommand struct {
TaskContentMatch string `json:"taskContentMatch,omitempty"`
// 任务类型
TaskType int `json:"taskType,omitempty"`
// 任务类型ID列表
TaskTypes []int `json:"taskTypes,omitempty"`
// 任务状态
TaskStatus int `json:"taskStatus,omitempty"`
// 项目归属
... ...
... ... @@ -181,6 +181,23 @@ func (taskNatureService *TaskNatureService) RemoveTaskNature(removeTaskNatureCom
if taskNature == nil {
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeTaskNatureCommand.TaskNatureId)))
}
var taskRepository domain.TaskRepository
if value, err := factory.CreateTaskRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
taskRepository = value
}
if count, _, err := taskRepository.Find(map[string]interface{}{
"taskNatures": []int{taskNature.TaskNatureId},
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if count > 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该标签已被使用,不可删除")
}
}
if taskNature, err := taskNatureRepository.Remove(taskNature); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ...
... ... @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/go-pg/pg"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg/models"
"time"
)
... ... @@ -56,6 +57,33 @@ func (dao *EmployeeDao) TransferSuMoney(uid int64, suMoney float64) error {
return err
}
func (dao *EmployeeDao) CalculatePersonUnReadNotification(uid int64) (map[string]int, error) {
var unReadSystemNotification int
var unReadInteractionNotification int
tx := dao.transactionContext.PgTx
sentNotificationModel := new(models.SentNotification)
if count, err := tx.Model(sentNotificationModel).Relation("Notification").
Where(`sent_notification.receiver @> '{"uid":?}'`, uid).
Where("notification.notification_type = ?", domain.NOTIFICATION_TYPE_SYSTEM).
Count(); err != nil {
return nil, err
} else {
unReadSystemNotification = count
}
if count, err := tx.Model(sentNotificationModel).Relation("Notification").
Where(`sent_notification.receiver @> '{"uid":?}'`, uid).
Where("notification.notification_type = ?", domain.NOTIFICATION_TYPE_INTERACTION).
Count(); err != nil {
return nil, err
} else {
unReadInteractionNotification = count
}
return map[string]int{
"unReadSystemNotification": unReadSystemNotification,
"unReadInteractionNotification": unReadInteractionNotification,
}, nil
}
func (dao *EmployeeDao) CalculatePersonSuMoney(uid int64) (map[string]interface{}, error) {
var incomeSuMoney float64
var incomeSuMoneyOfYesterday float64
... ...
... ... @@ -118,6 +118,14 @@ func (repository *TaskRepository) Find(queryOptions map[string]interface{}) (int
if taskType, ok := queryOptions["taskType"]; ok && (taskType != 0) {
query = query.Where(`task.task_type = ?`, taskType)
}
if taskTypes, ok := queryOptions["taskTypes"]; ok && len(taskTypes.([]int)) != 0 {
query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
for _, value := range taskTypes.([]int) {
q = q.WhereOr("task.task_type = ?", value)
}
return q, nil
})
}
if projectBelongs, ok := queryOptions["projectBelongs"]; ok && len(projectBelongs.([]int)) != 0 {
query = query.WhereGroup(func(q *orm.Query) (*orm.Query, error) {
for _, value := range projectBelongs.([]int) {
... ...
... ... @@ -57,3 +57,18 @@ func (controller *StatisticsController) PersonSuMoneyStatistics() {
controller.Data["json"] = response
controller.ServeJSON()
}
func (controller *StatisticsController) PersonNotificationStatistics() {
statisticsService := service.NewStatisticsService(nil)
personNotificationStatisticsCommand := &command.PersonNotificationStatisticsCommand{}
json.Unmarshal(controller.Ctx.Input.GetData("requestBody").([]byte), personNotificationStatisticsCommand)
data, err := statisticsService.PersonNotificationStatistics(personNotificationStatisticsCommand)
var response utils.JsonResponse
if err != nil {
response = utils.ResponseError(controller.Ctx, err)
} else {
response = utils.ResponseData(controller.Ctx, data)
}
controller.Data["json"] = response
controller.ServeJSON()
}
... ...
... ... @@ -9,4 +9,5 @@ func init() {
beego.Router("/statistics/system-task", &controllers.StatisticsController{}, "Post:SystemTaskStatistics")
beego.Router("/statistics/person-task", &controllers.StatisticsController{}, "Post:PersonTaskStatistics")
beego.Router("/statistics/person-su-money", &controllers.StatisticsController{}, "Post:PersonSuMoneyStatistics")
beego.Router("/statistics/person-notification", &controllers.StatisticsController{}, "Post:PersonNotificationStatistics")
}
... ...
package statistics
import (
"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
"net/http"
"time"
"github.com/gavv/httpexpect"
"github.com/go-pg/pg"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
pG "gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/infrastructure/pg"
)
var _ = Describe("获取个人消息通知统计", func() {
var notificationId int64
var sentNotificationId int64
BeforeEach(func() {
_, err := pG.DB.QueryOne(
pg.Scan(&notificationId),
"INSERT INTO notifications (id, notification_type, notification_title, notification_content, notification_time, external_resource_type, external_resource) VALUES (?, ?, ?, ?, ?, ?, ?) RETURNING id",
1, 1, "testNotificationTitle", "testNotificationContent", time.Now(), 1, 1)
Expect(err).NotTo(HaveOccurred())
_, err1 := pG.DB.QueryOne(
pg.Scan(&sentNotificationId),
"INSERT INTO sent_notifications (id, notification_id, receiver, is_read, read_time) VALUES (?, ?, ?, ?, ?) RETURNING id",
1, notificationId, &domain.EmployeeInfo{
Uid: 2499036607974745088,
}, false, time.Time{})
Expect(err1).NotTo(HaveOccurred())
_, err2 := pG.DB.QueryOne(
pg.Scan(),
"INSERT INTO employees (id, company_id, uid, employee_name, employee_account, su_money) VALUES (?, ?, ?, ?, ?, ?)",
1, 101, 2499036607974745088, "testEmployeeName", "testEmployeeAccount", 0)
Expect(err2).NotTo(HaveOccurred())
})
Describe("获取个人消息通知统计", func() {
Context("", func() {
It("", func() {
httpExpect := httpexpect.New(GinkgoT(), server.URL)
body := map[string]interface{}{
"uid": 2499036607974745088,
}
httpExpect.POST("/statistics/person-notification").
WithJSON(body).
Expect().
Status(http.StatusOK).
JSON().
Object().
ContainsKey("code").ValueEqual("code", 0).
ContainsKey("msg").ValueEqual("msg", "ok").
ContainsKey("data").Value("data").Object()
})
})
})
AfterEach(func() {
_, err := pG.DB.Exec("DELETE FROM sent_notifications WHERE true")
Expect(err).NotTo(HaveOccurred())
_, err1 := pG.DB.Exec("DELETE FROM notifications WHERE true")
Expect(err1).NotTo(HaveOccurred())
_, err2 := pG.DB.Exec("DELETE FROM employees WHERE true")
Expect(err2).NotTo(HaveOccurred())
})
})
... ...