contract.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/astaxie/beego/logs"
  6. "github.com/pkg/errors"
  7. "kpt.xdmy/apiserver/config"
  8. "strconv"
  9. "strings"
  10. "time"
  11. log "github.com/sirupsen/logrus"
  12. "kpt.xdmy/apiserver/model"
  13. "kpt.xdmy/apiserver/model/http"
  14. )
  15. const (
  16. //ContractToAspUrl = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
  17. ContractToAspDestID = "EQMAN"
  18. ContractToAspBussTp = "MM018"
  19. ContractLimit = 10
  20. )
  21. var (
  22. sapContractReq = &http.SapContractReq{
  23. Dest: &http.Dest{
  24. DestID: ContractToAspDestID,
  25. BussTp: ContractToAspBussTp,
  26. //Url: ContractToAspUrl,
  27. },
  28. DATA: []*http.ContractDataToASP{},
  29. }
  30. loc, _ = time.LoadLocation("Local")
  31. )
  32. type ContractInfo struct {
  33. PastureBigContract *model.PastureBigcontract // 牧场和合同关联表
  34. Contract []*model.Contract // 合同子表
  35. Provider *model.Provider // 供应商
  36. Pasture *model.Pasture // 牧场
  37. }
  38. // GetBigContractData 获取主合同信息
  39. func GetBigContractData() []*model.BigContract {
  40. bigContractData := make([]*model.BigContract, 0)
  41. if err := s.d.DB.Debug().Where("isToSap = ?", 0).Where("SHStatus = ?", 7).Find(&bigContractData).Debug().Error; err != nil {
  42. log.Errorf("AutoContractToASP Error:%v", err)
  43. }
  44. for _, item := range bigContractData {
  45. if item.SHStatus == 7 && item.SHtype == 3 && item.CGChargedate.UnixNano() > time.Now().AddDate(0, 0, -5).UnixNano() {
  46. contractCodeSlice := strings.Split(item.ContractCode, "-")
  47. contractCode := contractCodeSlice[0]
  48. s.d.DB.Exec(`update contract set isToSap = 0 where enable = 1 and bigid in(select id from bigContract where contractCode like ? )`, "%"+contractCode+"%")
  49. }
  50. }
  51. return bigContractData
  52. }
  53. func GetPastureBigContract(contractCode string) ([]*model.PastureBigcontract, error) {
  54. pastureBigContract := make([]*model.PastureBigcontract, 0)
  55. SQL := fmt.Sprintf("select t.* from( "+
  56. " select pastureId as pasture_id ,enable FROM bigcontract WHERE contractCode LIKE '%s' and SHStatus = 7 GROUP BY pastureId "+
  57. " union all "+
  58. " 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"+
  59. " join pasture p on p.id = t.pasture_id "+
  60. " group by pasture_id", "%"+contractCode+"%", "%"+contractCode+"%")
  61. if err := s.d.DB.Raw(SQL).Find(&pastureBigContract).Error; err != nil {
  62. return nil, err
  63. }
  64. return pastureBigContract, nil
  65. }
  66. // GetContractInfo 获取合同相关信息
  67. func GetContractInfo(bigContract *model.BigContract, pastureBigContract *model.PastureBigcontract) *ContractInfo {
  68. //wg := sync.WaitGroup{}
  69. //wg.Add(3)
  70. contractInfo := &ContractInfo{}
  71. // 获取牧场数据合同数据
  72. //go func() {
  73. pasture := &model.Pasture{
  74. ID: pastureBigContract.PastureId,
  75. }
  76. if err := s.d.DB.First(pasture).Error; err != nil {
  77. log.Errorf("AutoContractToASP Pasture Err:%v,bigContractData:%v", err, bigContract)
  78. }
  79. contractInfo.Pasture = pasture
  80. // wg.Done()
  81. //}()
  82. // 获取合同子表数据
  83. //go func() {
  84. contract := make([]*model.Contract, 0)
  85. if err := s.d.DB.Debug().Where("bigId = ?", bigContract.ID).Where("isToSap = ? ", 0).Find(&contract).Error; err != nil {
  86. log.Errorf("AutoContractToASP contract Err:%v,bigContractData:%v", err, bigContract)
  87. }
  88. contractInfo.Contract = contract
  89. // wg.Done()
  90. //}()
  91. // 获取供应商数据
  92. //go func() {
  93. provider := &model.Provider{
  94. ID: bigContract.ProviderID,
  95. }
  96. if err := s.d.DB.First(provider).Error; err != nil {
  97. log.Errorf("AutoContractToASP Provider Err:%v,bigContractData:%v", err, bigContract)
  98. }
  99. contractInfo.Provider = provider
  100. // wg.Done()
  101. //}()
  102. //wg.Wait()
  103. return contractInfo
  104. }
  105. // http://192.168.61.117/SAPQ0/SRM/MM018/PurchaseInfo
  106. // AutoContractToASP 合同信息自动同步到ASP
  107. func AutoContractToASP() {
  108. sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
  109. bigContractData := GetBigContractData()
  110. ContractCodeMap := make(map[string][]*model.PastureBigcontract, 0)
  111. errDataList := make(map[string][]*model.ContractSapErr, 0)
  112. errBigContract := make([]*model.BigContract, 0)
  113. errContract := make([]*model.Contract, 0)
  114. for _, bigContract := range bigContractData {
  115. contractCode := bigContract.ContractCode
  116. if bigContract.SHStatus != 7 {
  117. continue
  118. }
  119. var count int64
  120. s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Where("isToSap = ?", 0).Count(&count)
  121. if count == 0 {
  122. continue
  123. }
  124. if strings.Contains(bigContract.ContractCode, "-") {
  125. contractCodeSlice := strings.Split(bigContract.ContractCode, "-")
  126. contractCode = contractCodeSlice[0]
  127. }
  128. var err error
  129. pastureBigContractSlice := make([]*model.PastureBigcontract, 0)
  130. if _, ok := ContractCodeMap[contractCode]; !ok {
  131. pastureBigContractSlice, err = GetPastureBigContract(contractCode)
  132. if err != nil {
  133. log.Errorf("AutoContractToASP pastureBigContractSlice Err:%v", err)
  134. continue
  135. }
  136. ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContractSlice...)
  137. //if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("contractCode like ? ", "%"+contractCode+"%").Update("isToSap", 1).Error; err != nil {
  138. // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  139. //}
  140. }
  141. if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Update("isToSap", 1).Error; err != nil {
  142. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  143. }
  144. if _, ok := ContractCodeMap[contractCode]; ok {
  145. contractInfoList := make([]*ContractInfo, 0)
  146. for _, pastureBigContract := range ContractCodeMap[contractCode] {
  147. // 获取合同相关数据集合
  148. contractInfo := GetContractInfo(bigContract, pastureBigContract)
  149. if len(contractInfo.Contract) == 0 || pastureBigContract.PastureId == 18 {
  150. continue
  151. }
  152. // 发送数据至SAP
  153. errData, bigContracts, contracts := PushContractDataToSAP(bigContract, contractInfo, contractCode, false)
  154. for _, data := range errData {
  155. errDataList[contractCode] = append(errDataList[contractCode], data...)
  156. }
  157. contractInfoList = append(contractInfoList, contractInfo)
  158. errBigContract = append(errBigContract, bigContracts...)
  159. errContract = append(errContract, contracts...)
  160. }
  161. var idList []int
  162. for _, contractInfo := range contractInfoList {
  163. for _, c := range contractInfo.Contract {
  164. if c.IsToSap == 0 {
  165. exist := false
  166. for _, id := range idList {
  167. if id == c.ID {
  168. exist = true
  169. break
  170. }
  171. }
  172. if !exist {
  173. idList = append(idList, c.ID)
  174. if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 1).Error; err != nil {
  175. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. now := time.Now()
  184. for _, contractSapErr := range errDataList {
  185. for _, c := range contractSapErr {
  186. c.CreateTime = now
  187. s.d.DB.Create(&c)
  188. }
  189. }
  190. for _, item := range errBigContract {
  191. if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", item.ID).Update("isToSap", 0).Error; err != nil {
  192. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  193. }
  194. }
  195. for _, c := range errContract {
  196. if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 0).Error; err != nil {
  197. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  198. }
  199. }
  200. log.Info("AutoContractToASP Success")
  201. }
  202. // PushContractDataToSAP 同步数据给SAP
  203. func PushContractDataToSAP(bigContract *model.BigContract, contractInfo *ContractInfo, contractCode string, hand bool) (map[string][]*model.ContractSapErr, []*model.BigContract, []*model.Contract) {
  204. errMap := make(map[string][]*model.ContractSapErr, 0)
  205. errBigContract := make([]*model.BigContract, 0)
  206. errContract := make([]*model.Contract, 0)
  207. contractList := make([]*model.Contract, 0)
  208. i := 0
  209. for n, contract := range contractInfo.Contract {
  210. i++
  211. if !hand {
  212. if contract.IsToSap == 1 {
  213. continue
  214. }
  215. }
  216. LOEKZ := ""
  217. if contract.Enable == 0 {
  218. LOEKZ = "X"
  219. }
  220. startTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StartTime, loc)
  221. stopTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StopTime, loc)
  222. astr := strconv.FormatFloat(contract.Price, 'f', -1, 64)
  223. b := strings.Index(astr, ".")
  224. per := ""
  225. if len(astr[b+1:]) > 2 && b > 0 {
  226. num, _ := strconv.ParseFloat(astr[b+1:], 64)
  227. if num > 0 {
  228. contract.Price = contract.Price * 1000
  229. per = "1000"
  230. }
  231. }
  232. var isZeroStock int
  233. if bigContract.IsZeroStock == 1 {
  234. isZeroStock = 2
  235. } else {
  236. isZeroStock = 0
  237. }
  238. contractDataToASP := &http.ContractDataToASP{
  239. MATNR: contract.PartCode,
  240. LIFNR: contractInfo.Provider.SapCode,
  241. EKORG: contractInfo.Pasture.ParchaseOrganization,
  242. WERKS: contractInfo.Pasture.FactoryCode,
  243. WAERS: "CNY",
  244. PEINH: per,
  245. NETPR: fmt.Sprintf("%.2f", contract.Price),
  246. MWSKZ: "J0",
  247. EKGRP: contractInfo.Pasture.PurchasingGroup,
  248. DATAB: startTime.Format("20060102"),
  249. DATBI: stopTime.Format("20060102"),
  250. LOEKZ: LOEKZ,
  251. ESOKZ: fmt.Sprintf("%d", isZeroStock),
  252. MEINS: contract.Unit,
  253. Code: bigContract.ContractCode,
  254. RCKEY: fmt.Sprintf("%d", i+1),
  255. }
  256. contractList = append(contractList, contract)
  257. sapContractReq.DATA = append(sapContractReq.DATA, contractDataToASP)
  258. fmt.Println(n, len(contractInfo.Contract))
  259. if i == 300 || n+1 == len(contractInfo.Contract) {
  260. i = 0
  261. rp := &http.SapContractResp{}
  262. rbyte, _ := json.Marshal(sapContractReq)
  263. fmt.Println(string(rbyte))
  264. if err := s.SyncSap(sapContractReq, rp, sapContractReq); err != nil {
  265. log.Errorf("AutoContractToASP SyncSap Err:%v ", err)
  266. //continue
  267. }
  268. log.Info(rp)
  269. if rp.Dest.Status != "S" {
  270. log.Errorf("AutoContractToASP SyncSap Error:%v", rp)
  271. for _, contract1 := range contractList {
  272. contract1.PastureID = contractInfo.Pasture.ID
  273. errMap[contractCode] = append(errMap[contractCode], &model.ContractSapErr{
  274. ContractID: contract1.ID,
  275. PastureID: contractInfo.Pasture.ID,
  276. PastureName: contractInfo.Pasture.Name,
  277. PartID: contract1.PartID,
  278. PartName: contract1.PartName,
  279. PartCode: contract1.PartCode,
  280. Specification: contract1.Specification,
  281. Price: contract1.Price,
  282. BrandID: contract1.BrandID,
  283. Brand: contract1.Brand,
  284. InventoryType: contract1.InventoryType,
  285. PlanAmount: contract1.PlanAmount,
  286. Remark: contract1.Remark,
  287. Enable: contract1.Enable,
  288. Unit: contract1.Unit,
  289. IsZeroStock: contract1.IsZeroStock,
  290. ChangeID: contract1.ChangeID,
  291. SapText: rp.MessText,
  292. ContractCode: contractCode,
  293. })
  294. if rp.Dest.Status == "" {
  295. errContract = append(errContract, contract1)
  296. }
  297. }
  298. }
  299. if len(errContract) > 0 {
  300. errBigContract = append(errBigContract, bigContract)
  301. }
  302. contractList = make([]*model.Contract, 0)
  303. sapContractReq.DATA = make([]*http.ContractDataToASP, 0)
  304. }
  305. //break
  306. }
  307. return errMap, errBigContract, errContract
  308. }
  309. func RemoveRepeatedElement1(arr []string) (newArr []string) {
  310. newArr = make([]string, 0)
  311. for i := 0; i < len(arr); i++ {
  312. repeat := false
  313. for j := i + 1; j < len(arr); j++ {
  314. if arr[i] == arr[j] {
  315. repeat = true
  316. break
  317. }
  318. }
  319. if !repeat {
  320. newArr = append(newArr, arr[i])
  321. }
  322. }
  323. return
  324. }
  325. func (s *Service) PostPushContractDataToSAP(pastureId, providerId int64, contractIdList []int64) error {
  326. sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
  327. contractList := make([]*model.Contract, 0)
  328. err := s.d.DB.Debug().Where(" id in (?) ", contractIdList).Find(&contractList).Error
  329. if err != nil {
  330. logs.Error(err)
  331. return err
  332. }
  333. pasture := new(model.Pasture)
  334. err = s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
  335. if err != nil {
  336. logs.Error(err)
  337. return err
  338. }
  339. provider := new(model.Provider)
  340. err = s.d.DB.Where("id = ? ", providerId).First(&provider).Error
  341. if err != nil {
  342. logs.Error(err)
  343. return err
  344. }
  345. contractMap := make(map[int][]*model.Contract, 0)
  346. for _, contract := range contractList {
  347. contractMap[contract.BigID] = append(contractMap[contract.BigID], contract)
  348. }
  349. pastureBigcontractList := make([]*model.PastureBigcontract, 0)
  350. for bigId, m := range contractMap {
  351. bigContract := new(model.BigContract)
  352. err := s.d.DB.Where(" id = ? ", bigId).First(&bigContract).Error
  353. if err != nil {
  354. logs.Error(err)
  355. return err
  356. }
  357. if len(pastureBigcontractList) == 0 {
  358. contractCode := strings.Split(bigContract.ContractCode, "-")[0]
  359. pastureBigcontractList, _ = GetPastureBigContract(contractCode)
  360. }
  361. exist := false
  362. for _, item := range pastureBigcontractList {
  363. if item.PastureId == int(pastureId) {
  364. exist = true
  365. break
  366. }
  367. }
  368. if !exist {
  369. return errors.New("牧场没有该合同!")
  370. }
  371. contractInfo := new(ContractInfo)
  372. contractInfo.Contract = m
  373. contractInfo.Provider = provider
  374. contractInfo.Pasture = pasture
  375. fmt.Println(contractInfo)
  376. PushContractDataToSAP(bigContract, contractInfo, bigContract.ContractCode, true)
  377. }
  378. return nil
  379. }
  380. //
  381. //package service
  382. //
  383. //import (
  384. // "encoding/json"
  385. // "fmt"
  386. // "github.com/astaxie/beego/logs"
  387. // "github.com/pkg/errors"
  388. // "kpt.xdmy/apiserver/config"
  389. // "strconv"
  390. // "strings"
  391. // "time"
  392. //
  393. // log "github.com/sirupsen/logrus"
  394. // "kpt.xdmy/apiserver/model"
  395. // "kpt.xdmy/apiserver/model/http"
  396. //)
  397. //
  398. //const (
  399. // //ContractToAspUrl = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
  400. // ContractToAspDestID = "EQMAN"
  401. // ContractToAspBussTp = "MM018"
  402. // ContractLimit = 10
  403. //)
  404. //
  405. //var (
  406. // sapContractReq = &http.SapContractReq{
  407. // Dest: &http.Dest{
  408. // DestID: ContractToAspDestID,
  409. // BussTp: ContractToAspBussTp,
  410. // //Url: ContractToAspUrl,
  411. // },
  412. // DATA: &http.ContractDataToASP{},
  413. // }
  414. // loc, _ = time.LoadLocation("Local")
  415. //)
  416. //
  417. //type ContractInfo struct {
  418. // PastureBigContract *model.PastureBigcontract // 牧场和合同关联表
  419. // Contract []*model.Contract // 合同子表
  420. // Provider *model.Provider // 供应商
  421. // Pasture *model.Pasture // 牧场
  422. //}
  423. //
  424. //// GetBigContractData 获取主合同信息
  425. //func GetBigContractData() []*model.BigContract {
  426. // bigContractData := make([]*model.BigContract, 0)
  427. // if err := s.d.DB.Debug().Where("isToSap = ?", 0).Where("SHStatus = ?", 7).Find(&bigContractData).Debug().Error; err != nil {
  428. // log.Errorf("AutoContractToASP Error:%v", err)
  429. // }
  430. //
  431. // for _, item := range bigContractData {
  432. // if item.SHStatus == 7 && item.SHtype == 3 && item.CGChargedate.UnixNano() > time.Now().AddDate(0, 0, -5).UnixNano() {
  433. // contractCodeSlice := strings.Split(item.ContractCode, "-")
  434. // contractCode := contractCodeSlice[0]
  435. // s.d.DB.Exec(`update contract set isToSap = 0 where enable = 1 and bigid in(select id from bigContract where contractCode like ? )`, "%"+contractCode+"%")
  436. // }
  437. // }
  438. //
  439. // return bigContractData
  440. //}
  441. //
  442. //func GetPastureBigContract(contractCode string) ([]*model.PastureBigcontract, error) {
  443. // pastureBigContract := make([]*model.PastureBigcontract, 0)
  444. // SQL := fmt.Sprintf("select t.* from( "+
  445. // " select pastureId as pasture_id ,enable FROM bigcontract WHERE contractCode LIKE '%s' and SHStatus = 7 GROUP BY pastureId "+
  446. // " union all "+
  447. // " 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"+
  448. // " join pasture p on p.id = t.pasture_id "+
  449. // " group by pasture_id", "%"+contractCode+"%", "%"+contractCode+"%")
  450. // if err := s.d.DB.Raw(SQL).Find(&pastureBigContract).Error; err != nil {
  451. // return nil, err
  452. // }
  453. //
  454. // return pastureBigContract, nil
  455. //}
  456. //
  457. //// GetContractInfo 获取合同相关信息
  458. //func GetContractInfo(bigContract *model.BigContract, pastureBigContract *model.PastureBigcontract) *ContractInfo {
  459. // //wg := sync.WaitGroup{}
  460. // //wg.Add(3)
  461. // contractInfo := &ContractInfo{}
  462. // // 获取牧场数据合同数据
  463. // //go func() {
  464. // pasture := &model.Pasture{
  465. // ID: pastureBigContract.PastureId,
  466. // }
  467. // if err := s.d.DB.First(pasture).Error; err != nil {
  468. // log.Errorf("AutoContractToASP Pasture Err:%v,bigContractData:%v", err, bigContract)
  469. // }
  470. // contractInfo.Pasture = pasture
  471. // // wg.Done()
  472. // //}()
  473. //
  474. // // 获取合同子表数据
  475. // //go func() {
  476. // contract := make([]*model.Contract, 0)
  477. // if err := s.d.DB.Debug().Where("bigId = ?", bigContract.ID).Where("isToSap = ? ", 0).Find(&contract).Error; err != nil {
  478. // log.Errorf("AutoContractToASP contract Err:%v,bigContractData:%v", err, bigContract)
  479. // }
  480. // contractInfo.Contract = contract
  481. // // wg.Done()
  482. // //}()
  483. //
  484. // // 获取供应商数据
  485. // //go func() {
  486. // provider := &model.Provider{
  487. // ID: bigContract.ProviderID,
  488. // }
  489. // if err := s.d.DB.First(provider).Error; err != nil {
  490. // log.Errorf("AutoContractToASP Provider Err:%v,bigContractData:%v", err, bigContract)
  491. // }
  492. // contractInfo.Provider = provider
  493. // // wg.Done()
  494. // //}()
  495. // //wg.Wait()
  496. // return contractInfo
  497. //}
  498. //
  499. //// http://192.168.61.117/SAPQ0/SRM/MM018/PurchaseInfo
  500. //// AutoContractToASP 合同信息自动同步到ASP
  501. //func AutoContractToASP() {
  502. // sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
  503. //
  504. // bigContractData := GetBigContractData()
  505. // ContractCodeMap := make(map[string][]*model.PastureBigcontract, 0)
  506. // errDataList := make(map[string][]*model.ContractSapErr, 0)
  507. //
  508. // errBigContract := make([]*model.BigContract, 0)
  509. // errContract := make([]*model.Contract, 0)
  510. //
  511. // for _, bigContract := range bigContractData {
  512. // contractCode := bigContract.ContractCode
  513. // if bigContract.SHStatus != 7 {
  514. // continue
  515. // }
  516. //
  517. // var count int64
  518. // s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Where("isToSap = ?", 0).Count(&count)
  519. // if count == 0 {
  520. // continue
  521. // }
  522. //
  523. // if strings.Contains(bigContract.ContractCode, "-") {
  524. // contractCodeSlice := strings.Split(bigContract.ContractCode, "-")
  525. // contractCode = contractCodeSlice[0]
  526. // }
  527. // var err error
  528. // pastureBigContractSlice := make([]*model.PastureBigcontract, 0)
  529. // if _, ok := ContractCodeMap[contractCode]; !ok {
  530. // pastureBigContractSlice, err = GetPastureBigContract(contractCode)
  531. // if err != nil {
  532. // log.Errorf("AutoContractToASP pastureBigContractSlice Err:%v", err)
  533. // continue
  534. // }
  535. // ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContractSlice...)
  536. // //if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("contractCode like ? ", "%"+contractCode+"%").Update("isToSap", 1).Error; err != nil {
  537. // // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  538. // //}
  539. // }
  540. // if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Update("isToSap", 1).Error; err != nil {
  541. // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  542. // }
  543. //
  544. // if _, ok := ContractCodeMap[contractCode]; ok {
  545. // contractInfoList := make([]*ContractInfo, 0)
  546. // for _, pastureBigContract := range ContractCodeMap[contractCode] {
  547. // // 获取合同相关数据集合
  548. // contractInfo := GetContractInfo(bigContract, pastureBigContract)
  549. // if len(contractInfo.Contract) == 0 || pastureBigContract.PastureId == 18 {
  550. // continue
  551. // }
  552. //
  553. // // 发送数据至SAP
  554. // errData, bigContracts, contracts := PushContractDataToSAP(bigContract, contractInfo, contractCode, false)
  555. // for _, data := range errData {
  556. // errDataList[contractCode] = append(errDataList[contractCode], data...)
  557. // }
  558. // contractInfoList = append(contractInfoList, contractInfo)
  559. // errBigContract = append(errBigContract, bigContracts...)
  560. // errContract = append(errContract, contracts...)
  561. // }
  562. //
  563. // var idList []int
  564. // for _, contractInfo := range contractInfoList {
  565. // for _, c := range contractInfo.Contract {
  566. // if c.IsToSap == 0 {
  567. // exist := false
  568. // for _, id := range idList {
  569. // if id == c.ID {
  570. // exist = true
  571. // break
  572. // }
  573. // }
  574. // if !exist {
  575. // idList = append(idList, c.ID)
  576. // if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 1).Error; err != nil {
  577. // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  578. // }
  579. // }
  580. // }
  581. // }
  582. // }
  583. //
  584. // }
  585. // }
  586. // now := time.Now()
  587. // for _, contractSapErr := range errDataList {
  588. // for _, c := range contractSapErr {
  589. // c.CreateTime = now
  590. // s.d.DB.Create(&c)
  591. // }
  592. // }
  593. //
  594. // for _, item := range errBigContract {
  595. // if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", item.ID).Update("isToSap", 0).Error; err != nil {
  596. // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  597. // }
  598. // }
  599. // for _, c := range errContract {
  600. // if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 0).Error; err != nil {
  601. // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  602. // }
  603. // }
  604. //
  605. // log.Info("AutoContractToASP Success")
  606. //}
  607. //
  608. //// PushContractDataToSAP 同步数据给SAP
  609. //func PushContractDataToSAP(bigContract *model.BigContract, contractInfo *ContractInfo, contractCode string, hand bool) (map[string][]*model.ContractSapErr, []*model.BigContract, []*model.Contract) {
  610. // errMap := make(map[string][]*model.ContractSapErr, 0)
  611. // errBigContract := make([]*model.BigContract, 0)
  612. // errContract := make([]*model.Contract, 0)
  613. //
  614. // for _, contract := range contractInfo.Contract {
  615. // if !hand {
  616. // if contract.IsToSap == 1 {
  617. // continue
  618. // }
  619. // }
  620. // LOEKZ := ""
  621. // if contract.Enable == 0 {
  622. // LOEKZ = "X"
  623. // }
  624. //
  625. // startTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StartTime, loc)
  626. // stopTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StopTime, loc)
  627. //
  628. // astr := strconv.FormatFloat(contract.Price, 'f', -1, 64)
  629. // b := strings.Index(astr, ".")
  630. // per := ""
  631. // if len(astr[b+1:]) > 2 && b > 0 {
  632. // num, _ := strconv.ParseFloat(astr[b+1:], 64)
  633. // if num > 0 {
  634. // contract.Price = contract.Price * 1000
  635. // per = "1000"
  636. // }
  637. // }
  638. //
  639. // var isZeroStock int
  640. // if bigContract.IsZeroStock == 1 {
  641. // isZeroStock = 2
  642. // } else {
  643. // isZeroStock = 0
  644. // }
  645. // //LOEKZ = "X"
  646. // contractDataToASP := &http.ContractDataToASP{
  647. // MATNR: contract.PartCode,
  648. // LIFNR: contractInfo.Provider.SapCode,
  649. // EKORG: contractInfo.Pasture.ParchaseOrganization,
  650. // WERKS: contractInfo.Pasture.FactoryCode,
  651. // WAERS: "CNY",
  652. // PEINH: per,
  653. // NETPR: fmt.Sprintf("%.2f", contract.Price),
  654. // MWSKZ: "J0",
  655. // EKGRP: contractInfo.Pasture.PurchasingGroup,
  656. // DATAB: startTime.Format("20060102"),
  657. // DATBI: stopTime.Format("20060102"),
  658. // LOEKZ: LOEKZ,
  659. // ESOKZ: fmt.Sprintf("%d", isZeroStock),
  660. // MEINS: contract.Unit,
  661. // Code: bigContract.ContractCode,
  662. // }
  663. // sapContractReq.DATA = contractDataToASP
  664. //
  665. // rp := &http.SapContractResp{}
  666. //
  667. // rbyte, _ := json.Marshal(sapContractReq)
  668. // fmt.Println(string(rbyte))
  669. // if err := s.SyncSap(sapContractReq, rp, sapContractReq); err != nil {
  670. // log.Errorf("AutoContractToASP SyncSap Err:%v contract:%v", err, contract)
  671. // //continue
  672. // }
  673. // log.Info(rp)
  674. //
  675. // if rp.Dest.Status != "S" {
  676. // log.Errorf("AutoContractToASP SyncSap Error:%v,contract:%v", rp, contract)
  677. // contract.PastureID = contractInfo.Pasture.ID
  678. // errMap[contractCode] = append(errMap[contractCode], &model.ContractSapErr{
  679. // ContractID: contract.ID,
  680. // PastureID: contractInfo.Pasture.ID,
  681. // PastureName: contractInfo.Pasture.Name,
  682. // PartID: contract.PartID,
  683. // PartName: contract.PartName,
  684. // PartCode: contract.PartCode,
  685. // Specification: contract.Specification,
  686. // Price: contract.Price,
  687. // BrandID: contract.BrandID,
  688. // Brand: contract.Brand,
  689. // InventoryType: contract.InventoryType,
  690. // PlanAmount: contract.PlanAmount,
  691. // Remark: contract.Remark,
  692. // Enable: contract.Enable,
  693. // Unit: contract.Unit,
  694. // IsZeroStock: contract.IsZeroStock,
  695. // ChangeID: contract.ChangeID,
  696. // SapText: rp.MessText,
  697. // ContractCode: contractCode,
  698. // })
  699. // }
  700. // if rp.Dest.Status == "" {
  701. // errContract = append(errContract, contract)
  702. // }
  703. // }
  704. //
  705. // if len(errContract) > 0 {
  706. // errBigContract = append(errBigContract, bigContract)
  707. // }
  708. // return errMap, errBigContract, errContract
  709. //}
  710. //
  711. //func RemoveRepeatedElement1(arr []string) (newArr []string) {
  712. // newArr = make([]string, 0)
  713. // for i := 0; i < len(arr); i++ {
  714. // repeat := false
  715. // for j := i + 1; j < len(arr); j++ {
  716. // if arr[i] == arr[j] {
  717. // repeat = true
  718. // break
  719. // }
  720. // }
  721. // if !repeat {
  722. // newArr = append(newArr, arr[i])
  723. // }
  724. // }
  725. // return
  726. //}
  727. //
  728. //func (s *Service) PostPushContractDataToSAP(pastureId, providerId int64, contractIdList []int64) error {
  729. // sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
  730. // contractList := make([]*model.Contract, 0)
  731. // err := s.d.DB.Debug().Where(" id in (?) ", contractIdList).Find(&contractList).Error
  732. // if err != nil {
  733. // logs.Error(err)
  734. // return err
  735. // }
  736. //
  737. // pasture := new(model.Pasture)
  738. // err = s.d.DB.Where("id = ? ", pastureId).First(&pasture).Error
  739. // if err != nil {
  740. // logs.Error(err)
  741. // return err
  742. // }
  743. //
  744. // provider := new(model.Provider)
  745. // err = s.d.DB.Where("id = ? ", providerId).First(&provider).Error
  746. // if err != nil {
  747. // logs.Error(err)
  748. // return err
  749. // }
  750. //
  751. // contractMap := make(map[int][]*model.Contract, 0)
  752. // for _, contract := range contractList {
  753. // contractMap[contract.BigID] = append(contractMap[contract.BigID], contract)
  754. // }
  755. //
  756. // pastureBigcontractList := make([]*model.PastureBigcontract, 0)
  757. // for bigId, m := range contractMap {
  758. // bigContract := new(model.BigContract)
  759. // err := s.d.DB.Where(" id = ? ", bigId).First(&bigContract).Error
  760. // if err != nil {
  761. // logs.Error(err)
  762. // return err
  763. // }
  764. //
  765. // if len(pastureBigcontractList) == 0 {
  766. // contractCode := strings.Split(bigContract.ContractCode, "-")[0]
  767. // pastureBigcontractList, _ = GetPastureBigContract(contractCode)
  768. // }
  769. // exist := false
  770. // for _, item := range pastureBigcontractList {
  771. // if item.PastureId == int(pastureId) {
  772. // exist = true
  773. // break
  774. // }
  775. // }
  776. // if !exist {
  777. // return errors.New("牧场没有该合同!")
  778. // }
  779. //
  780. // contractInfo := new(ContractInfo)
  781. // contractInfo.Contract = m
  782. // contractInfo.Provider = provider
  783. // contractInfo.Pasture = pasture
  784. // fmt.Println(contractInfo)
  785. // PushContractDataToSAP(bigContract, contractInfo, bigContract.ContractCode, true)
  786. // }
  787. //
  788. // return nil
  789. //}