123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835 |
- package service
- import (
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/logs"
- "github.com/pkg/errors"
- "kpt.xdmy/apiserver/config"
- "strconv"
- "strings"
- "time"
- log "github.com/sirupsen/logrus"
- "kpt.xdmy/apiserver/model"
- "kpt.xdmy/apiserver/model/http"
- )
- const (
- //ContractToAspUrl = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
- ContractToAspDestID = "EQMAN"
- ContractToAspBussTp = "MM018"
- ContractLimit = 10
- )
- var (
- sapContractReq = &http.SapContractReq{
- Dest: &http.Dest{
- DestID: ContractToAspDestID,
- BussTp: ContractToAspBussTp,
- //Url: ContractToAspUrl,
- },
- DATA: []*http.ContractDataToASP{},
- }
- loc, _ = time.LoadLocation("Local")
- )
- type ContractInfo struct {
- PastureBigContract *model.PastureBigcontract // 牧场和合同关联表
- Contract []*model.Contract // 合同子表
- Provider *model.Provider // 供应商
- Pasture *model.Pasture // 牧场
- }
- // GetBigContractData 获取主合同信息
- func GetBigContractData() []*model.BigContract {
- bigContractData := make([]*model.BigContract, 0)
- if err := s.d.DB.Debug().Where("isToSap = ?", 0).Where("SHStatus = ?", 7).Find(&bigContractData).Debug().Error; err != nil {
- log.Errorf("AutoContractToASP Error:%v", err)
- }
- for _, item := range bigContractData {
- if item.SHStatus == 7 && item.SHtype == 3 && item.CGChargedate.UnixNano() > time.Now().AddDate(0, 0, -5).UnixNano() {
- contractCodeSlice := strings.Split(item.ContractCode, "-")
- contractCode := contractCodeSlice[0]
- s.d.DB.Exec(`update contract set isToSap = 0 where enable = 1 and bigid in(select id from bigContract where contractCode like ? )`, "%"+contractCode+"%")
- }
- }
- return bigContractData
- }
- func GetPastureBigContract(contractCode string) ([]*model.PastureBigcontract, error) {
- pastureBigContract := make([]*model.PastureBigcontract, 0)
- SQL := fmt.Sprintf("select t.* from( "+
- " select pastureId as pasture_id ,enable FROM bigcontract WHERE contractCode LIKE '%s' and SHStatus = 7 GROUP BY pastureId "+
- " union all "+
- " SELECT pasture_id, enable FROM pasture_bigcontract WHERE bigcontract_id IN (SELECT id FROM bigcontract WHERE contractCode LIKE '%s' AND enable = 1 and SHStatus = 7 ) )t"+
- " join pasture p on p.id = t.pasture_id "+
- " group by pasture_id", "%"+contractCode+"%", "%"+contractCode+"%")
- if err := s.d.DB.Raw(SQL).Find(&pastureBigContract).Error; err != nil {
- return nil, err
- }
- return pastureBigContract, nil
- }
- // GetContractInfo 获取合同相关信息
- func GetContractInfo(bigContract *model.BigContract, pastureBigContract *model.PastureBigcontract) *ContractInfo {
- //wg := sync.WaitGroup{}
- //wg.Add(3)
- contractInfo := &ContractInfo{}
- // 获取牧场数据合同数据
- //go func() {
- pasture := &model.Pasture{
- ID: pastureBigContract.PastureId,
- }
- if err := s.d.DB.First(pasture).Error; err != nil {
- log.Errorf("AutoContractToASP Pasture Err:%v,bigContractData:%v", err, bigContract)
- }
- contractInfo.Pasture = pasture
- // wg.Done()
- //}()
- // 获取合同子表数据
- //go func() {
- contract := make([]*model.Contract, 0)
- if err := s.d.DB.Debug().Where("bigId = ?", bigContract.ID).Where("isToSap = ? ", 0).Find(&contract).Error; err != nil {
- log.Errorf("AutoContractToASP contract Err:%v,bigContractData:%v", err, bigContract)
- }
- contractInfo.Contract = contract
- // wg.Done()
- //}()
- // 获取供应商数据
- //go func() {
- provider := &model.Provider{
- ID: bigContract.ProviderID,
- }
- if err := s.d.DB.First(provider).Error; err != nil {
- log.Errorf("AutoContractToASP Provider Err:%v,bigContractData:%v", err, bigContract)
- }
- contractInfo.Provider = provider
- // wg.Done()
- //}()
- //wg.Wait()
- return contractInfo
- }
- // http://192.168.61.117/SAPQ0/SRM/MM018/PurchaseInfo
- // AutoContractToASP 合同信息自动同步到ASP
- func AutoContractToASP() {
- sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
- bigContractData := GetBigContractData()
- ContractCodeMap := make(map[string][]*model.PastureBigcontract, 0)
- errDataList := make(map[string][]*model.ContractSapErr, 0)
- errBigContract := make([]*model.BigContract, 0)
- errContract := make([]*model.Contract, 0)
- for _, bigContract := range bigContractData {
- contractCode := bigContract.ContractCode
- if bigContract.SHStatus != 7 {
- continue
- }
- var count int64
- s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Where("isToSap = ?", 0).Count(&count)
- if count == 0 {
- continue
- }
- if strings.Contains(bigContract.ContractCode, "-") {
- contractCodeSlice := strings.Split(bigContract.ContractCode, "-")
- contractCode = contractCodeSlice[0]
- }
- var err error
- pastureBigContractSlice := make([]*model.PastureBigcontract, 0)
- if _, ok := ContractCodeMap[contractCode]; !ok {
- pastureBigContractSlice, err = GetPastureBigContract(contractCode)
- if err != nil {
- log.Errorf("AutoContractToASP pastureBigContractSlice Err:%v", err)
- continue
- }
- ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContractSlice...)
- //if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("contractCode like ? ", "%"+contractCode+"%").Update("isToSap", 1).Error; err != nil {
- // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- //}
- }
- if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Update("isToSap", 1).Error; err != nil {
- log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- }
- if _, ok := ContractCodeMap[contractCode]; ok {
- contractInfoList := make([]*ContractInfo, 0)
- for _, pastureBigContract := range ContractCodeMap[contractCode] {
- // 获取合同相关数据集合
- contractInfo := GetContractInfo(bigContract, pastureBigContract)
- if len(contractInfo.Contract) == 0 || pastureBigContract.PastureId == 18 {
- continue
- }
- // 发送数据至SAP
- errData, bigContracts, contracts := PushContractDataToSAP(bigContract, contractInfo, contractCode, false)
- for _, data := range errData {
- errDataList[contractCode] = append(errDataList[contractCode], data...)
- }
- contractInfoList = append(contractInfoList, contractInfo)
- errBigContract = append(errBigContract, bigContracts...)
- errContract = append(errContract, contracts...)
- }
- var idList []int
- for _, contractInfo := range contractInfoList {
- for _, c := range contractInfo.Contract {
- if c.IsToSap == 0 {
- exist := false
- for _, id := range idList {
- if id == c.ID {
- exist = true
- break
- }
- }
- if !exist {
- idList = append(idList, c.ID)
- if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 1).Error; err != nil {
- log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- }
- }
- }
- }
- }
- }
- }
- now := time.Now()
- for _, contractSapErr := range errDataList {
- for _, c := range contractSapErr {
- c.CreateTime = now
- s.d.DB.Create(&c)
- }
- }
- for _, item := range errBigContract {
- if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", item.ID).Update("isToSap", 0).Error; err != nil {
- log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- }
- }
- for _, c := range errContract {
- if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 0).Error; err != nil {
- log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- }
- }
- log.Info("AutoContractToASP Success")
- }
- // PushContractDataToSAP 同步数据给SAP
- func PushContractDataToSAP(bigContract *model.BigContract, contractInfo *ContractInfo, contractCode string, hand bool) (map[string][]*model.ContractSapErr, []*model.BigContract, []*model.Contract) {
- errMap := make(map[string][]*model.ContractSapErr, 0)
- errBigContract := make([]*model.BigContract, 0)
- errContract := make([]*model.Contract, 0)
- contractList := make([]*model.Contract, 0)
- i := 0
- for n, contract := range contractInfo.Contract {
- i++
- if !hand {
- if contract.IsToSap == 1 {
- continue
- }
- }
- LOEKZ := ""
- if contract.Enable == 0 {
- LOEKZ = "X"
- }
- startTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StartTime, loc)
- stopTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StopTime, loc)
- astr := strconv.FormatFloat(contract.Price, 'f', -1, 64)
- b := strings.Index(astr, ".")
- per := ""
- if len(astr[b+1:]) > 2 && b > 0 {
- num, _ := strconv.ParseFloat(astr[b+1:], 64)
- if num > 0 {
- contract.Price = contract.Price * 1000
- per = "1000"
- }
- }
- var isZeroStock int
- if bigContract.IsZeroStock == 1 {
- isZeroStock = 2
- } else {
- isZeroStock = 0
- }
- contractDataToASP := &http.ContractDataToASP{
- MATNR: contract.PartCode,
- LIFNR: contractInfo.Provider.SapCode,
- EKORG: contractInfo.Pasture.ParchaseOrganization,
- WERKS: contractInfo.Pasture.FactoryCode,
- WAERS: "CNY",
- PEINH: per,
- NETPR: fmt.Sprintf("%.2f", contract.Price),
- MWSKZ: "J0",
- EKGRP: contractInfo.Pasture.PurchasingGroup,
- DATAB: startTime.Format("20060102"),
- DATBI: stopTime.Format("20060102"),
- LOEKZ: LOEKZ,
- ESOKZ: fmt.Sprintf("%d", isZeroStock),
- MEINS: contract.Unit,
- Code: bigContract.ContractCode,
- RCKEY: fmt.Sprintf("%d", i+1),
- }
- contractList = append(contractList, contract)
- sapContractReq.DATA = append(sapContractReq.DATA, contractDataToASP)
- fmt.Println(n, len(contractInfo.Contract))
- if i == 300 || n+1 == len(contractInfo.Contract) {
- i = 0
- rp := &http.SapContractResp{}
- rbyte, _ := json.Marshal(sapContractReq)
- fmt.Println(string(rbyte))
- if err := s.SyncSap(sapContractReq, rp, sapContractReq); err != nil {
- log.Errorf("AutoContractToASP SyncSap Err:%v ", err)
- //continue
- }
- log.Info(rp)
- if rp.Dest.Status != "S" {
- log.Errorf("AutoContractToASP SyncSap Error:%v", rp)
- for _, contract1 := range contractList {
- contract1.PastureID = contractInfo.Pasture.ID
- errMap[contractCode] = append(errMap[contractCode], &model.ContractSapErr{
- ContractID: contract1.ID,
- PastureID: contractInfo.Pasture.ID,
- PastureName: contractInfo.Pasture.Name,
- PartID: contract1.PartID,
- PartName: contract1.PartName,
- PartCode: contract1.PartCode,
- Specification: contract1.Specification,
- Price: contract1.Price,
- BrandID: contract1.BrandID,
- Brand: contract1.Brand,
- InventoryType: contract1.InventoryType,
- PlanAmount: contract1.PlanAmount,
- Remark: contract1.Remark,
- Enable: contract1.Enable,
- Unit: contract1.Unit,
- IsZeroStock: contract1.IsZeroStock,
- ChangeID: contract1.ChangeID,
- SapText: rp.MessText,
- ContractCode: contractCode,
- })
- if rp.Dest.Status == "" {
- errContract = append(errContract, contract1)
- }
- }
- }
- if len(errContract) > 0 {
- errBigContract = append(errBigContract, bigContract)
- }
- contractList = make([]*model.Contract, 0)
- sapContractReq.DATA = make([]*http.ContractDataToASP, 0)
- }
- //break
- }
- return errMap, errBigContract, errContract
- }
- func RemoveRepeatedElement1(arr []string) (newArr []string) {
- newArr = make([]string, 0)
- for i := 0; i < len(arr); i++ {
- repeat := false
- for j := i + 1; j < len(arr); j++ {
- if arr[i] == arr[j] {
- repeat = true
- break
- }
- }
- if !repeat {
- newArr = append(newArr, arr[i])
- }
- }
- return
- }
- func (s *Service) PostPushContractDataToSAP(pastureId, providerId int64, contractIdList []int64) error {
- sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
- contractList := make([]*model.Contract, 0)
- err := s.d.DB.Debug().Where(" id in (?) ", contractIdList).Find(&contractList).Error
- if err != nil {
- logs.Error(err)
- return err
- }
- pasture := new(model.Pasture)
- err = s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
- if err != nil {
- logs.Error(err)
- return err
- }
- provider := new(model.Provider)
- err = s.d.DB.Where("id = ? ", providerId).First(&provider).Error
- if err != nil {
- logs.Error(err)
- return err
- }
- contractMap := make(map[int][]*model.Contract, 0)
- for _, contract := range contractList {
- contractMap[contract.BigID] = append(contractMap[contract.BigID], contract)
- }
- pastureBigcontractList := make([]*model.PastureBigcontract, 0)
- for bigId, m := range contractMap {
- bigContract := new(model.BigContract)
- err := s.d.DB.Where(" id = ? ", bigId).First(&bigContract).Error
- if err != nil {
- logs.Error(err)
- return err
- }
- if len(pastureBigcontractList) == 0 {
- contractCode := strings.Split(bigContract.ContractCode, "-")[0]
- pastureBigcontractList, _ = GetPastureBigContract(contractCode)
- }
- exist := false
- for _, item := range pastureBigcontractList {
- if item.PastureId == int(pastureId) {
- exist = true
- break
- }
- }
- if !exist {
- return errors.New("牧场没有该合同!")
- }
- contractInfo := new(ContractInfo)
- contractInfo.Contract = m
- contractInfo.Provider = provider
- contractInfo.Pasture = pasture
- fmt.Println(contractInfo)
- PushContractDataToSAP(bigContract, contractInfo, bigContract.ContractCode, true)
- }
- return nil
- }
- //
- //package service
- //
- //import (
- // "encoding/json"
- // "fmt"
- // "github.com/astaxie/beego/logs"
- // "github.com/pkg/errors"
- // "kpt.xdmy/apiserver/config"
- // "strconv"
- // "strings"
- // "time"
- //
- // log "github.com/sirupsen/logrus"
- // "kpt.xdmy/apiserver/model"
- // "kpt.xdmy/apiserver/model/http"
- //)
- //
- //const (
- // //ContractToAspUrl = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
- // ContractToAspDestID = "EQMAN"
- // ContractToAspBussTp = "MM018"
- // ContractLimit = 10
- //)
- //
- //var (
- // sapContractReq = &http.SapContractReq{
- // Dest: &http.Dest{
- // DestID: ContractToAspDestID,
- // BussTp: ContractToAspBussTp,
- // //Url: ContractToAspUrl,
- // },
- // DATA: &http.ContractDataToASP{},
- // }
- // loc, _ = time.LoadLocation("Local")
- //)
- //
- //type ContractInfo struct {
- // PastureBigContract *model.PastureBigcontract // 牧场和合同关联表
- // Contract []*model.Contract // 合同子表
- // Provider *model.Provider // 供应商
- // Pasture *model.Pasture // 牧场
- //}
- //
- //// GetBigContractData 获取主合同信息
- //func GetBigContractData() []*model.BigContract {
- // bigContractData := make([]*model.BigContract, 0)
- // if err := s.d.DB.Debug().Where("isToSap = ?", 0).Where("SHStatus = ?", 7).Find(&bigContractData).Debug().Error; err != nil {
- // log.Errorf("AutoContractToASP Error:%v", err)
- // }
- //
- // for _, item := range bigContractData {
- // if item.SHStatus == 7 && item.SHtype == 3 && item.CGChargedate.UnixNano() > time.Now().AddDate(0, 0, -5).UnixNano() {
- // contractCodeSlice := strings.Split(item.ContractCode, "-")
- // contractCode := contractCodeSlice[0]
- // s.d.DB.Exec(`update contract set isToSap = 0 where enable = 1 and bigid in(select id from bigContract where contractCode like ? )`, "%"+contractCode+"%")
- // }
- // }
- //
- // return bigContractData
- //}
- //
- //func GetPastureBigContract(contractCode string) ([]*model.PastureBigcontract, error) {
- // pastureBigContract := make([]*model.PastureBigcontract, 0)
- // SQL := fmt.Sprintf("select t.* from( "+
- // " select pastureId as pasture_id ,enable FROM bigcontract WHERE contractCode LIKE '%s' and SHStatus = 7 GROUP BY pastureId "+
- // " union all "+
- // " SELECT pasture_id, enable FROM pasture_bigcontract WHERE bigcontract_id IN (SELECT id FROM bigcontract WHERE contractCode LIKE '%s' AND enable = 1 and SHStatus = 7 ) )t"+
- // " join pasture p on p.id = t.pasture_id "+
- // " group by pasture_id", "%"+contractCode+"%", "%"+contractCode+"%")
- // if err := s.d.DB.Raw(SQL).Find(&pastureBigContract).Error; err != nil {
- // return nil, err
- // }
- //
- // return pastureBigContract, nil
- //}
- //
- //// GetContractInfo 获取合同相关信息
- //func GetContractInfo(bigContract *model.BigContract, pastureBigContract *model.PastureBigcontract) *ContractInfo {
- // //wg := sync.WaitGroup{}
- // //wg.Add(3)
- // contractInfo := &ContractInfo{}
- // // 获取牧场数据合同数据
- // //go func() {
- // pasture := &model.Pasture{
- // ID: pastureBigContract.PastureId,
- // }
- // if err := s.d.DB.First(pasture).Error; err != nil {
- // log.Errorf("AutoContractToASP Pasture Err:%v,bigContractData:%v", err, bigContract)
- // }
- // contractInfo.Pasture = pasture
- // // wg.Done()
- // //}()
- //
- // // 获取合同子表数据
- // //go func() {
- // contract := make([]*model.Contract, 0)
- // if err := s.d.DB.Debug().Where("bigId = ?", bigContract.ID).Where("isToSap = ? ", 0).Find(&contract).Error; err != nil {
- // log.Errorf("AutoContractToASP contract Err:%v,bigContractData:%v", err, bigContract)
- // }
- // contractInfo.Contract = contract
- // // wg.Done()
- // //}()
- //
- // // 获取供应商数据
- // //go func() {
- // provider := &model.Provider{
- // ID: bigContract.ProviderID,
- // }
- // if err := s.d.DB.First(provider).Error; err != nil {
- // log.Errorf("AutoContractToASP Provider Err:%v,bigContractData:%v", err, bigContract)
- // }
- // contractInfo.Provider = provider
- // // wg.Done()
- // //}()
- // //wg.Wait()
- // return contractInfo
- //}
- //
- //// http://192.168.61.117/SAPQ0/SRM/MM018/PurchaseInfo
- //// AutoContractToASP 合同信息自动同步到ASP
- //func AutoContractToASP() {
- // sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
- //
- // bigContractData := GetBigContractData()
- // ContractCodeMap := make(map[string][]*model.PastureBigcontract, 0)
- // errDataList := make(map[string][]*model.ContractSapErr, 0)
- //
- // errBigContract := make([]*model.BigContract, 0)
- // errContract := make([]*model.Contract, 0)
- //
- // for _, bigContract := range bigContractData {
- // contractCode := bigContract.ContractCode
- // if bigContract.SHStatus != 7 {
- // continue
- // }
- //
- // var count int64
- // s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Where("isToSap = ?", 0).Count(&count)
- // if count == 0 {
- // continue
- // }
- //
- // if strings.Contains(bigContract.ContractCode, "-") {
- // contractCodeSlice := strings.Split(bigContract.ContractCode, "-")
- // contractCode = contractCodeSlice[0]
- // }
- // var err error
- // pastureBigContractSlice := make([]*model.PastureBigcontract, 0)
- // if _, ok := ContractCodeMap[contractCode]; !ok {
- // pastureBigContractSlice, err = GetPastureBigContract(contractCode)
- // if err != nil {
- // log.Errorf("AutoContractToASP pastureBigContractSlice Err:%v", err)
- // continue
- // }
- // ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContractSlice...)
- // //if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("contractCode like ? ", "%"+contractCode+"%").Update("isToSap", 1).Error; err != nil {
- // // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- // //}
- // }
- // if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Update("isToSap", 1).Error; err != nil {
- // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- // }
- //
- // if _, ok := ContractCodeMap[contractCode]; ok {
- // contractInfoList := make([]*ContractInfo, 0)
- // for _, pastureBigContract := range ContractCodeMap[contractCode] {
- // // 获取合同相关数据集合
- // contractInfo := GetContractInfo(bigContract, pastureBigContract)
- // if len(contractInfo.Contract) == 0 || pastureBigContract.PastureId == 18 {
- // continue
- // }
- //
- // // 发送数据至SAP
- // errData, bigContracts, contracts := PushContractDataToSAP(bigContract, contractInfo, contractCode, false)
- // for _, data := range errData {
- // errDataList[contractCode] = append(errDataList[contractCode], data...)
- // }
- // contractInfoList = append(contractInfoList, contractInfo)
- // errBigContract = append(errBigContract, bigContracts...)
- // errContract = append(errContract, contracts...)
- // }
- //
- // var idList []int
- // for _, contractInfo := range contractInfoList {
- // for _, c := range contractInfo.Contract {
- // if c.IsToSap == 0 {
- // exist := false
- // for _, id := range idList {
- // if id == c.ID {
- // exist = true
- // break
- // }
- // }
- // if !exist {
- // idList = append(idList, c.ID)
- // if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 1).Error; err != nil {
- // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- // }
- // }
- // }
- // }
- // }
- //
- // }
- // }
- // now := time.Now()
- // for _, contractSapErr := range errDataList {
- // for _, c := range contractSapErr {
- // c.CreateTime = now
- // s.d.DB.Create(&c)
- // }
- // }
- //
- // for _, item := range errBigContract {
- // if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", item.ID).Update("isToSap", 0).Error; err != nil {
- // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- // }
- // }
- // for _, c := range errContract {
- // if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 0).Error; err != nil {
- // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
- // }
- // }
- //
- // log.Info("AutoContractToASP Success")
- //}
- //
- //// PushContractDataToSAP 同步数据给SAP
- //func PushContractDataToSAP(bigContract *model.BigContract, contractInfo *ContractInfo, contractCode string, hand bool) (map[string][]*model.ContractSapErr, []*model.BigContract, []*model.Contract) {
- // errMap := make(map[string][]*model.ContractSapErr, 0)
- // errBigContract := make([]*model.BigContract, 0)
- // errContract := make([]*model.Contract, 0)
- //
- // for _, contract := range contractInfo.Contract {
- // if !hand {
- // if contract.IsToSap == 1 {
- // continue
- // }
- // }
- // LOEKZ := ""
- // if contract.Enable == 0 {
- // LOEKZ = "X"
- // }
- //
- // startTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StartTime, loc)
- // stopTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StopTime, loc)
- //
- // astr := strconv.FormatFloat(contract.Price, 'f', -1, 64)
- // b := strings.Index(astr, ".")
- // per := ""
- // if len(astr[b+1:]) > 2 && b > 0 {
- // num, _ := strconv.ParseFloat(astr[b+1:], 64)
- // if num > 0 {
- // contract.Price = contract.Price * 1000
- // per = "1000"
- // }
- // }
- //
- // var isZeroStock int
- // if bigContract.IsZeroStock == 1 {
- // isZeroStock = 2
- // } else {
- // isZeroStock = 0
- // }
- // //LOEKZ = "X"
- // contractDataToASP := &http.ContractDataToASP{
- // MATNR: contract.PartCode,
- // LIFNR: contractInfo.Provider.SapCode,
- // EKORG: contractInfo.Pasture.ParchaseOrganization,
- // WERKS: contractInfo.Pasture.FactoryCode,
- // WAERS: "CNY",
- // PEINH: per,
- // NETPR: fmt.Sprintf("%.2f", contract.Price),
- // MWSKZ: "J0",
- // EKGRP: contractInfo.Pasture.PurchasingGroup,
- // DATAB: startTime.Format("20060102"),
- // DATBI: stopTime.Format("20060102"),
- // LOEKZ: LOEKZ,
- // ESOKZ: fmt.Sprintf("%d", isZeroStock),
- // MEINS: contract.Unit,
- // Code: bigContract.ContractCode,
- // }
- // sapContractReq.DATA = contractDataToASP
- //
- // rp := &http.SapContractResp{}
- //
- // rbyte, _ := json.Marshal(sapContractReq)
- // fmt.Println(string(rbyte))
- // if err := s.SyncSap(sapContractReq, rp, sapContractReq); err != nil {
- // log.Errorf("AutoContractToASP SyncSap Err:%v contract:%v", err, contract)
- // //continue
- // }
- // log.Info(rp)
- //
- // if rp.Dest.Status != "S" {
- // log.Errorf("AutoContractToASP SyncSap Error:%v,contract:%v", rp, contract)
- // contract.PastureID = contractInfo.Pasture.ID
- // errMap[contractCode] = append(errMap[contractCode], &model.ContractSapErr{
- // ContractID: contract.ID,
- // PastureID: contractInfo.Pasture.ID,
- // PastureName: contractInfo.Pasture.Name,
- // PartID: contract.PartID,
- // PartName: contract.PartName,
- // PartCode: contract.PartCode,
- // Specification: contract.Specification,
- // Price: contract.Price,
- // BrandID: contract.BrandID,
- // Brand: contract.Brand,
- // InventoryType: contract.InventoryType,
- // PlanAmount: contract.PlanAmount,
- // Remark: contract.Remark,
- // Enable: contract.Enable,
- // Unit: contract.Unit,
- // IsZeroStock: contract.IsZeroStock,
- // ChangeID: contract.ChangeID,
- // SapText: rp.MessText,
- // ContractCode: contractCode,
- // })
- // }
- // if rp.Dest.Status == "" {
- // errContract = append(errContract, contract)
- // }
- // }
- //
- // if len(errContract) > 0 {
- // errBigContract = append(errBigContract, bigContract)
- // }
- // return errMap, errBigContract, errContract
- //}
- //
- //func RemoveRepeatedElement1(arr []string) (newArr []string) {
- // newArr = make([]string, 0)
- // for i := 0; i < len(arr); i++ {
- // repeat := false
- // for j := i + 1; j < len(arr); j++ {
- // if arr[i] == arr[j] {
- // repeat = true
- // break
- // }
- // }
- // if !repeat {
- // newArr = append(newArr, arr[i])
- // }
- // }
- // return
- //}
- //
- //func (s *Service) PostPushContractDataToSAP(pastureId, providerId int64, contractIdList []int64) error {
- // sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
- // contractList := make([]*model.Contract, 0)
- // err := s.d.DB.Debug().Where(" id in (?) ", contractIdList).Find(&contractList).Error
- // if err != nil {
- // logs.Error(err)
- // return err
- // }
- //
- // pasture := new(model.Pasture)
- // err = s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
- // if err != nil {
- // logs.Error(err)
- // return err
- // }
- //
- // provider := new(model.Provider)
- // err = s.d.DB.Where("id = ? ", providerId).First(&provider).Error
- // if err != nil {
- // logs.Error(err)
- // return err
- // }
- //
- // contractMap := make(map[int][]*model.Contract, 0)
- // for _, contract := range contractList {
- // contractMap[contract.BigID] = append(contractMap[contract.BigID], contract)
- // }
- //
- // pastureBigcontractList := make([]*model.PastureBigcontract, 0)
- // for bigId, m := range contractMap {
- // bigContract := new(model.BigContract)
- // err := s.d.DB.Where(" id = ? ", bigId).First(&bigContract).Error
- // if err != nil {
- // logs.Error(err)
- // return err
- // }
- //
- // if len(pastureBigcontractList) == 0 {
- // contractCode := strings.Split(bigContract.ContractCode, "-")[0]
- // pastureBigcontractList, _ = GetPastureBigContract(contractCode)
- // }
- // exist := false
- // for _, item := range pastureBigcontractList {
- // if item.PastureId == int(pastureId) {
- // exist = true
- // break
- // }
- // }
- // if !exist {
- // return errors.New("牧场没有该合同!")
- // }
- //
- // contractInfo := new(ContractInfo)
- // contractInfo.Contract = m
- // contractInfo.Provider = provider
- // contractInfo.Pasture = pasture
- // fmt.Println(contractInfo)
- // PushContractDataToSAP(bigContract, contractInfo, bigContract.ContractCode, true)
- // }
- //
- // return nil
- //}
|