From 73b6baf6af3d88cdcb0e2df7932a9bd96b0b85c5 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 01 七月 2024 22:32:34 +0800 Subject: [PATCH] 月度统计出入库按类型汇总报表定时任务和手动跑任务接口 --- service/material.go | 127 +++++++++++++++++++++++++---------------- 1 files changed, 77 insertions(+), 50 deletions(-) diff --git a/service/material.go b/service/material.go index adabc1d..920fa36 100644 --- a/service/material.go +++ b/service/material.go @@ -45,7 +45,14 @@ return 0, errors.New("鏀规枃浠舵病鏈夋暟鎹唴瀹�") } - inserts := rows[1:len(rows)] + inserts := rows[1:] + headers := rows[0] + attributesColumns := make([]int, 0) + for i, header := range headers { + if mapAttribute[header] != 0 { + attributesColumns = append(attributesColumns, i) + } + } for index, insert := range inserts { errMsg := "" @@ -132,62 +139,82 @@ if len(insert) > 8 && insert[7] != "" { material.Type = insert[7] //鍨嬪彿 } - if len(insert) > 9 && insert[8] != "" { - if mapAttribute[insert[8]] == 0 { - errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒潗璐ㄥ睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�" - return 0, errors.New(errMsg) - } - //material.Quality = insert[8] //鏉愯川 - attributeValue1 := new(models.AttributeValue) - attributeValue1.EntityID = material.ID - attributeValue1.AttributeID = mapAttribute[insert[8]] - attributeValue1.Value = insert[8] - attributeValueList = append(attributeValueList, attributeValue1) - } - if len(insert) > 10 && insert[9] != "" { - if mapAttribute[insert[9]] == 0 { - errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒搧鐗屽睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�" - return 0, errors.New(errMsg) - } - //material.Brand = insert[9] //鍝佺墝 - attributeValue2 := new(models.AttributeValue) - attributeValue2.EntityID = material.ID - attributeValue2.AttributeID = mapAttribute[insert[9]] - attributeValue2.Value = insert[9] - attributeValueList = append(attributeValueList, attributeValue2) - } - if len(insert) > 11 && insert[10] != "" { - if mapAttribute[insert[10]] == 0 { - errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒瓑绾у睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�" - return 0, errors.New(errMsg) - } - //material.Level = insert[10] //绛夌骇 - attributeValue3 := new(models.AttributeValue) - attributeValue3.EntityID = material.ID - attributeValue3.AttributeID = mapAttribute[insert[10]] - attributeValue3.Value = insert[10] - attributeValueList = append(attributeValueList, attributeValue3) - } - if len(insert) > 12 && insert[11] != "" { - if mapAttribute[insert[11]] == 0 { - errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒簞鍙e睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�" - return 0, errors.New(errMsg) - } - //material.From = insert[11] //搴勫彛 - attributeValue4 := new(models.AttributeValue) - attributeValue4.EntityID = material.ID - attributeValue4.AttributeID = mapAttribute[insert[11]] - attributeValue4.Value = insert[11] - attributeValueList = append(attributeValueList, attributeValue4) + { + /* + if len(insert) > 9 && insert[8] != "" { + if mapAttribute[insert[8]] == 0 { + errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒潗璐ㄥ睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�" + return 0, errors.New(errMsg) + } + //material.Quality = insert[8] //鏉愯川 + attributeValue1 := new(models.AttributeValue) + attributeValue1.EntityID = material.ID + attributeValue1.AttributeID = mapAttribute[insert[8]] + attributeValue1.Value = insert[8] + attributeValueList = append(attributeValueList, attributeValue1) + } + if len(insert) > 10 && insert[9] != "" { + if mapAttribute[insert[9]] == 0 { + errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒搧鐗屽睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�" + return 0, errors.New(errMsg) + } + //material.Brand = insert[9] //鍝佺墝 + attributeValue2 := new(models.AttributeValue) + attributeValue2.EntityID = material.ID + attributeValue2.AttributeID = mapAttribute[insert[9]] + attributeValue2.Value = insert[9] + attributeValueList = append(attributeValueList, attributeValue2) + } + if len(insert) > 11 && insert[10] != "" { + if mapAttribute[insert[10]] == 0 { + errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒瓑绾у睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�" + return 0, errors.New(errMsg) + } + //material.Level = insert[10] //绛夌骇 + attributeValue3 := new(models.AttributeValue) + attributeValue3.EntityID = material.ID + attributeValue3.AttributeID = mapAttribute[insert[10]] + attributeValue3.Value = insert[10] + attributeValueList = append(attributeValueList, attributeValue3) + } + if len(insert) > 12 && insert[11] != "" { + if mapAttribute[insert[11]] == 0 { + errMsg = "绗�" + strconv.Itoa(index+2) + "琛岋紝浜у搧缂栫爜锛�" + insert[0] + "锛屾湭鑳借瘑鍒簞鍙e睘鎬э紝璇峰厛娣诲姞璇ュ睘鎬�" + return 0, errors.New(errMsg) + } + //material.From = insert[11] //搴勫彛 + attributeValue4 := new(models.AttributeValue) + attributeValue4.EntityID = material.ID + attributeValue4.AttributeID = mapAttribute[insert[11]] + attributeValue4.Value = insert[11] + attributeValueList = append(attributeValueList, attributeValue4) + } + */ + materialList = append(materialList, material) } + //淇濆瓨鍔ㄦ�佸睘鎬� + for _, attributesColumn := range attributesColumns { + attributeValueList = append(attributeValueList, &models.AttributeValue{ + EntityID: material.ID, + //AttributeID: mapAttribute[insert[attributesColumn]], + AttributeID: mapAttribute[headers[attributesColumn]], + Value: insert[attributesColumn], + }) + } + + } + if len(materialList) == 0 { + return 0, errors.New("鐗╂枡鏁版嵁涓虹┖") } err = models.WithTransaction(func(db *gorm.DB) error { if err := models.NewMaterialSearch().SetOrm(db).CreateBatch(materialList); err != nil { return err } - if err := models.NewAttributeValueSearch().SetOrm(db).CreateBatch(attributeValueList); err != nil { - return err + if len(attributeValueList) != 0 { + if err := models.NewAttributeValueSearch().SetOrm(db).CreateBatch(attributeValueList); err != nil { + return err + } } return nil }) -- Gitblit v1.8.0