xml_service.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package service
  2. import (
  3. "IC/models"
  4. "bytes"
  5. "encoding/json"
  6. "encoding/xml"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. )
  11. // 获取可下载标本列表
  12. func GetGPReportListData(start_date string, end_date string, key string) (result []models.GPReportListResultData) {
  13. // 构建SOAP请求数据
  14. requestData := &models.ReportListEnvelope{
  15. Soap: "http://schemas.xmlsoap.org/soap/envelope/",
  16. Xsi: "http://www.w3.org/2001/XMLSchema-instance",
  17. Xsd: "http://www.w3.org/2001/XMLSchema",
  18. Body: models.ReportListBody{
  19. GetReportReq: models.GetReportReq{
  20. XMLName: xml.Name{Local: "GetAllSampleList", Space: "http://tempuri.org/"},
  21. Key: key,
  22. BeginDateTime: start_date,
  23. EndDateTime: end_date,
  24. DateType: 1,
  25. PrintState: 0,
  26. },
  27. },
  28. }
  29. // 将SOAP请求数据转换为XML格式
  30. requestBody, err := xml.Marshal(requestData)
  31. if err != nil {
  32. fmt.Println("XML Marshal error:", err)
  33. return result
  34. }
  35. // 创建POST请求
  36. url := "http://gpis.cnhiqc.com:9799/InstituteLISService.asmx"
  37. req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
  38. if err != nil {
  39. fmt.Println("Request creation error:", err)
  40. return result
  41. }
  42. // 设置请求头
  43. req.Header.Set("Content-Type", "text/xml; charset=utf-8")
  44. req.Header.Set("Content-Length", "length")
  45. req.Header.Set("SOAPAction", "http://tempuri.org/GetAllSampleList")
  46. // 发送请求
  47. client := &http.Client{}
  48. resp, err := client.Do(req)
  49. if err != nil {
  50. fmt.Println("Request error:", err)
  51. return result
  52. }
  53. defer resp.Body.Close()
  54. // 读取响应数据
  55. responseBody, err := ioutil.ReadAll(resp.Body)
  56. //fmt.Println(string(responseBody))
  57. if err != nil {
  58. fmt.Println("Response read error:", err)
  59. return result
  60. }
  61. // 解析XML数据
  62. var envelope_two models.EnvelopeTwo
  63. err2 := xml.Unmarshal(responseBody, &envelope_two)
  64. if err2 != nil {
  65. fmt.Println("XML Unmarshal error:", err2)
  66. return result
  67. }
  68. // 获取GetReportListResult的JSON字符串
  69. jsonStr := envelope_two.Body.GetAllSampleListResponse.GetAllSampleListResult
  70. //fmt.Println(jsonStr)
  71. // 解析JSON字符串到结构体
  72. var reportData models.ReportData
  73. err = json.Unmarshal([]byte(jsonStr), &reportData)
  74. if err != nil {
  75. fmt.Println("JSON Unmarshal error:", err)
  76. return result
  77. }
  78. if reportData.Res == 0 {
  79. //fmt.Println( . ."Data:", reportData.GPReportListResultData)
  80. return reportData.GPReportListResultData
  81. }
  82. return result
  83. }
  84. // 根据院所自身条码获取标本检测结果或诊断结果,遍历结果
  85. //func GetGPResultData(result []models.GPReportListResultData, key string) (results []models.InspectResult) {
  86. // for _, item := range result {
  87. //
  88. // // 构建SOAP请求数据
  89. // requestData := &models.ReportResultEnvelope{
  90. // Soap: "http://schemas.xmlsoap.org/soap/envelope/",
  91. // Xsi: "http://www.w3.org/2001/XMLSchema-instance",
  92. // Xsd: "http://www.w3.org/2001/XMLSchema",
  93. // Body: models.ReportResultBody{
  94. // GetReportResultReq: models.GetReportResultReq{
  95. // XMLName: xml.Name{Local: "GetJSONReportItemListByCustomerBarocde", Space: "http://tempuri.org/"},
  96. // Key: key,
  97. // CustomerBarCode: item.CustomerBarcode,
  98. // },
  99. // },
  100. // }
  101. //
  102. // // 将SOAP请求数据转换为XML格式
  103. // requestBody, err := xml.Marshal(requestData)
  104. // if err != nil {
  105. // fmt.Println("XML Marshal error:", err)
  106. //
  107. // }
  108. //
  109. // // 创建POST请求
  110. // url := "http://gpis.cnhiqc.com:9799/InstituteLISService.asmx"
  111. // req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
  112. // if err != nil {
  113. // fmt.Println("Request creation error:", err)
  114. // }
  115. // // 设置请求头
  116. // req.Header.Set("Content-Type", "text/xml; charset=utf-8")
  117. // req.Header.Set("Content-Length", "length")
  118. // req.Header.Set("SOAPAction", "http://tempuri.org/GetJSONReportItemListByCustomerBarocde")
  119. // // 发送请求
  120. // client := &http.Client{}
  121. // resp, err := client.Do(req)
  122. // if err != nil {
  123. // fmt.Println("Request error:", err)
  124. //
  125. // }
  126. // defer resp.Body.Close()
  127. // // 读取响应数据
  128. // responseBody, err := ioutil.ReadAll(resp.Body)
  129. // if err != nil {
  130. // fmt.Println("Response read error:", err)
  131. // }
  132. //
  133. // // 解析XML数据
  134. // var envelope_three models.EnvelopeThree
  135. // err2 := xml.Unmarshal(responseBody, &envelope_three)
  136. // if err2 != nil {
  137. // fmt.Println("XML Unmarshal error:", err2)
  138. // }
  139. // // 获取GetReportListResult的JSON字符串
  140. // jsonStr := envelope_three.Body.GetJSONReportItemListByCustomerBarocdeResponse.GetJSONReportItemListByCustomerBarocdeResult
  141. // // 解析JSON字符串到结构体
  142. // fmt.Println(jsonStr)
  143. // var reportData models.ReportResultData
  144. // err = json.Unmarshal([]byte(jsonStr), &reportData)
  145. // if err != nil {
  146. // fmt.Println("JSON Unmarshal error:", err)
  147. // }
  148. // if reportData.Res == 0 {
  149. // fmt.Println("Data:", reportData.InspectResult)
  150. // reportData.InspectResult.ListResult = item
  151. // results = append(results, reportData.InspectResult)
  152. // }
  153. // }
  154. // return results
  155. //}
  156. // 根据高品条码获取标本检测结果或诊断结果,遍历结果
  157. func GetGPResultDataTwo(result []models.GPReportListResultData, key string) (results []models.InspectResultFive) {
  158. for _, item := range result {
  159. if len(item.CustomerBarcode) > 0 {
  160. fmt.Println("~~~~~~-------")
  161. // 构建SOAP请求数据
  162. requestData := &models.ReportResultEnvelope{
  163. Soap: "http://schemas.xmlsoap.org/soap/envelope/",
  164. Xsi: "http://www.w3.org/2001/XMLSchema-instance",
  165. Xsd: "http://www.w3.org/2001/XMLSchema",
  166. Body: models.ReportResultBody{
  167. GetReportResultReq: models.GetReportResultReq{
  168. XMLName: xml.Name{Local: "GetJSONReportItemListByCustomerBarocde", Space: "http://tempuri.org/"},
  169. Key: key,
  170. CustomerBarCode: item.CustomerBarcode,
  171. },
  172. },
  173. }
  174. // 将SOAP请求数据转换为XML格式
  175. requestBody, err := xml.Marshal(requestData)
  176. if err != nil {
  177. fmt.Println("XML Marshal error:", err)
  178. }
  179. // 创建POST请求
  180. url := "http://gpis.cnhiqc.com:9799/InstituteLISService.asmx"
  181. req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
  182. if err != nil {
  183. fmt.Println("Request creation error:", err)
  184. }
  185. // 设置请求头
  186. req.Header.Set("Content-Type", "text/xml; charset=utf-8")
  187. req.Header.Set("Content-Length", "length")
  188. req.Header.Set("SOAPAction", "http://tempuri.org/GetJSONReportItemListByCustomerBarocde")
  189. // 发送请求
  190. client := &http.Client{}
  191. resp, err := client.Do(req)
  192. if err != nil {
  193. fmt.Println("Request error:", err)
  194. }
  195. defer resp.Body.Close()
  196. // 读取响应数据
  197. responseBody, err := ioutil.ReadAll(resp.Body)
  198. if err != nil {
  199. fmt.Println("Response read error:", err)
  200. }
  201. // 解析XML数据
  202. var envelope_three models.EnvelopeThree
  203. err2 := xml.Unmarshal(responseBody, &envelope_three)
  204. if err2 != nil {
  205. fmt.Println("XML Unmarshal error:", err2)
  206. }
  207. // 获取GetReportListResult的JSON字符串
  208. jsonStr := envelope_three.Body.GetJSONReportItemListByCustomerBarocdeResponse.GetJSONReportItemListByCustomerBarocdeResult
  209. // 解析JSON字符串到结构体
  210. //fmt.Println(jsonStr)
  211. var reportData models.ReportResultData
  212. err = json.Unmarshal([]byte(jsonStr), &reportData)
  213. if err != nil {
  214. fmt.Println("JSON Unmarshal error:", err)
  215. }
  216. if reportData.Res == 0 {
  217. //reportData.ListResult = item
  218. for _, subItem := range reportData.InspectResult {
  219. subItem.ListResult = item
  220. var resultTwo models.InspectResultFive
  221. resultTwo.ListResult = subItem.ListResult
  222. resultTwo.Result = subItem.Result
  223. resultTwo.Barcode = subItem.Barcode
  224. resultTwo.Flag = subItem.Flag
  225. resultTwo.Unit = subItem.Unit
  226. resultTwo.ChargeItemName = subItem.ChargeItemName
  227. resultTwo.InspectionName = subItem.InspectionName
  228. resultTwo.InspectionCode = subItem.InspectionCode
  229. resultTwo.Reference = subItem.Reference
  230. results = append(results, resultTwo)
  231. }
  232. }
  233. } else {
  234. if len(item.Barcode) > 0 {
  235. //fmt.Println("11122222")
  236. // 构建SOAP请求数据
  237. requestData := &models.GPReportResultEnvelope{
  238. Soap: "http://schemas.xmlsoap.org/soap/envelope/",
  239. Xsi: "http://www.w3.org/2001/XMLSchema-instance",
  240. Xsd: "http://www.w3.org/2001/XMLSchema",
  241. Body: models.GPReportResultBody{
  242. GetReportResultReq: models.GetGPReportResultReq{
  243. XMLName: xml.Name{Local: "GetJSONReportItemListByGPBarocde", Space: "http://tempuri.org/"},
  244. Key: key,
  245. GPBarCode: item.Barcode,
  246. },
  247. },
  248. }
  249. // 将SOAP请求数据转换为XML格式
  250. requestBody, err := xml.Marshal(requestData)
  251. if err != nil {
  252. fmt.Println("XML Marshal error:", err)
  253. }
  254. // 创建POST请求
  255. url := "http://gpis.cnhiqc.com:9799/InstituteLISService.asmx"
  256. req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
  257. if err != nil {
  258. fmt.Println("Request creation error:", err)
  259. }
  260. // 设置请求头
  261. req.Header.Set("Content-Type", "text/xml; charset=utf-8")
  262. req.Header.Set("Content-Length", "length")
  263. req.Header.Set("SOAPAction", "http://tempuri.org/GetJSONReportItemListByGPBarocde")
  264. // 发送请求
  265. client := &http.Client{}
  266. resp, err := client.Do(req)
  267. if err != nil {
  268. fmt.Println("Request error:", err)
  269. }
  270. defer resp.Body.Close()
  271. // 读取响应数据
  272. responseBody, err := ioutil.ReadAll(resp.Body)
  273. if err != nil {
  274. fmt.Println("Response read error:", err)
  275. }
  276. // 解析XML数据
  277. var envelope_five models.EnvelopeFive
  278. err2 := xml.Unmarshal(responseBody, &envelope_five)
  279. if err2 != nil {
  280. fmt.Println("XML Unmarshal error:", err2)
  281. }
  282. // 获取GetReportListResult的JSON字符串
  283. jsonStr := envelope_five.Body.GetJSONReportItemListByGPBarocdeResponse.GetJSONReportItemListByGPBarocdeResult
  284. // 解析JSON字符串到结构体
  285. var reportData models.ReportResultDataFive
  286. err = json.Unmarshal([]byte(jsonStr), &reportData)
  287. if err != nil {
  288. fmt.Println("JSON Unmarshal error:", err)
  289. }
  290. //fmt.Println(reportData)
  291. if reportData.Res == 0 {
  292. //reportData.ListResult = item
  293. for _, subItem := range reportData.InspectResult {
  294. subItem.ListResult = item
  295. results = append(results, subItem)
  296. }
  297. }
  298. }
  299. }
  300. }
  301. return results
  302. }
  303. // 送检单
  304. func PostInspectApply(key string, apply models.JYApply, list []models.CustomerApplyDetail) models.InspectApplyData {
  305. // 构建SOAP请求数据
  306. requestData := &models.JYApplyEnvelope{
  307. Soap: "http://schemas.xmlsoap.org/soap/envelope/",
  308. Xsi: "http://www.w3.org/2001/XMLSchema-instance",
  309. Xsd: "http://www.w3.org/2001/XMLSchema",
  310. Body: models.JYApplyBody{
  311. InspectApplyReq: models.InspectApplyReq{
  312. XMLName: xml.Name{Local: "InspectApplyByInstituteItemCode", Space: "http://tempuri.org/"},
  313. ApiKey: key,
  314. Apply: apply,
  315. ApplyDetailList: struct {
  316. CustomerApplyDetail []models.CustomerApplyDetail `xml:"CustomerApplyDetail"`
  317. }{
  318. CustomerApplyDetail: list,
  319. },
  320. ApplyAdditional: nil,
  321. },
  322. },
  323. }
  324. // 将SOAP请求数据转换为XML格式
  325. requestBody, err := xml.Marshal(requestData)
  326. if err != nil {
  327. fmt.Println("XML Marshal error:", err)
  328. }
  329. fmt.Println(string(requestBody))
  330. // 创建POST请求
  331. url := "http://gpis.cnhiqc.com:9799/InstituteLISService.asmx"
  332. req, err := http.NewRequest("POST", url, bytes.NewBuffer(requestBody))
  333. if err != nil {
  334. fmt.Println("Request creation error:", err)
  335. }
  336. // 设置请求头
  337. req.Header.Set("Content-Type", "text/xml; charset=utf-8")
  338. req.Header.Set("Content-Length", "length")
  339. req.Header.Set("SOAPAction", "http://tempuri.org/InspectApplyByInstituteItemCode")
  340. // 发送请求
  341. client := &http.Client{}
  342. resp, err := client.Do(req)
  343. if err != nil {
  344. fmt.Println("Request error:", err)
  345. }
  346. defer resp.Body.Close()
  347. // 读取响应数据
  348. responseBody, err := ioutil.ReadAll(resp.Body)
  349. if err != nil {
  350. fmt.Println("Response read error:", err)
  351. }
  352. fmt.Println(string(responseBody))
  353. // 解析XML数据
  354. var envelope_four models.EnvelopeFour
  355. err2 := xml.Unmarshal(responseBody, &envelope_four)
  356. if err2 != nil {
  357. fmt.Println("XML Unmarshal error:", err2)
  358. }
  359. // 获取GetReportListResult的JSON字符串
  360. jsonStr := envelope_four.Body.InspectApplyByInstituteItemCodeResponse.InspectApply
  361. // 解析JSON字符串到结构体
  362. var reportData models.InspectApplyData
  363. err = json.Unmarshal([]byte(jsonStr), &reportData)
  364. if err != nil {
  365. fmt.Println("JSON Unmarshal error:", err)
  366. }
  367. if reportData.Res != 0 {
  368. fmt.Println("Data:", reportData)
  369. //return reportData.JYApplyResult
  370. }
  371. return reportData
  372. }