|
@@ -32,11 +32,12 @@ var (
|
|
|
|
|
|
type ContractInfo struct {
|
|
|
PastureBigContract *model.PastureBigcontract // 牧场和合同关联表
|
|
|
- Contract *model.Contract // 合同子表
|
|
|
+ Contract []*model.Contract // 合同子表
|
|
|
Provider *model.Provider // 供应商
|
|
|
Pasture *model.Pasture // 牧场
|
|
|
}
|
|
|
|
|
|
+// GetBigContractData 获取主合同信息
|
|
|
func GetBigContractData() []*model.BigContract {
|
|
|
bigContractData := make([]*model.BigContract, ContractLimit)
|
|
|
if err := s.d.DB.Where("isToSap = ?", 0).Limit(ContractLimit).Find(&bigContractData).Debug().Error; err != nil {
|
|
@@ -47,7 +48,7 @@ func GetBigContractData() []*model.BigContract {
|
|
|
|
|
|
func GetPastureBigContract(contractCode string) ([]*model.PastureBigcontract, error) {
|
|
|
pastureBigContract := make([]*model.PastureBigcontract, 0)
|
|
|
- SQL := fmt.Sprintf("SELECT * FROM pasture_bigcontract WHERE bigcontract_id IN (SELECT id FROM bigcontract WHERE contractCode LIKE '%s%' AND enable = 1", contractCode)
|
|
|
+ SQL := fmt.Sprintf("SELECT * FROM pasture_bigcontract WHERE bigcontract_id IN (SELECT id FROM bigcontract WHERE contractCode LIKE '%s' AND enable = 1)", contractCode)
|
|
|
if err := s.d.DB.Raw(SQL).Find(&pastureBigContract).Error; err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
@@ -74,13 +75,11 @@ func GetContractInfo(bigContract *model.BigContract, pastureBigContract *model.P
|
|
|
|
|
|
// 获取合同子表数据
|
|
|
go func() {
|
|
|
- contract := &model.Contract{}
|
|
|
- if err := s.d.DB.Where("bigId = ?", bigContract.ID).First(contract).Error; err != nil {
|
|
|
+ contract := make([]*model.Contract, 0)
|
|
|
+ if err := s.d.DB.Where("bigId = ?", bigContract.ID).Find(&contract).Error; err != nil {
|
|
|
log.Errorf("AutoContractToASP contract Err:%v,bigContractData:%v", err, bigContract)
|
|
|
}
|
|
|
- if contract.ID > 0 {
|
|
|
- contractInfo.Contract = contract
|
|
|
- }
|
|
|
+ contractInfo.Contract = contract
|
|
|
wg.Done()
|
|
|
}()
|
|
|
|
|
@@ -101,7 +100,6 @@ func GetContractInfo(bigContract *model.BigContract, pastureBigContract *model.P
|
|
|
|
|
|
// AutoContractToASP 合同信息自动同步到ASP
|
|
|
func AutoContractToASP() {
|
|
|
-
|
|
|
bigContractData := GetBigContractData()
|
|
|
for _, bigContract := range bigContractData {
|
|
|
contractCode := bigContract.ContractCode
|
|
@@ -109,54 +107,63 @@ func AutoContractToASP() {
|
|
|
contractCodeSlice := strings.Split(bigContract.ContractCode, "-")
|
|
|
contractCode = contractCodeSlice[0]
|
|
|
}
|
|
|
-
|
|
|
if _, ok := ContractCodeMap[contractCode]; ok {
|
|
|
continue
|
|
|
}
|
|
|
-
|
|
|
pastureBigContractSlice, err := GetPastureBigContract(contractCode)
|
|
|
if err != nil {
|
|
|
log.Errorf("AutoContractToASP pastureBigContractSlice Err:%v", err)
|
|
|
continue
|
|
|
}
|
|
|
-
|
|
|
for _, pastureBigContract := range pastureBigContractSlice {
|
|
|
// 获取合同相关数据集合
|
|
|
contractInfo := GetContractInfo(bigContract, pastureBigContract)
|
|
|
- ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContract)
|
|
|
- contractDataToASP := &http.ContractDataToASP{
|
|
|
- MATNR: contractInfo.Contract.PartCode,
|
|
|
- LIFNR: contractInfo.Provider.SapCode,
|
|
|
- EKORG: contractInfo.Pasture.ParchaseOrganization,
|
|
|
- WERKS: contractInfo.Pasture.FactoryCode,
|
|
|
- NETPR: "",
|
|
|
- WAERS: "CNY",
|
|
|
- BPRME: "",
|
|
|
- PEINH: "",
|
|
|
- MWSKZ: "J0",
|
|
|
- EKGRP: contractInfo.Pasture.PurchasingGroup,
|
|
|
- DATAB: bigContract.StartTime,
|
|
|
- DATBI: bigContract.StopTime,
|
|
|
- LOEKZ: "",
|
|
|
- }
|
|
|
- sapContractReq.DATA = contractDataToASP
|
|
|
- bodyByte, _ := json.Marshal(sapContractReq.DATA)
|
|
|
- rp := &http.OrderResp{}
|
|
|
- if err = s.SyncSap(sapContractReq, rp, bodyByte); err != nil {
|
|
|
- log.Errorf("AutoContractToASP SyncSap Err:%v", err)
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- if rp.Data.Status != "S" {
|
|
|
- log.Errorf("AutoContractToASP SyncSap Data:%v", rp.Data)
|
|
|
+ if len(contractInfo.Contract) == 0 {
|
|
|
continue
|
|
|
}
|
|
|
+ // 发送数据至SAP
|
|
|
+ PushContractDataToSAP(bigContract, contractInfo)
|
|
|
+ ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContract)
|
|
|
}
|
|
|
-
|
|
|
- if err = s.d.DB.Model(bigContract).Where(fmt.Sprintf("like %s", contractCode)).Update("isToSap", 1).Error; err != nil {
|
|
|
+ if err = s.d.DB.Model(bigContract).Where("id = ?", bigContract.ID).Update("isToSap", 1).Error; err != nil {
|
|
|
log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
log.Info("AutoContractToASP Success")
|
|
|
}
|
|
|
+
|
|
|
+// PushContractDataToSAP 同步数据给SAP
|
|
|
+func PushContractDataToSAP(bigContract *model.BigContract, contractInfo *ContractInfo) {
|
|
|
+ for _, contract := range contractInfo.Contract {
|
|
|
+ LOEKZ := ""
|
|
|
+ if contract.Enable == 0 {
|
|
|
+ LOEKZ = "X"
|
|
|
+ }
|
|
|
+ contractDataToASP := &http.ContractDataToASP{
|
|
|
+ MATNR: contract.PartCode,
|
|
|
+ LIFNR: contractInfo.Provider.SapCode,
|
|
|
+ EKORG: contractInfo.Pasture.ParchaseOrganization,
|
|
|
+ WERKS: contractInfo.Pasture.FactoryCode,
|
|
|
+ NETPR: "",
|
|
|
+ WAERS: "CNY",
|
|
|
+ BPRME: "",
|
|
|
+ PEINH: "",
|
|
|
+ MWSKZ: "J0",
|
|
|
+ EKGRP: contractInfo.Pasture.PurchasingGroup,
|
|
|
+ DATAB: bigContract.StartTime,
|
|
|
+ DATBI: bigContract.StopTime,
|
|
|
+ LOEKZ: LOEKZ,
|
|
|
+ }
|
|
|
+ sapContractReq.DATA = contractDataToASP
|
|
|
+ bodyByte, _ := json.Marshal(sapContractReq.DATA)
|
|
|
+ rp := &http.OrderResp{}
|
|
|
+ if err := s.SyncSap(sapContractReq, rp, bodyByte); err != nil {
|
|
|
+ log.Errorf("AutoContractToASP SyncSap Err:%v", err)
|
|
|
+
|
|
|
+ }
|
|
|
+ log.Infof("AutoContractToASP SyncSap Success:%v", rp)
|
|
|
+ if rp.Data.Status != "S" {
|
|
|
+ log.Errorf("AutoContractToASP SyncSap Error Data:%v", rp.Data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|