webservice.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. package util
  2. import (
  3. "../logging"
  4. "bytes"
  5. "encoding/xml"
  6. "fmt"
  7. "github.com/pkg/errors"
  8. "github.com/robfig/cron"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. "strings"
  13. "time"
  14. "../../models"
  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. body := bytes.NewBuffer([]byte(resstr))
  192. req, err := http.NewRequest("POST",
  193. url+"/ormrpc/services/EASLogin?wsdl",
  194. body,
  195. )
  196. req.Header.Set("Content-Type", "text/xml")
  197. req.Header.Set("SOAPAction", "application/soap+xml")
  198. if err != nil {
  199. return "", err
  200. }
  201. resp, err := client.Do(req)
  202. if err != nil {
  203. fmt.Println(err)
  204. return "", err
  205. }
  206. defer resp.Body.Close()
  207. result_body, err := ioutil.ReadAll(resp.Body)
  208. if err != nil {
  209. fmt.Println(err)
  210. return "", err
  211. }
  212. v := Response{}
  213. err = xml.Unmarshal([]byte(string(result_body)), &v)
  214. if err != nil {
  215. fmt.Println(err.Error())
  216. }
  217. if len(v.Body.Record) > 0 {
  218. return v.Body.Record[0].SessionId, nil
  219. }
  220. return "", err
  221. }
  222. //注销
  223. func EASWebServiceLogout() error {
  224. client := &http.Client{}
  225. resstr := ReplaceStrWebSevice("logout", user.UserName, "", user.SlnName, user.DcName, user.Language, "", "", "", "")
  226. body := bytes.NewBuffer([]byte(resstr))
  227. req, err := http.NewRequest("POST",
  228. user.Url+"/ormrpc/services/EASLogin?wsdl",
  229. body,
  230. )
  231. req.Header.Set("Content-Type", "text/xml")
  232. req.Header.Set("SOAPAction", "application/soap+xml")
  233. if err != nil {
  234. // handle error
  235. }
  236. resp, err := client.Do(req)
  237. if err != nil {
  238. return err
  239. }
  240. defer resp.Body.Close()
  241. _, err = ioutil.ReadAll(resp.Body)
  242. if err != nil {
  243. return err
  244. }
  245. return nil
  246. //fmt.Println(string(result_body))
  247. }
  248. func EASWebServiceQureyParts(SessionId, PartCode string) error {
  249. client := &http.Client{}
  250. resstr := ReplaceStrWebSevice("QureyPartsStr", "", "", "", "", "", "", SessionId, PartCode, "")
  251. body := bytes.NewBuffer([]byte(resstr))
  252. req, err := http.NewRequest("POST",
  253. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  254. body,
  255. )
  256. req.Header.Set("Content-Type", "text/xml")
  257. req.Header.Set("SOAPAction", "application/soap+xml")
  258. if err != nil {
  259. // handle error
  260. }
  261. resp, err := client.Do(req)
  262. if err != nil {
  263. return err
  264. }
  265. defer resp.Body.Close()
  266. result_body, err := ioutil.ReadAll(resp.Body)
  267. if err != nil {
  268. return err
  269. }
  270. v := Response{}
  271. err = xml.Unmarshal(result_body, &v)
  272. if err != nil {
  273. fmt.Println(err.Error())
  274. }
  275. resmap := make(map[string]interface{}, 0)
  276. err = json.Unmarshal([]byte(v.Body.QueryMaterialDataResponse.QueryMaterialDataReturn), &resmap)
  277. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  278. switch resmap["data"].(type) {
  279. case []interface{}:
  280. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  281. return errors.New("单据未查询到")
  282. }
  283. default:
  284. return errors.New("单据未查询到")
  285. }
  286. return nil
  287. }
  288. func EASWebServiceQureyBill(SessionId, BillCode,billType string) error {
  289. client := &http.Client{}
  290. resstr := ReplaceStrWebSevice("QureyBill", "", "", "", "", "", "", SessionId, BillCode, billType)
  291. body := bytes.NewBuffer([]byte(resstr))
  292. req, err := http.NewRequest("POST",
  293. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  294. body,
  295. )
  296. req.Header.Set("Content-Type", "text/xml")
  297. req.Header.Set("SOAPAction", "application/soap+xml")
  298. if err != nil {
  299. return err
  300. }
  301. resp, err := client.Do(req)
  302. if err != nil {
  303. return err
  304. }
  305. defer resp.Body.Close()
  306. result_body, err := ioutil.ReadAll(resp.Body)
  307. fmt.Println(string(result_body))
  308. if err != nil {
  309. fmt.Println(err)
  310. return err
  311. }
  312. v := Response{}
  313. err = xml.Unmarshal(result_body, &v)
  314. if err != nil {
  315. fmt.Println(err.Error())
  316. }
  317. resmap := make(map[string]interface{}, 0)
  318. err = json.Unmarshal([]byte(v.Body.QueryScmBillDataResponse.QueryScmBillDataReturn), &resmap)
  319. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  320. switch resmap["data"].(type) {
  321. case []interface{}:
  322. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  323. return errors.New("单据未查询到")
  324. }
  325. default:
  326. return errors.New("单据未查询到")
  327. }
  328. return nil
  329. }
  330. func EASWebServicInsertParts(SessionId string, parts Parts,url string) error {
  331. client := &http.Client{}
  332. soapencJson, err := json.Marshal(parts)
  333. if err != nil {
  334. return err
  335. }
  336. if url == ""{
  337. url = user.Url
  338. }
  339. println(string(soapencJson))
  340. resstr := ReplaceStrWebSevice("InsertParts", "", "", "", "", "", "", SessionId, string(soapencJson), "")
  341. body := bytes.NewBuffer([]byte(resstr))
  342. req, err := http.NewRequest("POST",
  343. url+"/ormrpc/services/WSSynMasterDataFacade?wsdl",
  344. body,
  345. )
  346. req.Header.Set("Content-Type", "text/xml")
  347. req.Header.Set("SOAPAction", "application/soap+xml")
  348. if err != nil {
  349. // handle error
  350. }
  351. resp, err := client.Do(req)
  352. if err != nil {
  353. fmt.Println(err)
  354. return err
  355. }
  356. defer resp.Body.Close()
  357. result_body, err := ioutil.ReadAll(resp.Body)
  358. if err != nil {
  359. fmt.Println(err)
  360. return err
  361. }
  362. v := Response{}
  363. err = xml.Unmarshal(result_body, &v)
  364. if err != nil {
  365. fmt.Println(err.Error())
  366. }
  367. resmap := make(map[string]string, 0)
  368. err = json.Unmarshal([]byte(v.Body.SynMaterialResponse.SynMaterialReturn), &resmap)
  369. _,ok := resmap["success"]
  370. if !ok {
  371. return errors.New(string(result_body))
  372. }
  373. if resmap["success"] == "0" {
  374. return errors.New(resmap["message"])
  375. }
  376. return nil
  377. }
  378. // 登录系统写入数据
  379. func EASWebServicInsertPartsuselaid(SessionId, strJson, billType string) error { //billType采购入库单:006 销售出库单:007 其他入库单:008 其他出库单:009 领料出库单:100 生产入库单:101
  380. client := &http.Client{}
  381. resstr := ReplaceStrWebSevice("InsertPartsuselaidStr", "", "", "", "", "", "", SessionId, strJson, billType)
  382. body := bytes.NewBuffer([]byte(resstr))
  383. req, err := http.NewRequest("POST",
  384. user.Url+"/ormrpc/services/WSSCMWebServiceFacade?wsdl",
  385. body,
  386. )
  387. req.Header.Set("Content-Type", "text/xml")
  388. req.Header.Set("SOAPAction", "application/soap+xml")
  389. if err != nil {
  390. // handle error
  391. }
  392. resp, err := client.Do(req)
  393. if err != nil {
  394. fmt.Println(err)
  395. return err
  396. }
  397. defer resp.Body.Close()
  398. result_body, err := ioutil.ReadAll(resp.Body)
  399. if err != nil {
  400. fmt.Println(err)
  401. return err
  402. }
  403. v := Response{}
  404. err = xml.Unmarshal(result_body, &v)
  405. if err != nil {
  406. fmt.Println(err.Error())
  407. }
  408. resmap := make(map[string]interface{}, 0)
  409. if v.Body.ImportBillResponse.ImportBillReturn != "" {
  410. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  411. fmt.Println(v.Body.ImportBillResponse.ImportBillReturn)
  412. //_,ok := resmap["billErrors"]
  413. //if !ok {
  414. // println(string(result_body))
  415. // return errors.New(string(result_body))
  416. //}
  417. _,ok := resmap["status"]
  418. if !ok {
  419. println(string(result_body))
  420. return errors.New(string(result_body))
  421. }
  422. //if len(resmap["billErrors"].([]interface{})) > 0 || resmap["status"] != "1" {
  423. if resmap["status"] != "0" {
  424. resmap := make(map[string]interface{}, 0)
  425. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  426. switch resmap["billErrors"].(type) {
  427. case []interface{}:
  428. if len(resmap["billErrors"].([]interface{}))>0{
  429. switch resmap["billErrors"].([]interface{})[0].(type) {
  430. case map[string]interface{} :
  431. str := resmap["billErrors"].([]interface{})[0].(map[string]interface{})["errorMsgs"]
  432. switch str.(type) {
  433. case []interface{}:
  434. if len(str.([]interface{})) >0 {
  435. s := str.([]interface{})[0]
  436. switch s.(type) {
  437. case string :
  438. return errors.New(s.(string))
  439. }
  440. }
  441. }
  442. }
  443. }
  444. }
  445. return errors.New(v.Body.ImportBillResponse.ImportBillReturn)
  446. }
  447. }
  448. return nil
  449. }
  450. //
  451. func ReplaceStrWebSevice(typeIn, userName, password, slnName, dcName, language, dbType, SessionId, strJson, billType string) string {
  452. res := ""
  453. switch typeIn {
  454. case "login":
  455. res = LoginStr
  456. res = strings.ReplaceAll(res, "userName?", userName)
  457. res = strings.ReplaceAll(res, "password?", password)
  458. res = strings.ReplaceAll(res, "slnName?", slnName)
  459. res = strings.ReplaceAll(res, "dcName?", dcName)
  460. res = strings.ReplaceAll(res, "language?", language)
  461. res = strings.ReplaceAll(res, "dbType?", dbType)
  462. case "logout":
  463. res = LogoutStr
  464. res = strings.ReplaceAll(res, "userName?", userName)
  465. res = strings.ReplaceAll(res, "slnName?", slnName)
  466. res = strings.ReplaceAll(res, "dcName?", dcName)
  467. res = strings.ReplaceAll(res, "language?", language)
  468. case "InsertParts":
  469. res = InsertPartsStr
  470. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  471. res = strings.ReplaceAll(res, "strJson?", strJson)
  472. case "QureyPartsStr":
  473. res = QureyPartsStr
  474. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  475. res = strings.ReplaceAll(res, "strJson?", strJson)
  476. case "InsertPartsuselaidStr":
  477. res = InsertPartsuselaidStr
  478. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  479. res = strings.ReplaceAll(res, "strJson?", strJson)
  480. res = strings.ReplaceAll(res, "billType?", billType)
  481. case "QureyBill":
  482. res = QureyBill
  483. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  484. res = strings.ReplaceAll(res, "strJson?", strJson)
  485. res = strings.ReplaceAll(res, "billType?", billType)
  486. }
  487. return res
  488. }
  489. func CronServer(){
  490. c := cron.New()
  491. log.Println("Starting CronWXwork...")
  492. _, err := c.AddFunc("0 1 1 * *", Pastures) // 每月1号1点
  493. _, err = c.AddFunc("1 1 1 * *", PartClass) // 每月1号1点1分
  494. if err != nil{
  495. log.Print("定时任务出错___",err.Error())
  496. }
  497. c.Start()
  498. }
  499. func Pastures(){
  500. //fmt.Println("Dingshi")
  501. tx := models.Engine.NewSession()
  502. err1 := tx.Begin()
  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. if tx != nil {
  516. tx.Close()
  517. }
  518. }()
  519. queryPasture,err := tx.SQL("select sum(p_r.reportery) reportery, w2.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime " +
  520. " from part_repertory p_r "+
  521. " left join warehouse w2 on w2.id = p_r.locationId "+
  522. " GROUP BY w2.pastureId ").Query().List()
  523. fmt.Println(queryPasture)
  524. if len(queryPasture)>0{
  525. for i:=0;i<len(queryPasture);i++{
  526. reportery:=queryPasture[i]["reportery"]
  527. pastureId:=queryPasture[i]["pastureId"]
  528. dateTime:=queryPasture[i]["dateTime"]
  529. _, err = tx.Exec("insert into inventory_temporary(currentInventory,pastureId,inventoryDate) VALUES(?,?,?)",reportery,pastureId,dateTime)
  530. }
  531. }
  532. go YuechuRuku()
  533. go Yuechuchuku()
  534. go Yuemochuku()
  535. go Yuemoruku()
  536. if err != nil{
  537. logging.Error("查询当前的不同牧场的库存__error:", err.Error())
  538. }
  539. }
  540. func PartClass(){
  541. tx := models.Engine.NewSession()
  542. err1 := tx.Begin()
  543. defer func() {
  544. switch {
  545. case err1 != nil:
  546. println("tx.Begin 事务启动失败__error:", err1.Error())
  547. if tx != nil {
  548. tx.Rollback()
  549. }
  550. default:
  551. if tx != nil {
  552. err1 = tx.Commit()
  553. }
  554. }
  555. if tx != nil {
  556. tx.Close()
  557. }
  558. }()
  559. queryPasture,err := tx.SQL("select sum(pr.reportery) reportery,ps.categoryId2,w2.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime " +
  560. " from part_repertory pr "+
  561. " left join warehouse w2 on w2.id = pr.locationId "+
  562. " INNER JOIN parts ps ON ps.id = pr.partId " +
  563. " GROUP BY ps.categoryId2,w2.pastureId").Query().List()
  564. if len(queryPasture)>0{
  565. for i:=0;i<len(queryPasture);i++{
  566. reportery:=queryPasture[i]["reportery"]
  567. pastureId:=queryPasture[i]["pastureId"]
  568. categoryId2:=queryPasture[i]["categoryId2"]
  569. dateTime:=queryPasture[i]["dateTime"]
  570. _, err = tx.Exec("insert into inventory_temporary(currentInventory,pastureId,inventoryDate,partClassId) VALUES(?,?,?,?)",reportery,
  571. pastureId,dateTime,categoryId2)
  572. }
  573. }
  574. go YuechuPasture()
  575. go YuechuchukuPasture()
  576. go YuemochukuPasture()
  577. go YuemorukuPasture()
  578. if err !=nil{
  579. fmt.Println(err.Error())
  580. }
  581. }
  582. func YuechuRuku(){
  583. tx := models.Engine.NewSession()
  584. err1 := tx.Begin()
  585. defer func() {
  586. switch {
  587. case err1 != nil:
  588. println("tx.Begin 事务启动失败__error:", err1.Error())
  589. if tx != nil {
  590. tx.Rollback()
  591. }
  592. default:
  593. if tx != nil {
  594. err1 = tx.Commit()
  595. }
  596. }
  597. if tx != nil {
  598. tx.Close()
  599. }
  600. }()
  601. // 用于月初入库的备件数量根据牧场
  602. queryPartYcRK,err := tx.SQL("select sum(storageAmount) yuechusum,w1.pastureId,DATE_FORMAT(DATE_SUB(now()" +
  603. " ,INTERVAL 1 MONTH),'%Y-%m') dateTime from partlaid pl " +
  604. " inner JOIN bigpartlaid bpl ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId " +
  605. " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') " +
  606. " GROUP BY w1.pastureId ").Query().List()
  607. fmt.Println(queryPartYcRK)
  608. if len(queryPartYcRK)>0{
  609. for i:=0;i<len(queryPartYcRK);i++{
  610. yuechusum:=queryPartYcRK[i]["yuechusum"]
  611. pastureId:=queryPartYcRK[i]["pastureId"]
  612. dateTime:=queryPartYcRK[i]["dateTime"]
  613. _,err = tx.Exec("update inventory_temporary set beginningInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId is null",yuechusum,pastureId,dateTime)
  614. }
  615. }
  616. if err !=nil{
  617. fmt.Println(err.Error())
  618. }
  619. }
  620. func Yuechuchuku(){
  621. tx := models.Engine.NewSession()
  622. err1 := tx.Begin()
  623. defer func() {
  624. switch {
  625. case err1 != nil:
  626. println("tx.Begin 事务启动失败__error:", err1.Error())
  627. if tx != nil {
  628. tx.Rollback()
  629. }
  630. default:
  631. if tx != nil {
  632. err1 = tx.Commit()
  633. }
  634. }
  635. if tx != nil {
  636. tx.Close()
  637. }
  638. }()
  639. //// 用于月初出库的备件数量根据牧场
  640. queryPartYcCK,err := tx.SQL("select sum(pu.`checkoutNumber` - pu.`quitNumber`) yuechuchuku, bpu.pastureId," +
  641. " DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime from partuse pu "+
  642. " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` " +
  643. " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') ) "+
  644. " GROUP BY bpu.pastureId ").Query().List()
  645. fmt.Println(queryPartYcCK)
  646. if len(queryPartYcCK)>0{
  647. //fmt.Println("jinglaile")
  648. fmt.Printf("%T\n",queryPartYcCK[0]["yuechuchuku"])
  649. for i:=0;i<len(queryPartYcCK);i++{
  650. yuechuchuku:=queryPartYcCK[i]["yuechuchuku"]
  651. pastureId:=queryPartYcCK[i]["pastureId"]
  652. dateTime:=queryPartYcCK[i]["dateTime"]
  653. fmt.Println(pastureId)
  654. _,err = tx.Exec("update inventory_temporary set beginningInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId is null",yuechuchuku,pastureId,dateTime)
  655. }
  656. }
  657. if err !=nil{
  658. fmt.Println(err.Error())
  659. }
  660. }
  661. func Yuemochuku(){
  662. tx := models.Engine.NewSession()
  663. err1 := tx.Begin()
  664. defer func() {
  665. switch {
  666. case err1 != nil:
  667. println("tx.Begin 事务启动失败__error:", err1.Error())
  668. if tx != nil {
  669. tx.Rollback()
  670. }
  671. default:
  672. if tx != nil {
  673. err1 = tx.Commit()
  674. }
  675. }
  676. if tx != nil {
  677. tx.Close()
  678. }
  679. }()
  680. //// 用于月末出库的备件数量根据牧场
  681. queryPartYmCK,err := tx.SQL("select sum(pu.`checkoutNumber` - pu.`quitNumber`) yumochuku, bpu.pastureId ," +
  682. " DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime " +
  683. "from partuse pu "+
  684. " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') ) "+
  685. " GROUP BY bpu.pastureId").Query().List()
  686. //
  687. if len(queryPartYmCK)>0{
  688. for i:=0;i<len(queryPartYmCK);i++{
  689. yumochuku :=queryPartYmCK[i]["yumochuku"]
  690. pastureId :=queryPartYmCK[i]["pastureId"]
  691. dateTime :=queryPartYmCK[i]["dateTime"]
  692. _,err = tx.Exec("update inventory_temporary set endInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId is null",yumochuku,pastureId,dateTime)
  693. }
  694. }
  695. if err !=nil{
  696. fmt.Println(err.Error())
  697. }
  698. }
  699. func Yuemoruku(){
  700. tx := models.Engine.NewSession()
  701. err1 := tx.Begin()
  702. defer func() {
  703. switch {
  704. case err1 != nil:
  705. println("tx.Begin 事务启动失败__error:", err1.Error())
  706. if tx != nil {
  707. tx.Rollback()
  708. }
  709. default:
  710. if tx != nil {
  711. err1 = tx.Commit()
  712. }
  713. }
  714. if tx != nil {
  715. tx.Close()
  716. }
  717. }()
  718. //// 用于月末入库的备件数量根据牧场
  719. queryPartYmRK,err := tx.SQL("select sum(storageAmount)yumoruku,w1.pastureId, DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') "+
  720. " from partlaid pl inner JOIN bigpartlaid bpl ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId "+
  721. " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') "+
  722. " GROUP BY w1.pastureId ").Query().List()
  723. //
  724. if len(queryPartYmRK)>0{
  725. for i:=0;i<len(queryPartYmRK);i++{
  726. yumoruku:=queryPartYmRK[i]["yumoruku"]
  727. pastureId:=queryPartYmRK[i]["pastureId"]
  728. dateTime:=queryPartYmRK[i]["dateTime"]
  729. _,err = tx.Exec("update inventory_temporary set endInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId is null ",yumoruku,pastureId,dateTime)
  730. }
  731. }
  732. if err !=nil{
  733. fmt.Println(err.Error())
  734. }
  735. }
  736. func YuechuPasture(){
  737. tx := models.Engine.NewSession()
  738. err1 := tx.Begin()
  739. defer func() {
  740. switch {
  741. case err1 != nil:
  742. println("tx.Begin 事务启动失败__error:", err1.Error())
  743. if tx != nil {
  744. tx.Rollback()
  745. }
  746. default:
  747. if tx != nil {
  748. err1 = tx.Commit()
  749. }
  750. }
  751. if tx != nil {
  752. tx.Close()
  753. }
  754. }()
  755. // 用于月初入库的备件数量根据牧场
  756. queryPartYcRK,err := tx.SQL(" select sum(storageAmount) storageAmount,p1.categoryId2,w1.pastureId,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime " +
  757. "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 " +
  758. " on w1.id = pl.locationId "+
  759. " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') "+
  760. " GROUP BY p1.categoryId2 , w1.pastureId ").Query().List()
  761. if len(queryPartYcRK)>0{
  762. for i:=0;i<len(queryPartYcRK);i++{
  763. storageAmount:=queryPartYcRK[i]["storageAmount"]
  764. pastureId:=queryPartYcRK[i]["pastureId"]
  765. dateTime:=queryPartYcRK[i]["dateTime"]
  766. categoryId2:=queryPartYcRK[i]["categoryId2"]
  767. _,err = tx.Exec("update inventory_temporary set beginningInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId =?",storageAmount,pastureId,dateTime,categoryId2)
  768. }
  769. }
  770. if err !=nil{
  771. fmt.Println(err.Error())
  772. }
  773. }
  774. func YuechuchukuPasture(){
  775. tx := models.Engine.NewSession()
  776. err1 := tx.Begin()
  777. defer func() {
  778. switch {
  779. case err1 != nil:
  780. println("tx.Begin 事务启动失败__error:", err1.Error())
  781. if tx != nil {
  782. tx.Rollback()
  783. }
  784. default:
  785. if tx != nil {
  786. err1 = tx.Commit()
  787. }
  788. }
  789. if tx != nil {
  790. tx.Close()
  791. }
  792. }()
  793. //// 用于月初出库的备件数量根据牧场
  794. queryPartYcCK,err := tx.SQL("select sum(pu.`checkoutNumber` - pu.`quitNumber`) yuechuchuku,bpu.pastureId, "+
  795. "p1.categoryId2,DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') dateTime from partuse pu "+
  796. " inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` left join parts p1 on p1.id = pu.partId " +
  797. " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 MONTH),'%Y-%m') ) "+
  798. " GROUP BY bpu.pastureId,p1.categoryId2 ").Query().List()
  799. if len(queryPartYcCK)>0{
  800. //fmt.Println("jinglaile")
  801. //fmt.Printf("%T\n",queryPartYcCK[0]["yuechuchuku"])
  802. for i:=0;i<len(queryPartYcCK);i++{
  803. yuechuchuku:=queryPartYcCK[i]["yuechuchuku"]
  804. pastureId:=queryPartYcCK[i]["pastureId"]
  805. dateTime:=queryPartYcCK[i]["dateTime"]
  806. categoryId2:=queryPartYcCK[i]["categoryId2"]
  807. fmt.Println(pastureId)
  808. _,err = tx.Exec("update inventory_temporary set beginningInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId =?",yuechuchuku,pastureId,dateTime,categoryId2)
  809. }
  810. }
  811. if err !=nil{
  812. fmt.Println(err.Error())
  813. }
  814. }
  815. func YuemochukuPasture(){
  816. tx := models.Engine.NewSession()
  817. err1 := tx.Begin()
  818. defer func() {
  819. switch {
  820. case err1 != nil:
  821. println("tx.Begin 事务启动失败__error:", err1.Error())
  822. if tx != nil {
  823. tx.Rollback()
  824. }
  825. default:
  826. if tx != nil {
  827. err1 = tx.Commit()
  828. }
  829. }
  830. if tx != nil {
  831. tx.Close()
  832. }
  833. }()
  834. //// 用于月末出库的备件数量根据牧场
  835. queryPartYmCK,err := tx.SQL("select sum(pu.`checkoutNumber` - pu.`quitNumber`) yumochuku,bpu.pastureId, "+
  836. " p1.categoryId2,DATE_FORMAT(now(),'%Y-%m') dateTime "+
  837. " from partuse pu inner JOIN bigpartuse bpu ON pu.`bigId` =bpu.`id` left join parts p1 on p1.id = pu.partId "+
  838. " where (DATE_FORMAT(bpu.receiveTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') ) "+
  839. "GROUP BY bpu.pastureId,p1.categoryId2 ").Query().List()
  840. if len(queryPartYmCK)>0{
  841. for i:=0;i<len(queryPartYmCK);i++{
  842. yumochuku :=queryPartYmCK[i]["yumochuku"]
  843. pastureId :=queryPartYmCK[i]["pastureId"]
  844. dateTime :=queryPartYmCK[i]["dateTime"]
  845. categoryId2:=queryPartYmCK[i]["categoryId2"]
  846. _,err = tx.Exec("update inventory_temporary set endInventoryCK = ? where pastureId =? and inventoryDate =? and partClassId =?",yumochuku,pastureId,dateTime,categoryId2)
  847. }
  848. }
  849. if err !=nil{
  850. fmt.Println(err.Error())
  851. }
  852. }
  853. func YuemorukuPasture(){
  854. tx := models.Engine.NewSession()
  855. err1 := tx.Begin()
  856. defer func() {
  857. switch {
  858. case err1 != nil:
  859. println("tx.Begin 事务启动失败__error:", err1.Error())
  860. if tx != nil {
  861. tx.Rollback()
  862. }
  863. default:
  864. if tx != nil {
  865. err1 = tx.Commit()
  866. }
  867. }
  868. if tx != nil {
  869. tx.Close()
  870. }
  871. }()
  872. //// 用于月末入库的备件数量根据牧场
  873. queryPartYmRK,err := tx.SQL("select sum(storageAmount) yumoruku, p1.categoryId2,w1.pastureId , DATE_FORMAT(now(),'%Y-%m') dateTime" +
  874. " from partlaid pl inner JOIN bigpartlaid bpl "+
  875. " ON bpl.`id` = pl.`bigId` left join warehouse w1 on w1.id = pl.locationId left join parts p1 on p1.id = pl.partId "+
  876. " where DATE_FORMAT(bpl.storageTime,'%Y-%m') > DATE_FORMAT(now(),'%Y-%m') GROUP BY p1.categoryId2,w1.pastureId ").Query().List()
  877. //
  878. if len(queryPartYmRK)>0{
  879. for i:=0;i<len(queryPartYmRK);i++{
  880. yumoruku:=queryPartYmRK[i]["yumoruku"]
  881. pastureId:=queryPartYmRK[i]["pastureId"]
  882. dateTime:=queryPartYmRK[i]["dateTime"]
  883. categoryId2:=queryPartYmRK[i]["categoryId2"]
  884. _,err = tx.Exec("update inventory_temporary set endInventoryRK = ? where pastureId =? and inventoryDate =? and partClassId=? ",yumoruku,pastureId,dateTime,categoryId2)
  885. }
  886. }
  887. if err !=nil{
  888. fmt.Println(err.Error())
  889. }
  890. }