作者 tangxvhui

修复一个查询错误

... ... @@ -5,6 +5,7 @@ import (
"strconv"
"time"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/dao"
... ... @@ -18,26 +19,24 @@ func TaskSendSummaryEvaluationV2() error {
str := fmt.Sprintf("下发周期评估耗时%.2f s", time.Since(nowTime).Seconds())
log.Logger.Info(str)
}()
var newPublisher summaryEvaluationPublisher
for {
projectList, err := getPrepareSummaryEvaluation()
cycleList, err := getPrepareEvaluationCycle()
if err != nil {
return err
}
if len(projectList) == 0 {
if len(cycleList) == 0 {
break
}
newPublisher = summaryEvaluationPublisher{}
for _, val := range projectList {
err = newPublisher.sendSummaryEvaluationV2(val)
err = sendSummaryEvaluationByCycle(cycleList[0])
if err != nil {
return err
}
}
return nil
}
func getPrepareEvaluationCycle() {}
func getPrepareSummaryEvaluation() ([]*domain.EvaluationProject, error) {
// 获取周期
func getPrepareEvaluationCycle() ([]*domain.EvaluationCycle, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, err
... ... @@ -48,56 +47,131 @@ func getPrepareSummaryEvaluation() ([]*domain.EvaluationProject, error) {
defer func() {
_ = transactionContext.RollbackTransaction()
}()
cycleRepo := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
cycleList, err := cycleRepo.FindCycleEnd(1)
if err != nil {
return nil, fmt.Errorf("获取可用的周期数据,%s", err)
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, err
}
return cycleList, nil
}
// 获取可用的项目
// func getPrepareSummaryEvaluation(cycleId int) ([]*domain.EvaluationProject, error) {
// transactionContext, err := factory.CreateTransactionContext(nil)
// if err != nil {
// return nil, err
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return nil, err
// }
// defer func() {
// _ = transactionContext.RollbackTransaction()
// }()
// projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
// "transactionContext": transactionContext,
// })
// // 获取项目数据总数
// _, projectList, err := projectRepo.Find(map[string]interface{}{
// "cycleId": cycleId,
// "summaryState": domain.ProjectSummaryStateNo,
// "state": domain.ProjectStateEnable,
// "limit": 200,
// }, "template")
// if err != nil {
// return nil, fmt.Errorf("获取可用的项目数据,%s", err)
// }
// if err := transactionContext.CommitTransaction(); err != nil {
// return nil, err
// }
// return projectList, nil
// }
// 按周期下发 综合评估任务
func sendSummaryEvaluationByCycle(cycleParam *domain.EvaluationCycle) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return err
}
if err := transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
projectRepo := factory.CreateEvaluationProjectRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
// 获取项目数据总数
_, projectList, err := projectRepo.Find(map[string]interface{}{
"endTime": time.Now(),
"cycleId": cycleParam.Id,
"summaryState": domain.ProjectSummaryStateNo,
"state": domain.ProjectStateEnable,
"limit": 200,
"limit": 500,
}, "template")
if err != nil {
return nil, fmt.Errorf("获取可用的项目数据,%s", err)
return fmt.Errorf("获取可用的项目数据,%s", err)
}
var newEvaluationList []domain.SummaryEvaluation
newPublisher := summaryEvaluationPublisher{}
for _, val := range projectList {
evaluationList, err := newPublisher.sendSummaryEvaluationV2(transactionContext, val, cycleParam)
if err != nil {
return fmt.Errorf("按项目下发综合评估任务数据,%s", err)
}
newEvaluationList = append(newEvaluationList, evaluationList...)
}
// 回填周期的状态
cycleDao := dao.NewEvaluationCycleDao(map[string]interface{}{"transactionContext": transactionContext})
err = cycleDao.UpdateSummaryState(cycleParam.Id, domain.ProjectSummaryStateYes)
if err != nil {
return fmt.Errorf("保存项目周期状态%s", err)
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, err
return err
}
err = sendSmsEvalation(newEvaluationList)
if err != nil {
return fmt.Errorf("设置短信消息%s", err)
}
return projectList, nil
return nil
}
// 下发周期综合评估
type summaryEvaluationPublisher struct {
userCache map[int64]*domain.User
departCache map[int]*domain.Department
cycleCache map[int64]*domain.EvaluationCycle
}
func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *domain.EvaluationProject) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return err
}
if err := transactionContext.StartTransaction(); err != nil {
return err
}
defer func() {
_ = transactionContext.RollbackTransaction()
}()
func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(
transactionContext application.TransactionContext,
projectParam *domain.EvaluationProject, cycleData *domain.EvaluationCycle,
) ([]domain.SummaryEvaluation, error) {
// transactionContext, err := factory.CreateTransactionContext(nil)
// if err != nil {
// return err
// }
// if err := transactionContext.StartTransaction(); err != nil {
// return err
// }
// defer func() {
// _ = transactionContext.RollbackTransaction()
// }()
userRepo := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
departmentRepo := factory.CreateDepartmentRepository(map[string]interface{}{"transactionContext": transactionContext})
cycleRepo := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
evaluationItemRepo := factory.CreateEvaluationItemUsedRepository(map[string]interface{}{"transactionContext": transactionContext})
_, evaluationItemList, err := evaluationItemRepo.Find(map[string]interface{}{
"evaluationProjectId": projectParam.Id,
"nodeType": int(domain.LinkNodeSelfAssessment),
})
if err != nil {
return err
return nil, err
}
if len(evaluationItemList) == 0 {
return nil
return nil, nil
}
nodeId := evaluationItemList[0].NodeId
executor360Map := map[int64]*domain.User{}
... ... @@ -112,20 +186,12 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma
}
user360, err := se.getUserData(userRepo, int64(v.EvaluatorId))
if err != nil {
return fmt.Errorf("获取360用户%s", err)
return nil, fmt.Errorf("获取360用户%s", err)
}
executor360Map[user360.Id] = user360
}
//获取周期
cycleData, err := se.getCycleData(cycleRepo, projectParam.CycleId)
if err != nil {
return err
}
if cycleData == nil {
return nil
}
if cycleData.TimeEnd == nil {
return fmt.Errorf("周期%d:%s 结束时间错误", cycleData.Id, cycleData.Name)
return nil, fmt.Errorf("周期%d:%s 结束时间错误", cycleData.Id, cycleData.Name)
}
//自评的时间范围
beginTimeSelf := *cycleData.TimeEnd
... ... @@ -167,14 +233,14 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma
}
targetUser, err := se.getUserData(userRepo, targetUserId)
if err != nil {
return fmt.Errorf("获取员工数据%s", err)
return nil, fmt.Errorf("获取员工数据%s", err)
}
if targetUser == nil {
continue
}
targetUserDepartment, err := se.getDepartmentData(departmentRepo, targetUser.DepartmentId)
if err != nil {
return fmt.Errorf("获取员工的部门数据%s", err)
return nil, fmt.Errorf("获取员工的部门数据%s", err)
}
evaluationTemp.TargetDepartment = []domain.StaffDepartment{}
evaluationTemp.Types = domain.EvaluationSelf
... ... @@ -255,39 +321,38 @@ func (se *summaryEvaluationPublisher) sendSummaryEvaluationV2(projectParam *doma
for i := range newEvaluationList {
err = summaryEvaluationRepo.Save(&newEvaluationList[i])
if err != nil {
return fmt.Errorf("保存周期综合评估%s", err)
return nil, fmt.Errorf("保存周期综合评估%s", err)
}
}
//回填项目的状态
projectDao := dao.NewEvaluationProjectDao(map[string]interface{}{"transactionContext": transactionContext})
err = projectDao.UpdateSummaryState(projectParam.Id, domain.ProjectSummaryStateYes)
if err != nil {
return fmt.Errorf("保存项目状态%s", err)
}
if err := transactionContext.CommitTransaction(); err != nil {
return err
return nil, fmt.Errorf("保存项目状态%s", err)
}
err = sendSmsEvalation(newEvaluationList)
return fmt.Errorf("设置短信发送%s", err)
// if err := transactionContext.CommitTransaction(); err != nil {
// return err
// }
return newEvaluationList, fmt.Errorf("设置短信发送%s", err)
}
// 获取周期设置数据
func (se *summaryEvaluationPublisher) getCycleData(cycleRepo domain.EvaluationCycleRepository, cycleId int64) (*domain.EvaluationCycle, error) {
var cycleData *domain.EvaluationCycle
if val, ok := se.cycleCache[cycleId]; ok {
cycleData = val
} else {
_, cycleList, err := cycleRepo.Find(map[string]interface{}{"id": cycleId})
if err != nil {
return nil, err
}
if len(cycleList) == 0 {
return nil, nil
}
cycleData = cycleList[0]
}
return cycleData, nil
}
// func (se *summaryEvaluationPublisher) getCycleData(cycleRepo domain.EvaluationCycleRepository, cycleId int64) (*domain.EvaluationCycle, error) {
// var cycleData *domain.EvaluationCycle
// if val, ok := se.cycleCache[cycleId]; ok {
// cycleData = val
// } else {
// _, cycleList, err := cycleRepo.Find(map[string]interface{}{"id": cycleId})
// if err != nil {
// return nil, err
// }
// if len(cycleList) == 0 {
// return nil, nil
// }
// cycleData = cycleList[0]
// }
// return cycleData, nil
// }
// 获取用户数据
func (se *summaryEvaluationPublisher) getUserData(userRepo domain.UserRepository, userId int64) (*domain.User, error) {
... ...
... ... @@ -40,4 +40,5 @@ type EvaluationCycleRepository interface {
FindOne(queryOptions map[string]interface{}) (*EvaluationCycle, error)
Find(queryOptions map[string]interface{}) (int64, []*EvaluationCycle, error)
Count(queryOptions map[string]interface{}) (int64, error)
FindCycleEnd(limit int) ([]*EvaluationCycle, error) // 获取已结束的周期,且还没下发周期评估
}
... ...
package dao
import (
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
)
type EvaluationCycleDao struct {
transactionContext *pgTransaction.TransactionContext
}
func NewEvaluationCycleDao(options map[string]interface{}) *EvaluationCycleDao {
var transactionContext *pgTransaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pgTransaction.TransactionContext)
}
return &EvaluationCycleDao{
transactionContext: transactionContext,
}
}
func (d *EvaluationCycleDao) UpdateSummaryState(id int64, status domain.ProjectSummaryState) error {
db := d.transactionContext.PgTx
_, err := db.Model(&models.EvaluationCycle{}).
Where("id=?", id).
Set("summary_state=?", int(status)).
Update()
return err
}
... ...
... ... @@ -1253,7 +1253,7 @@ group by level_value,category,"name" `
var result []AssessContentLevelCode
condition := []interface{}{
projectId, targetUserId, string(assessType), cycleId,
targetUserId, string(assessType), cycleId, projectId,
}
tx := d.transactionContext.PgTx
_, err := tx.Query(&result, sqlStr, condition...)
... ...
... ... @@ -197,3 +197,25 @@ func (repo *EvaluationCycleRepository) Count(queryOptions map[string]interface{}
}
return int64(count), nil
}
// 获取已结束的周期
func (repo *EvaluationCycleRepository) FindCycleEnd(limit int) ([]*domain.EvaluationCycle, error) {
tx := repo.transactionContext.PgTx
var m []*models.EvaluationCycle
query := tx.Model(&m).
Where("deleted_at isnull").
Where("time_end<=?", time.Now()).
Where("summary_state=0").
Limit(limit)
err := query.Select()
if err != nil {
return nil, err
}
var arrays []*domain.EvaluationCycle
for _, v := range m {
d := repo.TransformToDomain(v)
arrays = append(arrays, &d)
}
return arrays, nil
}
... ...
-- 添加summary_cycle 表字段
ALTER TABLE public.evaluation_cycle
ADD summary_state int4 NOT NULL DEFAULT 0;
-- 初始化数据的值
UPDATE
public.evaluation_cycle
SET
summary_state = 1
WHERE
time_end <= now()
AND summary_state = 0;
... ...