...
|
...
|
@@ -210,6 +210,101 @@ func (srv *PullDataK3CloudService) PullMaterial(timeFilter time.Time) error { |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (srv *PullDataK3CloudService) pullMaterial(timeFilter time.Time) ([]models.MaterialK3cloud, error) {
|
|
|
//拉取数据
|
|
|
var filterString []string
|
|
|
if !timeFilter.IsZero() {
|
|
|
str := timeFilter.Format("2006-01-02T15:04:05")
|
|
|
filterString = append(filterString, "FModifyDate>='"+str+"'")
|
|
|
}
|
|
|
client, err := newK3cloudClient()
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
fieldKeys := []string{
|
|
|
"FMATERIALID", "FSpecification", "FName", "FNumber",
|
|
|
"FDocumentStatus", "FForbidStatus", "FErpClsID",
|
|
|
"FBaseUnitId", "FBaseUnitId.FName", "FCreateDate", "FModifyDate",
|
|
|
"FForbidDate", "FApproveDate", "FMaterialGroup", "FMaterialGroup.FName",
|
|
|
"FRefStatus", "FMaterialGroup.FNumber", "FUseOrgId", "FUseOrgId.FName", "FCategoryID",
|
|
|
}
|
|
|
var (
|
|
|
startRow int
|
|
|
allResult []map[string]string
|
|
|
queryErr error
|
|
|
)
|
|
|
for {
|
|
|
result, err := client.ExecuteBillQuery(k3cloud.RequestExecuteBillQuery{
|
|
|
FormId: "BD_MATERIAL",
|
|
|
Data: k3cloud.ExecuteBillQueryData{
|
|
|
FormId: "BD_MATERIAL",
|
|
|
FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
|
|
|
StartRow: startRow,
|
|
|
Limit: 1000,
|
|
|
FilterString: strings.Join(filterString, " and "),
|
|
|
},
|
|
|
})
|
|
|
if err != nil {
|
|
|
queryErr = err
|
|
|
break
|
|
|
}
|
|
|
mp := result.ToMapString()
|
|
|
if len(mp) == 0 {
|
|
|
break
|
|
|
}
|
|
|
allResult = append(allResult, mp...)
|
|
|
startRow += 1000
|
|
|
}
|
|
|
if queryErr != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
var (
|
|
|
materialModels []models.MaterialK3cloud
|
|
|
materialTemp models.MaterialK3cloud
|
|
|
)
|
|
|
nowTime := time.Now()
|
|
|
for _, item := range allResult {
|
|
|
materialId, err := strconv.Atoi(item["FMATERIALID"])
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
fErpClsID, _ := strconv.Atoi(item["FErpClsID"])
|
|
|
fBaseUnitId, _ := strconv.Atoi(item["FBaseUnitId"])
|
|
|
materialGroup, _ := strconv.Atoi(item["MaterialGroup"])
|
|
|
fUseOrgId, _ := strconv.Atoi(item["FUseOrgId"])
|
|
|
refStatus, _ := strconv.Atoi(item["RefStatus"])
|
|
|
fCreateDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FCreateDate"])
|
|
|
fModifyDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FModifyDate"])
|
|
|
fForbidDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FForbidDate"])
|
|
|
fApproveDate, _ := time.Parse("2006-01-02T15:04:05.999", item["FApproveDate"])
|
|
|
materialTemp = models.MaterialK3cloud{
|
|
|
MaterialId: materialId,
|
|
|
Name: item["FName"],
|
|
|
Number: item["FNumber"],
|
|
|
Specification: item["FSpecification"],
|
|
|
ForbidStatus: item["FForbidStatus"],
|
|
|
ErpClsId: fErpClsID,
|
|
|
BaseUnitId: fBaseUnitId,
|
|
|
BaseUnitName: item["FBaseUnitId.FName"],
|
|
|
CreateDate: fCreateDate,
|
|
|
ModifyDate: fModifyDate,
|
|
|
ForbidDate: fForbidDate,
|
|
|
ApproveDate: fApproveDate,
|
|
|
MaterialGroup: materialGroup,
|
|
|
MaterialGroupNumber: item["FMaterialGroup.FNumber"],
|
|
|
MaterialGroupName: item["FMaterialGroup.FName"],
|
|
|
RefStatus: refStatus,
|
|
|
UseOrgId: fUseOrgId,
|
|
|
UseOrgName: item["FUseOrgId.FName"],
|
|
|
JoinProductId: 0,
|
|
|
DataVersion: nowTime.Unix(),
|
|
|
FCategoryID: item["FCategoryID"],
|
|
|
}
|
|
|
materialModels = append(materialModels, materialTemp)
|
|
|
}
|
|
|
return materialModels, nil
|
|
|
}
|
|
|
|
|
|
//PullMaterialGroup 拉取物料分组
|
|
|
func (srv *PullDataK3CloudService) PullMaterialGroup() error {
|
|
|
client, err := newK3cloudClient()
|
...
|
...
|
@@ -463,6 +558,33 @@ func (srv *PullDataK3CloudService) PullPrdMo(timeFilter time.Time) error { |
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// PullMaterialCategory 拉取物料分类
|
|
|
func (srv *PullDataK3CloudService) PullMaterialCategory() (map[string]string, error) {
|
|
|
client, err := newK3cloudClient()
|
|
|
if err != nil {
|
|
|
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
fieldKeys := []string{
|
|
|
"FName", "FNUMBER", "FMasterId",
|
|
|
}
|
|
|
result, err := client.ExecuteBillQuery(k3cloud.RequestExecuteBillQuery{
|
|
|
FormId: "BD_MATERIALCATEGORY",
|
|
|
Data: k3cloud.ExecuteBillQueryData{
|
|
|
FormId: "BD_MATERIALCATEGORY",
|
|
|
FieldKeys: strings.Join(fieldKeys, ","), //查询的字段
|
|
|
//TopRowCount: 4,
|
|
|
|
|
|
FilterString: "",
|
|
|
},
|
|
|
})
|
|
|
categorys := result.ToMapString()
|
|
|
var categorysById = make(map[string]string)
|
|
|
for _, v := range categorys {
|
|
|
categorysById[v["FMasterId"]] = v["FName"]
|
|
|
}
|
|
|
return categorysById, nil
|
|
|
}
|
|
|
|
|
|
// 同步产品
|
|
|
func (srv *PullDataK3CloudService) SyncDataProduct(ptr *pgTransaction.TransactionContext, fromTime time.Time) error {
|
|
|
prdMoDao, err := dao.NewMaterialK3cloudDao(ptr)
|
...
|
...
|
@@ -683,23 +805,15 @@ func (srv *PullDataK3CloudService) SyncDataMaterialGroup(ptr *pgTransaction.Tran |
|
|
}
|
|
|
|
|
|
// SyncDataMaterial 同步物料数据
|
|
|
func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.TransactionContext, fromTime time.Time) error {
|
|
|
/*
|
|
|
1.获取更新时间
|
|
|
2.获取prd_mo_k3cloud从更新时间开始的有变化的数据
|
|
|
3.查询是否有重复的批次号
|
|
|
4.有进行更新,其他的插入
|
|
|
*/
|
|
|
// pullType 拉取数据方式 full:全量 其他:增量
|
|
|
func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.TransactionContext, pullType string, fromTime time.Time) error {
|
|
|
var (
|
|
|
lastTime = utils.GetZeroTime(time.Now()).Add(-time.Hour * 24) //前一天有修改的记录
|
|
|
org *domain.Org
|
|
|
limit = 500
|
|
|
offset = 0
|
|
|
pageIndex = 0
|
|
|
err error
|
|
|
)
|
|
|
if !fromTime.IsZero() {
|
|
|
lastTime = fromTime
|
|
|
if pullType == "full" {
|
|
|
lastTime = time.Time{}
|
|
|
}
|
|
|
|
|
|
var userService = domainService.NewUserService()
|
...
|
...
|
@@ -710,9 +824,9 @@ func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.Transacti |
|
|
|
|
|
var (
|
|
|
cid = constant.MANUFACTURE_DEFAULT_COMPANYID
|
|
|
oid = constant.MANUFACTURE_DEFAULT_ORGID
|
|
|
productMaterialRepository, _, _ = factory.FastProductMaterial(ptr, 0)
|
|
|
productMaterialGroupRepository, _, _ = factory.FastProductMaterialGroup(ptr, 0)
|
|
|
prdMoK3cloudDao, _ = dao.NewPrdMoK3cloudDao(ptr)
|
|
|
materialGroups domain.ProductMaterialGroups
|
|
|
)
|
|
|
|
...
|
...
|
@@ -721,38 +835,63 @@ func (srv *PullDataK3CloudService) SyncDataMaterial(ptr *pgTransaction.Transacti |
|
|
return err
|
|
|
}
|
|
|
mapMaterialGroup := materialGroups.ToMapByGroupNumber()
|
|
|
for {
|
|
|
offset += pageIndex * limit
|
|
|
pageIndex += 1
|
|
|
|
|
|
records, _, err := prdMoK3cloudDao.GetLatestMaterialData(lastTime, offset, limit)
|
|
|
records, err := srv.pullMaterial(lastTime)
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
for _, v := range records {
|
|
|
mapMaterialCategory, err := srv.PullMaterialCategory()
|
|
|
if err != nil {
|
|
|
return err
|
|
|
}
|
|
|
log.Logger.Debug(fmt.Sprintf("【同步物料数据】查询数据条数:%v", len(records)))
|
|
|
for index, item := range records {
|
|
|
var (
|
|
|
material *domain.ProductMaterial
|
|
|
materialGroup *domain.ProductMaterialGroup
|
|
|
ok bool
|
|
|
attribute string //物料属性
|
|
|
category string //物料类别
|
|
|
)
|
|
|
if materialGroup, ok = mapMaterialGroup[v.MaterialNumber]; !ok {
|
|
|
if materialGroup, ok = mapMaterialGroup[item.MaterialGroupNumber]; !ok {
|
|
|
continue
|
|
|
}
|
|
|
material, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": cid, "materialNumber": v.MaterialNumber})
|
|
|
material, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": cid, "materialNumber": item.Number})
|
|
|
if err == nil && material != nil {
|
|
|
continue
|
|
|
}
|
|
|
if err == domain.ErrorNotFound {
|
|
|
if err != domain.ErrorNotFound {
|
|
|
continue
|
|
|
}
|
|
|
if v, ok := mapMaterialCategory[item.FCategoryID]; ok {
|
|
|
category = v
|
|
|
}
|
|
|
attribute = domain.MaterialAttributeDescription(item.ErpClsId)
|
|
|
|
|
|
material = &domain.ProductMaterial{
|
|
|
CompanyId: cid,
|
|
|
OrgId: oid,
|
|
|
ProductMaterialGroupId: materialGroup.ProductMaterialGroupId,
|
|
|
MaterialName: item.Name,
|
|
|
MaterialNumber: item.Number,
|
|
|
MaterialAttribute: &domain.MaterialAttribute{Attribute: attribute},
|
|
|
MaterialCategory: &domain.MaterialCategory{Category: category},
|
|
|
ProductMaterialExt: &domain.MaterialExt{
|
|
|
Specification: item.Specification,
|
|
|
Unit: item.BaseUnitName,
|
|
|
ExpiredDay: 0,
|
|
|
Remark: "",
|
|
|
Source: "k3cloud",
|
|
|
},
|
|
|
CreatedAt: time.Now(),
|
|
|
UpdatedAt: time.Now(),
|
|
|
Ext: domain.NewExt(org.OrgName),
|
|
|
}
|
|
|
if material, err = productMaterialRepository.Save(material); err != nil {
|
|
|
return err
|
|
|
}
|
|
|
}
|
|
|
if len(records) == 0 || len(records) < limit {
|
|
|
break
|
|
|
}
|
|
|
log.Logger.Debug(fmt.Sprintf("同步 序号:%v 物料数据 id:%v number:%v name:%v", index, material.ProductMaterialId, material.MaterialNumber, material.MaterialName))
|
|
|
}
|
|
|
return nil
|
|
|
} |
...
|
...
|
|