contract.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "kpt.xdmy/apiserver/config"
  6. "strconv"
  7. "strings"
  8. "time"
  9. log "github.com/sirupsen/logrus"
  10. "kpt.xdmy/apiserver/model"
  11. "kpt.xdmy/apiserver/model/http"
  12. )
  13. const (
  14. //ContractToAspUrl = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
  15. ContractToAspDestID = "EQMAN"
  16. ContractToAspBussTp = "MM018"
  17. ContractLimit = 10
  18. )
  19. var (
  20. sapContractReq = &http.SapContractReq{
  21. Dest: &http.Dest{
  22. DestID: ContractToAspDestID,
  23. BussTp: ContractToAspBussTp,
  24. //Url: ContractToAspUrl,
  25. },
  26. DATA: &http.ContractDataToASP{},
  27. }
  28. loc, _ = time.LoadLocation("Local")
  29. )
  30. type ContractInfo struct {
  31. PastureBigContract *model.PastureBigcontract // 牧场和合同关联表
  32. Contract []*model.Contract // 合同子表
  33. Provider *model.Provider // 供应商
  34. Pasture *model.Pasture // 牧场
  35. }
  36. // GetBigContractData 获取主合同信息
  37. func GetBigContractData() []*model.BigContract {
  38. bigContractData := make([]*model.BigContract, 0)
  39. if err := s.d.DB.Where("isToSap = ?", 0).Where("SHStatus = ?", 7).Find(&bigContractData).Debug().Error; err != nil {
  40. log.Errorf("AutoContractToASP Error:%v", err)
  41. }
  42. for _, item := range bigContractData {
  43. if item.SHStatus == 7 && item.SHtype == 3 && item.CGChargedate.UnixNano() > time.Now().AddDate(0, 0, -1).UnixNano() {
  44. contractCodeSlice := strings.Split(item.ContractCode, "-")
  45. contractCode := contractCodeSlice[0]
  46. s.d.DB.Exec(`update contract set isToSap = 0 where enable = 1 and bigid in(select id from bigContract where like ? )`, "%"+contractCode+"%")
  47. }
  48. }
  49. return bigContractData
  50. }
  51. func GetPastureBigContract(contractCode string) ([]*model.PastureBigcontract, error) {
  52. pastureBigContract := make([]*model.PastureBigcontract, 0)
  53. SQL := fmt.Sprintf("select t.* from( "+
  54. " select pastureId as pasture_id ,enable FROM bigcontract WHERE contractCode LIKE '%s' and SHStatus = 7 GROUP BY pastureId "+
  55. " union all "+
  56. " 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"+
  57. " join pasture p on p.id = t.pasture_id "+
  58. " group by pasture_id", "%"+contractCode+"%", "%"+contractCode+"%")
  59. if err := s.d.DB.Raw(SQL).Find(&pastureBigContract).Error; err != nil {
  60. return nil, err
  61. }
  62. return pastureBigContract, nil
  63. }
  64. // GetContractInfo 获取合同相关信息
  65. func GetContractInfo(bigContract *model.BigContract, pastureBigContract *model.PastureBigcontract) *ContractInfo {
  66. //wg := sync.WaitGroup{}
  67. //wg.Add(3)
  68. contractInfo := &ContractInfo{}
  69. // 获取牧场数据合同数据
  70. //go func() {
  71. pasture := &model.Pasture{
  72. ID: pastureBigContract.PastureId,
  73. }
  74. if err := s.d.DB.First(pasture).Error; err != nil {
  75. log.Errorf("AutoContractToASP Pasture Err:%v,bigContractData:%v", err, bigContract)
  76. }
  77. contractInfo.Pasture = pasture
  78. // wg.Done()
  79. //}()
  80. // 获取合同子表数据
  81. //go func() {
  82. contract := make([]*model.Contract, 0)
  83. if err := s.d.DB.Debug().Where("bigId = ?", bigContract.ID).Where("isToSap = ? ", 0).Find(&contract).Error; err != nil {
  84. log.Errorf("AutoContractToASP contract Err:%v,bigContractData:%v", err, bigContract)
  85. }
  86. contractInfo.Contract = contract
  87. // wg.Done()
  88. //}()
  89. // 获取供应商数据
  90. //go func() {
  91. provider := &model.Provider{
  92. ID: bigContract.ProviderID,
  93. }
  94. if err := s.d.DB.First(provider).Error; err != nil {
  95. log.Errorf("AutoContractToASP Provider Err:%v,bigContractData:%v", err, bigContract)
  96. }
  97. contractInfo.Provider = provider
  98. // wg.Done()
  99. //}()
  100. //wg.Wait()
  101. return contractInfo
  102. }
  103. // http://192.168.61.117/SAPQ0/SRM/MM018/PurchaseInfo
  104. // AutoContractToASP 合同信息自动同步到ASP
  105. func AutoContractToASP() {
  106. sapContractReq.Url = config.Conf.Http.Routing + "/SRM/MM018/PurchaseInfo"
  107. bigContractData := GetBigContractData()
  108. ContractCodeMap := make(map[string][]*model.PastureBigcontract, 0)
  109. errDataList := make(map[string][]*model.ContractSapErr, 0)
  110. errBigContract := make([]*model.BigContract, 0)
  111. errContract := make([]*model.Contract, 0)
  112. for _, bigContract := range bigContractData {
  113. contractCode := bigContract.ContractCode
  114. if bigContract.SHStatus != 7 {
  115. continue
  116. }
  117. var count int64
  118. s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Where("isToSap = ?", 0).Count(&count)
  119. if count == 0 {
  120. continue
  121. }
  122. if strings.Contains(bigContract.ContractCode, "-") {
  123. contractCodeSlice := strings.Split(bigContract.ContractCode, "-")
  124. contractCode = contractCodeSlice[0]
  125. }
  126. var err error
  127. pastureBigContractSlice := make([]*model.PastureBigcontract, 0)
  128. if _, ok := ContractCodeMap[contractCode]; !ok {
  129. pastureBigContractSlice, err = GetPastureBigContract(contractCode)
  130. if err != nil {
  131. log.Errorf("AutoContractToASP pastureBigContractSlice Err:%v", err)
  132. continue
  133. }
  134. ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContractSlice...)
  135. //if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("contractCode like ? ", "%"+contractCode+"%").Update("isToSap", 1).Error; err != nil {
  136. // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  137. //}
  138. }
  139. if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Update("isToSap", 1).Error; err != nil {
  140. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  141. }
  142. if _, ok := ContractCodeMap[contractCode]; ok {
  143. contractInfoList := make([]*ContractInfo, 0)
  144. for _, pastureBigContract := range ContractCodeMap[contractCode] {
  145. // 获取合同相关数据集合
  146. contractInfo := GetContractInfo(bigContract, pastureBigContract)
  147. if len(contractInfo.Contract) == 0 || pastureBigContract.PastureId == 18 {
  148. continue
  149. }
  150. // 发送数据至SAP
  151. errData, bigContracts, contracts := PushContractDataToSAP(bigContract, contractInfo, contractCode)
  152. for _, data := range errData {
  153. errDataList[contractCode] = append(errDataList[contractCode], data...)
  154. }
  155. contractInfoList = append(contractInfoList, contractInfo)
  156. errBigContract = append(errBigContract, bigContracts...)
  157. errContract = append(errContract, contracts...)
  158. }
  159. for _, contractInfo := range contractInfoList {
  160. for _, c := range contractInfo.Contract {
  161. if c.IsToSap == 0 {
  162. if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 1).Error; err != nil {
  163. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. now := time.Now()
  171. for _, contractSapErr := range errDataList {
  172. for _, c := range contractSapErr {
  173. c.CreateTime = now
  174. s.d.DB.Create(&c)
  175. }
  176. }
  177. for _, item := range errBigContract {
  178. if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", item.ID).Update("isToSap", 0).Error; err != nil {
  179. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  180. }
  181. }
  182. for _, c := range errContract {
  183. if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 0).Error; err != nil {
  184. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  185. }
  186. }
  187. log.Info("AutoContractToASP Success")
  188. }
  189. // PushContractDataToSAP 同步数据给SAP
  190. func PushContractDataToSAP(bigContract *model.BigContract, contractInfo *ContractInfo, contractCode string) (map[string][]*model.ContractSapErr, []*model.BigContract, []*model.Contract) {
  191. errMap := make(map[string][]*model.ContractSapErr, 0)
  192. errBigContract := make([]*model.BigContract, 0)
  193. errContract := make([]*model.Contract, 0)
  194. for _, contract := range contractInfo.Contract {
  195. if contract.IsToSap == 1 {
  196. continue
  197. }
  198. LOEKZ := ""
  199. if contract.Enable == 0 {
  200. LOEKZ = "X"
  201. }
  202. startTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StartTime, loc)
  203. stopTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StopTime, loc)
  204. astr := strconv.FormatFloat(contract.Price, 'f', -1, 64)
  205. b := strings.Index(astr, ".")
  206. per := ""
  207. if len(astr[b+1:]) > 2 && b > 0 {
  208. num, _ := strconv.ParseFloat(astr[b+1:], 64)
  209. if num > 0 {
  210. contract.Price = contract.Price * 1000
  211. per = "1000"
  212. }
  213. }
  214. var isZeroStock int
  215. if bigContract.IsZeroStock == 1 {
  216. isZeroStock = 2
  217. } else {
  218. isZeroStock = 0
  219. }
  220. //LOEKZ = "X"
  221. contractDataToASP := &http.ContractDataToASP{
  222. MATNR: contract.PartCode,
  223. LIFNR: contractInfo.Provider.SapCode,
  224. EKORG: contractInfo.Pasture.ParchaseOrganization,
  225. WERKS: contractInfo.Pasture.FactoryCode,
  226. WAERS: "CNY",
  227. PEINH: per,
  228. NETPR: fmt.Sprintf("%.2f", contract.Price),
  229. MWSKZ: "J0",
  230. EKGRP: contractInfo.Pasture.PurchasingGroup,
  231. DATAB: startTime.Format("20060102"),
  232. DATBI: stopTime.Format("20060102"),
  233. LOEKZ: LOEKZ,
  234. ESOKZ: fmt.Sprintf("%d", isZeroStock),
  235. MEINS: contract.Unit,
  236. Code: bigContract.ContractCode,
  237. }
  238. sapContractReq.DATA = contractDataToASP
  239. rp := &http.SapContractResp{}
  240. rbyte, _ := json.Marshal(sapContractReq)
  241. fmt.Println(string(rbyte))
  242. if err := s.SyncSap(sapContractReq, rp, sapContractReq); err != nil {
  243. log.Errorf("AutoContractToASP SyncSap Err:%v contract:%v", err, contract)
  244. continue
  245. }
  246. log.Info(rp)
  247. if rp.Dest.Status != "S" {
  248. log.Errorf("AutoContractToASP SyncSap Error:%v,contract:%v", rp, contract)
  249. contract.PastureID = contractInfo.Pasture.ID
  250. errMap[contractCode] = append(errMap[contractCode], &model.ContractSapErr{
  251. ContractID: contract.ID,
  252. PastureID: contractInfo.Pasture.ID,
  253. PastureName: contractInfo.Pasture.Name,
  254. PartID: contract.PartID,
  255. PartName: contract.PartName,
  256. PartCode: contract.PartCode,
  257. Specification: contract.Specification,
  258. Price: contract.Price,
  259. BrandID: contract.BrandID,
  260. Brand: contract.Brand,
  261. InventoryType: contract.InventoryType,
  262. PlanAmount: contract.PlanAmount,
  263. Remark: contract.Remark,
  264. Enable: contract.Enable,
  265. Unit: contract.Unit,
  266. IsZeroStock: contract.IsZeroStock,
  267. ChangeID: contract.ChangeID,
  268. SapText: rp.MessText,
  269. ContractCode: contractCode,
  270. })
  271. }
  272. if rp.Dest.Status == "" {
  273. errContract = append(errContract, contract)
  274. }
  275. }
  276. if len(errContract) > 0 {
  277. errBigContract = append(errBigContract, bigContract)
  278. }
  279. return errMap, errBigContract, errContract
  280. }
  281. func RemoveRepeatedElement1(arr []string) (newArr []string) {
  282. newArr = make([]string, 0)
  283. for i := 0; i < len(arr); i++ {
  284. repeat := false
  285. for j := i + 1; j < len(arr); j++ {
  286. if arr[i] == arr[j] {
  287. repeat = true
  288. break
  289. }
  290. }
  291. if !repeat {
  292. newArr = append(newArr, arr[i])
  293. }
  294. }
  295. return
  296. }