作者 庄敏学

导入成本结构化

... ... @@ -17,5 +17,6 @@ require (
github.com/pdfcpu/pdfcpu v0.3.13
github.com/stretchr/testify v1.7.0
github.com/tal-tech/go-queue v1.0.5
github.com/xuri/excelize/v2 v2.4.1 // indirect
golang.org/x/text v0.3.6
)
... ...
package command
type ImportCostDataCommand struct {
Types int `json:"types"`
}
... ...
... ... @@ -14,6 +14,8 @@ type ImportDataCommand struct {
FileExt string `json:"-"`
// 业务编码
Code string `form:"code"`
// 条件 - 成本结构会增加条件
Where string `form:"where"`
}
func (importDataCommand *ImportDataCommand) Valid(validation *validation.Validation) {
... ...
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{}{
... ...
... ... @@ -37,7 +37,13 @@ type CostManagemant struct {
ParentId int64 `json:"parentId,string"`
// 现状值,字符串,如果纯数字的时候参与计算
Present string `json:"present"`
// 当前选择的节点类型 1、combination 组合框 2、staff 员工选取 3、text 文本框 4、BOM BOM表
// 5、number数值框 6、calculate计算框
ChoiceId int `cname:"当前选择的节点类型 1、combination 组合框 2、staff 员工选取 3、text 文本框 4、BOM BOM表 5、number数值框 6、calculate计算框" json:"choiceId"`
// 图片
Urls []string `cname:"图片" json:"urls"`
// calculate 计算框:计算框公式
Formula string `cname:"calculate 计算框:计算框公式" json:"formula" `
// 项目id
ProjectId int64 `json:"projectId,string"`
// 项目名称
... ... @@ -54,6 +60,7 @@ type CostManagemant struct {
TopCostManagemantId int64 `json:"topCostManagemantId,string"`
// 项目分类: 1风险 2成本 3品质 4交期 5服务 6客户关系 7品牌
Types int `json:"types"`
// 昨日实际值
YesterdayActual string `json:"yesterdayActual"`
}
\ No newline at end of file
... ...
... ... @@ -68,6 +68,8 @@ const (
ImportProducts = "BUSINESS_ALLIED-MANUFACTURING_BASIC_PRODUCT"
// 导入设备
ImportDevices = "BUSINESS_ALLIED-MANUFACTURING_BASIC_DEVICE"
//导入成本结构
ImportCosts = "BUSINESS_ALLIED-COST"
)
const (
... ...
... ... @@ -46,7 +46,7 @@ func (gateway HttpLibCostStructured) BatchCreateCostManagemant(param BatchAddCos
type BatchAddCostManagemantRequest struct {
*domain.BatchCreateCostManagemant
}
type BatchAddCostManagemantResponse interface{}
type BatchAddCostManagemantResponse []interface{}
func (gateway HttpLibCostStructured) ListCostManagemant(param ListCostManagemantRequest) (*ListCostManagemantResponse, error) {
url := fmt.Sprintf("%s%s", gateway.BaseUrl(), "/cost-managemants/")
... ...
... ... @@ -214,6 +214,8 @@ func defaultImport(controller *ExcelDataController) {
data, err = excelService.ImportProduct(cmd)
case domain.ImportDevices:
data, err = excelService.ImportDevice(cmd)
case domain.ImportCosts:
data,err = excelService.ImportCost(cmd)
default:
err = fmt.Errorf("导入不存在 Code:%v", cmd.Code)
}
... ...