作者 庄敏学

导入成本结构化

@@ -17,5 +17,6 @@ require ( @@ -17,5 +17,6 @@ require (
17 github.com/pdfcpu/pdfcpu v0.3.13 17 github.com/pdfcpu/pdfcpu v0.3.13
18 github.com/stretchr/testify v1.7.0 18 github.com/stretchr/testify v1.7.0
19 github.com/tal-tech/go-queue v1.0.5 19 github.com/tal-tech/go-queue v1.0.5
  20 + github.com/xuri/excelize/v2 v2.4.1 // indirect
20 golang.org/x/text v0.3.6 21 golang.org/x/text v0.3.6
21 ) 22 )
  1 +package command
  2 +
  3 +type ImportCostDataCommand struct {
  4 + Types int `json:"types"`
  5 +}
@@ -14,6 +14,8 @@ type ImportDataCommand struct { @@ -14,6 +14,8 @@ type ImportDataCommand struct {
14 FileExt string `json:"-"` 14 FileExt string `json:"-"`
15 // 业务编码 15 // 业务编码
16 Code string `form:"code"` 16 Code string `form:"code"`
  17 + // 条件 - 成本结构会增加条件
  18 + Where string `form:"where"`
17 } 19 }
18 20
19 func (importDataCommand *ImportDataCommand) Valid(validation *validation.Validation) { 21 func (importDataCommand *ImportDataCommand) Valid(validation *validation.Validation) {
1 package service 1 package service
2 2
3 import ( 3 import (
  4 + "bytes"
  5 + "encoding/json"
4 "fmt" 6 "fmt"
5 - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_manufacture"  
6 - "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/util/converter"  
7 - "strings"  
8 -  
9 "github.com/linmadan/egglib-go/core/application" 7 "github.com/linmadan/egglib-go/core/application"
10 "github.com/linmadan/egglib-go/utils/excel" 8 "github.com/linmadan/egglib-go/utils/excel"
  9 + "github.com/xuri/excelize/v2"
11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/command"
12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query" 11 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/excelData/query"
13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
14 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/domainService" 13 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/domainService"
  14 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_manufacture"
15 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user" 15 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
  16 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/cost_structured"
  17 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/util/converter"
  18 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/util/oss"
  19 + "io"
  20 + "strconv"
  21 + "strings"
16 ) 22 )
17 23
18 type ExcelDataService struct { 24 type ExcelDataService struct {
@@ -245,6 +251,120 @@ func (srv ExcelDataService) ImportDevice(importDataCommand *command.ImportDataCo @@ -245,6 +251,120 @@ func (srv ExcelDataService) ImportDevice(importDataCommand *command.ImportDataCo
245 return srv.importResultWithHeader(excelImport.DataFields, *result, len(items)), nil 251 return srv.importResultWithHeader(excelImport.DataFields, *result, len(items)), nil
246 } 252 }
247 253
  254 +// ImportCost 导入成本结构化
  255 +func (srv ExcelDataService) ImportCost(importDataCommand *command.ImportDataCommand) (interface{}, error) {
  256 + excelImport := excel.NewExcelImport()
  257 + excelImport.RowBegin = 2 //第二行开始读取
  258 + excelImport.DataFields = []excel.DataField{
  259 + {EnName: "projectCode", CnName: "*项目编码"},
  260 + {EnName: "itemName", CnName: "*细项名称"},
  261 + {EnName: "choiceId", CnName: "*节点类型"},
  262 + {EnName: "text", CnName: "文本(数值)内容"},
  263 + {EnName: "image", CnName: "图片"},
  264 + {EnName: "formula", CnName: "计算公式"},
  265 + {EnName: "chargePersons", CnName: "*负责人"},
  266 + {EnName: "target", CnName: "目标值"},
  267 + {EnName: "targetPeriod", CnName: "目标值期限"},
  268 + {EnName: "present", CnName: "现状值"},
  269 + {EnName: "schedulePlanPercent", CnName: "进度计划比"},
  270 + {EnName: "benchmark", CnName: "标杆值"},
  271 + {EnName: "highest", CnName: "历史最高值"},
  272 + {EnName: "yesterdayActual", CnName: "昨日实际值"},
  273 + }
  274 + var buf bytes.Buffer
  275 + tee := io.TeeReader(importDataCommand.Reader, &buf)
  276 + excelData, err := converter.OpenImportFileFromIoReader(excelImport, tee, importDataCommand.FileExt) //excelImport.OpenExcelFromIoReader(importDataCommand.Reader)
  277 + if err != nil {
  278 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  279 + }
  280 + importCostCommand := &command.ImportCostDataCommand{}
  281 + err = json.Unmarshal([]byte(importDataCommand.Where),importCostCommand)
  282 + if err != nil {
  283 + return nil,application.ThrowError(application.ARG_ERROR,"风险类型不能为空")
  284 + }
  285 + if importCostCommand.Types <= 0 {
  286 + return nil, application.ThrowError(application.ARG_ERROR, "风险类型不能为空")
  287 + }
  288 + f, err := excelize.OpenReader(&buf)
  289 + if err != nil {
  290 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  291 + }
  292 + index := f.GetActiveSheetIndex()
  293 + //获取公司下所有用户
  294 + users, err := allied_creation_user.NewHttplibAlliedCreationUser(importDataCommand.Operator).UserSearch(allied_creation_user.ReqUserSearch{CompanyId: importDataCommand.Operator.CompanyId, Limit: 10000})
  295 + if err != nil {
  296 + return nil, application.ThrowError(application.ARG_ERROR, "获取用户数据失败")
  297 + }
  298 + userMap := make(map[string]allied_creation_user.UserDetail)
  299 + for _, item := range users.Users {
  300 + userMap[item.UserInfo.UserName] = item
  301 + }
  302 + createCostManagemant := cost_structured.BatchAddCostManagemantRequest{
  303 + BatchCreateCostManagemant: &domain.BatchCreateCostManagemant{
  304 + UserId: importDataCommand.Operator.UserId,
  305 + CompanyId: importDataCommand.Operator.CompanyId,
  306 + ProjectName: "",
  307 + Types: importCostCommand.Types,
  308 + CostManagemants: make([]*domain.CostManagemant, 0),
  309 + },
  310 + }
  311 + for key, v := range excelData {
  312 + if key == 0 {
  313 + createCostManagemant.ProjectName = strings.TrimSpace(v["itemName"])
  314 + }
  315 + _, mBytes, err := f.GetPicture(f.GetSheetName(index), "E"+fmt.Sprintf("%v", excelImport.RowBegin+key))
  316 + if err != nil {
  317 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "读取图片失败")
  318 + }
  319 + var ossFile string
  320 + if mBytes != nil {
  321 + ossFile, err = oss.UploadOss(mBytes)
  322 + if err != nil {
  323 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "上传图片失败")
  324 + }
  325 + }
  326 + choiceId, err := strconv.Atoi(v["choiceId"])
  327 + if err != nil {
  328 + return nil, application.ThrowError(application.ARG_ERROR, "节点类型非数字类型")
  329 + }
  330 + // 负责人
  331 + chargePersons := make([]string, 0)
  332 + names := strings.Split(strings.TrimSpace(v["chargePersons"]), " ")
  333 + for _, userName := range names {
  334 + if userName == "" {
  335 + continue
  336 + }
  337 + if user, ok := userMap[userName]; ok {
  338 + chargePersons = append(chargePersons, fmt.Sprintf("%v", user.UserId))
  339 + }
  340 + }
  341 + //目标值期限
  342 + targetPeriod, _ := strconv.Atoi(strings.TrimSpace(v["targetPeriod"]))
  343 + item := &domain.CostManagemant{
  344 + ProjectCode: strings.TrimSpace(v["projectCode"]),
  345 + ItemName: strings.TrimSpace(v["itemName"]),
  346 + ChoiceId: choiceId,
  347 + Urls: []string{ossFile},
  348 + Formula: strings.TrimSpace(v["formula"]),
  349 + ChargePersons: chargePersons,
  350 + Target: strings.TrimSpace(v["target"]),
  351 + TargetPeriod: targetPeriod,
  352 + Present: strings.TrimSpace(v["present"]),
  353 + SchedulePlanPercent: strings.TrimSpace(v["schedulePlanPercent"]),
  354 + Benchmark: strings.TrimSpace(v["benchmark"]),
  355 + Highest: strings.TrimSpace(v["highest"]),
  356 + YesterdayActual: strings.TrimSpace(v["yesterdayActual"]),
  357 + Types: importCostCommand.Types,
  358 + }
  359 + createCostManagemant.CostManagemants = append(createCostManagemant.CostManagemants, item)
  360 + }
  361 + result, err := cost_structured.NewHttpLibCostStructured(importDataCommand.Operator).BatchCreateCostManagemant(createCostManagemant)
  362 + if err != nil {
  363 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  364 + }
  365 + return srv.importResultWithHeader(excelImport.DataFields, *result, len(createCostManagemant.CostManagemants)), nil
  366 +}
  367 +
248 // 导入结果 368 // 导入结果
249 func (srv ExcelDataService) importResultWithHeader(headers []excel.DataField, failRows []interface{}, totalRow int) interface{} { 369 func (srv ExcelDataService) importResultWithHeader(headers []excel.DataField, failRows []interface{}, totalRow int) interface{} {
250 var result = map[string]interface{}{ 370 var result = map[string]interface{}{
@@ -37,7 +37,13 @@ type CostManagemant struct { @@ -37,7 +37,13 @@ type CostManagemant struct {
37 ParentId int64 `json:"parentId,string"` 37 ParentId int64 `json:"parentId,string"`
38 // 现状值,字符串,如果纯数字的时候参与计算 38 // 现状值,字符串,如果纯数字的时候参与计算
39 Present string `json:"present"` 39 Present string `json:"present"`
40 - 40 + // 当前选择的节点类型 1、combination 组合框 2、staff 员工选取 3、text 文本框 4、BOM BOM表
  41 + // 5、number数值框 6、calculate计算框
  42 + ChoiceId int `cname:"当前选择的节点类型 1、combination 组合框 2、staff 员工选取 3、text 文本框 4、BOM BOM表 5、number数值框 6、calculate计算框" json:"choiceId"`
  43 + // 图片
  44 + Urls []string `cname:"图片" json:"urls"`
  45 + // calculate 计算框:计算框公式
  46 + Formula string `cname:"calculate 计算框:计算框公式" json:"formula" `
41 // 项目id 47 // 项目id
42 ProjectId int64 `json:"projectId,string"` 48 ProjectId int64 `json:"projectId,string"`
43 // 项目名称 49 // 项目名称
@@ -54,6 +60,7 @@ type CostManagemant struct { @@ -54,6 +60,7 @@ type CostManagemant struct {
54 TopCostManagemantId int64 `json:"topCostManagemantId,string"` 60 TopCostManagemantId int64 `json:"topCostManagemantId,string"`
55 // 项目分类: 1风险 2成本 3品质 4交期 5服务 6客户关系 7品牌 61 // 项目分类: 1风险 2成本 3品质 4交期 5服务 6客户关系 7品牌
56 Types int `json:"types"` 62 Types int `json:"types"`
57 - 63 + // 昨日实际值
  64 + YesterdayActual string `json:"yesterdayActual"`
58 65
59 } 66 }
@@ -68,6 +68,8 @@ const ( @@ -68,6 +68,8 @@ const (
68 ImportProducts = "BUSINESS_ALLIED-MANUFACTURING_BASIC_PRODUCT" 68 ImportProducts = "BUSINESS_ALLIED-MANUFACTURING_BASIC_PRODUCT"
69 // 导入设备 69 // 导入设备
70 ImportDevices = "BUSINESS_ALLIED-MANUFACTURING_BASIC_DEVICE" 70 ImportDevices = "BUSINESS_ALLIED-MANUFACTURING_BASIC_DEVICE"
  71 + //导入成本结构
  72 + ImportCosts = "BUSINESS_ALLIED-COST"
71 ) 73 )
72 74
73 const ( 75 const (
@@ -46,7 +46,7 @@ func (gateway HttpLibCostStructured) BatchCreateCostManagemant(param BatchAddCos @@ -46,7 +46,7 @@ func (gateway HttpLibCostStructured) BatchCreateCostManagemant(param BatchAddCos
46 type BatchAddCostManagemantRequest struct { 46 type BatchAddCostManagemantRequest struct {
47 *domain.BatchCreateCostManagemant 47 *domain.BatchCreateCostManagemant
48 } 48 }
49 -type BatchAddCostManagemantResponse interface{} 49 +type BatchAddCostManagemantResponse []interface{}
50 50
51 func (gateway HttpLibCostStructured) ListCostManagemant(param ListCostManagemantRequest) (*ListCostManagemantResponse, error) { 51 func (gateway HttpLibCostStructured) ListCostManagemant(param ListCostManagemantRequest) (*ListCostManagemantResponse, error) {
52 url := fmt.Sprintf("%s%s", gateway.BaseUrl(), "/cost-managemants/") 52 url := fmt.Sprintf("%s%s", gateway.BaseUrl(), "/cost-managemants/")
@@ -214,6 +214,8 @@ func defaultImport(controller *ExcelDataController) { @@ -214,6 +214,8 @@ func defaultImport(controller *ExcelDataController) {
214 data, err = excelService.ImportProduct(cmd) 214 data, err = excelService.ImportProduct(cmd)
215 case domain.ImportDevices: 215 case domain.ImportDevices:
216 data, err = excelService.ImportDevice(cmd) 216 data, err = excelService.ImportDevice(cmd)
  217 + case domain.ImportCosts:
  218 + data,err = excelService.ImportCost(cmd)
217 default: 219 default:
218 err = fmt.Errorf("导入不存在 Code:%v", cmd.Code) 220 err = fmt.Errorf("导入不存在 Code:%v", cmd.Code)
219 } 221 }