package service import ( "github.com/go-xorm/xorm" "leit.com/leit_seat_aps/common" "leit.com/leit_seat_aps/db" "strings" "time" ) type BL_BomHead struct { Bomid string Materialid string bomheadtab db.Me_bomhead Bomitemlst []BL_BomItem } type BL_BomItem struct { Fmaterialid string // 父物料 Fmatqty float32 // 父物料数量 Cmaterialid string // 子物料 Mattype string // 子物料类型:自制件 / 外购件 Cmatqty float32 // 子物料数量 Leadtime float32 // 前置时间 Timeuom string // 时间单位:SEC MIN HOUR DAY bomitemtab db.Me_bomlst Bomitemlst []BL_BomItem // 子件的下级物料需求 } // 获取物料以秒计算的生产提前期 func (t *BL_BomItem) GetChildLeadTimeInSecond() (lt int) { switch strings.ToUpper(strings.TrimSpace(t.Timeuom)) { case "S": lt = int(t.Leadtime) case "SEC": lt = int(t.Leadtime) case "SECOND": lt = int(t.Leadtime) case "M": lt = int(t.Leadtime * 60) case "MIN": lt = int(t.Leadtime * 60) case "MINUTE": lt = int(t.Leadtime * 60) case "H": lt = int(t.Leadtime * 60 * 60) case "HOUR": lt = int(t.Leadtime * 60 * 60) case "D": lt = int(t.Leadtime * 60 * 60 * 24) case "DAY": lt = int(t.Leadtime * 60 * 60 * 24) default: lt = int(t.Leadtime) } return } // 计算自身需求,并向下继续调用 func (bh *BL_BomHead) CalLowerLevelDemand(session *xorm.Session, adtabInlst []db.Pln_material_demand, matdict map[string]db.Pln_material) (err error) { var ( i, j int mattab db.Pln_material adtab db.Pln_material_demand adtabLowlst []db.Pln_material_demand t time.Time ok bool ) adtabLowlst = []db.Pln_material_demand{} for i = 0; i < len(adtabInlst); i++ { for j = 0; j < len(bh.Bomitemlst); j++ { if bh.Bomitemlst[j].Fmaterialid == adtabInlst[i].Materialid { // 判断子物料是否存在 if mattab, ok = matdict[bh.Bomitemlst[i].Cmaterialid]; !ok { continue } if bh.Bomitemlst[i].Fmatqty <= 0 || bh.Bomitemlst[i].Cmatqty <= 0 { continue } // 存在则计算独立需求 adtab = db.Pln_material_demand{} adtab.Finr = db.G_FINR adtab.Materialid = mattab.Materialid adtab.Workordernr = adtabInlst[i].Workordernr adtab.Demandqty = adtabInlst[i].Demandqty * int(bh.Bomitemlst[j].Cmatqty/bh.Bomitemlst[j].Fmatqty) // 获取物料的需求时间(扣除前置期) loc, _ := time.LoadLocation("UTC") t, _ = common.DateParse(adtabInlst[i].Swet, "YmdHis") t = t.In(loc).Add(time.Duration(bh.Bomitemlst[j].GetChildLeadTimeInSecond()) * time.Second * -1) if t.Unix() < time.Now().Unix() { t = time.Now() } adtab.Swet = common.Date(t.Unix(), "YYYYMMDDHHmmss") adtab.Demandyear = t.Year() adtab.Demandmonth = int(t.Month()) _, adtab.Demandweek = t.ISOWeek() adtab.Demandday = t.Day() adtab.Demandhour = t.Hour() adtab.Usestatus = 0 // 默认未使用 adtab.Credatuz = common.Date(time.Now().Unix(), "YYYYMMDDHHmmss") adtab.Lastmodif = common.Date(time.Now().Unix(), "YYYYMMDDHHmmss") adtab.Lastuser = "service" // 插入需求 if err = adtab.InsertAndUpdate(session); err != nil { return } // 添加到循环遍历 adtabLowlst = append(adtabLowlst, adtab) } } } if len(adtabLowlst) > 0 { if err = bh.CalLowerLevelDemand(session, adtabLowlst, matdict); err != nil { return } } return }