xml_service.go 12KB

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