webservice.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "github.com/pkg/errors"
  12. "github.com/robfig/cron"
  13. "kpt.xdmy/apiserver/routers/restful"
  14. "kpt.xdmy/pkg/logging"
  15. )
  16. //eas webservice 目前使用字符串拼接的方式模拟xml发送
  17. var (
  18. LoginStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="http://login.webservice.bos.kingdee.com">
  19. <soapenv:Header/>
  20. <soapenv:Body>
  21. <log:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  22. <userName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
  23. <password xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">password?</password>
  24. <slnName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
  25. <dcName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
  26. <language xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
  27. <dbType xsi:type="xsd:int">dbType?</dbType>
  28. </log:login>
  29. </soapenv:Body>
  30. </soapenv:Envelope>
  31. `
  32. LogoutStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="http://login.webservice.bos.kingdee.com">
  33. <soapenv:Header/>
  34. <soapenv:Body>
  35. <log:logout soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  36. <userName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
  37. <slnName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
  38. <dcName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
  39. <language xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
  40. </log:logout>
  41. </soapenv:Body>
  42. </soapenv:Envelope>
  43. `
  44. InsertPartsStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.synfacade.modernfarming.eas.kingdee.com">
  45. <soapenv:Header>
  46. <ns1:SessionId
  47. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  48. </ns1:SessionId>
  49. </soapenv:Header>
  50. <soapenv:Body>
  51. <web:synMaterial soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  52. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</request>
  53. </web:synMaterial>
  54. </soapenv:Body>
  55. </soapenv:Envelope>
  56. `
  57. QureyPartsStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.synfacade.modernfarming.eas.kingdee.com">
  58. <soapenv:Header>
  59. <ns1:SessionId
  60. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  61. </ns1:SessionId>
  62. </soapenv:Header>
  63. <soapenv:Body>
  64. <web:queryMaterialData soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  65. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{"number":"strJson?"}</request>
  66. </web:queryMaterialData>
  67. </soapenv:Body>
  68. </soapenv:Envelope>`
  69. InsertPartsuselaidStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="http://app.ws.scm.eas.kingdee.com">
  70. <soapenv:Header>
  71. <ns1:SessionId
  72. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  73. </ns1:SessionId>
  74. </soapenv:Header>
  75. <soapenv:Body>
  76. <app:importBill soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  77. <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
  78. <billDataJSON xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</billDataJSON>
  79. <billStatus xsi:type="xsd:int">2</billStatus>
  80. </app:importBill>
  81. </soapenv:Body>
  82. </soapenv:Envelope>
  83. `
  84. QureyBill = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="http://app.ws.scm.eas.kingdee.com">
  85. <soapenv:Header>
  86. <ns1:SessionId
  87. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  88. </ns1:SessionId>
  89. </soapenv:Header>
  90. <soapenv:Body>
  91. <app:queryScmBillData soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  92. <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
  93. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{"number":"strJson?"}</request>
  94. </app:queryScmBillData>
  95. </soapenv:Body>
  96. </soapenv:Envelope>`
  97. )
  98. var (
  99. singelChan chan struct{}
  100. lasttime time.Time
  101. sessionId string
  102. sessionidChan chan string
  103. )
  104. var user EASLoginInfo
  105. type EASLoginInfo struct {
  106. UserName string `json:"userName"`
  107. Password string `json:"password"`
  108. SlnName string `json:"slnName"`
  109. DcName string `json:"dcName"`
  110. Language string `json:"language"`
  111. DbType string `json:"dbType"`
  112. Url string `json:"dbType"`
  113. }
  114. type Response struct {
  115. Body rspBody `xml:"Body" json:"body"`
  116. }
  117. type rspBody struct {
  118. Record []rqRecord `xml:"multiRef" json:"multiRef"`
  119. SynMaterialResponse SMReturn `xml:"synMaterialResponse" json:"SynMaterialResponse"`
  120. ImportBillResponse ImportBillResponse `xml:"importBillResponse" json:"importBillResponse"`
  121. QueryMaterialDataResponse QureyParts `xml:"queryMaterialDataResponse" json:"queryMaterialDataResponse"`
  122. QueryScmBillDataResponse QureyBills `xml:"queryScmBillDataResponse" json:"queryScmBillDataResponse"`
  123. }
  124. type ImportBillResponse struct {
  125. ImportBillReturn string `xml:"importBillReturn" json:"importBillReturn"`
  126. }
  127. type QureyParts struct {
  128. QueryMaterialDataReturn string `xml:"queryMaterialDataReturn" json:"queryMaterialDataReturn"`
  129. }
  130. type QureyBills struct {
  131. QueryScmBillDataReturn string `xml:"queryScmBillDataReturn" json:"queryScmBillDataReturn"`
  132. }
  133. type SMReturn struct {
  134. SynMaterialReturn string `xml:"synMaterialReturn" json:"synMaterialReturn"`
  135. }
  136. type rqRecord struct {
  137. SessionId string `xml:"sessionId" json:"sessionId"`
  138. }
  139. func InitWebS(use, pass, url string) {
  140. user.Password = pass
  141. user.UserName = use
  142. user.DbType = "1"
  143. //user.DcName = "MASM" //正式环境是 XDMY ,备件先手动改为XDMY,后面全部切换正式环境后统一修改
  144. user.DcName = "XDMY" //正式环境是 XDMY ,备件先手动改为XDMY,后面全部切换正式环境后统一修改
  145. user.Language = "L2"
  146. user.SlnName = "eas"
  147. user.Url = url
  148. singelChan = make(chan struct{}, 100)
  149. sessionidChan = make(chan string, 100)
  150. lasttime = time.Now().Add(-6 * time.Minute)
  151. go Runsessionid()
  152. }
  153. // sessionId 获取,本次请求与上次间隔5分钟以上 就重新登录
  154. func Runsessionid() {
  155. for {
  156. func() {
  157. defer func() {
  158. if err := recover(); err != nil {
  159. logging.Error("Runsessionid recover err", err)
  160. }
  161. }()
  162. select {
  163. case <-singelChan:
  164. if time.Now().Sub(lasttime) > 5*time.Minute { // 大于五分钟重新退出再登录
  165. println("重新登录")
  166. //EASWebServiceLogout()
  167. sd, err := EASWebServiceLogin("")
  168. if err != nil {
  169. sessionidChan <- "error: " + err.Error()
  170. } else {
  171. lasttime = time.Now()
  172. sessionId = sd
  173. sessionidChan <- sessionId
  174. }
  175. } else {
  176. sessionidChan <- sessionId
  177. }
  178. }
  179. }()
  180. }
  181. }
  182. //登录
  183. func EASWebServiceLogin(url string) (string, error) {
  184. client := &http.Client{}
  185. resstr := ReplaceStrWebSevice("login", user.UserName, user.Password, user.SlnName, user.DcName, user.Language, user.DbType, "", "", "")
  186. if url == "" {
  187. url = user.Url
  188. } else {
  189. resstr = strings.ReplaceAll(resstr, "MASM", "XDMY") // dcname正式和测试环境不一致
  190. }
  191. log.Default().Println("resstr:", resstr)
  192. body := bytes.NewBuffer([]byte(resstr))
  193. req, err := http.NewRequest("POST",
  194. url+"/ormrpc/services/EASLogin?wsdl",
  195. body,
  196. )
  197. req.Header.Set("Content-Type", "text/xml")
  198. req.Header.Set("SOAPAction", "application/soap+xml")
  199. if err != nil {
  200. return "", err
  201. }
  202. resp, err := client.Do(req)
  203. if err != nil {
  204. fmt.Println(err)
  205. return "", err
  206. }
  207. defer resp.Body.Close()
  208. result_body, err := ioutil.ReadAll(resp.Body)
  209. if err != nil {
  210. fmt.Println(err)
  211. return "", err
  212. }
  213. v := Response{}
  214. err = xml.Unmarshal([]byte(string(result_body)), &v)
  215. if err != nil {
  216. fmt.Println(err.Error())
  217. }
  218. if len(v.Body.Record) > 0 {
  219. return v.Body.Record[0].SessionId, nil
  220. }
  221. return "", err
  222. }
  223. //注销
  224. func EASWebServiceLogout() error {
  225. client := &http.Client{}
  226. resstr := ReplaceStrWebSevice("logout", user.UserName, "", user.SlnName, user.DcName, user.Language, "", "", "", "")
  227. body := bytes.NewBuffer([]byte(resstr))
  228. req, err := http.NewRequest("POST",
  229. user.Url+"/ormrpc/services/EASLogin?wsdl",
  230. body,
  231. )
  232. req.Header.Set("Content-Type", "text/xml")
  233. req.Header.Set("SOAPAction", "application/soap+xml")
  234. if err != nil {
  235. // handle error
  236. }
  237. resp, err := client.Do(req)
  238. if err != nil {
  239. return err
  240. }
  241. defer resp.Body.Close()
  242. _, err = ioutil.ReadAll(resp.Body)
  243. if err != nil {
  244. return err
  245. }
  246. return nil
  247. //fmt.Println(string(result_body))
  248. }
  249. func EASWebServiceQureyParts(SessionId, PartCode string) error {
  250. client := &http.Client{}
  251. resstr := ReplaceStrWebSevice("QureyPartsStr", "", "", "", "", "", "", SessionId, PartCode, "")
  252. body := bytes.NewBuffer([]byte(resstr))
  253. req, err := http.NewRequest("POST",
  254. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  255. body,
  256. )
  257. req.Header.Set("Content-Type", "text/xml")
  258. req.Header.Set("SOAPAction", "application/soap+xml")
  259. if err != nil {
  260. // handle error
  261. }
  262. resp, err := client.Do(req)
  263. if err != nil {
  264. return err
  265. }
  266. defer resp.Body.Close()
  267. result_body, err := ioutil.ReadAll(resp.Body)
  268. if err != nil {
  269. return err
  270. }
  271. v := Response{}
  272. err = xml.Unmarshal(result_body, &v)
  273. if err != nil {
  274. fmt.Println(err.Error())
  275. }
  276. resmap := make(map[string]interface{}, 0)
  277. err = json.Unmarshal([]byte(v.Body.QueryMaterialDataResponse.QueryMaterialDataReturn), &resmap)
  278. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  279. switch resmap["data"].(type) {
  280. case []interface{}:
  281. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  282. return errors.New("单据未查询到")
  283. }
  284. default:
  285. return errors.New("单据未查询到")
  286. }
  287. return nil
  288. }
  289. func EASWebServiceQureyBill(SessionId, BillCode, billType string) error {
  290. client := &http.Client{}
  291. resstr := ReplaceStrWebSevice("QureyBill", "", "", "", "", "", "", SessionId, BillCode, billType)
  292. body := bytes.NewBuffer([]byte(resstr))
  293. req, err := http.NewRequest("POST",
  294. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  295. body,
  296. )
  297. req.Header.Set("Content-Type", "text/xml")
  298. req.Header.Set("SOAPAction", "application/soap+xml")
  299. if err != nil {
  300. return err
  301. }
  302. resp, err := client.Do(req)
  303. if err != nil {
  304. return err
  305. }
  306. defer resp.Body.Close()
  307. result_body, err := ioutil.ReadAll(resp.Body)
  308. fmt.Println(string(result_body))
  309. if err != nil {
  310. fmt.Println(err)
  311. return err
  312. }
  313. v := Response{}
  314. err = xml.Unmarshal(result_body, &v)
  315. if err != nil {
  316. fmt.Println(err.Error())
  317. }
  318. resmap := make(map[string]interface{}, 0)
  319. err = json.Unmarshal([]byte(v.Body.QueryScmBillDataResponse.QueryScmBillDataReturn), &resmap)
  320. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  321. switch resmap["data"].(type) {
  322. case []interface{}:
  323. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  324. return errors.New("单据未查询到")
  325. }
  326. default:
  327. return errors.New("单据未查询到")
  328. }
  329. return nil
  330. }
  331. func EASWebServicInsertParts(SessionId string, parts Parts, url string) error {
  332. client := &http.Client{}
  333. soapencJson, err := json.Marshal(parts)
  334. if err != nil {
  335. return err
  336. }
  337. if url == "" {
  338. url = user.Url
  339. }
  340. println(string(soapencJson))
  341. resstr := ReplaceStrWebSevice("InsertParts", "", "", "", "", "", "", SessionId, string(soapencJson), "")
  342. body := bytes.NewBuffer([]byte(resstr))
  343. req, err := http.NewRequest("POST",
  344. url+"/ormrpc/services/WSSynMasterDataFacade?wsdl",
  345. body,
  346. )
  347. req.Header.Set("Content-Type", "text/xml")
  348. req.Header.Set("SOAPAction", "application/soap+xml")
  349. if err != nil {
  350. // handle error
  351. }
  352. resp, err := client.Do(req)
  353. if err != nil {
  354. fmt.Println(err)
  355. return err
  356. }
  357. defer resp.Body.Close()
  358. result_body, err := ioutil.ReadAll(resp.Body)
  359. if err != nil {
  360. fmt.Println(err)
  361. return err
  362. }
  363. v := Response{}
  364. err = xml.Unmarshal(result_body, &v)
  365. if err != nil {
  366. fmt.Println(err.Error())
  367. }
  368. resmap := make(map[string]string, 0)
  369. err = json.Unmarshal([]byte(v.Body.SynMaterialResponse.SynMaterialReturn), &resmap)
  370. _, ok := resmap["success"]
  371. if !ok {
  372. return errors.New(string(result_body))
  373. }
  374. if resmap["success"] == "0" {
  375. return errors.New(resmap["message"])
  376. }
  377. return nil
  378. }
  379. // 登录系统写入数据
  380. func EASWebServicInsertPartsuselaid(SessionId, strJson, billType string) error { //billType采购入库单:006 销售出库单:007 其他入库单:008 其他出库单:009 领料出库单:100 生产入库单:101
  381. client := &http.Client{}
  382. resstr := ReplaceStrWebSevice("InsertPartsuselaidStr", "", "", "", "", "", "", SessionId, strJson, billType)
  383. body := bytes.NewBuffer([]byte(resstr))
  384. req, err := http.NewRequest("POST",
  385. user.Url+"/ormrpc/services/WSSCMWebServiceFacade?wsdl",
  386. body,
  387. )
  388. req.Header.Set("Content-Type", "text/xml")
  389. req.Header.Set("SOAPAction", "application/soap+xml")
  390. if err != nil {
  391. // handle error
  392. }
  393. resp, err := client.Do(req)
  394. if err != nil {
  395. fmt.Println(err)
  396. return err
  397. }
  398. defer resp.Body.Close()
  399. result_body, err := ioutil.ReadAll(resp.Body)
  400. if err != nil {
  401. fmt.Println(err)
  402. return err
  403. }
  404. v := Response{}
  405. err = xml.Unmarshal(result_body, &v)
  406. if err != nil {
  407. fmt.Println(err.Error())
  408. }
  409. resmap := make(map[string]interface{}, 0)
  410. if v.Body.ImportBillResponse.ImportBillReturn != "" {
  411. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  412. fmt.Println(v.Body.ImportBillResponse.ImportBillReturn)
  413. //_,ok := resmap["billErrors"]
  414. //if !ok {
  415. // println(string(result_body))
  416. // return errors.New(string(result_body))
  417. //}
  418. _, ok := resmap["status"]
  419. if !ok {
  420. println(string(result_body))
  421. return errors.New(string(result_body))
  422. }
  423. //if len(resmap["billErrors"].([]interface{})) > 0 || resmap["status"] != "1" {
  424. if resmap["status"] != "0" {
  425. resmap := make(map[string]interface{}, 0)
  426. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  427. switch resmap["billErrors"].(type) {
  428. case []interface{}:
  429. if len(resmap["billErrors"].([]interface{})) > 0 {
  430. switch resmap["billErrors"].([]interface{})[0].(type) {
  431. case map[string]interface{}:
  432. str := resmap["billErrors"].([]interface{})[0].(map[string]interface{})["errorMsgs"]
  433. switch str.(type) {
  434. case []interface{}:
  435. if len(str.([]interface{})) > 0 {
  436. s := str.([]interface{})[0]
  437. switch s.(type) {
  438. case string:
  439. return errors.New(s.(string))
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. return errors.New(v.Body.ImportBillResponse.ImportBillReturn)
  447. }
  448. }
  449. return nil
  450. }
  451. //
  452. func ReplaceStrWebSevice(typeIn, userName, password, slnName, dcName, language, dbType, SessionId, strJson, billType string) string {
  453. res := ""
  454. switch typeIn {
  455. case "login":
  456. res = LoginStr
  457. res = strings.ReplaceAll(res, "userName?", userName)
  458. res = strings.ReplaceAll(res, "password?", password)
  459. res = strings.ReplaceAll(res, "slnName?", slnName)
  460. res = strings.ReplaceAll(res, "dcName?", dcName)
  461. res = strings.ReplaceAll(res, "language?", language)
  462. res = strings.ReplaceAll(res, "dbType?", dbType)
  463. case "logout":
  464. res = LogoutStr
  465. res = strings.ReplaceAll(res, "userName?", userName)
  466. res = strings.ReplaceAll(res, "slnName?", slnName)
  467. res = strings.ReplaceAll(res, "dcName?", dcName)
  468. res = strings.ReplaceAll(res, "language?", language)
  469. case "InsertParts":
  470. res = InsertPartsStr
  471. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  472. res = strings.ReplaceAll(res, "strJson?", strJson)
  473. case "QureyPartsStr":
  474. res = QureyPartsStr
  475. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  476. res = strings.ReplaceAll(res, "strJson?", strJson)
  477. case "InsertPartsuselaidStr":
  478. res = InsertPartsuselaidStr
  479. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  480. res = strings.ReplaceAll(res, "strJson?", strJson)
  481. res = strings.ReplaceAll(res, "billType?", billType)
  482. case "QureyBill":
  483. res = QureyBill
  484. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  485. res = strings.ReplaceAll(res, "strJson?", strJson)
  486. res = strings.ReplaceAll(res, "billType?", billType)
  487. }
  488. return res
  489. }
  490. func CronServer() {
  491. c := cron.New()
  492. log.Println("Starting CronWXworkkpt.xdmy/pkg.")
  493. err := c.AddFunc("0 1 1 * *", Pastures) // 每月1号1点
  494. err = c.AddFunc("1 1 1 * *", PartClass) // 每月1号1点1分
  495. if err != nil {
  496. log.Print("定时任务出错___", err.Error())
  497. }
  498. c.Start()
  499. }
  500. func Pastures() {
  501. //fmt.Println("Dingshi")
  502. tx, err1 := restful.Dbs.Beginx()
  503. defer func() {
  504. switch {
  505. case err1 != nil:
  506. println("tx.Begin 事务启动失败__error:", err1.Error())
  507. if tx != nil {
  508. tx.Rollback()
  509. }
  510. default:
  511. if tx != nil {
  512. err1 = tx.Commit()
  513. }
  514. }
  515. }()
  516. queryPasture, err := restful.MapStrListTx(tx, "select sum(p_r.reportery) reportery, w2.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime "+
  517. " from part_repertory p_r "+
  518. " left join warehouse w2 on w2.id = p_r.locationId "+
  519. " GROUP BY w2.pastureId ")
  520. fmt.Println(queryPasture)
  521. if len(queryPasture) > 0 {
  522. for i := 0; i < len(queryPasture); i++ {
  523. reportery := queryPasture[i]["reportery"]
  524. pastureId := queryPasture[i]["pastureId"]
  525. dateTime := queryPasture[i]["dateTime"]
  526. _, err = tx.Exec("insert into inventory_temporary(currentInventory,pastureId,inventoryDate) VALUES(?,?,?)", reportery, pastureId, dateTime)
  527. }
  528. }
  529. go YuechuRuku()
  530. go Yuechuchuku()
  531. go Yuemochuku()
  532. go Yuemoruku()
  533. if err != nil {
  534. logging.Error("查询当前的不同牧场的库存__error:", err.Error())
  535. }
  536. }
  537. func PartClass() {
  538. tx, err1 := restful.Dbs.Beginx()
  539. defer func() {
  540. switch {
  541. case err1 != nil:
  542. println("tx.Begin 事务启动失败__error:", err1.Error())
  543. if tx != nil {
  544. tx.Rollback()
  545. }
  546. default:
  547. if tx != nil {
  548. err1 = tx.Commit()
  549. }
  550. }
  551. }()
  552. queryPasture, err := restful.MapStrListTx(tx, "select sum(pr.reportery) reportery,ps.categoryId2,w2.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime "+
  553. " from part_repertory pr "+
  554. " left join warehouse w2 on w2.id = pr.locationId "+
  555. " INNER JOIN parts ps ON ps.id = pr.partId "+
  556. " GROUP BY ps.categoryId2,w2.pastureId")
  557. if len(queryPasture) > 0 {
  558. for i := 0; i < len(queryPasture); i++ {
  559. reportery := queryPasture[i]["reportery"]
  560. pastureId := queryPasture[i]["pastureId"]
  561. categoryId2 := queryPasture[i]["categoryId2"]
  562. dateTime := queryPasture[i]["dateTime"]
  563. _, err = tx.Exec("insert into inventory_temporary(currentInventory,pastureId,inventoryDate,partClassId) VALUES(?,?,?,?)", reportery,
  564. pastureId, dateTime, categoryId2)
  565. }
  566. }
  567. go YuechuPasture()
  568. go YuechuchukuPasture()
  569. go YuemochukuPasture()
  570. go YuemorukuPasture()
  571. if err != nil {
  572. fmt.Println(err.Error())
  573. }
  574. }
  575. func YuechuRuku() {
  576. tx, err1 := restful.Dbs.Beginx()
  577. defer func() {
  578. switch {
  579. case err1 != nil:
  580. println("tx.Begin 事务启动失败__error:", err1.Error())
  581. if tx != nil {
  582. tx.Rollback()
  583. }
  584. default:
  585. if tx != nil {
  586. err1 = tx.Commit()
  587. }
  588. }
  589. }()
  590. // 用于月初入库的备件数量根据牧场
  591. queryPartYcRK, err := restful.MapStrListTx(tx, "select sum(storageAmount) yuechusum,w1.pastureId,DATE_FORMAT(DATE_SUB(now()"+
  592. " ,INTERVAL 1 MONTH),'%Y-%m') dateTime from partlaid pl "+
  593. " inner JOIN bigpartlaid bpl ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId "+
  594. " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') "+
  595. " GROUP BY w1.pastureId ")
  596. fmt.Println(queryPartYcRK)
  597. if len(queryPartYcRK) > 0 {
  598. for i := 0; i < len(queryPartYcRK); i++ {
  599. yuechusum := queryPartYcRK[i]["yuechusum"]
  600. pastureId := queryPartYcRK[i]["pastureId"]
  601. dateTime := queryPartYcRK[i]["dateTime"]
  602. _, err = tx.Exec("update inventory_temporary set beginningInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId is null", yuechusum, pastureId, dateTime)
  603. }
  604. }
  605. if err != nil {
  606. fmt.Println(err.Error())
  607. }
  608. }
  609. func Yuechuchuku() {
  610. tx, err1 := restful.Dbs.Beginx()
  611. defer func() {
  612. switch {
  613. case err1 != nil:
  614. println("tx.Begin 事务启动失败__error:", err1.Error())
  615. if tx != nil {
  616. tx.Rollback()
  617. }
  618. default:
  619. if tx != nil {
  620. err1 = tx.Commit()
  621. }
  622. }
  623. }()
  624. //// 用于月初出库的备件数量根据牧场
  625. queryPartYcCK, err := restful.MapStrListTx(tx, "select sum(pu.`checkoutNumber` - pu.`quitNumber`) yuechuchuku, bpu.pastureId,"+
  626. " DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime from partuse pu "+
  627. " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` "+
  628. " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') ) "+
  629. " GROUP BY bpu.pastureId ")
  630. fmt.Println(queryPartYcCK)
  631. if len(queryPartYcCK) > 0 {
  632. //fmt.Println("jinglaile")
  633. fmt.Printf("%T\n", queryPartYcCK[0]["yuechuchuku"])
  634. for i := 0; i < len(queryPartYcCK); i++ {
  635. yuechuchuku := queryPartYcCK[i]["yuechuchuku"]
  636. pastureId := queryPartYcCK[i]["pastureId"]
  637. dateTime := queryPartYcCK[i]["dateTime"]
  638. fmt.Println(pastureId)
  639. _, err = tx.Exec("update inventory_temporary set beginningInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId is null", yuechuchuku, pastureId, dateTime)
  640. }
  641. }
  642. if err != nil {
  643. fmt.Println(err.Error())
  644. }
  645. }
  646. func Yuemochuku() {
  647. tx, err1 := restful.Dbs.Beginx()
  648. defer func() {
  649. switch {
  650. case err1 != nil:
  651. println("tx.Begin 事务启动失败__error:", err1.Error())
  652. if tx != nil {
  653. tx.Rollback()
  654. }
  655. default:
  656. if tx != nil {
  657. err1 = tx.Commit()
  658. }
  659. }
  660. }()
  661. //// 用于月末出库的备件数量根据牧场
  662. queryPartYmCK, err := restful.MapStrListTx(tx, "select sum(pu.`checkoutNumber` - pu.`quitNumber`) yumochuku, bpu.pastureId ,"+
  663. " DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime "+
  664. "from partuse pu "+
  665. " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') ) "+
  666. " GROUP BY bpu.pastureId")
  667. //
  668. if len(queryPartYmCK) > 0 {
  669. for i := 0; i < len(queryPartYmCK); i++ {
  670. yumochuku := queryPartYmCK[i]["yumochuku"]
  671. pastureId := queryPartYmCK[i]["pastureId"]
  672. dateTime := queryPartYmCK[i]["dateTime"]
  673. _, err = tx.Exec("update inventory_temporary set endInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId is null", yumochuku, pastureId, dateTime)
  674. }
  675. }
  676. if err != nil {
  677. fmt.Println(err.Error())
  678. }
  679. }
  680. func Yuemoruku() {
  681. tx, err1 := restful.Dbs.Beginx()
  682. defer func() {
  683. switch {
  684. case err1 != nil:
  685. println("tx.Begin 事务启动失败__error:", err1.Error())
  686. if tx != nil {
  687. tx.Rollback()
  688. }
  689. default:
  690. if tx != nil {
  691. err1 = tx.Commit()
  692. }
  693. }
  694. }()
  695. //// 用于月末入库的备件数量根据牧场
  696. queryPartYmRK, err := restful.MapStrListTx(tx, "select sum(storageAmount)yumoruku,w1.pastureId, DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') "+
  697. " from partlaid pl inner JOIN bigpartlaid bpl ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId "+
  698. " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') "+
  699. " GROUP BY w1.pastureId ")
  700. //
  701. if len(queryPartYmRK) > 0 {
  702. for i := 0; i < len(queryPartYmRK); i++ {
  703. yumoruku := queryPartYmRK[i]["yumoruku"]
  704. pastureId := queryPartYmRK[i]["pastureId"]
  705. dateTime := queryPartYmRK[i]["dateTime"]
  706. _, err = tx.Exec("update inventory_temporary set endInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId is null ", yumoruku, pastureId, dateTime)
  707. }
  708. }
  709. if err != nil {
  710. fmt.Println(err.Error())
  711. }
  712. }
  713. func YuechuPasture() {
  714. tx, err1 := restful.Dbs.Beginx()
  715. defer func() {
  716. switch {
  717. case err1 != nil:
  718. println("tx.Begin 事务启动失败__error:", err1.Error())
  719. if tx != nil {
  720. tx.Rollback()
  721. }
  722. default:
  723. if tx != nil {
  724. err1 = tx.Commit()
  725. }
  726. }
  727. }()
  728. // 用于月初入库的备件数量根据牧场
  729. queryPartYcRK, err := restful.MapStrListTx(tx, " select sum(storageAmount) storageAmount,p1.categoryId2,w1.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime "+
  730. "from partlaid pl inner JOIN bigpartlaid bpl ON bpl.`id` = pl.`bigId` left join parts p1 on p1.id = pl.partId left join warehouse w1 "+
  731. " on w1.id = pl.locationId "+
  732. " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') "+
  733. " GROUP BY p1.categoryId2 , w1.pastureId ")
  734. if len(queryPartYcRK) > 0 {
  735. for i := 0; i < len(queryPartYcRK); i++ {
  736. storageAmount := queryPartYcRK[i]["storageAmount"]
  737. pastureId := queryPartYcRK[i]["pastureId"]
  738. dateTime := queryPartYcRK[i]["dateTime"]
  739. categoryId2 := queryPartYcRK[i]["categoryId2"]
  740. _, err = tx.Exec("update inventory_temporary set beginningInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId =?", storageAmount, pastureId, dateTime, categoryId2)
  741. }
  742. }
  743. if err != nil {
  744. fmt.Println(err.Error())
  745. }
  746. }
  747. func YuechuchukuPasture() {
  748. tx, err1 := restful.Dbs.Beginx()
  749. defer func() {
  750. switch {
  751. case err1 != nil:
  752. println("tx.Begin 事务启动失败__error:", err1.Error())
  753. if tx != nil {
  754. tx.Rollback()
  755. }
  756. default:
  757. if tx != nil {
  758. err1 = tx.Commit()
  759. }
  760. }
  761. }()
  762. //// 用于月初出库的备件数量根据牧场
  763. queryPartYcCK, err := restful.MapStrListTx(tx, "select sum(pu.`checkoutNumber` - pu.`quitNumber`) yuechuchuku,bpu.pastureId, "+
  764. "p1.categoryId2,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime from partuse pu "+
  765. " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` left join parts p1 on p1.id = pu.partId "+
  766. " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') ) "+
  767. " GROUP BY bpu.pastureId,p1.categoryId2 ")
  768. if len(queryPartYcCK) > 0 {
  769. //fmt.Println("jinglaile")
  770. //fmt.Printf("%T\n",queryPartYcCK[0]["yuechuchuku"])
  771. for i := 0; i < len(queryPartYcCK); i++ {
  772. yuechuchuku := queryPartYcCK[i]["yuechuchuku"]
  773. pastureId := queryPartYcCK[i]["pastureId"]
  774. dateTime := queryPartYcCK[i]["dateTime"]
  775. categoryId2 := queryPartYcCK[i]["categoryId2"]
  776. fmt.Println(pastureId)
  777. _, err = tx.Exec("update inventory_temporary set beginningInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId =?", yuechuchuku, pastureId, dateTime, categoryId2)
  778. }
  779. }
  780. if err != nil {
  781. fmt.Println(err.Error())
  782. }
  783. }
  784. func YuemochukuPasture() {
  785. tx, err1 := restful.Dbs.Beginx()
  786. defer func() {
  787. switch {
  788. case err1 != nil:
  789. println("tx.Begin 事务启动失败__error:", err1.Error())
  790. if tx != nil {
  791. tx.Rollback()
  792. }
  793. default:
  794. if tx != nil {
  795. err1 = tx.Commit()
  796. }
  797. }
  798. }()
  799. //// 用于月末出库的备件数量根据牧场
  800. queryPartYmCK, err := restful.MapStrListTx(tx, "select sum(pu.`checkoutNumber` - pu.`quitNumber`) yumochuku,bpu.pastureId, "+
  801. " p1.categoryId2,DATE_FORMAT(now(),'%Y-%m') dateTime "+
  802. " from partuse pu inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` left join parts p1 on p1.id = pu.partId "+
  803. " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') ) "+
  804. "GROUP BY bpu.pastureId,p1.categoryId2 ")
  805. if len(queryPartYmCK) > 0 {
  806. for i := 0; i < len(queryPartYmCK); i++ {
  807. yumochuku := queryPartYmCK[i]["yumochuku"]
  808. pastureId := queryPartYmCK[i]["pastureId"]
  809. dateTime := queryPartYmCK[i]["dateTime"]
  810. categoryId2 := queryPartYmCK[i]["categoryId2"]
  811. _, err = tx.Exec("update inventory_temporary set endInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId =?", yumochuku, pastureId, dateTime, categoryId2)
  812. }
  813. }
  814. if err != nil {
  815. fmt.Println(err.Error())
  816. }
  817. }
  818. func YuemorukuPasture() {
  819. tx, err1 := restful.Dbs.Beginx()
  820. defer func() {
  821. switch {
  822. case err1 != nil:
  823. println("tx.Begin 事务启动失败__error:", err1.Error())
  824. if tx != nil {
  825. tx.Rollback()
  826. }
  827. default:
  828. if tx != nil {
  829. err1 = tx.Commit()
  830. }
  831. }
  832. }()
  833. //// 用于月末入库的备件数量根据牧场
  834. queryPartYmRK, err := restful.MapStrListTx(tx, "select sum(storageAmount) yumoruku, p1.categoryId2,w1.pastureId , DATE_FORMAT(now(),'%Y-%m') dateTime"+
  835. " from partlaid pl inner JOIN bigpartlaid bpl "+
  836. " ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId left join parts p1 on p1.id = pl.partId "+
  837. " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') GROUP BY p1.categoryId2,w1.pastureId ")
  838. //
  839. if len(queryPartYmRK) > 0 {
  840. for i := 0; i < len(queryPartYmRK); i++ {
  841. yumoruku := queryPartYmRK[i]["yumoruku"]
  842. pastureId := queryPartYmRK[i]["pastureId"]
  843. dateTime := queryPartYmRK[i]["dateTime"]
  844. categoryId2 := queryPartYmRK[i]["categoryId2"]
  845. _, err = tx.Exec("update inventory_temporary set endInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId=? ", yumoruku, pastureId, dateTime, categoryId2)
  846. }
  847. }
  848. if err != nil {
  849. fmt.Println(err.Error())
  850. }
  851. }