|
|
package service
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_manufacture"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/util/converter"
|
|
|
"strings"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/core/application"
|
|
|
"github.com/linmadan/egglib-go/utils/excel"
|
|
|
"github.com/xuri/excelize/v2"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/domainService"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_manufacture"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/cost_structured"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/util/converter"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/util/oss"
|
|
|
"io"
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
type ExcelDataService struct {
|
...
|
...
|
@@ -245,6 +251,120 @@ func (srv ExcelDataService) ImportDevice(importDataCommand *command.ImportDataCo |
|
|
return srv.importResultWithHeader(excelImport.DataFields, *result, len(items)), nil
|
|
|
}
|
|
|
|
|
|
// ImportCost 导入成本结构化
|
|
|
func (srv ExcelDataService) ImportCost(importDataCommand *command.ImportDataCommand) (interface{}, error) {
|
|
|
excelImport := excel.NewExcelImport()
|
|
|
excelImport.RowBegin = 2 //第二行开始读取
|
|
|
excelImport.DataFields = []excel.DataField{
|
|
|
{EnName: "projectCode", CnName: "*项目编码"},
|
|
|
{EnName: "itemName", CnName: "*细项名称"},
|
|
|
{EnName: "choiceId", CnName: "*节点类型"},
|
|
|
{EnName: "text", CnName: "文本(数值)内容"},
|
|
|
{EnName: "image", CnName: "图片"},
|
|
|
{EnName: "formula", CnName: "计算公式"},
|
|
|
{EnName: "chargePersons", CnName: "*负责人"},
|
|
|
{EnName: "target", CnName: "目标值"},
|
|
|
{EnName: "targetPeriod", CnName: "目标值期限"},
|
|
|
{EnName: "present", CnName: "现状值"},
|
|
|
{EnName: "schedulePlanPercent", CnName: "进度计划比"},
|
|
|
{EnName: "benchmark", CnName: "标杆值"},
|
|
|
{EnName: "highest", CnName: "历史最高值"},
|
|
|
{EnName: "yesterdayActual", CnName: "昨日实际值"},
|
|
|
}
|
|
|
var buf bytes.Buffer
|
|
|
tee := io.TeeReader(importDataCommand.Reader, &buf)
|
|
|
excelData, err := converter.OpenImportFileFromIoReader(excelImport, tee, importDataCommand.FileExt) //excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
importCostCommand := &command.ImportCostDataCommand{}
|
|
|
err = json.Unmarshal([]byte(importDataCommand.Where),importCostCommand)
|
|
|
if err != nil {
|
|
|
return nil,application.ThrowError(application.ARG_ERROR,"风险类型不能为空")
|
|
|
}
|
|
|
if importCostCommand.Types <= 0 {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, "风险类型不能为空")
|
|
|
}
|
|
|
f, err := excelize.OpenReader(&buf)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
index := f.GetActiveSheetIndex()
|
|
|
//获取公司下所有用户
|
|
|
users, err := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator).UserSearch(allied_creation_user.ReqUserSearch{CompanyId: importDataCommand.Operator.CompanyId, Limit: 10000})
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, "获取用户数据失败")
|
|
|
}
|
|
|
userMap := make(map[string]allied_creation_user.UserDetail)
|
|
|
for _, item := range users.Users {
|
|
|
userMap[item.UserInfo.UserName] = item
|
|
|
}
|
|
|
createCostManagemant := cost_structured.BatchAddCostManagemantRequest{
|
|
|
BatchCreateCostManagemant: &domain.BatchCreateCostManagemant{
|
|
|
UserId: importDataCommand.Operator.UserId,
|
|
|
CompanyId: importDataCommand.Operator.CompanyId,
|
|
|
ProjectName: "",
|
|
|
Types: importCostCommand.Types,
|
|
|
CostManagemants: make([]*domain.CostManagemant, 0),
|
|
|
},
|
|
|
}
|
|
|
for key, v := range excelData {
|
|
|
if key == 0 {
|
|
|
createCostManagemant.ProjectName = strings.TrimSpace(v["itemName"])
|
|
|
}
|
|
|
_, mBytes, err := f.GetPicture(f.GetSheetName(index), "E"+fmt.Sprintf("%v", excelImport.RowBegin+key))
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "读取图片失败")
|
|
|
}
|
|
|
var ossFile string
|
|
|
if mBytes != nil {
|
|
|
ossFile, err = oss.UploadOss(mBytes)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传图片失败")
|
|
|
}
|
|
|
}
|
|
|
choiceId, err := strconv.Atoi(v["choiceId"])
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.ARG_ERROR, "节点类型非数字类型")
|
|
|
}
|
|
|
// 负责人
|
|
|
chargePersons := make([]string, 0)
|
|
|
names := strings.Split(strings.TrimSpace(v["chargePersons"]), " ")
|
|
|
for _, userName := range names {
|
|
|
if userName == "" {
|
|
|
continue
|
|
|
}
|
|
|
if user, ok := userMap[userName]; ok {
|
|
|
chargePersons = append(chargePersons, fmt.Sprintf("%v", user.UserId))
|
|
|
}
|
|
|
}
|
|
|
//目标值期限
|
|
|
targetPeriod, _ := strconv.Atoi(strings.TrimSpace(v["targetPeriod"]))
|
|
|
item := &domain.CostManagemant{
|
|
|
ProjectCode: strings.TrimSpace(v["projectCode"]),
|
|
|
ItemName: strings.TrimSpace(v["itemName"]),
|
|
|
ChoiceId: choiceId,
|
|
|
Urls: []string{ossFile},
|
|
|
Formula: strings.TrimSpace(v["formula"]),
|
|
|
ChargePersons: chargePersons,
|
|
|
Target: strings.TrimSpace(v["target"]),
|
|
|
TargetPeriod: targetPeriod,
|
|
|
Present: strings.TrimSpace(v["present"]),
|
|
|
SchedulePlanPercent: strings.TrimSpace(v["schedulePlanPercent"]),
|
|
|
Benchmark: strings.TrimSpace(v["benchmark"]),
|
|
|
Highest: strings.TrimSpace(v["highest"]),
|
|
|
YesterdayActual: strings.TrimSpace(v["yesterdayActual"]),
|
|
|
Types: importCostCommand.Types,
|
|
|
}
|
|
|
createCostManagemant.CostManagemants = append(createCostManagemant.CostManagemants, item)
|
|
|
}
|
|
|
result, err := cost_structured.NewHttpLibCostStructured(importDataCommand.Operator).BatchCreateCostManagemant(createCostManagemant)
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
|
|
|
}
|
|
|
return srv.importResultWithHeader(excelImport.DataFields, *result, len(createCostManagemant.CostManagemants)), nil
|
|
|
}
|
|
|
|
|
|
// 导入结果
|
|
|
func (srv ExcelDataService) importResultWithHeader(headers []excel.DataField, failRows []interface{}, totalRow int) interface{} {
|
|
|
var result = map[string]interface{}{
|
...
|
...
|
|