contract.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.Debug().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, -5).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 contractCode 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. var idList []int
  160. for _, contractInfo := range contractInfoList {
  161. for _, c := range contractInfo.Contract {
  162. if c.IsToSap == 0 {
  163. exist := false
  164. for _, id := range idList {
  165. if id == c.ID {
  166. exist = true
  167. break
  168. }
  169. }
  170. if !exist {
  171. idList = append(idList, c.ID)
  172. if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 1).Error; err != nil {
  173. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. now := time.Now()
  182. for _, contractSapErr := range errDataList {
  183. for _, c := range contractSapErr {
  184. c.CreateTime = now
  185. s.d.DB.Create(&c)
  186. }
  187. }
  188. for _, item := range errBigContract {
  189. if err := s.d.DB.Debug().Table(new(model.BigContract).TableName()).Where("id = ? ", item.ID).Update("isToSap", 0).Error; err != nil {
  190. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  191. }
  192. }
  193. for _, c := range errContract {
  194. if err := s.d.DB.Table("contract").Where("id = ?", c.ID).Update("isToSap", 0).Error; err != nil {
  195. log.Errorf("AutoContractToASP SQLUpdate Error:%v", err)
  196. }
  197. }
  198. log.Info("AutoContractToASP Success")
  199. }
  200. // PushContractDataToSAP 同步数据给SAP
  201. func PushContractDataToSAP(bigContract *model.BigContract, contractInfo *ContractInfo, contractCode string) (map[string][]*model.ContractSapErr, []*model.BigContract, []*model.Contract) {
  202. errMap := make(map[string][]*model.ContractSapErr, 0)
  203. errBigContract := make([]*model.BigContract, 0)
  204. errContract := make([]*model.Contract, 0)
  205. for _, contract := range contractInfo.Contract {
  206. if contract.IsToSap == 1 {
  207. continue
  208. }
  209. LOEKZ := ""
  210. if contract.Enable == 0 {
  211. LOEKZ = "X"
  212. }
  213. startTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StartTime, loc)
  214. stopTime, _ := time.ParseInLocation(time.RFC3339, bigContract.StopTime, loc)
  215. astr := strconv.FormatFloat(contract.Price, 'f', -1, 64)
  216. b := strings.Index(astr, ".")
  217. per := ""
  218. if len(astr[b+1:]) > 2 && b > 0 {
  219. num, _ := strconv.ParseFloat(astr[b+1:], 64)
  220. if num > 0 {
  221. contract.Price = contract.Price * 1000
  222. per = "1000"
  223. }
  224. }
  225. var isZeroStock int
  226. if bigContract.IsZeroStock == 1 {
  227. isZeroStock = 2
  228. } else {
  229. isZeroStock = 0
  230. }
  231. //LOEKZ = "X"
  232. contractDataToASP := &http.ContractDataToASP{
  233. MATNR: contract.PartCode,
  234. LIFNR: contractInfo.Provider.SapCode,
  235. EKORG: contractInfo.Pasture.ParchaseOrganization,
  236. WERKS: contractInfo.Pasture.FactoryCode,
  237. WAERS: "CNY",
  238. PEINH: per,
  239. NETPR: fmt.Sprintf("%.2f", contract.Price),
  240. MWSKZ: "J0",
  241. EKGRP: contractInfo.Pasture.PurchasingGroup,
  242. DATAB: startTime.Format("20060102"),
  243. DATBI: stopTime.Format("20060102"),
  244. LOEKZ: LOEKZ,
  245. ESOKZ: fmt.Sprintf("%d", isZeroStock),
  246. MEINS: contract.Unit,
  247. Code: bigContract.ContractCode,
  248. }
  249. sapContractReq.DATA = contractDataToASP
  250. rp := &http.SapContractResp{}
  251. rbyte, _ := json.Marshal(sapContractReq)
  252. fmt.Println(string(rbyte))
  253. if err := s.SyncSap(sapContractReq, rp, sapContractReq); err != nil {
  254. log.Errorf("AutoContractToASP SyncSap Err:%v contract:%v", err, contract)
  255. //continue
  256. }
  257. log.Info(rp)
  258. if rp.Dest.Status != "S" {
  259. log.Errorf("AutoContractToASP SyncSap Error:%v,contract:%v", rp, contract)
  260. contract.PastureID = contractInfo.Pasture.ID
  261. errMap[contractCode] = append(errMap[contractCode], &model.ContractSapErr{
  262. ContractID: contract.ID,
  263. PastureID: contractInfo.Pasture.ID,
  264. PastureName: contractInfo.Pasture.Name,
  265. PartID: contract.PartID,
  266. PartName: contract.PartName,
  267. PartCode: contract.PartCode,
  268. Specification: contract.Specification,
  269. Price: contract.Price,
  270. BrandID: contract.BrandID,
  271. Brand: contract.Brand,
  272. InventoryType: contract.InventoryType,
  273. PlanAmount: contract.PlanAmount,
  274. Remark: contract.Remark,
  275. Enable: contract.Enable,
  276. Unit: contract.Unit,
  277. IsZeroStock: contract.IsZeroStock,
  278. ChangeID: contract.ChangeID,
  279. SapText: rp.MessText,
  280. ContractCode: contractCode,
  281. })
  282. }
  283. if rp.Dest.Status == "" {
  284. errContract = append(errContract, contract)
  285. }
  286. }
  287. if len(errContract) > 0 {
  288. errBigContract = append(errBigContract, bigContract)
  289. }
  290. return errMap, errBigContract, errContract
  291. }
  292. func RemoveRepeatedElement1(arr []string) (newArr []string) {
  293. newArr = make([]string, 0)
  294. for i := 0; i < len(arr); i++ {
  295. repeat := false
  296. for j := i + 1; j < len(arr); j++ {
  297. if arr[i] == arr[j] {
  298. repeat = true
  299. break
  300. }
  301. }
  302. if !repeat {
  303. newArr = append(newArr, arr[i])
  304. }
  305. }
  306. return
  307. }