transfer.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. _ "github.com/go-sql-driver/mysql"
  6. "github.com/jmoiron/sqlx"
  7. log "github.com/sirupsen/logrus"
  8. "kpt.xdmy/apiserver/config"
  9. "strconv"
  10. "time"
  11. "github.com/pkg/errors"
  12. "kpt.xdmy/apiserver/model"
  13. "kpt.xdmy/apiserver/model/http"
  14. )
  15. // 定时每分钟读取已完成 未同步的的调拨单,同步到sap
  16. // 每个调拨单定时同步只进行一次
  17. func autoTransfer() {
  18. bp := []model.BigPartTransfer{}
  19. if e := s.d.DB.Raw(`select b.* from bigparttransfer b join pasture p on b.pastureId = p.id
  20. where CreatDate >='2022-11-01' and p.vendor = 1 and b.sapStatus =0 and b.statue = 1`).Find(&bp, model.BigPartTransfer{Statue: 1}).Error; e != nil {
  21. log.Errorf("autoTransfer find :%v", e)
  22. return
  23. }
  24. log.Infof("sap transfer len=%d", len(bp))
  25. //for _, v := range bp {
  26. //err := SapTransfer(&v)
  27. //TransferLog("auto", v.ApplyCode, err)
  28. //}
  29. }
  30. // 手动同步调拨单到sap
  31. func ManualTransfer(bigPartTransferid float64, tx *sqlx.Tx) error {
  32. bp := model.BigPartTransfer{}
  33. //re := make(map[string]interface{})
  34. //r := s.d.DB.Where("CreatDate >'2022-10-28' ")
  35. //if code != "" {
  36. // r = r.Where("applycode", code)
  37. //}
  38. //r = r.Find(&bp, model.BigPartTransfer{Statue: 1})
  39. //if r.Error != nil {
  40. // log.Errorf("ManualTransfers find :%v", r.Error)
  41. // re["find"] = r.Error
  42. // return 0, re
  43. //}
  44. bigpartlaidByte, err := MapStr(tx, `select * from bigparttransfer where id = ? `, bigPartTransferid)
  45. if err != nil {
  46. log.Error(err)
  47. return errors.New("无入库信息!!!")
  48. }
  49. if bigpartlaidByte != nil {
  50. data := make(map[string]interface{})
  51. err := json.Unmarshal(bigpartlaidByte, &data)
  52. if err != nil {
  53. log.Error(err)
  54. return errors.New("入库详情转码出错!!!")
  55. }
  56. bp.ID, _ = strconv.Atoi(data["id"].(string))
  57. t, err := time.Parse("2006-01-02T15:04:05", data["creatDate"].(string)[:len("2006-01-02 15:04:05")])
  58. if err != nil {
  59. log.Error(err)
  60. return errors.New(fmt.Sprintf("入库详情转码出错 errors:%v", err))
  61. }
  62. bp.CreatDate = t
  63. t1, err := time.Parse("2006-01-02T15:04:05", data["applyDate"].(string)[:len("2006-01-02 15:04:05")])
  64. if err != nil {
  65. log.Error(err)
  66. return errors.New(fmt.Sprintf("入库详情转码出错 errors:%v", err))
  67. }
  68. bp.ApplyDate = t1
  69. bp.InPastureID, _ = strconv.Atoi(data["inPastureId"].(string))
  70. bp.PastureID, _ = strconv.Atoi(data["pastureId"].(string))
  71. bp.ApplyCode = data["applyCode"].(string)
  72. bp.LaidCode = data["laidCode"].(string)
  73. }
  74. err = SapTransfer(&bp, tx)
  75. if err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. // 调拨接口日志
  81. func TransferLog(c, code string, err error) {
  82. m := make(map[string]interface{})
  83. if err != nil {
  84. err = errors.Wrap(err, code)
  85. m["sapText"] = err.Error()
  86. if c == "auto" {
  87. m["sapStatus"] = 2
  88. }
  89. } else {
  90. m["sapStatus"] = 1
  91. }
  92. if e := s.d.DB.Model(&model.BigPartTransfer{}).Where("applycode =?", code).Updates(m).Error; e != nil {
  93. log.Errorf("BigPartTransfer update :%v", e)
  94. }
  95. }
  96. func SapTransfer(bp *model.BigPartTransfer, tx *sqlx.Tx) error {
  97. // 寄售(零库存)无调拨
  98. r := new(http.TransferReq)
  99. parts := []model.PartTransferStr{}
  100. blaid := new(model.BigPartLaid)
  101. inpa := new(model.Pasture)
  102. outpa := new(model.Pasture)
  103. //dp := new(model.Department)
  104. //if e := s.d.DB.Find(&parts, model.PartTransfer{BigID: bp.ID}).Error; e != nil || len(parts) == 0 {
  105. // return errors.Wrapf(e, "parttarnsfer query bigid: %d", bp.ID)
  106. //}
  107. partsByte, err := GetDataList(tx, `select * from parttransfer where bigid = ? `, []interface{}{bp.ID})
  108. if err != nil {
  109. log.Error(err)
  110. return errors.New("无入库信息!!!")
  111. }
  112. if partsByte != nil {
  113. err := json.Unmarshal(partsByte, &parts)
  114. if err != nil {
  115. log.Error(err)
  116. return errors.New("入库信息转码出错!!!")
  117. }
  118. }
  119. //fmt.Println(bp.LaidCode)
  120. //if e := s.d.DB.First(blaid, model.BigPartLaid{LaidCode: bp.LaidCode}).Error; e != nil {
  121. // return errors.Wrapf(e, "bigpartlaid query laidcode: %s", bp.LaidCode)
  122. //}
  123. bigpartlaidByte, err := MapStr(tx, `select * from bigpartlaid where laidCode = ? `, bp.LaidCode)
  124. if err != nil {
  125. log.Error(err)
  126. return errors.New("无入库信息!!!")
  127. }
  128. if bigpartlaidByte != nil {
  129. data := make(map[string]interface{})
  130. err := json.Unmarshal(bigpartlaidByte, &data)
  131. if err != nil {
  132. log.Error(err)
  133. return errors.New("入库详情转码出错!!!")
  134. }
  135. blaid.ID, _ = strconv.Atoi(data["id"].(string))
  136. t, err := time.Parse("2006-01-02T15:04:05", data["storageTime"].(string)[:len("2006-01-02 15:04:05")])
  137. if err != nil {
  138. log.Error(err)
  139. return errors.New(fmt.Sprintf("入库详情转码出错 errors:%v", err))
  140. }
  141. blaid.StorageTime = t
  142. }
  143. if e := s.d.DB.First(inpa, model.Pasture{ID: bp.InPastureID}).Error; e != nil {
  144. return errors.Wrapf(e, "pasture :id=%d", bp.InPastureID)
  145. }
  146. if e := s.d.DB.First(outpa, model.Pasture{ID: bp.PastureID}).Error; e != nil {
  147. return errors.Wrapf(e, "pasture :id=%d", bp.PastureID)
  148. }
  149. //if e := s.d.DB.First(dp, model.Department{ID: bp.DeptId}).Error; e != nil {
  150. // return errors.Wrapf(e, "pasture :id=%d", bp.DeptId)
  151. //}
  152. r.Dest.DestID = "EQMAN"
  153. r.Dest.BussTp = "MM012"
  154. //r.Dest.Url = "http://192.168.61.117/SAPP0/Common/MM012/TransferStock"
  155. r.Dest.Url = fmt.Sprintf("%s/Common/MM012/TransferStock", config.Conf.Http.Routing)
  156. r.Data.OrderDate = bp.CreatDate.Format("20060102")
  157. r.Data.PostDate = bp.ApplyDate.Format("20060102")
  158. r.Data.TransferCode = bp.ApplyCode
  159. r.Data.Group = inpa.PurchasingGroup
  160. // r.Data.Group = inpa.PurchasingGroup //"B11"
  161. for i, v := range parts {
  162. detail := new(http.TransferDetail)
  163. outStock := new(model.PartRepertory)
  164. pslaid := new(model.PartLaidStr)
  165. //inw := new(model.Warehouse)
  166. c := new(model.Contract)
  167. bc := new(model.BigContract)
  168. if e := s.d.DB.Where(" id = ?", v.PartRepID).First(outStock).Error; e != nil {
  169. return errors.Wrapf(e, "partrepertory query :id=%d", v.PartRepID)
  170. }
  171. bigpartlaidByte, err := MapStr(tx, `select * from partlaid where bigid = ? and partid = ? `, blaid.ID, v.PartID)
  172. if err != nil {
  173. log.Error(err)
  174. return errors.New("无入库信息!!!")
  175. }
  176. if bigpartlaidByte != nil {
  177. err := json.Unmarshal(bigpartlaidByte, &pslaid)
  178. if err != nil {
  179. log.Error(err)
  180. return errors.New("入库详情转码出错!!!")
  181. }
  182. }
  183. //if e := s.d.DB.First(inw, model.Warehouse{ID: pslaid.LocationID}).Error; e != nil {
  184. // return errors.Wrapf(e, "warehouse query :id=%d", pslaid.LocationID)
  185. //}
  186. if outStock.ContractID > 0 {
  187. // if e := s.d.DB.Table("BigContract").Select("BigContract.IsZeroStock ").Joins("join Contract c on c.bigid =BigContract.id").
  188. // Where("c.id =?", outStock.ContractID).First(bc).Error; e != nil {
  189. // return errors.Wrapf(e, "BigContract IsZeroStock :id=%d", outStock.ContractID)
  190. // }
  191. if e := s.d.DB.First(c, model.Contract{ID: outStock.ContractID}).Error; e != nil {
  192. }
  193. if e := s.d.DB.First(bc, model.BigContract{ID: c.BigID}).Error; e != nil {
  194. return errors.Wrapf(e, "bigcontract query :id=%d", c.BigID)
  195. }
  196. if bc.IsZeroStock == 1 {
  197. return errors.New("寄售无调拨")
  198. }
  199. }
  200. if v.RowNumber != nil {
  201. if v.RowNumber.(float64) > 0 {
  202. detail.RowNumber = fmt.Sprintf("%v", v.RowNumber)
  203. } else {
  204. detail.RowNumber = fmt.Sprintf("%d", (i + 1))
  205. }
  206. } else {
  207. detail.RowNumber = fmt.Sprintf("%d", (i + 1))
  208. }
  209. detail.MaterialCode = v.PartCode.(string)
  210. detail.Quantity = v.Amount.(string)
  211. detail.Unit = v.Unit.(string)
  212. detail.Per = "1"
  213. detail.DeliverDate = blaid.StorageTime.Format("20060102")
  214. detail.OutFactory = outpa.FactoryCode
  215. detail.InFactory = inpa.FactoryCode
  216. detail.NetPrice = outStock.Price
  217. detail.OutLocation = "1004"
  218. detail.InLocation = "1004"
  219. r.Data.Detail = append(r.Data.Detail, *detail)
  220. _, err = tx.Exec(` update parttransfer set rowNumber = ? where id = ? `, detail.RowNumber, v.ID)
  221. if err != nil {
  222. log.Error(err)
  223. return errors.New("修改调拨行号出错!!!")
  224. }
  225. }
  226. rp := new(http.TransferResp)
  227. rbyte, _ := json.Marshal(r)
  228. fmt.Println(string(rbyte))
  229. if e := s.SyncSapTransfer(r, rp, rbyte); e != nil {
  230. log.Info(r)
  231. s.AddSapLog(r, e)
  232. return errors.Wrapf(e, "transfer error:%v")
  233. } else {
  234. s.AddSapLog(r, e)
  235. if rp.Dest.Status == "S" {
  236. log.Printf("transfer success:%s,%s", rp.Data.Proof.EqTransferCode, rp.Data.Proof.ProofCode)
  237. _, err = tx.Exec(` update bigparttransfer set ProofCode = ?,sapStatus = 1 where id = ? `, rp.Data.Proof.ProofCode, bp.ID)
  238. if err != nil {
  239. log.Error(err)
  240. //return errors.New("修改调拨状态出错!!!")
  241. }
  242. return nil
  243. } else {
  244. return errors.New(fmt.Sprintf("tranfer fail: %s", rp.Dest.MessText))
  245. }
  246. }
  247. }