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{}{
|