From c61b44b474a0e434a54f5b6ca67aa7088fffd6e4 Mon Sep 17 00:00:00 2001 From: zhangxin Date: Mon, 20 Dec 2021 16:25:34 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=BC=E5=87=BAmps?= =?UTF-8?q?=E8=AE=A1=E5=88=92=E5=8A=9F=E8=83=BD=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/ap/ap.go | 35 +- services/ap/implments/Balance.service.impl.go | 713 +++++++++++------- 2 files changed, 476 insertions(+), 272 deletions(-) diff --git a/models/ap/ap.go b/models/ap/ap.go index 4aac1b8..09b5f70 100644 --- a/models/ap/ap.go +++ b/models/ap/ap.go @@ -189,13 +189,13 @@ type ReviewInfo struct { } type MpsPlan struct { - ArticleResourceMpsGroupLst []ArticleResourceMpsGroup `json:"ArticleResourceMpsGroupLst"` //资源分组信息 - ArticleSecondResourceMpsGroupLst [][]ArticleResourceMpsGroup `json:"ArticleSecondResourceMpsGroupLst"` //资源分组信息 + ArticleResourceMpsGroupLst []ArticleResourceMpsGroup `json:"ArticleResourceMpsGroupLst"` //资源分组信息 + ArticleSecondResourceMpsGroupLst [][]ArticleResourceMpsGroup `json:"ArticleSecondResourceMpsGroupLst"` //资源分组信息 ArticleAlternativeResourceMpsGroupLst [][]ArticleResourceMpsGroup `json:"ArticleAlternativeResourceMpsGroupLst"` //资源分组信息 - ArticleDemandLst []ArticleDemand `json:"ArticleDemandLst"` //需求分组信息 - ArticleSecondDemandLst [][]ArticleDemand `json:"ArticleSecondDemandLst"` //需求分组信息 - ArticleAlternativeDemandLst [][]ArticleDemand `json:"ArticleAlternativeDemandLst"` //替代资源需求分组信息 - CustArtStockItem CustArtStockItem `json:"CustArtStockItem"` + ArticleDemandLst []ArticleDemand `json:"ArticleDemandLst"` //需求分组信息 + ArticleSecondDemandLst [][]ArticleDemand `json:"ArticleSecondDemandLst"` //需求分组信息 + ArticleAlternativeDemandLst [][]ArticleDemand `json:"ArticleAlternativeDemandLst"` //替代资源需求分组信息 + CustArtStockItem CustArtStockItem `json:"CustArtStockItem"` } type MpsPlan_DemandList struct { @@ -226,8 +226,27 @@ type ArtStockResourceData struct { ArticleSecondaryResourceLi []ArticleSecondaryResource `json:"ArtStockResourceData-ArticleSecondaryResourceLi"` ArticleAlternativeResourceLi []ArticleAlternativeResource `json:"ArtStockResourceData-ArticleAlternativeResourceLi"` } + // ArtStockAndResource 物料库存和主要资源连表查询 type ArtStockAndResource struct { - ArticleStockLevel ArticleStockLevel `xorm:"extends"` - ArticleResource ArticleResource `xorm:"extends"` + ArticleStockLevel ArticleStockLevel `xorm:"extends"` + ArticleResource ArticleResource `xorm:"extends"` +} + +// ArticleDemandData 要导出的物料需求数据 +type ArticleDemandData struct { + ArtId string + Desc string + OpeningInventory float64 + OutSourcingQty float64 + IndependentDemandQty float64 + WipQty float64 + DemandQtyLi []QtyData + ProductQtyLi []QtyData +} + +// QtyData 数量数据 +type QtyData struct { + DemandKey string + Qty float64 } diff --git a/services/ap/implments/Balance.service.impl.go b/services/ap/implments/Balance.service.impl.go index 182e9d5..5a54ce3 100644 --- a/services/ap/implments/Balance.service.impl.go +++ b/services/ap/implments/Balance.service.impl.go @@ -3,7 +3,7 @@ package implments import ( "encoding/json" "fmt" - "github.com/360EntSecGroup-Skylar/excelize" + "github.com/360EntSecGroup-Skylar/excelize/v2" dal "leit.com/LAPP_CHEERSSON_BACKEND/dao/ap" dal_base "leit.com/LAPP_CHEERSSON_BACKEND/dao/base" "leit.com/LAPP_CHEERSSON_BACKEND/db" @@ -20,6 +20,7 @@ import ( "reflect" "sort" "strconv" + "strings" "time" ) @@ -63,7 +64,7 @@ func NewBalanceServiceImplement() *BalanceServiceImplement { return &BalanceServiceImplement{} } -func (impl *BalanceServiceImplement) GetCustomerArticleDemand(user *global.User, ArtId,CustomerId,Planner string) (*model.CustArtStockItem, error) { +func (impl *BalanceServiceImplement) GetCustomerArticleDemand(user *global.User, ArtId, CustomerId, Planner string) (*model.CustArtStockItem, error) { grmi.Log(user, "/services/ap/implments/ArticleDemand.service.impl.go", "GetCustomerArticleDemand", "GetCustomerArticleDemand") engine := db.Eloquent.Master() @@ -94,50 +95,48 @@ func (impl *BalanceServiceImplement) GetCustomerArticleDemand(user *global.User, // return nil, grmi.NewDataBaseError(err) //} - var custArtlst1,custArtlst2,custArtlst3 []model.CustArtlst - var CustomerList []interface{} - if CustomerId!=""{ + var custArtlst1, custArtlst2, custArtlst3 []model.CustArtlst + var CustomerList []interface{} + if CustomerId != "" { customerInfo, err := dao_Customer.SelectOne(CustomerId) if err != nil { return nil, grmi.NewDataBaseError(err) } - if customerInfo == nil{ + if customerInfo == nil { return nil, grmi.NewBusinessError("未找到该用户数据") } CustomerList = append(CustomerList, CustomerId) - custArtlst1, err = dao_CustArtlst.Select([]grmi.Predicate{meta.CustArtlst_CustomerId.NewPredicate(grmi.Include, CustomerList...)}, nil) - }else{ + } else { custArtlst1 = alldata_CustArtlst } - if ArtId!=""{ + if ArtId != "" { custArtlst2, err = dao_CustArtlst.Select([]grmi.Predicate{meta.CustArtlst_ArtId.NewPredicate(grmi.Equal, ArtId)}, nil) - }else{ + } else { custArtlst2 = alldata_CustArtlst } - if Planner!=""{ + if Planner != "" { - }else{ + } else { custArtlst3 = alldata_CustArtlst } var artList []interface{} - for _, info1 := range custArtlst1{ - for _, info2 := range custArtlst2{ - for _, info3 := range custArtlst3{ - if info1.ArtId == info2.ArtId && info2.ArtId == info3.ArtId{ + for _, info1 := range custArtlst1 { + for _, info2 := range custArtlst2 { + for _, info3 := range custArtlst3 { + if info1.ArtId == info2.ArtId && info2.ArtId == info3.ArtId { artList = append(artList, info1.ArtId) } } } } - artStockList, err := dao_ArticleStock.Select([]grmi.Predicate{meta_base.ArticleStock_ArtId.NewPredicate(grmi.Include, artList...)}, nil) if err != nil { return nil, grmi.NewDataBaseError(err) @@ -151,23 +150,23 @@ func (impl *BalanceServiceImplement) GetCustomerArticleDemand(user *global.User, data := make([]model.CustArtStockInfo, 0, 10) for _, ArtId := range artList { var artInfo *model_base.Article - for _,info := range alldata_Article{ - if info.ArtId == ArtId.(string){ + for _, info := range alldata_Article { + if info.ArtId == ArtId.(string) { artInfo = &info break } } - if artInfo == nil{ + if artInfo == nil { continue } custArtStockInfo := model.CustArtStockInfo{ //CustomerId: custArtInfo.CustomerId, - ArtId: artInfo.ArtId, - Descr1: artInfo.Descr1, + ArtId: artInfo.ArtId, + Descr1: artInfo.Descr1, Descr2: artInfo.Descr2, } for _, artStockInfo := range artStockList { - if artStockInfo.ArtId == artInfo.ArtId{ + if artStockInfo.ArtId == artInfo.ArtId { switch artStockInfo.StockType { case "FG": custArtStockInfo.FGStock = artStockInfo.FGQty @@ -187,16 +186,16 @@ func (impl *BalanceServiceImplement) GetCustomerArticleDemand(user *global.User, } for index, artStockInfo := range data { - data_ArticleDemand, err := dao_ArticleDemand.Select([]grmi.Predicate{ meta.ArticleDemand_ArtId.NewPredicate(grmi.Equal, artStockInfo.ArtId)}, nil) + data_ArticleDemand, err := dao_ArticleDemand.Select([]grmi.Predicate{meta.ArticleDemand_ArtId.NewPredicate(grmi.Equal, artStockInfo.ArtId)}, nil) if err != nil { return nil, grmi.NewDataBaseError(err) } var rockData []model.ArticleDemand - for index, info := range data_ArticleDemand{ - if info.DemandKey[len(info.DemandKey)-1:len(info.DemandKey)] == "*"{ + for index, info := range data_ArticleDemand { + if info.DemandKey[len(info.DemandKey)-1:len(info.DemandKey)] == "*" { rockData = append(rockData, info) - data_ArticleDemand = append(data_ArticleDemand[:index],data_ArticleDemand[index+1:]...) + data_ArticleDemand = append(data_ArticleDemand[:index], data_ArticleDemand[index+1:]...) } } @@ -216,7 +215,7 @@ func (impl *BalanceServiceImplement) GetCustomerArticleDemand(user *global.User, return nil, grmi.NewDataBaseError(err) } - data_ArticleDemand_new = append(data_ArticleDemand_new, rockData ...) + data_ArticleDemand_new = append(data_ArticleDemand_new, rockData...) data[index].ArticleDemandLst = data_ArticleDemand_new } @@ -444,10 +443,10 @@ func (impl *BalanceServiceImplement) RefreshCustomerArticleDemand(user *global.U } var rockData []model.ArticleDemand - for index, info := range data_ArticleDemand{ - if info.DemandKey[len(info.DemandKey)-1:len(info.DemandKey)] == "*"{ + for index, info := range data_ArticleDemand { + if info.DemandKey[len(info.DemandKey)-1:len(info.DemandKey)] == "*" { rockData = append(rockData, info) - data_ArticleDemand = append(data_ArticleDemand[:index],data_ArticleDemand[index+1:]...) + data_ArticleDemand = append(data_ArticleDemand[:index], data_ArticleDemand[index+1:]...) } } @@ -456,44 +455,44 @@ func (impl *BalanceServiceImplement) RefreshCustomerArticleDemand(user *global.U return nil, nil, grmi.NewDataBaseError(err) } - data_ArticleDemand_new = append(data_ArticleDemand_new, rockData ...) + data_ArticleDemand_new = append(data_ArticleDemand_new, rockData...) data := make([]model.CustArtStockInfo, 0, 10) //for _, custArtInfo := range custArtlst { var artInfo *model_base.Article - for _,info := range alldata_Article{ - if info.ArtId == ArtId{ + for _, info := range alldata_Article { + if info.ArtId == ArtId { artInfo = &info break } } - if artInfo==nil{ + if artInfo == nil { return nil, nil, grmi.NewBusinessError("物料为空") } - custArtStockInfo := model.CustArtStockInfo{ - //CustomerId: custArtInfo.CustomerId, - ArtId: ArtId, - Descr1: artInfo.Descr1, - Descr2: artInfo.Descr2, - } - for _, artStockInfo := range artStockList { - if artStockInfo.ArtId == ArtId { - switch artStockInfo.StockType { - case "FG": - custArtStockInfo.FGStock = artStockInfo.FGQty - case "SFG": - custArtStockInfo.SFGStock = artStockInfo.SFGQty - case "WIP": - custArtStockInfo.WIPStock = artStockInfo.WIPQty - case "PUC": - custArtStockInfo.PUCStock = artStockInfo.PUCQty - case "OSC": - custArtStockInfo.OSCStock = artStockInfo.OSCQty - } - break + custArtStockInfo := model.CustArtStockInfo{ + //CustomerId: custArtInfo.CustomerId, + ArtId: ArtId, + Descr1: artInfo.Descr1, + Descr2: artInfo.Descr2, + } + for _, artStockInfo := range artStockList { + if artStockInfo.ArtId == ArtId { + switch artStockInfo.StockType { + case "FG": + custArtStockInfo.FGStock = artStockInfo.FGQty + case "SFG": + custArtStockInfo.SFGStock = artStockInfo.SFGQty + case "WIP": + custArtStockInfo.WIPStock = artStockInfo.WIPQty + case "PUC": + custArtStockInfo.PUCStock = artStockInfo.PUCQty + case "OSC": + custArtStockInfo.OSCStock = artStockInfo.OSCQty } + break } - data = append(data, custArtStockInfo) + } + data = append(data, custArtStockInfo) //} data[0].ArticleDemandLst = data_ArticleDemand_new @@ -577,20 +576,19 @@ func (impl *BalanceServiceImplement) RefreshCustomerArticleDemand(user *global.U return data_ArticleDemand_new, &CustArtStockaLL, err } -func (impl *BalanceServiceImplement) SaveCustomerArticleDemand(user *global.User, CustomerId, ArtId string, mapList []map[string]string) (*model.MpsPlan, error) { +func (impl *BalanceServiceImplement) SaveCustomerArticleDemand(user *global.User, CustomerId, ArtId string, mapList []map[string]string) (*model.MpsPlan, error) { engine := db.Eloquent.Master() session := engine.NewSession() defer session.Close() var ( - mainselectArticleDemandList []model.ArticleDemand - resouceId string + mainselectArticleDemandList []model.ArticleDemand + resouceId string ) data_ArticleResource := make([]model.ArticleResource, 0, 10) - dao_ArticleResource := dal.NewArticleResourceDAO(session, user.PlantNr, user.UserId) dao_ArticleDemand := dal.NewArticleDemandDAO(session, user.PlantNr, user.UserId) @@ -608,12 +606,12 @@ func (impl *BalanceServiceImplement) SaveCustomerArticleDemand(user *global.User return nil, grmi.NewDataBaseError(err) } - if ArtId!= ""{ + if ArtId != "" { mainArtResourceList, err := dao_ArticleResource.Select([]grmi.Predicate{meta.ArticleResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleResource_ArtId.NewPredicate(grmi.Equal, ArtId)}, nil) if err != nil { return nil, grmi.NewDataBaseError(err) } - if len(mainArtResourceList) > 0{ + if len(mainArtResourceList) > 0 { resouceId = mainArtResourceList[0].ResourceId } } @@ -1049,8 +1047,8 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewOne(user *global.User, q session := engine.NewSession() defer session.Close() var ( - articleDemandListMap map[string][]model.ArticleDemand - mainselectArticleDemandList []model.ArticleDemand + articleDemandListMap map[string][]model.ArticleDemand + mainselectArticleDemandList []model.ArticleDemand ) // 创建dao实例 //custArtDao := dal.NewCustArtlstDAO(session, user.PlantNr, user.UserId) @@ -1103,10 +1101,10 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewOne(user *global.User, q } var rockData []model.ArticleDemand - for index, info := range data_ArticleDemand{ - if info.DemandKey[len(info.DemandKey)-1:len(info.DemandKey)] == "*"{ + for index, info := range data_ArticleDemand { + if info.DemandKey[len(info.DemandKey)-1:len(info.DemandKey)] == "*" { rockData = append(rockData, info) - data_ArticleDemand = append(data_ArticleDemand[:index],data_ArticleDemand[index+1:]...) + data_ArticleDemand = append(data_ArticleDemand[:index], data_ArticleDemand[index+1:]...) } } @@ -1189,22 +1187,22 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewOne(user *global.User, q OrderQty: demandInfo.OrderQty, DemandDate: demandInfo.DemandDate, } - MpsGroupWorkTime,ResourceWorkTime := 0.0,0.0 - for _, groupInfo := range mainmpsGroupLst{ - if groupInfo.DemandKey == demandInfo.DemandKey{ + MpsGroupWorkTime, ResourceWorkTime := 0.0, 0.0 + for _, groupInfo := range mainmpsGroupLst { + if groupInfo.DemandKey == demandInfo.DemandKey { MpsGroupWorkTime = groupInfo.MpsGroupWorkTime ResourceWorkTime = groupInfo.ResourceWorkTime break } } resourceQty := demandInfo.ResourceWorkTime * articleResource.QuantityPerHour - mpsQty := (ResourceWorkTime-MpsGroupWorkTime) * articleResource.QuantityPerHour + mpsQty := (ResourceWorkTime - MpsGroupWorkTime) * articleResource.QuantityPerHour fmt.Println(resourceQty, mpsQty, demandInfo.ForecastDemandQty) demanddate := demandInfo.DemandDate.Restore() if nowDate.Before(demanddate) { fst := demandInfo.ForecastDemandQty - if demandInfo.ForecastDemandQty < demandInfo.OrderQty{ + if demandInfo.ForecastDemandQty < demandInfo.OrderQty { fst = demandInfo.OrderQty } if mpsQty > fst { @@ -1243,22 +1241,22 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewOne(user *global.User, q OrderQty: demandInfo.OrderQty, DemandDate: demandInfo.DemandDate, } - MpsGroupWorkTime,ResourceWorkTime := 0.0,0.0 - for _, groupInfo := range mainmpsGroupLst{ - if groupInfo.DemandKey == demandInfo.DemandKey{ + MpsGroupWorkTime, ResourceWorkTime := 0.0, 0.0 + for _, groupInfo := range mainmpsGroupLst { + if groupInfo.DemandKey == demandInfo.DemandKey { MpsGroupWorkTime = groupInfo.MpsGroupWorkTime ResourceWorkTime = groupInfo.ResourceWorkTime break } } resourceQty := demandInfo.ResourceWorkTime * articleResource.QuantityPerHour - mpsQty := (ResourceWorkTime-MpsGroupWorkTime) * articleResource.QuantityPerHour + mpsQty := (ResourceWorkTime - MpsGroupWorkTime) * articleResource.QuantityPerHour fmt.Println(resourceQty, mpsQty, demandInfo.ForecastDemandQty) demanddate := demandInfo.DemandDate.Restore() if demanddate.Before(expecteddate) { fst := demandInfo.ForecastDemandQty - if demandInfo.ForecastDemandQty < demandInfo.OrderQty{ + if demandInfo.ForecastDemandQty < demandInfo.OrderQty { fst = demandInfo.OrderQty } if mpsQty > fst { @@ -1301,25 +1299,25 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewOne(user *global.User, q } } - MpsGroupWorkTime,ResourceWorkTime := 0.0,0.0 - for _, groupInfo := range mainmpsGroupLst{ - if groupInfo.DemandKey == demandInfo.DemandKey{ + MpsGroupWorkTime, ResourceWorkTime := 0.0, 0.0 + for _, groupInfo := range mainmpsGroupLst { + if groupInfo.DemandKey == demandInfo.DemandKey { MpsGroupWorkTime = groupInfo.MpsGroupWorkTime ResourceWorkTime = groupInfo.ResourceWorkTime break } } resourceQty := demandInfo.ResourceWorkTime * articleResource.QuantityPerHour - mpsQty := (ResourceWorkTime-MpsGroupWorkTime) * articleResource.QuantityPerHour + mpsQty := (ResourceWorkTime - MpsGroupWorkTime) * articleResource.QuantityPerHour fmt.Println(resourceQty, mpsQty, demandInfo.ForecastDemandQty) demanddate := demandInfo.DemandDate.Restore() if demanddate.Before(expecteddate) { fst := demandInfo.ForecastDemandQty - if demandInfo.ForecastDemandQty < demandInfo.OrderQty{ + if demandInfo.ForecastDemandQty < demandInfo.OrderQty { fst = demandInfo.OrderQty } - if mpsQty > fst + reviewInfo.InquiryQty{ + if mpsQty > fst+reviewInfo.InquiryQty { //扣除 moreWorkQty := mpsQty - fst - reviewInfo.InquiryQty if reviewQty-moreWorkQty > 0 { @@ -1346,16 +1344,16 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewOne(user *global.User, q }) nowQty := 0.0 - for _, info := range result.ReviewInfoLst{ + for _, info := range result.ReviewInfoLst { nowQty += info.InquiryQty - if int(nowQty) >= int(reviewQty){ + if int(nowQty) >= int(reviewQty) { result.ReplyDate = &info.DemandDate break } } - if result.ReplyDate!=nil{ + if result.ReplyDate != nil { monday := utils.WeekDayMondayZeroTs(result.ReplyDate.Restore()) - date := monday.AddDate(0,0,custDemandModelInfo.WeekStartDay) + date := monday.AddDate(0, 0, custDemandModelInfo.WeekStartDay) dateGrmi := grmi.Date(date) result.ReplyDate = &dateGrmi } @@ -1366,7 +1364,7 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewOne(user *global.User, q return nil, grmi.NewDataBaseError(err) } - if data_customer != nil && result.ReplyDate != nil{ + if data_customer != nil && result.ReplyDate != nil { switch data_customer.DeliveryFrontTimeType { case "Y": for i := 0; i < data_customer.DeliveryFrontTime; i++ { @@ -1415,7 +1413,10 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewUpload(user *global.User pos += 1 var CustomerOrderReviewInfo model.CustomerOrderReviewInfo - row := rowsIterator.Columns() + row, err := rowsIterator.Columns() + if err != nil { + return nil, err + } for index, colCell := range row { switch index { case 0: @@ -1483,9 +1484,9 @@ func (impl *BalanceServiceImplement) CustomerOrderReviewExport(user *global.User } func (impl *BalanceServiceImplement) refreshMpsArticleDemand(isRefresh bool, data_ArticleStockLevel []model.ArticleStockLevel, data_ArticleDemand []model.ArticleDemand, data_ArticleResource []model.ArticleResource, mpsSetting *model.MpsSetting) ([]model.ArticleDemand, error) { - if data_ArticleDemand == nil || len(data_ArticleDemand) == 0{ + if data_ArticleDemand == nil || len(data_ArticleDemand) == 0 { fmt.Println(data_ArticleDemand) - return nil,nil + return nil, nil } ////1.计算前置期 //lockWeek := 0 @@ -1737,11 +1738,11 @@ func (impl *BalanceServiceImplement) GetMpsPlan(user *global.User, resouceId, se defer session.Close() var ( - articleDemandListMap map[string][]model.ArticleDemand - mainselectArticleDemandList []model.ArticleDemand - secondselectArticleDemandList []model.ArticleDemand + articleDemandListMap map[string][]model.ArticleDemand + mainselectArticleDemandList []model.ArticleDemand + secondselectArticleDemandList []model.ArticleDemand alternativeselectArticleDemandList []model.ArticleDemand - mpsPlan = new(model.MpsPlan) + mpsPlan = new(model.MpsPlan) ) var secondResourceIdList []string @@ -1772,24 +1773,24 @@ func (impl *BalanceServiceImplement) GetMpsPlan(user *global.User, resouceId, se return nil, grmi.NewDataBaseError(err) } - if artId!= ""{ - mainArtResourceList, err := dao_ArticleResource.Select([]grmi.Predicate{meta.ArticleResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleResource_ArtId.NewPredicate(grmi.Equal, artId)}, nil) - if err != nil { - return nil, grmi.NewDataBaseError(err) - } - if len(mainArtResourceList) > 0{ - resouceId = mainArtResourceList[0].ResourceId - } + if artId != "" { + mainArtResourceList, err := dao_ArticleResource.Select([]grmi.Predicate{meta.ArticleResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleResource_ArtId.NewPredicate(grmi.Equal, artId)}, nil) + if err != nil { + return nil, grmi.NewDataBaseError(err) + } + if len(mainArtResourceList) > 0 { + resouceId = mainArtResourceList[0].ResourceId + } - secondArtResourceList, err := dao_ArticleSecondResource.Select([]grmi.Predicate{meta.ArticleSecondaryResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleSecondaryResource_ArtId.NewPredicate(grmi.Equal, artId)}, nil) - if err != nil { - return nil, grmi.NewDataBaseError(err) - } - if len(secondArtResourceList) > 0 { - for _, info := range secondArtResourceList{ - secondResourceIdList = append(secondResourceIdList, info.SecondaryResourceId) - } + secondArtResourceList, err := dao_ArticleSecondResource.Select([]grmi.Predicate{meta.ArticleSecondaryResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleSecondaryResource_ArtId.NewPredicate(grmi.Equal, artId)}, nil) + if err != nil { + return nil, grmi.NewDataBaseError(err) + } + if len(secondArtResourceList) > 0 { + for _, info := range secondArtResourceList { + secondResourceIdList = append(secondResourceIdList, info.SecondaryResourceId) } + } } data_ArticleResource, err = dao_ArticleResource.Select([]grmi.Predicate{meta.ArticleResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleResource_ResourceId.NewPredicate(grmi.Equal, resouceId)}, nil) @@ -1806,12 +1807,11 @@ func (impl *BalanceServiceImplement) GetMpsPlan(user *global.User, resouceId, se return nil, grmi.NewDataBaseError(err) } - var rockData []model.ArticleDemand - for index, info := range data_ArticleDemand{ - if info.DemandKey[len(info.DemandKey)-1:len(info.DemandKey)] == "!"{ + for index, info := range data_ArticleDemand { + if info.DemandKey[len(info.DemandKey)-1:len(info.DemandKey)] == "!" { rockData = append(rockData, info) - data_ArticleDemand = append(data_ArticleDemand[:index],data_ArticleDemand[index+1:]...) + data_ArticleDemand = append(data_ArticleDemand[:index], data_ArticleDemand[index+1:]...) } } @@ -1820,7 +1820,7 @@ func (impl *BalanceServiceImplement) GetMpsPlan(user *global.User, resouceId, se alternativeresourcelist, err := ArticleAlternativeResourceDao.Select([]grmi.Predicate{meta.ArticleAlternativeResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleAlternativeResource_ArtId.NewPredicate(grmi.Equal, artId)}, nil) if err == nil && len(alternativeresourcelist) > 0 { - for _, AlternativeResource := range alternativeresourcelist{ + for _, AlternativeResource := range alternativeresourcelist { data_ArticleAlternativeResource, err := dao_ArticleAlternativeResource.Select([]grmi.Predicate{meta.ArticleAlternativeResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleAlternativeResource_AlternativeResourceId.NewPredicate(grmi.Equal, AlternativeResource.AlternativeResourceId)}, nil) if err != nil { return nil, grmi.NewDataBaseError(err) @@ -1836,7 +1836,7 @@ func (impl *BalanceServiceImplement) GetMpsPlan(user *global.User, resouceId, se } } - alternativeArticleDemandList, mpsGroupLst, plan, err2, done := impl.makeMpsGroup(false,user, AlternativeResource.AlternativeResourceId, secondresourceid, alternativeselectArticleDemandList, data_ArticleStockLevel, articleDemandListMap, dao_Customer, dao_CustDemandModel, data_ArticleResource, mpsSettingOne) + alternativeArticleDemandList, mpsGroupLst, plan, err2, done := impl.makeMpsGroup(false, user, AlternativeResource.AlternativeResourceId, secondresourceid, alternativeselectArticleDemandList, data_ArticleStockLevel, articleDemandListMap, dao_Customer, dao_CustDemandModel, data_ArticleResource, mpsSettingOne) if done { return plan, err2 } @@ -1848,7 +1848,7 @@ func (impl *BalanceServiceImplement) GetMpsPlan(user *global.User, resouceId, se } //主要 - if resouceId!= "" { + if resouceId != "" { for index, ArticleDemandDDataInfo := range data_ArticleDemand { for _, articleResourceInfo := range data_ArticleResource { if articleResourceInfo.ArtId == ArticleDemandDDataInfo.ArtId { @@ -1868,29 +1868,29 @@ func (impl *BalanceServiceImplement) GetMpsPlan(user *global.User, resouceId, se mpsPlan.ArticleDemandLst = mainArticleDemandList } - if secondresourceid!= ""{ - data_ArticleSecondResource, err = dao_ArticleSecondResource.Select([]grmi.Predicate{meta.ArticleSecondaryResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleSecondaryResource_SecondaryResourceId.NewPredicate(grmi.Equal, secondresourceid)}, nil) - if err != nil { - return nil, grmi.NewDataBaseError(err) - } - //筛选物料 - for index, ArticleDemandDDataInfo := range data_ArticleDemand { - for _, articleResourceInfo := range data_ArticleSecondResource { - if articleResourceInfo.ArtId == ArticleDemandDDataInfo.ArtId { - data_ArticleDemand[index].MpsWorkTime = ArticleDemandDDataInfo.NetDemandQty / articleResourceInfo.QuantityPerHour - secondselectArticleDemandList = append(secondselectArticleDemandList, data_ArticleDemand[index]) - break - } + if secondresourceid != "" { + data_ArticleSecondResource, err = dao_ArticleSecondResource.Select([]grmi.Predicate{meta.ArticleSecondaryResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleSecondaryResource_SecondaryResourceId.NewPredicate(grmi.Equal, secondresourceid)}, nil) + if err != nil { + return nil, grmi.NewDataBaseError(err) + } + //筛选物料 + for index, ArticleDemandDDataInfo := range data_ArticleDemand { + for _, articleResourceInfo := range data_ArticleSecondResource { + if articleResourceInfo.ArtId == ArticleDemandDDataInfo.ArtId { + data_ArticleDemand[index].MpsWorkTime = ArticleDemandDDataInfo.NetDemandQty / articleResourceInfo.QuantityPerHour + secondselectArticleDemandList = append(secondselectArticleDemandList, data_ArticleDemand[index]) + break } } - secondArticleDemandList, mpsGroupLst, plan, err2, done := impl.makeMpsGroup(false, user, resouceId, secondresourceid, secondselectArticleDemandList, data_ArticleStockLevel, articleDemandListMap, dao_Customer, dao_CustDemandModel, data_ArticleResource, mpsSettingOne) - if done { - return plan, err2 - } - mpsPlan.ArticleSecondDemandLst = append(mpsPlan.ArticleSecondDemandLst, secondArticleDemandList) - mpsPlan.ArticleSecondResourceMpsGroupLst = append(mpsPlan.ArticleSecondResourceMpsGroupLst, mpsGroupLst) - }else{ - for _, secondresourceid := range secondResourceIdList{ + } + secondArticleDemandList, mpsGroupLst, plan, err2, done := impl.makeMpsGroup(false, user, resouceId, secondresourceid, secondselectArticleDemandList, data_ArticleStockLevel, articleDemandListMap, dao_Customer, dao_CustDemandModel, data_ArticleResource, mpsSettingOne) + if done { + return plan, err2 + } + mpsPlan.ArticleSecondDemandLst = append(mpsPlan.ArticleSecondDemandLst, secondArticleDemandList) + mpsPlan.ArticleSecondResourceMpsGroupLst = append(mpsPlan.ArticleSecondResourceMpsGroupLst, mpsGroupLst) + } else { + for _, secondresourceid := range secondResourceIdList { data_ArticleSecondResource, err = dao_ArticleSecondResource.Select([]grmi.Predicate{meta.ArticleSecondaryResource_PlantNr.NewPredicate(grmi.Equal, user.PlantNr), meta.ArticleSecondaryResource_SecondaryResourceId.NewPredicate(grmi.Equal, secondresourceid)}, nil) if err != nil { return nil, grmi.NewDataBaseError(err) @@ -1987,7 +1987,7 @@ func (impl *BalanceServiceImplement) makeMpsGroup(isrefresh bool, user *global.U Time: finalArticleDemandList[0].DemandDate, TimeType: finalArticleDemandList[0].DemandPeriodType, DemandKey: finalArticleDemandList[0].DemandKey, - Title: resouceId, + Title: resouceId, } workTime, err := impl.getWorkTime(user, resouceId, secondresourceid, mpsGroupInfo.TimeType, mpsGroupInfo.Time) if err != nil { @@ -2011,7 +2011,7 @@ func (impl *BalanceServiceImplement) makeMpsGroup(isrefresh bool, user *global.U Time: selectArticleDemandInfo.DemandDate, TimeType: selectArticleDemandInfo.DemandPeriodType, DemandKey: selectArticleDemandInfo.DemandKey, - Title: resouceId, + Title: resouceId, } workTime, err := impl.getWorkTime(user, resouceId, secondresourceid, mpsGroupInfo.TimeType, mpsGroupInfo.Time) if err != nil { @@ -2037,7 +2037,7 @@ func (impl *BalanceServiceImplement) makeMpsGroup(isrefresh bool, user *global.U return finalArticleDemandList, mpsGroupLst, nil, nil, false } -func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secondresourceid, artId string, MpsSettingNr int,CustomerId string) (*model.MpsPlan, error) { +func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secondresourceid, artId string, MpsSettingNr int, CustomerId string) (*model.MpsPlan, error) { grmi.Log(user, "/services/ap/implments/Balance.service.impl.go", "MpsPlan", "MpsPlan") @@ -2084,14 +2084,13 @@ func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secon return nil, err } - switch mpsSettingOne.MpsRule { case "1": // 有效产能约束 //只在主计划资源上进行,并根据计划资源产能压缩产品的生产量。可以基于不同产品需求的最小覆盖周数由大到小 //排序,找出最大的那个按订货批量压缩,重复以上步骤直到需求和产能达到平衡。在这个过程中如果产品的库 //存水平低于最低库存,系统将停止压缩该产品的生产量。 - plan, err2 := impl.MpsPlanConstraint(mpsPlan, data_ArticleResource, data_ArticleStockLevel,artId) + plan, err2 := impl.MpsPlanConstraint(mpsPlan, data_ArticleResource, data_ArticleStockLevel, artId) if err2 != nil { return plan, err2 } @@ -2106,7 +2105,7 @@ func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secon } case "3": //先约束后适配替代资源模式 - mpsPlan, err2 := impl.MpsPlanConstraint(mpsPlan, data_ArticleResource, data_ArticleStockLevel,artId) + mpsPlan, err2 := impl.MpsPlanConstraint(mpsPlan, data_ArticleResource, data_ArticleStockLevel, artId) if err2 != nil { return mpsPlan, err2 } @@ -2120,14 +2119,13 @@ func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secon if err2 != nil { return mpsPlan, err2 } - mpsPlan, err2 = impl.MpsPlanConstraint(mpsPlan, data_ArticleResource, data_ArticleStockLevel,artId) + mpsPlan, err2 = impl.MpsPlanConstraint(mpsPlan, data_ArticleResource, data_ArticleStockLevel, artId) if err2 != nil { return mpsPlan, err2 } case "5": } - // 统计 for index, mpsGroupInfo := range mpsPlan.ArticleResourceMpsGroupLst { mpsPlan.ArticleResourceMpsGroupLst[index].MpsGroupWorkTime = 0 @@ -2141,7 +2139,7 @@ func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secon mpsPlan.ArticleDemandLst = append(mpsPlan.ArticleDemandLst, groupArticleInfo.ArticleDemandLst...) } - for _, ArticleAlternativeDemand := range mpsPlan.ArticleAlternativeDemandLst{ + for _, ArticleAlternativeDemand := range mpsPlan.ArticleAlternativeDemandLst { for _, info := range ArticleAlternativeDemand { if info.NetDemandQty == 0 { continue @@ -2181,7 +2179,7 @@ func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secon } } - if CustomerId == ""{ + if CustomerId == "" { return mpsPlan, err } @@ -2211,14 +2209,13 @@ func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secon return nil, grmi.NewDataBaseError(err) } - data := make([]model.CustArtStockInfo, 0, 10) for _, custArtInfo := range custArtlst { custArtStockInfo := model.CustArtStockInfo{ //CustomerId: custArtInfo.CustomerId, - ArtId: custArtInfo.ArtId, - Descr1: custArtInfo.Descr1, - Descr2: custArtInfo.Descr2, + ArtId: custArtInfo.ArtId, + Descr1: custArtInfo.Descr1, + Descr2: custArtInfo.Descr2, } for _, artStockInfo := range artStockList { if artStockInfo.ArtId == custArtInfo.ArtId { @@ -2242,8 +2239,8 @@ func (impl *BalanceServiceImplement) MpsPlan(user *global.User, resouceId, secon for index, artStockInfo := range data { var data_ArticleDemand []model.ArticleDemand - for _, info := range mpsPlan.ArticleDemandLst{ - if info.PlantNr == user.PlantNr && info.CustomerId == CustomerId && info.ArtId == artStockInfo.ArtId{ + for _, info := range mpsPlan.ArticleDemandLst { + if info.PlantNr == user.PlantNr && info.CustomerId == CustomerId && info.ArtId == artStockInfo.ArtId { data_ArticleDemand = append(data_ArticleDemand, info) } } @@ -2362,16 +2359,16 @@ func (impl *BalanceServiceImplement) MpsPlanFit(user *global.User, mpsPlan *mode isDelete := false cutWorkTime := float64(mpsGroupInfo.ArticleDemandLst[length-1].MidBatchQuantity) //压缩量 - if cutWorkTime == 0{ + if cutWorkTime == 0 { cutWorkTime = 10 } //替代资源 增加 isEmpty := false AlternativeResourceId := "" - for index, AlternativeResourceMpsInfo := range mpsPlan.ArticleAlternativeResourceMpsGroupLst{ + for index, AlternativeResourceMpsInfo := range mpsPlan.ArticleAlternativeResourceMpsGroupLst { for index2, mpsGroupInfo := range AlternativeResourceMpsInfo { AlternativeOutWorkTime := mpsGroupInfo.MpsGroupWorkTime - mpsGroupInfo.ResourceWorkTime - if AlternativeOutWorkTime > cutWorkTime{ + if AlternativeOutWorkTime > cutWorkTime { mpsPlan.ArticleAlternativeResourceMpsGroupLst[index][index2].ResourceWorkTime += cutWorkTime isEmpty = true AlternativeResourceId = mpsGroupInfo.Title @@ -2380,7 +2377,7 @@ func (impl *BalanceServiceImplement) MpsPlanFit(user *global.User, mpsPlan *mode } } - if !isEmpty{ + if !isEmpty { break } @@ -2460,14 +2457,14 @@ func (impl *BalanceServiceImplement) MpsPlanFit(user *global.User, mpsPlan *mode return mpsPlan, err } -func (impl *BalanceServiceImplement) MpsPlanConstraint(mpsPlan *model.MpsPlan, data_ArticleResource []model.ArticleResource, data_ArticleStockLevel []model.ArticleStockLevel,artId string) (*model.MpsPlan, error) { +func (impl *BalanceServiceImplement) MpsPlanConstraint(mpsPlan *model.MpsPlan, data_ArticleResource []model.ArticleResource, data_ArticleStockLevel []model.ArticleStockLevel, artId string) (*model.MpsPlan, error) { var err error for index, mpsGroupInfo := range mpsPlan.ArticleResourceMpsGroupLst { outWorkTime := 0.0 outWorkTime = mpsGroupInfo.MpsGroupWorkTime - mpsGroupInfo.ResourceWorkTime for { // 过量 - if outWorkTime > 0 && mpsGroupInfo.ArticleDemandLst[0].ArtId == artId{ + if outWorkTime > 0 && mpsGroupInfo.ArticleDemandLst[0].ArtId == artId { length := len(mpsGroupInfo.ArticleDemandLst) if length == 0 { break @@ -2475,7 +2472,7 @@ func (impl *BalanceServiceImplement) MpsPlanConstraint(mpsPlan *model.MpsPlan, d isDelete := false cutWorkTime := float64(mpsGroupInfo.ArticleDemandLst[length-1].MidBatchQuantity) //压缩量 - if cutWorkTime == 0{ + if cutWorkTime == 0 { cutWorkTime = 10 } if mpsGroupInfo.ArticleDemandLst[length-1].MpsWorkTime <= cutWorkTime { @@ -2583,7 +2580,6 @@ func (impl *BalanceServiceImplement) SaveMpsPlan(user *global.User, ArticleDeman err = dao_ArticleDemand.Update(ArticleDemandLst) } - return err } @@ -2595,18 +2591,18 @@ func (impl *BalanceServiceImplement) PublishMpsPlan(user *global.User, artId str dao_PlanOrder := dal.NewPlanOrderDAO(session, user.PlantNr, user.UserId) dao_Independent := dal.NewArticleIndependentDemandDAO(session, user.PlantNr, user.UserId) - PlanOrderVersionList , err := dao_PlanOrderVersion.Select([]grmi.Predicate{},[]grmi.Field{meta.PlanOrderVersion_VersionNr}) + PlanOrderVersionList, err := dao_PlanOrderVersion.Select([]grmi.Predicate{}, []grmi.Field{meta.PlanOrderVersion_VersionNr}) if err != nil { return grmi.NewDataBaseError(err) } - if len(PlanOrderVersionList)> 0{ + if len(PlanOrderVersionList) > 0 { CloseVersionNr := PlanOrderVersionList[len(PlanOrderVersionList)-1].VersionNr planList, err := dao_PlanOrder.Select([]grmi.Predicate{meta.PlanOrder_VersionNr.NewPredicate(grmi.Equal, CloseVersionNr)}, nil) if err != nil { return grmi.NewDataBaseError(err) } for _, info := range planList { - err = utilService.PLN_PLANORDER_Init().Close( user.PlantNr, user.UserId, &info) + err = utilService.PLN_PLANORDER_Init().Close(user.PlantNr, user.UserId, &info) if err != nil { fmt.Println(err) //return grmi.NewDataBaseError(err) @@ -2614,12 +2610,12 @@ func (impl *BalanceServiceImplement) PublishMpsPlan(user *global.User, artId str } } data_PlanOrderVersion := model.PlanOrderVersion{ - PlantNr:user.PlantNr, - Descr :"", - RunTime:grmi.DateTime(time.Now()), + PlantNr: user.PlantNr, + Descr: "", + RunTime: grmi.DateTime(time.Now()), //Operator string `xorm:"nvarchar(40) 'Operator' not null" json:"AP_PlanOrderVersion-Operator"` //MpsSettingNr int `xorm:"int 'MpsSettingNr' not null" json:"AP_PlanOrderVersion-MpsSettingNr"` - Status:20, + Status: 20, } err = dao_PlanOrderVersion.InsertOne(&data_PlanOrderVersion) if err != nil { @@ -2627,47 +2623,47 @@ func (impl *BalanceServiceImplement) PublishMpsPlan(user *global.User, artId str } dao_ArticleDemand := dal.NewArticleDemandDAO(session, user.PlantNr, user.UserId) - data_ArticleDemand, err := dao_ArticleDemand.Select([]grmi.Predicate{ meta.ArticleDemand_ArtId.NewPredicate(grmi.Equal, artId)}, nil) + data_ArticleDemand, err := dao_ArticleDemand.Select([]grmi.Predicate{meta.ArticleDemand_ArtId.NewPredicate(grmi.Equal, artId)}, nil) if err != nil { return grmi.NewDataBaseError(err) } var planOrderList []model.PlanOrder var IndependentDemandQtyList []model.ArticleIndependentDemand - for _, info := range data_ArticleDemand{ - if info.NetDemandQty > 0{ + for _, info := range data_ArticleDemand { + if info.NetDemandQty > 0 { planOrder := model.PlanOrder{ - PlantNr:user.PlantNr, - ArtId:info.ArtId, - VersionNr:data_PlanOrderVersion.VersionNr, - DemandKey:info.DemandKey, - ProjectId:info.ProjectId, - CustomerId:info.CustomerId, + PlantNr: user.PlantNr, + ArtId: info.ArtId, + VersionNr: data_PlanOrderVersion.VersionNr, + DemandKey: info.DemandKey, + ProjectId: info.ProjectId, + CustomerId: info.CustomerId, //Descr - PlanDate:info.DemandDate, - PlanYear:info.DemandYear, - PlanWeek :info.DemandWeek, - PlanMonth :info.DemandMonth, - PlanQty :info.NetDemandQty, + PlanDate: info.DemandDate, + PlanYear: info.DemandYear, + PlanWeek: info.DemandWeek, + PlanMonth: info.DemandMonth, + PlanQty: info.NetDemandQty, //RunTime //Operator string `xorm:"nvarchar(40) 'Operator' not null" json:"AP_PlanOrder-Operator"` //MpsSettingNr int `xorm:"int 'MpsSettingNr' not null" json:"AP_PlanOrder-MpsSettingNr"` } planOrderList = append(planOrderList, planOrder) } - if info.OutSourcingQty > 0{ + if info.OutSourcingQty > 0 { IndependentDemandQty := model.ArticleIndependentDemand{ - PlantNr:user.PlantNr, - ArtId:info.ArtId, - DemandKey:info.DemandKey, - VersionNr:data_PlanOrderVersion.VersionNr, - ProjectId:info.ProjectId, - CustomerId:info.CustomerId, - DemandDate:info.DemandDate, - DemandYear:info.DemandYear, - DemandWeek :info.DemandWeek, - DemandMonth :info.DemandMonth, - DemandQty :info.OutSourcingQty, + PlantNr: user.PlantNr, + ArtId: info.ArtId, + DemandKey: info.DemandKey, + VersionNr: data_PlanOrderVersion.VersionNr, + ProjectId: info.ProjectId, + CustomerId: info.CustomerId, + DemandDate: info.DemandDate, + DemandYear: info.DemandYear, + DemandWeek: info.DemandWeek, + DemandMonth: info.DemandMonth, + DemandQty: info.OutSourcingQty, } IndependentDemandQtyList = append(IndependentDemandQtyList, IndependentDemandQty) } @@ -2738,7 +2734,6 @@ func (impl *BalanceServiceImplement) RefreshData() error { alldata_CustArtlst = make([]model.CustArtlst, 0) err = session.Table("AP_CustArtlst").Find(&alldata_CustArtlst) - model_base.Mem_Customer = make([]model_base.Customer, 0) err = session.Table("Customer").Find(&model_base.Mem_Customer) @@ -2776,12 +2771,12 @@ func (impl *BalanceServiceImplement) RefreshBalanceData(user *global.User) error demandDetailDao := dal.NewCustDemandVerDetailDAO(session, user.PlantNr, user.UserId) // 聚合数据 // 查询需求数据 - demandHeadList, err := dao_CustDemandHead.Select([]grmi.Predicate{},nil) + demandHeadList, err := dao_CustDemandHead.Select([]grmi.Predicate{}, nil) if err != nil { session.Rollback() return grmi.NewBusinessError("查询客户需求数据失败, error:" + err.Error()) } - if len(demandHeadList )== 0 { + if len(demandHeadList) == 0 { session.Rollback() return grmi.NewBusinessError("不存在客户需求数据") } @@ -2793,19 +2788,19 @@ func (impl *BalanceServiceImplement) RefreshBalanceData(user *global.User) error } if len(demandLstLi) == 0 { session.Rollback() - return grmi.NewBusinessError("不存在客户需求版本详细数据") + return grmi.NewBusinessError("不存在客户需求版本详细数据") } var articleMap = make(map[string]int) - for _, info := range demandLstLi{ + for _, info := range demandLstLi { _, isIn := articleMap[info.ArtId] - if !isIn{ + if !isIn { articleMap[info.ArtId] = 1 - }else{ + } else { articleMap[info.ArtId] += 1 } } - for artId, _ := range articleMap{ + for artId, _ := range articleMap { err := dao_ArticleDemand.DeleteWhere([]grmi.Predicate{meta.ArticleDemand_ArtId.NewPredicate(grmi.Equal, artId)}) if err != nil { session.Rollback() @@ -2815,65 +2810,64 @@ func (impl *BalanceServiceImplement) RefreshBalanceData(user *global.User) error timeNow := time.Now() var firstDemandList []model.ArticleDemand - for _, info := range demandLstLi{ + for _, info := range demandLstLi { resourceId := "" MidBatchQuantity := 0 demandWeek := strconv.Itoa(info.DemandWeek) - for _, resource := range alldata_ArticleResource{ - if info.ArtId == resource.ArtId{ + for _, resource := range alldata_ArticleResource { + if info.ArtId == resource.ArtId { resourceId = resource.ResourceId break } } - if resourceId == ""{ - for _, resource := range alldata_ArticleSecondResource{ - if info.ArtId == resource.ArtId{ + if resourceId == "" { + for _, resource := range alldata_ArticleSecondResource { + if info.ArtId == resource.ArtId { resourceId = resource.SecondaryResourceId break } } } - if resourceId == ""{ + if resourceId == "" { continue } lockWeeks_article := 0 - for _, article := range alldata_Article{ - if info.ArtId == article.ArtId{ - MidBatchQuantity= article.MidBatchQuantity + for _, article := range alldata_Article { + if info.ArtId == article.ArtId { + MidBatchQuantity = article.MidBatchQuantity lockWeeks_article = int(article.DeliveryBuffer) break } } - if MidBatchQuantity == 0{ + if MidBatchQuantity == 0 { MidBatchQuantity = 10 } - if len(demandWeek) == 1{ + if len(demandWeek) == 1 { demandWeek = "0" + demandWeek } demandKey := strconv.Itoa(info.DemandYear) + "." + demandWeek + "_" + resourceId - custArticleList, err := dao_CustArticle.Select([]grmi.Predicate{meta.CustArtlst_ArtId.NewPredicate(grmi.Equal, info.ArtId)},nil) + custArticleList, err := dao_CustArticle.Select([]grmi.Predicate{meta.CustArtlst_ArtId.NewPredicate(grmi.Equal, info.ArtId)}, nil) if err != nil { session.Rollback() return grmi.NewBusinessError(" error:" + err.Error()) } - - for _, custArticleInfo := range custArticleList{ + for _, custArticleInfo := range custArticleList { for _, customerInfo := range model_base.Mem_Customer { - if custArticleInfo.CustomerId == customerInfo.CustomerId{ + if custArticleInfo.CustomerId == customerInfo.CustomerId { //物料和客户都设置吸收期,优先取物料,如果物料吸收期<=0则取该物料所属客户的 lockWeeks := 0 - if lockWeeks_article > 0{ + if lockWeeks_article > 0 { lockWeeks = lockWeeks_article - }else{ + } else { lockWeeks = customerInfo.LockWeeks } - MinCoverPeriod,MaxCoverPeriod := 0.0,0.0 + MinCoverPeriod, MaxCoverPeriod := 0.0, 0.0 //基于物料的吸收期 + 目标库存覆盖周数从MDS中抓取需求(包括预测、正式订单和独立订单) for _, articleStockLevelInfo := range alldata_ArticleStockLevel { if articleStockLevelInfo.ArtId == custArticleInfo.ArtId && articleStockLevelInfo.MinCoverPeriodType == "W" { @@ -2884,42 +2878,41 @@ func (impl *BalanceServiceImplement) RefreshBalanceData(user *global.User) error } } - //需求周期 > 当前时间+ mps策略表偏离周数 并且 需求周期 < (当前时间+吸收期+最大覆盖周期) weekTimeNow := utils.WeekDayMondayZeroTs(timeNow) weekTimeDemand := utils.WeekDayMondayZeroTs(info.DemandDate.Restore()) - if weekTimeNow.After(weekTimeDemand){ + if weekTimeNow.After(weekTimeDemand) { continue } - if info.DemandDate.Restore().After(timeNow.Add(time.Duration((int(MaxCoverPeriod)+lockWeeks)*24*7) * time.Hour)){ + if info.DemandDate.Restore().After(timeNow.Add(time.Duration((int(MaxCoverPeriod)+lockWeeks)*24*7) * time.Hour)) { continue } firstArticleDemand := model.ArticleDemand{ - PlantNr :user.PlantNr, - ArtId :custArticleInfo.ArtId, - DemandKey :demandKey, - DemandPeriodType :"W", - CustomerId :custArticleInfo.CustomerId, - DemandDate :info.DemandDate, - DemandYear :info.DemandYear, - DemandWeek :info.DemandWeek, - DemandMonth :info.DemandMonth, - ForecastDemandQty :info.DemandQty,//预测需求 - OrderQty :0,//订单需求 - OutSourcingQty :0,//外购需求 - IndependentDemandQty :0,//独立需求 - WipQty :0, //在制量 - OpeningInventory :0,//期初库存 - TargetInventory :0,//目标库存 - MinInventory :0,//最低库存 - NetDemandQty :0,// 净生产量 mps - EndingInventory :0,//期末库存 - - MidBatchQuantity :MidBatchQuantity, //每次削减量 - LockWeeks: lockWeeks,//前置期 - MinCoverPeriod: MinCoverPeriod, - MaxCoverPeriod: MaxCoverPeriod, + PlantNr: user.PlantNr, + ArtId: custArticleInfo.ArtId, + DemandKey: demandKey, + DemandPeriodType: "W", + CustomerId: custArticleInfo.CustomerId, + DemandDate: info.DemandDate, + DemandYear: info.DemandYear, + DemandWeek: info.DemandWeek, + DemandMonth: info.DemandMonth, + ForecastDemandQty: info.DemandQty, //预测需求 + OrderQty: 0, //订单需求 + OutSourcingQty: 0, //外购需求 + IndependentDemandQty: 0, //独立需求 + WipQty: 0, //在制量 + OpeningInventory: 0, //期初库存 + TargetInventory: 0, //目标库存 + MinInventory: 0, //最低库存 + NetDemandQty: 0, // 净生产量 mps + EndingInventory: 0, //期末库存 + + MidBatchQuantity: MidBatchQuantity, //每次削减量 + LockWeeks: lockWeeks, //前置期 + MinCoverPeriod: MinCoverPeriod, + MaxCoverPeriod: MaxCoverPeriod, MinCoverPeriodType: "W", } firstDemandList = append(firstDemandList, firstArticleDemand) @@ -2930,24 +2923,24 @@ func (impl *BalanceServiceImplement) RefreshBalanceData(user *global.User) error //聚合 keyArtIdMap := make(map[string]map[string]*model.ArticleDemand) - for index, _ := range firstDemandList{ + for index, _ := range firstDemandList { _, isIn := keyArtIdMap[firstDemandList[index].ArtId] - if !isIn{ + if !isIn { keyArtIdMap[firstDemandList[index].ArtId] = make(map[string]*model.ArticleDemand) keyArtIdMap[firstDemandList[index].ArtId][firstDemandList[index].DemandKey] = &firstDemandList[index] - }else{ + } else { _, isIn := keyArtIdMap[firstDemandList[index].ArtId][firstDemandList[index].DemandKey] - if !isIn{ + if !isIn { keyArtIdMap[firstDemandList[index].ArtId][firstDemandList[index].DemandKey] = &firstDemandList[index] - }else{ + } else { value := keyArtIdMap[firstDemandList[index].ArtId][firstDemandList[index].DemandKey] value.ForecastDemandQty += firstDemandList[index].ForecastDemandQty } } } - for _, value1 := range keyArtIdMap{ - for _, value2 := range value1{ + for _, value1 := range keyArtIdMap { + for _, value2 := range value1 { err := dao_ArticleDemand.InsertOne(value2) if err != nil { session.Rollback() @@ -2980,14 +2973,206 @@ func (impl *BalanceServiceImplement) RefreshBalanceData(user *global.User) error return nil } -func (impl *BalanceServiceImplement)GetGlobalArticleData()([]model_base.Article){ +func (impl *BalanceServiceImplement) GetGlobalArticleData() []model_base.Article { return alldata_Article } -func (impl *BalanceServiceImplement)MpsPlanUpload(user *global.User, savePath string) (result *model.CustArtStockItem, err error){ - return nil,nil +func (impl *BalanceServiceImplement) MpsPlanUpload(user *global.User, savePath string) (result *model.CustArtStockItem, err error) { + return nil, nil } -func (impl *BalanceServiceImplement)MpsPlanExport(user *global.User, CustArtStockItem *model.CustArtStockItem) (result string, err error){ - return "",nil -} \ No newline at end of file +func (impl *BalanceServiceImplement) MpsPlanExport(user *global.User, CustArtStockItem *model.CustArtStockItem) (result string, err error) { + engine := db.Eloquent.Master() + session := engine.NewSession() + defer session.Close() + // excel 表头 + titleLi := []string{ + "物料编码", + "物料名称", + "期初库存", + "外购需求", + "独立需求", + "在制量", + "数量", + } + // 通过前端传入的结构体 提取artId 和 demandKey + artIdLi := make([]interface{}, 0) + artIdMap := make(map[string]interface{}) + demandKeyLi := make([]interface{}, 0) + for index, sorKey := range CustArtStockItem.SortKeys { + if index >= 8 { + demandKeyLi = append(demandKeyLi, sorKey) + } + } + for _, itemMap := range CustArtStockItem.ItemLst { + artId, exist := itemMap["AP_ArticleDemand-ArtId"] + if exist { + _, exist = artIdMap[artId] + if !exist { + artIdMap[artId] = nil + artIdLi = append(artIdLi, artId) + } + } + } + if len(demandKeyLi) == 0 { + return "", grmi.NewBusinessError("未获取到需求周数据") + } + if len(artIdLi) == 0 { + return "", grmi.NewBusinessError("未获取到物料数据") + } + daoArticleDemand := dal.NewArticleDemandDAO(session, user.PlantNr, user.UserId) + // 查询需要导出的物料数据 用于聚合物料描述 + daoArticle := dal_base.NewArticleDAO(session, user.PlantNr, user.UserId) + articleLi, err := daoArticle.Select([]grmi.Predicate{meta_base.Article_ArtId.NewPredicate(grmi.Include, artIdLi...)}, nil) + if err != nil { + return "", grmi.NewBusinessError("查询物料数据失败, 错误:" + err.Error()) + } + if len(articleLi) != len(artIdLi) { + return "", grmi.NewBusinessError("需要导出的物料不是都存在") + } + artDescMap := make(map[string]string) + for _, article := range articleLi { + artDescMap[article.ArtId] = article.Descr1 + } + // 通过传入的artIdLi 和 demandKeyLi查询所有的物料需求 + artDemandLi, err := daoArticleDemand.Select([]grmi.Predicate{meta.ArticleDemand_ArtId.NewPredicate(grmi.Include, artIdLi...), meta.ArticleDemand_DemandKey.NewPredicate(grmi.Include, demandKeyLi...)}, + []grmi.Field{meta.ArticleDemand_ArtId, meta.ArticleDemand_DemandKey}) + if err != nil { + return "", grmi.NewBusinessError("查询物料需求数据失败, 错误:" + err.Error()) + } + if len(artDemandLi) == 0 { + return "", grmi.NewBusinessError("未查询到需要到处的需求数据") + } + // 构建map数据 map[artId]map[demandKey]model.ArticleDemand + artOpeningInventoryMap := make(map[string]float64) + artDemandMap := make(map[string]map[string][]model.ArticleDemand) + var currentArtId string + demandKeyTitleMap := make(map[string]interface{}) + for _, artDemand := range artDemandLi { + if currentArtId == "" { + currentArtId = artDemand.ArtId + } + _, exist := artDemandMap[artDemand.ArtId] + if !exist { + artDemandMap[artDemand.ArtId] = make(map[string][]model.ArticleDemand) + artOpeningInventoryMap[artDemand.ArtId] = artDemand.OpeningInventory + } + _, exist = artDemandMap[artDemand.ArtId][artDemand.DemandKey] + if !exist { + _, exist = demandKeyTitleMap[artDemand.DemandKey] + if !exist { + demandKeyTitleMap[artDemand.DemandKey] = nil + titleLi = append(titleLi, artDemand.DemandKey) + } + artDemandMap[artDemand.ArtId][artDemand.DemandKey] = make([]model.ArticleDemand, 0) + } + artDemandMap[artDemand.ArtId][artDemand.DemandKey] = append(artDemandMap[artDemand.ArtId][artDemand.DemandKey], artDemand) + } + + exportLi := make([]model.ArticleDemandData, 0, len(artDemandMap)) + // 通过map生成中间过程数据 每一条是一个要导出的物料需求数据 + for artId, demandKeyMap := range artDemandMap { + var artDemandData model.ArticleDemandData + artDemandData.ArtId = artId + artDemandData.Desc = artDescMap[artId] + artDemandData.OpeningInventory = artOpeningInventoryMap[artId] + for _, demandKey := range demandKeyLi { + artDemandLi, exist := demandKeyMap[demandKey.(string)] + var demandQty model.QtyData + var productQty model.QtyData + demandQty.DemandKey = demandKey.(string) + productQty.DemandKey = demandKey.(string) + if exist { + for _, artDemand := range artDemandLi { + var qty float64 + if artDemand.ForecastDemandQty > artDemand.OrderQty { + qty = artDemand.ForecastDemandQty + } else { + qty = artDemand.OrderQty + } + qty = qty + artDemand.IndependentDemandQty - artDemand.OutSourcingQty + demandQty.Qty += qty + productQty.Qty += artDemand.NetDemandQty + artDemandData.OutSourcingQty += artDemand.OutSourcingQty + artDemandData.OpeningInventory += artDemandData.OpeningInventory + artDemandData.WipQty += artDemand.WipQty + } + } + artDemandData.DemandQtyLi = append(artDemandData.DemandQtyLi, demandQty) + artDemandData.ProductQtyLi = append(artDemandData.ProductQtyLi, productQty) + } + exportLi = append(exportLi, artDemandData) + } + file := excelize.NewFile() + streamWriter, err := file.NewStreamWriter("Sheet1") + if err != nil { + return "", grmi.NewBusinessError("excel创建Sheet页失败, 错误:" + err.Error()) + } + var rowIndex = 1 + for index, item := range exportLi { + fmt.Println("item:", item) + if index == 0 { + row := make([]interface{}, 0, len(titleLi)) + + for _, title := range titleLi { + row = append(row, title) + } + cell, err := excelize.CoordinatesToCellName(1, rowIndex) + rowIndex++ + if err != nil { + return "", grmi.NewBusinessError("创建单元格失败, 错误:" + err.Error()) + } + if err = streamWriter.SetRow(cell, row); err != nil { + return "", grmi.NewBusinessError("写入单元格失败, 错误:" + err.Error()) + } + } + row := make([]interface{}, 0, len(titleLi)) + row = append(row, item.ArtId, item.Desc, item.OpeningInventory, item.OutSourcingQty, item.IndependentDemandQty, item.WipQty, "需求量") + for _, demandQty := range item.DemandQtyLi { + row = append(row, demandQty.Qty) + } + cell, err := excelize.CoordinatesToCellName(1, rowIndex) + if err != nil { + return "", grmi.NewBusinessError("创建单元格失败, 错误:" + err.Error()) + } + fmt.Println("rowIndex:", rowIndex) + fmt.Println("row:", row) + if err = streamWriter.SetRow(cell, row); err != nil { + return "", grmi.NewBusinessError("写入单元格失败, 错误:" + err.Error()) + } + rowIndex++ + row = make([]interface{}, 0, len(titleLi)) + row = append(row, "", "", "", "", "", "", "生产量") + for _, productQty := range item.ProductQtyLi { + row = append(row, productQty.Qty) + } + fmt.Println("rowIndex:", rowIndex) + fmt.Println("row:", row) + cell, err = excelize.CoordinatesToCellName(1, rowIndex) + if err != nil { + return "", grmi.NewBusinessError("创建单元格失败, 错误:" + err.Error()) + } + if err = streamWriter.SetRow(cell, row); err != nil { + return "", grmi.NewBusinessError("写入单元格失败, 错误:" + err.Error()) + } + rowIndex++ + } + if err := streamWriter.Flush(); err != nil { + return "", grmi.NewBusinessError("写入excel数据失败, 错误:" + err.Error()) + } + filename := utils.MakeOrderSn("MPS主生产计划") + ".xlsx" + dirname, err := utils.GetCurrentPath("web/public/uploadxlsx") + if err != nil { + return "", grmi.NewBusinessError("生成文件失败, 错误:" + err.Error()) + } + filepath := dirname + "/" + filename + err = file.SaveAs(filepath) + if err != nil { + return "", err + } + pathLi := strings.Split(filepath, "web\\") + if len(pathLi) != 2 { + return "", grmi.NewBusinessError("导出失败,请重试") + } + return pathLi[1], nil +}