作者 郑周

1.模板导入数据

2.优化内容
... ... @@ -2,7 +2,7 @@ package main
import (
"github.com/beego/beego/v2/server/web"
service "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/node_task"
serviceTask "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/node_task"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/log"
_ "gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/consumer"
... ... @@ -24,7 +24,7 @@ func main() {
// 定时任务-间隔发送评估环节
func startNodeTask() {
go func() {
nodeTaskService := service.NewNodeTaskService()
nodeTaskService := serviceTask.NewNodeTaskService()
for {
timer := time.NewTimer(time.Second * 60)
<-timer.C
... ...
... ... @@ -328,7 +328,7 @@ func (rs *EvaluationCycleService) Remove(in *command.DeleteCycleCommand) (interf
}
func (rs *EvaluationCycleService) List(in *command.QueryCycleCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
... ...
... ... @@ -301,7 +301,7 @@ func (rs *EvaluationProjectService) Remove(in *command.DeleteProjectCommand) (in
}
func (rs *EvaluationProjectService) List(in *command.QueryProjectCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
... ...
... ... @@ -51,6 +51,7 @@ func (rs *EvaluationRuleService) Create(in *command.CreateRuleCommand) (interfac
CompanyId: in.CompanyId,
CreatorId: in.CreatorId,
Type: in.Type,
SysType: domain.EvaluationSysTypeCommon,
Rating: in.Rating,
Score: in.Score,
}
... ... @@ -147,6 +148,10 @@ func (rs *EvaluationRuleService) Remove(in *command.DeleteRuleCommand) (interfac
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if rule.SysType == domain.EvaluationSysTypeSystem {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "系统默认规则不可删除")
}
if _, err := ruleRepository.Remove(rule); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
... ... @@ -175,7 +180,7 @@ func (rs *EvaluationRuleService) Remove(in *command.DeleteRuleCommand) (interfac
//}
func (rs *EvaluationRuleService) ListRelCreator(in *command.QueryRuleCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
... ... @@ -192,12 +197,31 @@ func (rs *EvaluationRuleService) ListRelCreator(in *command.QueryRuleCommand) (i
ras := make([]*adapter.RuleAdapter, 0)
creatorIds := make([]int64, 0)
var havaSystemType = false
for i := range rules {
ra := &adapter.RuleAdapter{}
ra.EvaluationRule = rules[i]
ras = append(ras, ra)
creatorIds = append(creatorIds, rules[i].CreatorId)
if rules[i].SysType == domain.EvaluationSysTypeSystem {
havaSystemType = true
break
}
}
// 如果不存在系统默认就生成一个
if !havaSystemType {
newRule := domain.GenerateSysRule(in.CompanyId)
rule, err := ruleRepository.Insert(newRule)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
ra := &adapter.RuleAdapter{}
ra.EvaluationRule = rule
ras = append(ras, ra)
}
_, users, _ := userRepository.Find(map[string]interface{}{"ids": creatorIds, "limit": len(creatorIds)})
userNameMap := map[int64]string{}
if users != nil {
... ... @@ -214,7 +238,7 @@ func (rs *EvaluationRuleService) ListRelCreator(in *command.QueryRuleCommand) (i
}
func (rs *EvaluationRuleService) ListCreator(in *command.QueryCreatorCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
... ...
... ... @@ -186,7 +186,7 @@ func (rs *EvaluationTemplateService) Remove(in *command.DeleteTemplateCommand) (
}
func (rs *EvaluationTemplateService) List(in *command.QueryTemplateCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
... ...
... ... @@ -141,7 +141,7 @@ func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error
}
func (rs *RoleService) ListForUser(in *command.QueryRoleUserCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
... ...
... ... @@ -95,7 +95,7 @@ func (rs *RoleUserService) Remove(in *command.UserRoleDeleteCommand) (interface{
}
func (rs *RoleUserService) ListRole(in *command.UserRoleQueryCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
... ...
... ... @@ -9,6 +9,11 @@ const (
EvaluationTypeScore int = 1 // 评估方式-评分
)
const (
EvaluationSysTypeCommon int = 0 // 系统类型-系统添加
EvaluationSysTypeSystem int = 1 // 系统类型-系统预制(不可删除、编辑)
)
type Rating struct {
Levels []*RatingLevel `json:"levels" comment:"配置等级"`
}
... ... @@ -42,6 +47,7 @@ type EvaluationRule struct {
CompanyId int64 `json:"companyId,string" comment:"公司ID"`
CreatorId int64 `json:"creatorId,string" comment:"创建人ID"`
Type int `json:"type" comment:"评估方式(0评级、1评分)"`
SysType int `json:"sysType" comment:"系统类型(0普通、1系统固定)" pg:",use_zero"`
Rating Rating `json:"rating" comment:"评级"`
Score Score `json:"score" comment:"评分"`
CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
... ... @@ -49,6 +55,58 @@ type EvaluationRule struct {
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
}
// GenerateSysRule 当前公司下的生成默认规则
func GenerateSysRule(companyId int64) *EvaluationRule {
levels := make([]*RatingLevel, 0)
levels = append(levels, &RatingLevel{
Code: "S",
Name: "SS",
Color: 1,
QuantizedValue: 90,
})
levels = append(levels, &RatingLevel{
Code: "A",
Name: "AA",
Color: 2,
QuantizedValue: 80,
})
levels = append(levels, &RatingLevel{
Code: "B",
Name: "BB",
Color: 3,
QuantizedValue: 70,
})
levels = append(levels, &RatingLevel{
Code: "C",
Name: "CC",
Color: 4,
QuantizedValue: 60,
})
levels = append(levels, &RatingLevel{
Code: "D",
Name: "DD",
Color: 5,
QuantizedValue: 50,
})
newRule := &EvaluationRule{
Id: 0,
Name: "评级(默认)",
Remark: "",
CompanyId: companyId,
CreatorId: 0,
Type: EvaluationTypeRating,
SysType: EvaluationSysTypeSystem,
Rating: Rating{
Levels: levels,
},
Score: Score{},
}
return newRule
}
type EvaluationRuleRepository interface {
Insert(rule *EvaluationRule) (*EvaluationRule, error)
Remove(rule *EvaluationRule) (*EvaluationRule, error)
... ...
... ... @@ -13,6 +13,7 @@ type EvaluationRule struct {
CompanyId int64 `comment:"公司ID"`
CreatorId int64 `comment:"创建人ID"`
Type int `comment:"评估方式(0评级、1评分)" pg:",use_zero"`
SysType int `comment:"系统类型(0普通、1系统固定)" pg:",use_zero"`
Rating domain.Rating `comment:"评级"`
Score domain.Score `comment:"评分"`
CreatedAt time.Time `comment:"创建时间"`
... ...
... ... @@ -28,6 +28,7 @@ func (repo *EvaluationRuleRepository) TransformToDomain(m *models.EvaluationRule
CompanyId: m.CompanyId,
CreatorId: m.CreatorId,
Type: m.Type,
SysType: m.SysType,
Rating: m.Rating,
Score: m.Score,
CreatedAt: m.CreatedAt,
... ... @@ -44,6 +45,7 @@ func (repo *EvaluationRuleRepository) TransformToModel(d *domain.EvaluationRule)
CompanyId: d.CompanyId,
CreatorId: d.CreatorId,
Type: d.Type,
SysType: d.SysType,
Rating: d.Rating,
Score: d.Score,
CreatedAt: d.CreatedAt,
... ... @@ -143,6 +145,10 @@ func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{})
query.Where("type = ?", v)
}
if v, ok := queryOptions["sysType"]; ok && v.(int) >= 0 {
query.Where("sys_type = ?", v)
}
if v, ok := queryOptions["limit"].(int64); ok {
query.Limit(int(v))
}
... ...
... ... @@ -2,9 +2,14 @@ package controllers
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"github.com/linmadan/egglib-go/web/beego"
"github.com/xuri/excelize/v2"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/middlewares"
"strconv"
"strings"
)
type ImportController struct {
... ... @@ -37,8 +42,82 @@ func (controller *ImportController) Import() {
switch formType {
case "PerformanceDimension":
dimensions, _ := domain.LoadPerformanceDimensions(rows)
controller.Response(dimensions, nil)
list := controller.parseTemplateNodeContent(dimensions)
controller.Response(tool_funs.SimpleWrapGridMap(int64(len(list)), list), nil)
default:
controller.Response(nil, application.ThrowError(application.ARG_ERROR, "请确认您导入的表单类型"))
}
}
func (controller *ImportController) parseTemplateNodeContent(data []*domain.PerformanceDimension) []*domain.NodeContent {
nodeContents := make([]*domain.NodeContent, 0)
transactionContext, err := factory.StartTransaction()
if err != nil {
return nodeContents
}
defer func() {
transactionContext.RollbackTransaction()
}()
// 获取当前公司下的默认规则
ua := middlewares.GetUser(controller.Ctx)
ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
_, rules, err := ruleRepository.Find(map[string]interface{}{"companyId": ua.CompanyId, "sysType": domain.EvaluationSysTypeSystem, "limit": 1})
if err != nil {
return nodeContents
}
var ruleId = int64(0)
if len(rules) == 0 {
newRule := domain.GenerateSysRule(ua.CompanyId) // 生成一个系统默认规则
if rule, err := ruleRepository.Insert(newRule); err != nil {
return nodeContents
} else {
ruleId = rule.Id
}
if err := transactionContext.CommitTransaction(); err != nil {
return nodeContents
}
} else {
ruleId = rules[0].Id
}
for i := range data {
dimension := data[i]
for i2 := range dimension.PerformanceModule {
nc := &domain.NodeContent{}
nc.Category = dimension.Name // 类别
module := dimension.PerformanceModule[i2]
nc.Name = module.ModuleName // 名称
nc.RuleId = ruleId // 规则ID
sIndex := strings.Index(module.Weight, "%") // 权重
if sIndex != -1 {
iWeight, _ := strconv.Atoi(module.Weight[:sIndex])
nc.Weight = iWeight
} else {
nc.Weight = 0
}
nc.PromptTitle = "" // 提示项标题
nc.PromptText = module.Standard // 提示项内容
nc.EntryItems = make([]*domain.EntryItem, 0) // 输入项
for i3 := range module.Target {
target := module.Target[i3]
nc.EntryItems = append(nc.EntryItems, &domain.EntryItem{
Title: target.Task, // 输入型标题
HintText: "", // 输入项提示文本
})
}
// 没有任何输入项时,默认1个
if len(nc.EntryItems) == 0 {
nc.EntryItems = append(nc.EntryItems, &domain.EntryItem{
Title: "填写反馈",
HintText: "",
})
}
nodeContents = append(nodeContents, nc)
}
}
return nodeContents
}
... ...
package controllers
import (
"io/ioutil"
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/web/beego"
)
type TemplateImplController struct {
beego.BaseController
}
func (controller *TemplateImplController) TemplateQuestion() {
fileBytes, err := ioutil.ReadFile("./templates/tpl_template_question.xlsx")
if err != nil {
controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "未读取到模板文件"))
return
}
controller.WriteExcel(fileBytes, "评估导入模板.xlsx")
}
func (controller *TemplateImplController) WriteExcel(fileBytes []byte, fileName string) {
controller.Ctx.Output.Header("Content-Disposition", "attachment;filename="+fileName)
controller.Ctx.Output.Header("Content-Description", "FileTransfer")
controller.Ctx.Output.Header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
controller.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
controller.Ctx.Output.Header("Expires", "0")
_, _ = controller.Ctx.ResponseWriter.Write(fileBytes)
}
func (controller *TemplateImplController) WriteBinary(fileBytes []byte, fileName string) {
controller.Ctx.Output.Header("Content-Disposition", "attachment;filename="+fileName)
controller.Ctx.Output.Header("Content-Description", "FileTransfer")
controller.Ctx.Output.Header("Content-Type", "application/octet-stream")
controller.Ctx.Output.Header("Content-Transfer-Encoding", "binary")
controller.Ctx.Output.Header("Expires", "0")
_, _ = controller.Ctx.ResponseWriter.Write(fileBytes)
}
... ...
package routers
import (
"github.com/beego/beego/v2/server/web"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/port/beego/controllers"
)
func init() {
web.Router("/templates/tpl_template_question.xlsx", &controllers.TemplateImplController{}, "Get:TemplateQuestion")
}
... ...
不能预览此文件类型