sssy_service.go 37KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. package service
  2. import (
  3. "IC/models"
  4. "IC/utils"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/astaxie/beego"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. key = "e7a96b253a9e45e78eda2a849ae5e200"
  15. )
  16. func saveLog1(result string, request string, infno string, desc string) {
  17. org_id, _ := beego.AppConfig.Int64("org_id")
  18. //miConfig, _ := service.FindMedicalInsuranceInfo(org_id)
  19. dir := "下载数据日志"
  20. utils.Mkdir(dir)
  21. month := time.Unix(1557042972, 0).Format("1")
  22. year := time.Now().Format("2006")
  23. month = time.Now().Format("01")
  24. day := time.Now().Format("02")
  25. hour := time.Now().Format("15")
  26. min := time.Now().Format("04")
  27. sec := time.Now().Format("05")
  28. result_time := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
  29. file := strconv.FormatInt(org_id, 10) + "_" + year + month + day + "_log"
  30. file_name := file + ".txt"
  31. file_path := "下载数据日志" + "/" + file_name
  32. exist, _ := utils.PathExists(file_path)
  33. if exist { //存在
  34. fmt.Println("存在")
  35. f, err := os.OpenFile(file_path, os.O_WRONLY, 0644)
  36. if err != nil {
  37. fmt.Println("read fail")
  38. }
  39. content := "\r\n" + "\r\n" + "\r\n" + result_time + " " + "【 " + desc + infno + "入参" + " 】:" + "\r\n" + request + "\r\n" + result_time + " " + "【 " + desc + infno + "出参" + " 】:" + "\r\n" + result
  40. n, _ := f.Seek(0, 2)
  41. _, err = f.WriteAt([]byte(content), n)
  42. } else { //不存在
  43. fmt.Println("文件不存在,创建文件")
  44. f, err := os.Create("下载数据日志" + "/" + file_name)
  45. defer f.Close()
  46. if err != nil {
  47. } else {
  48. _, err = f.Write([]byte("记录日志"))
  49. }
  50. }
  51. }
  52. func SaveErrorLog(result string, request string, infno string, desc string) {
  53. //org_id, _ := beego.AppConfig.Int64("org_id")
  54. //miConfig, _ := service.FindMedicalInsuranceInfo(org_id)
  55. dir := "faild"
  56. utils.Mkdir(dir)
  57. month := time.Unix(1557042972, 0).Format("1")
  58. year := time.Now().Format("2006")
  59. month = time.Now().Format("01")
  60. day := time.Now().Format("02")
  61. hour := time.Now().Format("15")
  62. min := time.Now().Format("04")
  63. sec := time.Now().Format("05")
  64. result_time := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
  65. file := "failed" + "_" + year + month + day + "_log"
  66. file_name := file + ".txt"
  67. file_path := "faild" + "/" + file_name
  68. exist, _ := utils.PathExists(file_path)
  69. if exist { //存在
  70. fmt.Println("存在")
  71. f, err := os.OpenFile(file_path, os.O_WRONLY, 0644)
  72. if err != nil {
  73. fmt.Println("read fail")
  74. }
  75. content := "\r\n" + "\r\n" + "\r\n" + result_time + " " + "【 " + desc + infno + "入参" + " 】:" + "\r\n" + request + "\r\n" + result_time + " " + "【 " + desc + infno + "出参" + " 】:" + "\r\n" + result
  76. n, _ := f.Seek(0, 2)
  77. _, err = f.WriteAt([]byte(content), n)
  78. } else { //不存在
  79. fmt.Println("文件不存在,创建文件")
  80. f, err := os.Create("faild" + "/" + file_name)
  81. defer f.Close()
  82. if err != nil {
  83. } else {
  84. _, err = f.Write([]byte("记录日志"))
  85. }
  86. }
  87. }
  88. func saveSuccessLog(result string, request string, infno string, desc string) {
  89. //org_id, _ := beego.AppConfig.Int64("org_id")
  90. //miConfig, _ := service.FindMedicalInsuranceInfo(org_id)
  91. //dir := "同步成功日志"
  92. //utils.Mkdir(dir)
  93. month := time.Unix(1557042972, 0).Format("1")
  94. year := time.Now().Format("2006")
  95. month = time.Now().Format("01")
  96. day := time.Now().Format("02")
  97. hour := time.Now().Format("15")
  98. min := time.Now().Format("04")
  99. sec := time.Now().Format("05")
  100. result_time := year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec
  101. file := "success" + "_" + year + month + day + "_log"
  102. file_name := file + ".txt"
  103. file_path := file_name
  104. exist, _ := utils.PathExists(file_path)
  105. if exist { //存在
  106. fmt.Println("存在")
  107. f, err := os.OpenFile(file_path, os.O_WRONLY, 0644)
  108. if err != nil {
  109. fmt.Println("read fail")
  110. }
  111. content := "\r\n" + "\r\n" + "\r\n" + result_time + " " + "【 " + desc + infno + "入参" + " 】:" + "\r\n" + request + "\r\n" + result_time + " " + "【 " + desc + infno + "出参" + " 】:" + "\r\n" + result
  112. n, _ := f.Seek(0, 2)
  113. _, err = f.WriteAt([]byte(content), n)
  114. } else { //不存在
  115. fmt.Println("文件不存在,创建文件")
  116. f, err := os.Create(file_name)
  117. defer f.Close()
  118. if err != nil {
  119. } else {
  120. _, err = f.Write([]byte("记录日志"))
  121. }
  122. }
  123. }
  124. // 发送申请单逻辑
  125. func UploadSYData(org_id int64) {
  126. //record, _ := GetLastSyncRecord(org_id)
  127. //if record.ID == 0 {
  128. list, _ := GetAllLisDataFor10191(org_id)
  129. if len(list) > 0 {
  130. //插入中间库中
  131. for _, item := range list {
  132. if len(item.ApplyCode) == 0 {
  133. var apply models.JYApply
  134. apply.BedCode = ""
  135. apply.PatientName = item.Patient.Name
  136. idCard := item.Patient.IdCardNo // 身份证号码示例
  137. age, err := GetAge(idCard)
  138. if err != nil {
  139. age = int(item.Patient.Age)
  140. fmt.Println("计算年龄时出错:", err)
  141. }
  142. apply.Age = strconv.FormatInt(int64(age), 10)
  143. apply.AgeUnit = "岁"
  144. if item.Patient.Gender == 1 {
  145. apply.Sex = "男"
  146. } else if item.Patient.Gender == 2 {
  147. apply.Sex = "女"
  148. } else {
  149. apply.Sex = "不详"
  150. }
  151. apply.RegisterCode = ""
  152. apply.Diagnosis = ""
  153. apply.Ward = ""
  154. role, _ := GetAdminUserInfoByID(org_id, 2398)
  155. apply.SendDoctor = role.UserName
  156. apply.PatientType = 1
  157. apply.PatientPhone = item.Patient.Phone
  158. //applys = append(applys, apply)
  159. var jYApplyDetails []models.CustomerApplyDetail
  160. if len(item.ItemIds) > 0 {
  161. team_ids := strings.Split(item.ItemIds, ",")
  162. for index, subItem := range team_ids {
  163. //判断一些需要拆分的组套 血透体检(定量):需要拆分出传染病检查(定性),丙型肝炎抗体测试,血透体检(定量)
  164. if subItem == "267" {
  165. //血透体检(定量)
  166. var jYApplyDetail models.CustomerApplyDetail
  167. id, _ := strconv.ParseInt(subItem, 10, 64)
  168. jYApplyDetail.CustomerChargeItemCode = strconv.FormatInt(id, 10)
  169. jYApplyDetail.RowIndex = int64(index + 1)
  170. jYApplyDetail.Barcode = ""
  171. jYApplyDetail.CustomerBarcode = strconv.FormatInt(item.ID, 10)
  172. jYApplyDetail.Comment = ""
  173. jYApplyDetail.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05")
  174. jYApplyDetails = append(jYApplyDetails, jYApplyDetail)
  175. //丙型肝炎抗体测定
  176. var jYApplyDetail2 models.CustomerApplyDetail
  177. id2, _ := strconv.ParseInt("26701", 10, 64)
  178. jYApplyDetail2.CustomerChargeItemCode = strconv.FormatInt(id2, 10)
  179. jYApplyDetail2.RowIndex = int64(index + 1)
  180. jYApplyDetail2.Barcode = ""
  181. jYApplyDetail2.CustomerBarcode = strconv.FormatInt(item.ID, 10)
  182. jYApplyDetail2.Comment = ""
  183. jYApplyDetail2.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05")
  184. jYApplyDetails = append(jYApplyDetails, jYApplyDetail2)
  185. //传染病检查(定性)
  186. var jYApplyDetail3 models.CustomerApplyDetail
  187. id3, _ := strconv.ParseInt("26700", 10, 64)
  188. jYApplyDetail3.CustomerChargeItemCode = strconv.FormatInt(id3, 10)
  189. jYApplyDetail3.RowIndex = int64(index + 1)
  190. jYApplyDetail3.Barcode = ""
  191. jYApplyDetail3.CustomerBarcode = strconv.FormatInt(item.ID, 10)
  192. jYApplyDetail3.Comment = ""
  193. jYApplyDetail3.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05")
  194. jYApplyDetails = append(jYApplyDetails, jYApplyDetail3)
  195. }
  196. //判断一些需要拆分的组套 血透体检(定性):需要拆分出传染病检查(定性),丙型肝炎抗体测试,血透体检(定性)
  197. if subItem == "424" {
  198. //血透体检(定性)
  199. var jYApplyDetail models.CustomerApplyDetail
  200. id, _ := strconv.ParseInt(subItem, 10, 64)
  201. jYApplyDetail.CustomerChargeItemCode = strconv.FormatInt(id, 10)
  202. jYApplyDetail.RowIndex = int64(index + 1)
  203. jYApplyDetail.Barcode = ""
  204. jYApplyDetail.CustomerBarcode = strconv.FormatInt(item.ID, 10)
  205. jYApplyDetail.Comment = ""
  206. jYApplyDetail.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05")
  207. jYApplyDetails = append(jYApplyDetails, jYApplyDetail)
  208. //丙型肝炎抗体测定
  209. var jYApplyDetail2 models.CustomerApplyDetail
  210. id2, _ := strconv.ParseInt("42401", 10, 64)
  211. jYApplyDetail2.CustomerChargeItemCode = strconv.FormatInt(id2, 10)
  212. jYApplyDetail2.RowIndex = int64(index + 1)
  213. jYApplyDetail2.Barcode = ""
  214. jYApplyDetail2.CustomerBarcode = strconv.FormatInt(item.ID, 10)
  215. jYApplyDetail2.Comment = ""
  216. jYApplyDetail2.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05")
  217. jYApplyDetails = append(jYApplyDetails, jYApplyDetail2)
  218. //传染病检查(定性)
  219. var jYApplyDetail3 models.CustomerApplyDetail
  220. id3, _ := strconv.ParseInt("42400", 10, 64)
  221. jYApplyDetail3.CustomerChargeItemCode = strconv.FormatInt(id3, 10)
  222. jYApplyDetail3.RowIndex = int64(index + 1)
  223. jYApplyDetail3.Barcode = ""
  224. jYApplyDetail3.CustomerBarcode = strconv.FormatInt(item.ID, 10)
  225. jYApplyDetail3.Comment = ""
  226. jYApplyDetail3.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05")
  227. jYApplyDetails = append(jYApplyDetails, jYApplyDetail3)
  228. }
  229. var jYApplyDetail models.CustomerApplyDetail
  230. id, _ := strconv.ParseInt(subItem, 10, 64)
  231. jYApplyDetail.CustomerChargeItemCode = strconv.FormatInt(id, 10)
  232. jYApplyDetail.RowIndex = int64(index + 1)
  233. jYApplyDetail.Barcode = ""
  234. jYApplyDetail.CustomerBarcode = strconv.FormatInt(item.ID, 10)
  235. jYApplyDetail.Comment = ""
  236. jYApplyDetail.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05")
  237. jYApplyDetails = append(jYApplyDetails, jYApplyDetail)
  238. }
  239. }
  240. if len(item.ProjectIds) > 0 {
  241. team_ids := strings.Split(item.ProjectIds, ",")
  242. for index, subItem := range team_ids {
  243. var jYApplyDetail models.CustomerApplyDetail
  244. id, _ := strconv.ParseInt(subItem, 10, 64)
  245. jYApplyDetail.CustomerChargeItemCode = strconv.FormatInt(id, 10)
  246. jYApplyDetail.RowIndex = int64(index)
  247. jYApplyDetail.Barcode = ""
  248. jYApplyDetail.CustomerBarcode = strconv.FormatInt(item.ID, 10)
  249. jYApplyDetail.Comment = ""
  250. jYApplyDetail.TakeSampleTime = time.Unix(item.Ctime, 0).Format("2006-01-02 15:04:05")
  251. jYApplyDetails = append(jYApplyDetails, jYApplyDetail)
  252. }
  253. }
  254. result := PostInspectApply(key, apply, jYApplyDetails)
  255. if result.Res == 0 {
  256. item.ApplyCode = result.JYApplyResult.ApplyCode
  257. writeDb.Save(&item)
  258. jsonData, err := json.Marshal(apply)
  259. if err != nil {
  260. fmt.Println("转换为JSON时发生错误:", err)
  261. }
  262. jsonStr := string(jsonData)
  263. jsonData2, err2 := json.Marshal(jYApplyDetails)
  264. if err2 != nil {
  265. fmt.Println("转换为JSON时发生错误:", err2)
  266. }
  267. jsonStr2 := string(jsonData2)
  268. jsonData3, err3 := json.Marshal(result)
  269. if err3 != nil {
  270. fmt.Println("转换为JSON时发生错误:", err3)
  271. }
  272. jsonStr3 := string(jsonData3)
  273. saveSuccessLog(jsonStr3, jsonStr+"\r\n"+jsonStr2, "申清单成功日志", "申清单")
  274. } else {
  275. jsonData, err := json.Marshal(apply)
  276. if err != nil {
  277. fmt.Println("转换为JSON时发生错误:", err)
  278. }
  279. jsonStr := string(jsonData)
  280. jsonData2, err2 := json.Marshal(jYApplyDetails)
  281. if err2 != nil {
  282. fmt.Println("转换为JSON时发生错误:", err2)
  283. }
  284. jsonStr2 := string(jsonData2)
  285. jsonData3, err3 := json.Marshal(result)
  286. if err3 != nil {
  287. fmt.Println("转换为JSON时发生错误:", err3)
  288. }
  289. jsonStr3 := string(jsonData3)
  290. SaveErrorLog(jsonStr3, jsonStr+"\r\n"+jsonStr2, "申清单错误日志", "申清单")
  291. }
  292. }
  293. }
  294. //插入一条插入中间库记录数据
  295. var info LisSyncProcessStatusInfo
  296. info.Ctime = time.Now().Unix()
  297. info.Status = 1
  298. info.Mtime = time.Now().Unix()
  299. info.OrgId = org_id
  300. info.IsInsert = 1
  301. info.IsResult = 0
  302. //info.LastId = list[len(list)-1].ID
  303. info.ResultDate = 0
  304. writeMiddleDb.Save(&info)
  305. }
  306. }
  307. // 同步逻辑
  308. //func SyncSYData(org_id int64) {
  309. // record, _ := GetLastSyncResultRecord(org_id)
  310. // if record.ID == 0 {
  311. // //通过接口从高品那里获取检验数据
  312. // start_time := "2023-06-01"
  313. // end_time := "2023-06-30"
  314. // result := GetGPReportListData(start_time, end_time, key)
  315. // list := GetGPResultDataTwo(result, key)
  316. // jsonData, err := json.Marshal(result)
  317. // if err != nil {
  318. // fmt.Println("转换为JSON时发生错误:", err)
  319. // return
  320. // }
  321. // jsonStr := string(jsonData)
  322. // jsonData1, err1 := json.Marshal(list)
  323. // if err1 != nil {
  324. // fmt.Println("转换为JSON时发生错误:", err1)
  325. // return
  326. // }
  327. // jsonStr1 := string(jsonData1)
  328. // saveLog1(jsonStr, "", "列表数据", "列表数据")
  329. // saveLog1(jsonStr1, "", "结果数据", "结果数据")
  330. //
  331. // if err == nil {
  332. // fmt.Println(list)
  333. // //插入到系统检验检查数据
  334. // if len(list) > 0 {
  335. // for _, item := range list {
  336. // project_id := int64(0)
  337. // //xt_project_id := int64(0)
  338. // if len(item.ListResult.Barcode) > 0 {
  339. // project_id, _, _ = GetSyjhProjectID(org_id, item.ChargeItemName)
  340. // } else {
  341. // continue
  342. // }
  343. // //var patient models.Patients
  344. // printInfo, _ := GetHisLabelPrintStatusInfoById(item.ListResult.CustomerBarcode)
  345. // if printInfo.ID == 0 {
  346. // patient, _ := GetPatientID(org_id, item.ListResult.PatientName)
  347. // printInfo.PatientId = patient.ID
  348. //
  349. // }
  350. // if printInfo.PatientId == 0 {
  351. // continue
  352. // }
  353. // item_id, _ := GetSyItemID(org_id, item.ChargeItemName, item.InspectionName, project_id)
  354. // tx := writeMiddleDb.Begin()
  355. // var inspection models.MiddleInspection
  356. // var inspection_reference models.MiddleInspectionReference
  357. //
  358. // recordDateStr := ""
  359. // inspect_date := ""
  360. // if len(item.ListResult.InspectDate) > 0 {
  361. //
  362. // inspect_date = item.ListResult.InspectDate
  363. //
  364. // } else {
  365. // inspect_date = strings.Split(item.ListResult.ReceiveDateTime, " ")[0]
  366. //
  367. // }
  368. //
  369. // recordDateStr = inspect_date
  370. //
  371. // date, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  372. // record_date := date
  373. // var total int
  374. // var RangeOptions string
  375. // var RangeMin string
  376. // var RangeMax string
  377. // // 判断检查类型
  378. // var ItemType int
  379. // if strings.Contains(item.Reference, "--") {
  380. // ItemType = 1
  381. // } else {
  382. // ItemType = 2
  383. //
  384. // }
  385. //
  386. // if ItemType == 1 {
  387. // Range := strings.Split(item.Reference, "--")
  388. // RangeMin = Range[0]
  389. // RangeMax = Range[1]
  390. // } else {
  391. // RangeOptions = item.Reference
  392. // }
  393. // err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error
  394. // if inspection_reference.ID > 0 {
  395. // ItemType = inspection_reference.RangeType
  396. // }
  397. // if total <= 0 {
  398. // inspection_reference.OrgId = org_id
  399. // inspection_reference.ProjectName = item.ChargeItemName
  400. // inspection_reference.Project = item.ChargeItemName
  401. // inspection_reference.ProjectId = project_id
  402. // inspection_reference.ItemName = item.InspectionName
  403. // inspection_reference.ItemNameAddition = item.Barcode
  404. // inspection_reference.ItemId = item_id
  405. // inspection_reference.RangeType = ItemType
  406. // inspection_reference.RangeMin = RangeMin
  407. // inspection_reference.RangeMax = RangeMax
  408. // inspection_reference.RangeOptions = RangeOptions
  409. // inspection_reference.Unit = item.Unit
  410. // inspection_reference.Status = 1
  411. // inspection_reference.CreatedTime = time.Now().Unix()
  412. // inspection_reference.UpdatedTime = time.Now().Unix()
  413. // inspection_reference.InspectDate = inspect_date
  414. // inspection_reference.UTime = inspect_date
  415. // err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  416. // if err != nil {
  417. // tx.Rollback()
  418. // }
  419. // }
  420. // var itotal int
  421. // err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, record_date.Unix(), printInfo.PatientId).Find(&inspection).Count(&itotal).Error
  422. // if itotal <= 0 {
  423. // //item.Result = strings.Replace(item.Result, "&gt;", ">", -1)
  424. // //item.Result = strings.Replace(item.Result, "&lt;", "<", -1)
  425. // inspection.PatientId = printInfo.PatientId
  426. // inspection.OrgId = org_id
  427. // inspection.ProjectId = project_id
  428. // inspection.ItemName = inspection_reference.ItemName
  429. // inspection.ProjectName = inspection_reference.ProjectName
  430. // inspection.InspectType = ItemType
  431. // inspection.ItemId = item_id
  432. //
  433. // inspection.InspectValue = item.Result
  434. // inspection.InspectDate = inspect_date
  435. // inspection.RecordDate = record_date.Unix()
  436. // inspection.Status = 1
  437. // inspection.CreatedTime = time.Now().Unix()
  438. // inspection.UpdatedTime = time.Now().Unix()
  439. // inspection.UTime = inspect_date
  440. // inspection.HisUserId = strconv.FormatInt(printInfo.PatientId, 10)
  441. // err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  442. // if err != nil {
  443. // tx.Rollback()
  444. // }
  445. // }
  446. // tx.Commit()
  447. //
  448. // }
  449. // //插入一条查询检验检查结果记录状态数据
  450. // var info LisSyncResultStatusInfo
  451. // info.Ctime = time.Now().Unix()
  452. // info.Status = 1
  453. // info.Mtime = time.Now().Unix()
  454. // info.OrgId = org_id
  455. // info.IsResult = 1
  456. // code, _ := strconv.ParseInt(list[0].Barcode, 10, 64)
  457. // info.LastId = code
  458. // info.ResultDate = time.Now().Unix()
  459. // info.GpSyncEndDate = end_time
  460. // writeMiddleDb.Save(&info)
  461. // }
  462. // }
  463. //
  464. // } else {
  465. // //start_time := record.GpSyncEndDate
  466. // //end_time := time.Now().Format("2006-01-02")
  467. // //
  468. // //result := GetGPReportListData(start_time, end_time, key)
  469. // //list := GetGPResultDataTwo(result, key)
  470. // //
  471. // ////插入一条插入中间库记录数据
  472. // //if err == nil {
  473. // // if len(list) > 0 {
  474. // // //插入到系统检验检查数据
  475. // // for _, item := range list {
  476. // // project_id := int64(0)
  477. // // if len(item.ListResult.CustomerBarcode) > 0 {
  478. // // project_id, _, _ = GetSyjhProjectID(org_id, item.ChargeItemName)
  479. // // } else {
  480. // // continue
  481. // // }
  482. // // var patient models.Patients
  483. // // printInfo, _ := GetHisLabelPrintStatusInfoById(item.ListResult.CustomerBarcode)
  484. // // if printInfo.ID == 0 {
  485. // // patient, _ = GetPatientID(org_id, item.ListResult.PatientName)
  486. // // }
  487. // // if patient.ID > 0 {
  488. // // printInfo.PatientId = patient.ID
  489. // // }
  490. // // if printInfo.PatientId == 0 {
  491. // // continue
  492. // // }
  493. // //
  494. // // item_id, _ := GetSyItemID(org_id, item.ChargeItemName, item.InspectionName, project_id)
  495. // // tx := writeMiddleDb.Begin()
  496. // // var inspection models.MiddleInspection
  497. // // var inspection_reference models.MiddleInspectionReference
  498. // //
  499. // // recordDateStr := ""
  500. // // inspect_date := ""
  501. // // if len(item.ListResult.InspectDate) > 0 {
  502. // //
  503. // // inspect_date = item.ListResult.InspectDate
  504. // // } else {
  505. // // inspect_date = strings.Split(item.ListResult.ReceiveDateTime, " ")[0]
  506. // //
  507. // // }
  508. // // recordDateStr = inspect_date
  509. // //
  510. // // date, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  511. // // record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  512. // // var total int
  513. // // var RangeOptions string
  514. // // var RangeMin string
  515. // // var RangeMax string
  516. // // // 判断检查类型
  517. // // var ItemType int
  518. // // if strings.Contains(item.Reference, "--") {
  519. // // ItemType = 1
  520. // // } else {
  521. // // ItemType = 2
  522. // //
  523. // // }
  524. // //
  525. // // if ItemType == 1 {
  526. // // Range := strings.Split(item.Reference, "--")
  527. // // RangeMin = Range[0]
  528. // // RangeMax = Range[1]
  529. // // } else {
  530. // // RangeOptions = item.Reference
  531. // // }
  532. // //
  533. // // err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project_id = ? and item_id = ? and status = 1", org_id, project_id, item_id).Find(&inspection_reference).Count(&total).Error
  534. // // if inspection_reference.ID > 0 {
  535. // // ItemType = inspection_reference.RangeType
  536. // // }
  537. // // if total <= 0 {
  538. // // inspection_reference.OrgId = org_id
  539. // // inspection_reference.ProjectName = item.ChargeItemName
  540. // // inspection_reference.Project = item.ChargeItemName
  541. // // inspection_reference.ProjectId = project_id
  542. // // inspection_reference.ItemName = item.InspectionName
  543. // // inspection_reference.ItemNameAddition = item.Barcode
  544. // // inspection_reference.ItemId = item_id
  545. // // inspection_reference.RangeType = ItemType
  546. // // inspection_reference.RangeMin = RangeMin
  547. // // inspection_reference.RangeMax = RangeMax
  548. // // inspection_reference.RangeOptions = RangeOptions
  549. // // inspection_reference.Unit = item.Unit
  550. // // inspection_reference.Status = 1
  551. // // inspection_reference.CreatedTime = time.Now().Unix()
  552. // // inspection_reference.UpdatedTime = time.Now().Unix()
  553. // // inspection_reference.InspectDate = inspect_date
  554. // // inspection_reference.UTime = inspect_date
  555. // // err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  556. // // if err != nil {
  557. // // tx.Rollback()
  558. // // }
  559. // // }
  560. // //
  561. // // var itotal int
  562. // // err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, item_id, record_date.Unix(), printInfo.PatientId).Find(&inspection).Count(&itotal).Error
  563. // // if itotal <= 0 {
  564. // // //item.Result = strings.Replace(item.Result, "&gt;", ">", -1)
  565. // // //item.Result = strings.Replace(item.Result, "&lt;", "<", -1)
  566. // // inspection.PatientId = printInfo.PatientId
  567. // // inspection.OrgId = org_id
  568. // // inspection.ProjectId = project_id
  569. // // inspection.ItemName = inspection_reference.ItemName
  570. // // inspection.ProjectName = inspection_reference.ProjectName
  571. // // inspection.InspectType = ItemType
  572. // // inspection.ItemId = item_id
  573. // //
  574. // // inspection.InspectValue = item.Result
  575. // // inspection.InspectDate = inspect_date
  576. // // inspection.RecordDate = record_date.Unix()
  577. // // inspection.Status = 1
  578. // // inspection.CreatedTime = time.Now().Unix()
  579. // // inspection.UpdatedTime = time.Now().Unix()
  580. // // inspection.UTime = inspect_date
  581. // // inspection.HisUserId = strconv.FormatInt(printInfo.PatientId, 10)
  582. // // err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  583. // // if err != nil {
  584. // // tx.Rollback()
  585. // // }
  586. // // }
  587. // //
  588. // // tx.Commit()
  589. // //
  590. // // }
  591. // // //插入一条查询检验检查结果记录状态数据
  592. // // var info LisSyncResultStatusInfo
  593. // // info.Ctime = time.Now().Unix()
  594. // // info.Status = 1
  595. // // info.Mtime = time.Now().Unix()
  596. // // info.OrgId = org_id
  597. // // info.IsResult = 1
  598. // // info.ResultDate = time.Now().Unix()
  599. // // info.GpSyncEndDate = end_time
  600. // // writeMiddleDb.Save(&info)
  601. // // }
  602. // //}
  603. // }
  604. // //// 第一步:跟进org_id 去中间库查出需要同步的数据
  605. //
  606. // //inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  607. // //inspections, _ := GetSyncInspectionByOrgId(org_id)
  608. // //// 第二步:将数据同步到业务库
  609. // //if len(inspection_references) > 0 {
  610. // // for _, inspection_reference := range inspection_references {
  611. // // SyncInspectionReference(&inspection_reference)
  612. // // }
  613. // //}
  614. // //if len(inspections) > 0 {
  615. // // for _, inspection := range inspections {
  616. // // SyncInspection(&inspection)
  617. // // }
  618. // //}
  619. // //return
  620. //}
  621. func SyncSYData2() {
  622. org_id := int64(10191)
  623. start_time := "2022-01-10"
  624. end_time := "2024-04-26"
  625. record, _ := GetLastSyncResultRecord(org_id)
  626. if record.ID > 0 {
  627. start_time = record.GpSyncEndDate
  628. end_time = time.Now().Format("2006-01-02")
  629. }
  630. loc, _ := time.LoadLocation("Local")
  631. theTime, _ := time.ParseInLocation("2006-01-02", start_time, loc)
  632. newtheTime := theTime.Unix() - 60*60*24
  633. SyncTime := time.Unix(newtheTime, 0)
  634. syncTimeStr := SyncTime.Format("2006-01-02")
  635. result := GetGPReportListData(syncTimeStr, end_time, key)
  636. if len(result) > 0 {
  637. for _, item := range result {
  638. list := GetGPResultDataTwo(item, key)
  639. //插入到系统检验检查数据
  640. if len(list) > 0 {
  641. for _, item := range list {
  642. project_id := int64(0)
  643. patient, _ := GetPatientID(org_id, item.ListResult.PatientName)
  644. if patient.ID == 0 {
  645. continue
  646. }
  647. tx := writeMiddleDb.Begin()
  648. var inspection models.MiddleInspection
  649. var inspection_reference models.MiddleInspectionReference
  650. recordDateStr := item.ListResult.ReceiveDateTime
  651. recordDate, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  652. inspect_date := recordDate.Format("2006-01-02 15:04")
  653. date, _ := time.Parse("2006-01-02 15:04:05", recordDateStr)
  654. utils.InfoLog("date2: %v", date.Unix())
  655. record_date_time, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
  656. record_date := record_date_time.Unix()
  657. var total int
  658. var RangeOptions string
  659. var RangeMin string
  660. var RangeMax string
  661. // 判断检查类型
  662. var ItemType int
  663. if strings.Contains(item.Reference, "--") {
  664. ItemType = 1
  665. } else {
  666. ItemType = 2
  667. }
  668. if ItemType == 1 {
  669. Range := strings.Split(item.Reference, "--")
  670. RangeMin = Range[0]
  671. RangeMax = Range[1]
  672. } else {
  673. RangeOptions = item.Reference
  674. }
  675. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and project = ? and item_name = ? and status = 1", org_id, item.ChargeItemName, item.InspectionName).Find(&inspection_reference).Count(&total).Error
  676. if inspection_reference.ID > 0 {
  677. ItemType = inspection_reference.RangeType
  678. project_id = inspection_reference.ProjectId
  679. }
  680. if total <= 0 {
  681. var inspection_referenceTwo models.MiddleInspectionReference
  682. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, item.ChargeItemName).First(&inspection_referenceTwo).Error
  683. if inspection_referenceTwo.ID > 0 {
  684. project_id = inspection_referenceTwo.ProjectId
  685. } else {
  686. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_referenceTwo).Error
  687. if inspection_referenceTwo.ProjectId > 0 {
  688. project_id = inspection_referenceTwo.ProjectId + 1
  689. } else {
  690. project_id = 1019101
  691. }
  692. }
  693. var item_id int64
  694. var inspection_referenceThree models.MiddleInspectionReference
  695. readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and project_id = ?", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_referenceThree)
  696. utils.InfoLog("inspection_reference: %v", inspection_reference)
  697. if inspection_referenceThree.ItemId > 0 {
  698. item_id = inspection_referenceThree.ItemId + 1
  699. } else {
  700. item_id = project_id*100 + 1
  701. }
  702. inspection_reference.OrgId = org_id
  703. inspection_reference.ProjectName = item.ChargeItemName
  704. inspection_reference.Project = item.ChargeItemName
  705. inspection_reference.ProjectId = project_id
  706. inspection_reference.ItemName = item.InspectionName
  707. inspection_reference.ItemNameAddition = item.Barcode
  708. inspection_reference.ItemId = item_id
  709. inspection_reference.RangeType = ItemType
  710. inspection_reference.RangeMin = RangeMin
  711. inspection_reference.RangeMax = RangeMax
  712. inspection_reference.RangeOptions = RangeOptions
  713. inspection_reference.Unit = item.Unit
  714. inspection_reference.Status = 1
  715. inspection_reference.CreatedTime = time.Now().Unix()
  716. inspection_reference.UpdatedTime = time.Now().Unix()
  717. inspection_reference.InspectDate = inspect_date
  718. inspection_reference.UTime = inspect_date
  719. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  720. if err != nil {
  721. tx.Rollback()
  722. }
  723. }
  724. var itotal int
  725. err = readMiddleDb.Model(&models.MiddleInspection{}).Where("org_id = ? and project_id = ? and item_id = ? and record_date = ? and patient_id = ? and status = 1", org_id, project_id, inspection_reference.ItemId, record_date, patient.ID).Find(&inspection).Count(&itotal).Error
  726. if itotal <= 0 {
  727. //item.Result = strings.Replace(item.Result, "&gt;", ">", -1)
  728. //item.Result = strings.Replace(item.Result, "&lt;", "<", -1)
  729. inspection.PatientId = patient.ID
  730. inspection.OrgId = org_id
  731. inspection.ProjectId = project_id
  732. inspection.ItemName = inspection_reference.ItemName
  733. inspection.ProjectName = inspection_reference.ProjectName
  734. inspection.InspectType = ItemType
  735. inspection.ItemId = inspection_reference.ItemId
  736. inspection.InspectValue = item.Result
  737. inspection.InspectDate = inspect_date
  738. inspection.RecordDate = record_date
  739. inspection.Status = 1
  740. inspection.CreatedTime = time.Now().Unix()
  741. inspection.UpdatedTime = time.Now().Unix()
  742. inspection.UTime = inspect_date
  743. inspection.HisUserId = strconv.FormatInt(patient.ID, 10)
  744. inspection.SysProjectId = inspection_reference.XtProjectId
  745. inspection.SysItemId = inspection_reference.XtItemId
  746. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  747. if err != nil {
  748. tx.Rollback()
  749. }
  750. }
  751. tx.Commit()
  752. }
  753. }
  754. }
  755. //插入一条查询检验检查结果记录状态数据
  756. var info LisSyncResultStatusInfo
  757. info.Ctime = time.Now().Unix()
  758. info.Status = 1
  759. info.Mtime = time.Now().Unix()
  760. info.OrgId = org_id
  761. info.IsResult = 1
  762. info.ResultDate = time.Now().Unix()
  763. info.GpSyncEndDate = end_time
  764. writeMiddleDb.Save(&info)
  765. }
  766. SyncToSssytx()
  767. //UpdateJg()
  768. }
  769. func SyncToSssytx() {
  770. utils.TraceLog("检验检查同步任务开始执行")
  771. org_id := int64(10191)
  772. // 第一步:跟进org_id 去中间库查出需要同步的数据
  773. inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  774. inspections, _ := GetSyncInspectionByOrgId(org_id)
  775. // 第二步:将数据同步到业务库
  776. if len(inspection_references) > 0 {
  777. for _, inspection_reference := range inspection_references {
  778. SyncInspectionReference(&inspection_reference)
  779. }
  780. }
  781. if len(inspections) > 0 {
  782. for _, inspection := range inspections {
  783. SyncSssyInspection(&inspection)
  784. }
  785. }
  786. utils.SuccessLog("检验检查同步任务完成")
  787. }
  788. func UpdateJg() error {
  789. var inspections []models.Inspection
  790. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and inspect_date >= ? and status = 1", 10191, 15, 1704038400).Group("patient_id,inspect_date").Find(&inspections).Error
  791. for _, info := range inspections {
  792. var inspectionByOne []models.Inspection
  793. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and inspect_date = ? and patient_id = ? and status = 1", 10191, 15, info.InspectDate, info.PatientId).Find(&inspectionByOne).Error
  794. if len(inspectionByOne) == 2 {
  795. for _, tinfo := range inspectionByOne {
  796. utils.InfoLog("date1: %v", tinfo.ID)
  797. if tinfo.ItemId != 115 && tinfo.ItemId != 37 {
  798. // 报告单里只有肌酐和尿素这两个项目要上传到“透后肾功能”里
  799. continue
  800. }
  801. if tinfo.ItemId == 115 {
  802. tinfo.ProjectId = 8060
  803. tinfo.ItemId = 8060
  804. }
  805. if tinfo.ItemId == 37 {
  806. tinfo.ProjectId = 8060
  807. tinfo.ItemId = 8061
  808. }
  809. writeDb.Save(&tinfo)
  810. }
  811. }
  812. }
  813. return err
  814. }
  815. // 从机构将数据同步到中间库
  816. func SyncSssyInspection(data *models.MiddleInspection) error {
  817. tx := writeDb.Begin()
  818. var inspection models.Inspection
  819. var total int
  820. err = readDb.Model(&models.Inspection{}).Where("org_id = ? and project_id = ? and item_id = ? and patient_id =? and inspect_date=? and status = 1", data.OrgId, data.ProjectId, data.ItemId, data.PatientId, data.RecordDate).Find(&inspection).Count(&total).Error
  821. if total <= 0 {
  822. inspection.OrgId = data.OrgId
  823. inspection.PatientId = data.PatientId
  824. inspection.ProjectName = data.ProjectName
  825. // inspection.Project = data.ProjectName
  826. inspection.ProjectId = data.ProjectId
  827. inspection.ItemName = data.ItemName
  828. inspection.ItemId = data.ItemId
  829. inspection.InspectType = int64(data.InspectType)
  830. inspection.InspectValue = data.InspectValue
  831. inspection.InspectTips = data.InspectTips
  832. inspection.InspectDate = data.RecordDate
  833. inspection.Status = 1
  834. inspection.CreatedTime = time.Now().Unix()
  835. inspection.UpdatedTime = time.Now().Unix()
  836. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  837. if err != nil {
  838. tx.Rollback()
  839. }
  840. data.IsSync = 1
  841. data.SyncId = inspection.ID
  842. data.UpdatedTime = time.Now().Unix()
  843. ierr := writeMiddleDb.Save(&data).Error
  844. if ierr != nil {
  845. tx.Rollback()
  846. }
  847. }
  848. tx.Commit()
  849. return err
  850. }
  851. func GetAge(idCard string) (int, error) {
  852. birthDate, err := getBirthDateFromIDCard(idCard)
  853. if err != nil {
  854. return 0, err
  855. }
  856. age := calculateAge(birthDate)
  857. return age, nil
  858. }
  859. func getBirthDateFromIDCard(idCard string) (time.Time, error) {
  860. birthDateStr := idCard[6:14] // 身份证号码中生日部分的字符
  861. birthDate, err := time.Parse("20060102", birthDateStr)
  862. if err != nil {
  863. return time.Time{}, err
  864. }
  865. return birthDate, nil
  866. }
  867. func calculateAge(birthDate time.Time) int {
  868. now := time.Now()
  869. age := now.Year() - birthDate.Year()
  870. // 如果当前月份小于出生月份,或者当前月份等于出生月份但当前日期小于出生日期,则年龄减一
  871. if now.Month() < birthDate.Month() || (now.Month() == birthDate.Month() && now.Day() < birthDate.Day()) {
  872. age--
  873. }
  874. return age
  875. }
  876. func GetHisLabelPrintStatusInfoById(id string) (info models.HisLabelPrintStatusInfo, err error) {
  877. err = readDb.Model(&models.HisLabelPrintStatusInfo{}).Where("id = ?", id).First(&info).Error
  878. return
  879. }
  880. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  881. func GetSyjhProjectID(org_id int64, project_name string) (project_id int64, sys_project_id int64, err error) {
  882. var inspection_reference models.MiddleInspectionReference
  883. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  884. if inspection_reference.ID > 0 {
  885. return inspection_reference.ProjectId, inspection_reference.XtProjectId, err
  886. } else {
  887. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  888. if inspection_reference.ProjectId > 0 {
  889. return inspection_reference.ProjectId + 1, 0, err
  890. } else {
  891. return 10191000, 0, err
  892. }
  893. }
  894. }
  895. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  896. func GetSyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  897. var inspection_reference models.MiddleInspectionReference
  898. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  899. if inspection_reference.ID > 0 {
  900. return inspection_reference.ItemId, err
  901. } else {
  902. err := readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id = ? and project_id = ? ", org_id, project_id).Select("max(item_id) as item_id").First(&inspection_reference).Error
  903. utils.InfoLog("inspection_reference: %v", inspection_reference)
  904. if inspection_reference.ItemId > 0 {
  905. return inspection_reference.ItemId + 1, err
  906. } else {
  907. return project_id*10 + 1, err
  908. }
  909. }
  910. }
  911. // 从机构将数据同步到中间库
  912. func SyncInspectionReferenceThree(data *models.MiddleInspectionReference) error {
  913. tx := writeDb.Begin()
  914. var inspection_reference models.InspectionReference
  915. var total int
  916. err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? and item_id = ? and status = 1", data.OrgId, data.ItemId).Find(&inspection_reference).Count(&total).Error
  917. if total <= 0 {
  918. inspection_reference.OrgId = data.OrgId
  919. inspection_reference.ProjectName = data.ProjectName
  920. inspection_reference.Project = data.ProjectName
  921. inspection_reference.ProjectId = data.ProjectId
  922. inspection_reference.ItemName = data.ItemName
  923. inspection_reference.ItemId = data.ItemId
  924. inspection_reference.RangeType = data.RangeType
  925. inspection_reference.RangeMin = data.RangeMin
  926. inspection_reference.RangeMax = data.RangeMax
  927. inspection_reference.RangeValue = data.RangeValue
  928. inspection_reference.RangeOptions = data.RangeOptions
  929. inspection_reference.Unit = data.Unit
  930. inspection_reference.Status = 1
  931. inspection_reference.CreatedTime = time.Now().Unix()
  932. inspection_reference.UpdatedTime = time.Now().Unix()
  933. err := tx.Model(&models.InspectionReference{}).Create(&inspection_reference).Error
  934. if err != nil {
  935. tx.Rollback()
  936. }
  937. data.IsSync = 1
  938. data.SyncId = inspection_reference.ID
  939. data.UpdatedTime = time.Now().Unix()
  940. ierr := writeMiddleDb.Save(&data).Error
  941. if ierr != nil {
  942. tx.Rollback()
  943. }
  944. }
  945. tx.Commit()
  946. return err
  947. }