contract.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. contractInfoList := make([]*ContractInfo, 0)
  111. for _, bigContract := range bigContractData {
  112. contractCode := bigContract.ContractCode
  113. if bigContract.SHStatus != 7 {
  114. continue
  115. }
  116. var count int64
  117. s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Where("isToSap = ?", 0).Count(&count)
  118. if count == 0 {
  119. continue
  120. }
  121. if strings.Contains(bigContract.ContractCode, "-") {
  122. contractCodeSlice := strings.Split(bigContract.ContractCode, "-")
  123. contractCode = contractCodeSlice[0]
  124. }
  125. var err error
  126. pastureBigContractSlice := make([]*model.PastureBigcontract, 0)
  127. if _, ok := ContractCodeMap[contractCode]; !ok {
  128. pastureBigContractSlice, err = GetPastureBigContract(contractCode)
  129. if err != nil {
  130. log.Errorf("AutoContractToASP pastureBigContractSlice Err:%v", err)
  131. continue
  132. }
  133. ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContractSlice...)
  134. //if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("contractCode like ? ", "%"+contractCode+"%").Update("isToSap", 1).Error; err != nil {
  135. // log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  136. //}
  137. }
  138. if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", bigContract.ID).Update("isToSap", 1).Error; err != nil {
  139. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  140. }
  141. if _, ok := ContractCodeMap[contractCode]; ok {
  142. for _, pastureBigContract := range ContractCodeMap[contractCode] {
  143. // 获取合同相关数据集合
  144. contractInfo := GetContractInfo(bigContract, pastureBigContract)
  145. if len(contractInfo.Contract) == 0 || pastureBigContract.PastureId == 18 {
  146. continue
  147. }
  148. // 发送数据至SAP
  149. errData := PushContractDataToSAP(bigContract, contractInfo, contractCode)
  150. for _, data := range errData {
  151. errDataList[contractCode] = append(errDataList[contractCode], data...)
  152. }
  153. contractInfoList = append(contractInfoList, contractInfo)
  154. //go func(bigContract *model.BigContract, contractInfo *ContractInfo) {
  155. //}(bigContract, contractInfo)
  156. //ContractCodeMap[contractCode] = append(ContractCodeMap[contractCode], pastureBigContract)
  157. }
  158. }
  159. }
  160. now := time.Now()
  161. for _, contractSapErr := range errDataList {
  162. for _, c := range contractSapErr {
  163. c.CreateTime = now
  164. s.d.DB.Create(&c)
  165. }
  166. }
  167. for _, contractInfo := range contractInfoList {
  168. for _, c := range contractInfo.Contract {
  169. if c.IsToSap == 0 {
  170. if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 1).Error; err != nil {
  171. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  172. }
  173. }
  174. }
  175. }
  176. log.Info("AutoContractToASP Success")
  177. }
  178. // PushContractDataToSAP 同步数据给SAP
  179. func PushContractDataToSAP(bigContract *model.BigContract, contractInfo *ContractInfo, contractCode string) map[string][]*model.ContractSapErr {
  180. errMap := make(map[string][]*model.ContractSapErr, 0)
  181. for _, contract := range contractInfo.Contract {
  182. if contract.IsToSap == 1 {
  183. continue
  184. }
  185. LOEKZ := ""
  186. if contract.Enable == 0 {
  187. LOEKZ = "X"
  188. }
  189. startTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StartTime, loc)
  190. stopTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StopTime, loc)
  191. astr := strconv.FormatFloat(contract.Price, 'f', -1, 64)
  192. b := strings.Index(astr, ".")
  193. per := ""
  194. if len(astr[b+1:]) > 2 && b > 0 {
  195. num, _ := strconv.ParseFloat(astr[b+1:], 64)
  196. if num > 0 {
  197. contract.Price = contract.Price * 1000
  198. per = "1000"
  199. }
  200. }
  201. var isZeroStock int
  202. if bigContract.IsZeroStock == 1 {
  203. isZeroStock = 2
  204. } else {
  205. isZeroStock = 0
  206. }
  207. //LOEKZ = "X"
  208. contractDataToASP := &http.ContractDataToASP{
  209. MATNR: contract.PartCode,
  210. LIFNR: contractInfo.Provider.SapCode,
  211. EKORG: contractInfo.Pasture.ParchaseOrganization,
  212. WERKS: contractInfo.Pasture.FactoryCode,
  213. WAERS: "CNY",
  214. PEINH: per,
  215. NETPR: fmt.Sprintf("%.2f", contract.Price),
  216. MWSKZ: "J0",
  217. EKGRP: contractInfo.Pasture.PurchasingGroup,
  218. DATAB: startTime.Format("20060102"),
  219. DATBI: stopTime.Format("20060102"),
  220. LOEKZ: LOEKZ,
  221. ESOKZ: fmt.Sprintf("%d", isZeroStock),
  222. MEINS: contract.Unit,
  223. Code: bigContract.ContractCode,
  224. }
  225. sapContractReq.DATA = contractDataToASP
  226. rp := &http.SapContractResp{}
  227. rbyte, _ := json.Marshal(sapContractReq)
  228. fmt.Println(string(rbyte))
  229. if err := s.SyncSap(sapContractReq, rp, sapContractReq); err != nil {
  230. log.Errorf("AutoContractToASP SyncSap Err:%v contract:%v", err, contract)
  231. continue
  232. }
  233. log.Info(rp)
  234. if rp.Dest.Status != "S" {
  235. log.Errorf("AutoContractToASP SyncSap Error:%v,contract:%v", rp, contract)
  236. contract.PastureID = contractInfo.Pasture.ID
  237. errMap[contractCode] = append(errMap[contractCode], &model.ContractSapErr{
  238. ContractID: contract.ID,
  239. PastureID: contractInfo.Pasture.ID,
  240. PastureName: contractInfo.Pasture.Name,
  241. PartID: contract.PartID,
  242. PartName: contract.PartName,
  243. PartCode: contract.PartCode,
  244. Specification: contract.Specification,
  245. Price: contract.Price,
  246. BrandID: contract.BrandID,
  247. Brand: contract.Brand,
  248. InventoryType: contract.InventoryType,
  249. PlanAmount: contract.PlanAmount,
  250. Remark: contract.Remark,
  251. Enable: contract.Enable,
  252. Unit: contract.Unit,
  253. IsZeroStock: contract.IsZeroStock,
  254. ChangeID: contract.ChangeID,
  255. SapText: rp.MessText,
  256. ContractCode: contractCode,
  257. })
  258. }
  259. }
  260. return errMap
  261. }
  262. func RemoveRepeatedElement1(arr []string) (newArr []string) {
  263. newArr = make([]string, 0)
  264. for i := 0; i < len(arr); i++ {
  265. repeat := false
  266. for j := i + 1; j < len(arr); j++ {
  267. if arr[i] == arr[j] {
  268. repeat = true
  269. break
  270. }
  271. }
  272. if !repeat {
  273. newArr = append(newArr, arr[i])
  274. }
  275. }
  276. return
  277. }