import_controller.go 4.2 KB
package controllers

import (
	"strconv"
	"strings"

	"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"
)

type ImportController struct {
	beego.BaseController
}

func (controller *ImportController) Import() {
	_, header, err := controller.GetFile("file")
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
		return
	}
	file, err := header.Open()
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
		return
	}
	reader, err := excelize.OpenReader(file)
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传错误:"+err.Error()))
		return
	}
	index := reader.GetActiveSheetIndex()
	rows, err := reader.GetRows(reader.GetSheetName(index))
	if err != nil {
		controller.Response(nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "读取excel错误:"+err.Error()))
		return
	}
	formType := controller.GetString("type")
	switch formType {
	case "PerformanceDimension":
		dimensions, err := domain.LoadPerformanceDimensions(rows)
		if err != nil {
			controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
		}
		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.ParseFloat(module.Weight[:sIndex], 64)
				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:   "",                // 输入项提示文本
					Definition: target.Definition, //定义
				})
			}
			// 没有任何输入项时,默认1个
			if len(nc.EntryItems) == 0 {
				nc.EntryItems = append(nc.EntryItems, &domain.EntryItem{
					Title:    "填写反馈",
					HintText: "",
				})
			}

			// 必填项
			nc.Required = module.Required
			nodeContents = append(nodeContents, nc)
		}
	}

	return nodeContents
}