sssy_service.go 36KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. if err == nil {
  331. fmt.Println(list)
  332. //插入到系统检验检查数据
  333. if len(list) > 0 {
  334. for _, item := range list {
  335. project_id := int64(0)
  336. //xt_project_id := int64(0)
  337. if len(item.ListResult.Barcode) > 0 {
  338. project_id, _, _ = GetSyjhProjectID(org_id, item.ChargeItemName)
  339. } else {
  340. continue
  341. }
  342. //var patient models.Patients
  343. printInfo, _ := GetHisLabelPrintStatusInfoById(item.ListResult.CustomerBarcode)
  344. if printInfo.ID == 0 {
  345. patient, _ := GetPatientID(org_id, item.ListResult.PatientName)
  346. printInfo.PatientId = patient.ID
  347. }
  348. if printInfo.PatientId == 0 {
  349. continue
  350. }
  351. item_id, _ := GetSyItemID(org_id, item.ChargeItemName, item.InspectionName, project_id)
  352. tx := writeMiddleDb.Begin()
  353. var inspection models.MiddleInspection
  354. var inspection_reference models.MiddleInspectionReference
  355. recordDateStr := ""
  356. inspect_date := ""
  357. if len(item.ListResult.InspectDate) > 0 {
  358. inspect_date = item.ListResult.InspectDate
  359. } else {
  360. inspect_date = strings.Split(item.ListResult.ReceiveDateTime, " ")[0]
  361. }
  362. recordDateStr = inspect_date
  363. date, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  364. record_date := date
  365. var total int
  366. var RangeOptions string
  367. var RangeMin string
  368. var RangeMax string
  369. // 判断检查类型
  370. var ItemType int
  371. if strings.Contains(item.Reference, "--") {
  372. ItemType = 1
  373. } else {
  374. ItemType = 2
  375. }
  376. if ItemType == 1 {
  377. Range := strings.Split(item.Reference, "--")
  378. RangeMin = Range[0]
  379. RangeMax = Range[1]
  380. } else {
  381. RangeOptions = item.Reference
  382. }
  383. 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
  384. if inspection_reference.ID > 0 {
  385. ItemType = inspection_reference.RangeType
  386. }
  387. if total <= 0 {
  388. inspection_reference.OrgId = org_id
  389. inspection_reference.ProjectName = item.ChargeItemName
  390. inspection_reference.Project = item.ChargeItemName
  391. inspection_reference.ProjectId = project_id
  392. inspection_reference.ItemName = item.InspectionName
  393. inspection_reference.ItemNameAddition = item.Barcode
  394. inspection_reference.ItemId = item_id
  395. inspection_reference.RangeType = ItemType
  396. inspection_reference.RangeMin = RangeMin
  397. inspection_reference.RangeMax = RangeMax
  398. inspection_reference.RangeOptions = RangeOptions
  399. inspection_reference.Unit = item.Unit
  400. inspection_reference.Status = 1
  401. inspection_reference.CreatedTime = time.Now().Unix()
  402. inspection_reference.UpdatedTime = time.Now().Unix()
  403. inspection_reference.InspectDate = inspect_date
  404. inspection_reference.UTime = inspect_date
  405. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  406. if err != nil {
  407. tx.Rollback()
  408. }
  409. }
  410. var itotal int
  411. 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
  412. if itotal <= 0 {
  413. //item.Result = strings.Replace(item.Result, "&gt;", ">", -1)
  414. //item.Result = strings.Replace(item.Result, "&lt;", "<", -1)
  415. inspection.PatientId = printInfo.PatientId
  416. inspection.OrgId = org_id
  417. inspection.ProjectId = project_id
  418. inspection.ItemName = inspection_reference.ItemName
  419. inspection.ProjectName = inspection_reference.ProjectName
  420. inspection.InspectType = ItemType
  421. inspection.ItemId = item_id
  422. inspection.InspectValue = item.Result
  423. inspection.InspectDate = inspect_date
  424. inspection.RecordDate = record_date.Unix()
  425. inspection.Status = 1
  426. inspection.CreatedTime = time.Now().Unix()
  427. inspection.UpdatedTime = time.Now().Unix()
  428. inspection.UTime = inspect_date
  429. inspection.HisUserId = strconv.FormatInt(printInfo.PatientId, 10)
  430. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  431. if err != nil {
  432. tx.Rollback()
  433. }
  434. }
  435. tx.Commit()
  436. }
  437. //插入一条查询检验检查结果记录状态数据
  438. var info LisSyncResultStatusInfo
  439. info.Ctime = time.Now().Unix()
  440. info.Status = 1
  441. info.Mtime = time.Now().Unix()
  442. info.OrgId = org_id
  443. info.IsResult = 1
  444. code, _ := strconv.ParseInt(list[0].Barcode, 10, 64)
  445. info.LastId = code
  446. info.ResultDate = time.Now().Unix()
  447. info.GpSyncEndDate = end_time
  448. writeMiddleDb.Save(&info)
  449. }
  450. }
  451. } else {
  452. //start_time := record.GpSyncEndDate
  453. //end_time := time.Now().Format("2006-01-02")
  454. //
  455. //result := GetGPReportListData(start_time, end_time, key)
  456. //list := GetGPResultDataTwo(result, key)
  457. //
  458. ////插入一条插入中间库记录数据
  459. //if err == nil {
  460. // if len(list) > 0 {
  461. // //插入到系统检验检查数据
  462. // for _, item := range list {
  463. // project_id := int64(0)
  464. // if len(item.ListResult.CustomerBarcode) > 0 {
  465. // project_id, _, _ = GetSyjhProjectID(org_id, item.ChargeItemName)
  466. // } else {
  467. // continue
  468. // }
  469. // var patient models.Patients
  470. // printInfo, _ := GetHisLabelPrintStatusInfoById(item.ListResult.CustomerBarcode)
  471. // if printInfo.ID == 0 {
  472. // patient, _ = GetPatientID(org_id, item.ListResult.PatientName)
  473. // }
  474. // if patient.ID > 0 {
  475. // printInfo.PatientId = patient.ID
  476. // }
  477. // if printInfo.PatientId == 0 {
  478. // continue
  479. // }
  480. //
  481. // item_id, _ := GetSyItemID(org_id, item.ChargeItemName, item.InspectionName, project_id)
  482. // tx := writeMiddleDb.Begin()
  483. // var inspection models.MiddleInspection
  484. // var inspection_reference models.MiddleInspectionReference
  485. //
  486. // recordDateStr := ""
  487. // inspect_date := ""
  488. // if len(item.ListResult.InspectDate) > 0 {
  489. //
  490. // inspect_date = item.ListResult.InspectDate
  491. // } else {
  492. // inspect_date = strings.Split(item.ListResult.ReceiveDateTime, " ")[0]
  493. //
  494. // }
  495. // recordDateStr = inspect_date
  496. //
  497. // date, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  498. // record_date, _ := utils.ParseTimeStringToTime("2006-01-02", date.Format("2006-01-02"))
  499. // var total int
  500. // var RangeOptions string
  501. // var RangeMin string
  502. // var RangeMax string
  503. // // 判断检查类型
  504. // var ItemType int
  505. // if strings.Contains(item.Reference, "--") {
  506. // ItemType = 1
  507. // } else {
  508. // ItemType = 2
  509. //
  510. // }
  511. //
  512. // if ItemType == 1 {
  513. // Range := strings.Split(item.Reference, "--")
  514. // RangeMin = Range[0]
  515. // RangeMax = Range[1]
  516. // } else {
  517. // RangeOptions = item.Reference
  518. // }
  519. //
  520. // 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
  521. // if inspection_reference.ID > 0 {
  522. // ItemType = inspection_reference.RangeType
  523. // }
  524. // if total <= 0 {
  525. // inspection_reference.OrgId = org_id
  526. // inspection_reference.ProjectName = item.ChargeItemName
  527. // inspection_reference.Project = item.ChargeItemName
  528. // inspection_reference.ProjectId = project_id
  529. // inspection_reference.ItemName = item.InspectionName
  530. // inspection_reference.ItemNameAddition = item.Barcode
  531. // inspection_reference.ItemId = item_id
  532. // inspection_reference.RangeType = ItemType
  533. // inspection_reference.RangeMin = RangeMin
  534. // inspection_reference.RangeMax = RangeMax
  535. // inspection_reference.RangeOptions = RangeOptions
  536. // inspection_reference.Unit = item.Unit
  537. // inspection_reference.Status = 1
  538. // inspection_reference.CreatedTime = time.Now().Unix()
  539. // inspection_reference.UpdatedTime = time.Now().Unix()
  540. // inspection_reference.InspectDate = inspect_date
  541. // inspection_reference.UTime = inspect_date
  542. // err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  543. // if err != nil {
  544. // tx.Rollback()
  545. // }
  546. // }
  547. //
  548. // var itotal int
  549. // 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
  550. // if itotal <= 0 {
  551. // //item.Result = strings.Replace(item.Result, "&gt;", ">", -1)
  552. // //item.Result = strings.Replace(item.Result, "&lt;", "<", -1)
  553. // inspection.PatientId = printInfo.PatientId
  554. // inspection.OrgId = org_id
  555. // inspection.ProjectId = project_id
  556. // inspection.ItemName = inspection_reference.ItemName
  557. // inspection.ProjectName = inspection_reference.ProjectName
  558. // inspection.InspectType = ItemType
  559. // inspection.ItemId = item_id
  560. //
  561. // inspection.InspectValue = item.Result
  562. // inspection.InspectDate = inspect_date
  563. // inspection.RecordDate = record_date.Unix()
  564. // inspection.Status = 1
  565. // inspection.CreatedTime = time.Now().Unix()
  566. // inspection.UpdatedTime = time.Now().Unix()
  567. // inspection.UTime = inspect_date
  568. // inspection.HisUserId = strconv.FormatInt(printInfo.PatientId, 10)
  569. // err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  570. // if err != nil {
  571. // tx.Rollback()
  572. // }
  573. // }
  574. //
  575. // tx.Commit()
  576. //
  577. // }
  578. // //插入一条查询检验检查结果记录状态数据
  579. // var info LisSyncResultStatusInfo
  580. // info.Ctime = time.Now().Unix()
  581. // info.Status = 1
  582. // info.Mtime = time.Now().Unix()
  583. // info.OrgId = org_id
  584. // info.IsResult = 1
  585. // info.ResultDate = time.Now().Unix()
  586. // info.GpSyncEndDate = end_time
  587. // writeMiddleDb.Save(&info)
  588. // }
  589. //}
  590. }
  591. //// 第一步:跟进org_id 去中间库查出需要同步的数据
  592. //inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  593. //inspections, _ := GetSyncInspectionByOrgId(org_id)
  594. //// 第二步:将数据同步到业务库
  595. //if len(inspection_references) > 0 {
  596. // for _, inspection_reference := range inspection_references {
  597. // SyncInspectionReference(&inspection_reference)
  598. // }
  599. //}
  600. //if len(inspections) > 0 {
  601. // for _, inspection := range inspections {
  602. // SyncInspection(&inspection)
  603. // }
  604. //}
  605. //return
  606. }
  607. func SyncSYData2() {
  608. org_id := int64(10191)
  609. start_time := "2023-01-01"
  610. end_time := "2023-11-22"
  611. record, _ := GetLastSyncResultRecord(org_id)
  612. if record.ID > 0 {
  613. start_time = record.GpSyncEndDate
  614. end_time = time.Now().Format("2006-01-02")
  615. }
  616. result := GetGPReportListData(start_time, end_time, key)
  617. list := GetGPResultDataTwo(result, key)
  618. jsonData, err := json.Marshal(result)
  619. if err != nil {
  620. fmt.Println("转换为JSON时发生错误:", err)
  621. return
  622. }
  623. jsonStr := string(jsonData)
  624. jsonData1, err1 := json.Marshal(list)
  625. if err1 != nil {
  626. fmt.Println("转换为JSON时发生错误:", err1)
  627. return
  628. }
  629. jsonStr1 := string(jsonData1)
  630. saveLog1(jsonStr, "", "列表数据", "列表数据")
  631. saveLog1(jsonStr1, "", "结果数据", "结果数据")
  632. if err == nil {
  633. fmt.Println(list)
  634. //插入到系统检验检查数据
  635. if len(list) > 0 {
  636. for _, item := range list {
  637. project_id := int64(0)
  638. patient, _ := GetPatientID(org_id, item.ListResult.PatientName)
  639. if patient.ID == 0 {
  640. continue
  641. }
  642. tx := writeMiddleDb.Begin()
  643. var inspection models.MiddleInspection
  644. var inspection_reference models.MiddleInspectionReference
  645. recordDateStr := item.ListResult.ReceiveDateTime
  646. recordDate, _ := utils.ParseTimeStringToTime("2006-01-02 15:04:05", recordDateStr)
  647. inspect_date := recordDate.Format("2006-01-02 15:04")
  648. date, _ := time.Parse("2006-01-02 15:04:05", recordDateStr)
  649. utils.InfoLog("date2: %v", date.Unix())
  650. record_date_time, _ := utils.ParseTimeStringToTime("2006-01-02 15:04", inspect_date)
  651. record_date := record_date_time.Unix()
  652. var total int
  653. var RangeOptions string
  654. var RangeMin string
  655. var RangeMax string
  656. // 判断检查类型
  657. var ItemType int
  658. if strings.Contains(item.Reference, "--") {
  659. ItemType = 1
  660. } else {
  661. ItemType = 2
  662. }
  663. if ItemType == 1 {
  664. Range := strings.Split(item.Reference, "--")
  665. RangeMin = Range[0]
  666. RangeMax = Range[1]
  667. } else {
  668. RangeOptions = item.Reference
  669. }
  670. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and item_name = ? and status = 1", org_id, item.InspectionName).Find(&inspection_reference).Count(&total).Error
  671. if inspection_reference.ID > 0 {
  672. ItemType = inspection_reference.RangeType
  673. project_id = inspection_reference.ProjectId
  674. }
  675. if total <= 0 {
  676. var inspection_referenceTwo models.MiddleInspectionReference
  677. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, item.ChargeItemName).First(&inspection_referenceTwo).Error
  678. if inspection_referenceTwo.ID > 0 {
  679. project_id = inspection_referenceTwo.ProjectId
  680. } else {
  681. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_referenceTwo).Error
  682. if inspection_referenceTwo.ProjectId > 0 {
  683. project_id = inspection_referenceTwo.ProjectId + 1
  684. } else {
  685. project_id = 1019101
  686. }
  687. }
  688. var item_id int64
  689. var inspection_referenceThree models.MiddleInspectionReference
  690. 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)
  691. utils.InfoLog("inspection_reference: %v", inspection_reference)
  692. if inspection_referenceThree.ItemId > 0 {
  693. item_id = inspection_referenceThree.ItemId + 1
  694. } else {
  695. item_id = project_id*100 + 1
  696. }
  697. inspection_reference.OrgId = org_id
  698. inspection_reference.ProjectName = item.ChargeItemName
  699. inspection_reference.Project = item.ChargeItemName
  700. inspection_reference.ProjectId = project_id
  701. inspection_reference.ItemName = item.InspectionName
  702. inspection_reference.ItemNameAddition = item.Barcode
  703. inspection_reference.ItemId = item_id
  704. inspection_reference.RangeType = ItemType
  705. inspection_reference.RangeMin = RangeMin
  706. inspection_reference.RangeMax = RangeMax
  707. inspection_reference.RangeOptions = RangeOptions
  708. inspection_reference.Unit = item.Unit
  709. inspection_reference.Status = 1
  710. inspection_reference.CreatedTime = time.Now().Unix()
  711. inspection_reference.UpdatedTime = time.Now().Unix()
  712. inspection_reference.InspectDate = inspect_date
  713. inspection_reference.UTime = inspect_date
  714. err = tx.Model(&models.MiddleInspectionReference{}).Create(&inspection_reference).Error
  715. if err != nil {
  716. tx.Rollback()
  717. }
  718. }
  719. var itotal int
  720. 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
  721. if itotal <= 0 {
  722. //item.Result = strings.Replace(item.Result, "&gt;", ">", -1)
  723. //item.Result = strings.Replace(item.Result, "&lt;", "<", -1)
  724. inspection.PatientId = patient.ID
  725. inspection.OrgId = org_id
  726. inspection.ProjectId = project_id
  727. inspection.ItemName = inspection_reference.ItemName
  728. inspection.ProjectName = inspection_reference.ProjectName
  729. inspection.InspectType = ItemType
  730. inspection.ItemId = inspection_reference.ItemId
  731. inspection.InspectValue = item.Result
  732. inspection.InspectDate = inspect_date
  733. inspection.RecordDate = record_date
  734. inspection.Status = 1
  735. inspection.CreatedTime = time.Now().Unix()
  736. inspection.UpdatedTime = time.Now().Unix()
  737. inspection.UTime = inspect_date
  738. inspection.HisUserId = strconv.FormatInt(patient.ID, 10)
  739. inspection.SysProjectId = inspection_reference.XtProjectId
  740. inspection.SysItemId = inspection_reference.XtItemId
  741. err = tx.Model(&models.MiddleInspection{}).Create(&inspection).Error
  742. if err != nil {
  743. tx.Rollback()
  744. }
  745. }
  746. tx.Commit()
  747. }
  748. //插入一条查询检验检查结果记录状态数据
  749. var info LisSyncResultStatusInfo
  750. info.Ctime = time.Now().Unix()
  751. info.Status = 1
  752. info.Mtime = time.Now().Unix()
  753. info.OrgId = org_id
  754. info.IsResult = 1
  755. code, _ := strconv.ParseInt(list[0].Barcode, 10, 64)
  756. info.LastId = code
  757. info.ResultDate = time.Now().Unix()
  758. info.GpSyncEndDate = end_time
  759. writeMiddleDb.Save(&info)
  760. }
  761. }
  762. SyncToSssytx()
  763. }
  764. func SyncToSssytx() {
  765. utils.TraceLog("检验检查同步任务开始执行")
  766. org_id := int64(10191)
  767. // 第一步:跟进org_id 去中间库查出需要同步的数据
  768. //inspection_references, _ := GetSyncInspectionReferenceByOrgId(org_id)
  769. inspections, _ := GetSyncInspectionByOrgId(org_id)
  770. // 第二步:将数据同步到业务库
  771. //if len(inspection_references) > 0 {
  772. // for _, inspection_reference := range inspection_references {
  773. // SyncInspectionReference(&inspection_reference)
  774. // }
  775. //}
  776. if len(inspections) > 0 {
  777. for _, inspection := range inspections {
  778. if inspection.SysProjectId > 0 && inspection.SysItemId > 0 {
  779. SyncSssyInspection(&inspection)
  780. }
  781. }
  782. }
  783. utils.SuccessLog("检验检查同步任务完成")
  784. }
  785. // 从机构将数据同步到中间库
  786. func SyncSssyInspection(data *models.MiddleInspection) error {
  787. tx := writeDb.Begin()
  788. var inspection models.Inspection
  789. var total int
  790. 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
  791. if total <= 0 {
  792. inspection.OrgId = data.OrgId
  793. inspection.PatientId = data.PatientId
  794. inspection.ProjectName = data.ProjectName
  795. // inspection.Project = data.ProjectName
  796. inspection.ProjectId = data.SysProjectId
  797. inspection.ItemName = data.ItemName
  798. inspection.ItemId = data.SysItemId
  799. inspection.InspectType = int64(data.InspectType)
  800. inspection.InspectValue = data.InspectValue
  801. inspection.InspectTips = data.InspectTips
  802. inspection.InspectDate = data.RecordDate
  803. inspection.Status = 1
  804. inspection.CreatedTime = time.Now().Unix()
  805. inspection.UpdatedTime = time.Now().Unix()
  806. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  807. if err != nil {
  808. tx.Rollback()
  809. }
  810. data.IsSync = 1
  811. data.SyncId = inspection.ID
  812. data.UpdatedTime = time.Now().Unix()
  813. ierr := writeMiddleDb.Save(&data).Error
  814. if ierr != nil {
  815. tx.Rollback()
  816. }
  817. }
  818. tx.Commit()
  819. return err
  820. }
  821. func GetAge(idCard string) (int, error) {
  822. birthDate, err := getBirthDateFromIDCard(idCard)
  823. if err != nil {
  824. return 0, err
  825. }
  826. age := calculateAge(birthDate)
  827. return age, nil
  828. }
  829. func getBirthDateFromIDCard(idCard string) (time.Time, error) {
  830. birthDateStr := idCard[6:14] // 身份证号码中生日部分的字符
  831. birthDate, err := time.Parse("20060102", birthDateStr)
  832. if err != nil {
  833. return time.Time{}, err
  834. }
  835. return birthDate, nil
  836. }
  837. func calculateAge(birthDate time.Time) int {
  838. now := time.Now()
  839. age := now.Year() - birthDate.Year()
  840. // 如果当前月份小于出生月份,或者当前月份等于出生月份但当前日期小于出生日期,则年龄减一
  841. if now.Month() < birthDate.Month() || (now.Month() == birthDate.Month() && now.Day() < birthDate.Day()) {
  842. age--
  843. }
  844. return age
  845. }
  846. func GetHisLabelPrintStatusInfoById(id string) (info models.HisLabelPrintStatusInfo, err error) {
  847. err = readDb.Model(&models.HisLabelPrintStatusInfo{}).Where("id = ?", id).First(&info).Error
  848. return
  849. }
  850. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  851. func GetSyjhProjectID(org_id int64, project_name string) (project_id int64, sys_project_id int64, err error) {
  852. var inspection_reference models.MiddleInspectionReference
  853. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  854. if inspection_reference.ID > 0 {
  855. return inspection_reference.ProjectId, inspection_reference.XtProjectId, err
  856. } else {
  857. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  858. if inspection_reference.ProjectId > 0 {
  859. return inspection_reference.ProjectId + 1, 0, err
  860. } else {
  861. return 10191000, 0, err
  862. }
  863. }
  864. }
  865. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  866. func GetSyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  867. var inspection_reference models.MiddleInspectionReference
  868. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  869. if inspection_reference.ID > 0 {
  870. return inspection_reference.ItemId, err
  871. } else {
  872. 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
  873. utils.InfoLog("inspection_reference: %v", inspection_reference)
  874. if inspection_reference.ItemId > 0 {
  875. return inspection_reference.ItemId + 1, err
  876. } else {
  877. return project_id*10 + 1, err
  878. }
  879. }
  880. }
  881. // 从机构将数据同步到中间库
  882. func SyncInspectionReferenceThree(data *models.MiddleInspectionReference) error {
  883. tx := writeDb.Begin()
  884. var inspection_reference models.InspectionReference
  885. var total int
  886. err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? and item_id = ? and status = 1", data.OrgId, data.ItemId).Find(&inspection_reference).Count(&total).Error
  887. if total <= 0 {
  888. inspection_reference.OrgId = data.OrgId
  889. inspection_reference.ProjectName = data.ProjectName
  890. inspection_reference.Project = data.ProjectName
  891. inspection_reference.ProjectId = data.ProjectId
  892. inspection_reference.ItemName = data.ItemName
  893. inspection_reference.ItemId = data.ItemId
  894. inspection_reference.RangeType = data.RangeType
  895. inspection_reference.RangeMin = data.RangeMin
  896. inspection_reference.RangeMax = data.RangeMax
  897. inspection_reference.RangeValue = data.RangeValue
  898. inspection_reference.RangeOptions = data.RangeOptions
  899. inspection_reference.Unit = data.Unit
  900. inspection_reference.Status = 1
  901. inspection_reference.CreatedTime = time.Now().Unix()
  902. inspection_reference.UpdatedTime = time.Now().Unix()
  903. err := tx.Model(&models.InspectionReference{}).Create(&inspection_reference).Error
  904. if err != nil {
  905. tx.Rollback()
  906. }
  907. data.IsSync = 1
  908. data.SyncId = inspection_reference.ID
  909. data.UpdatedTime = time.Now().Unix()
  910. ierr := writeMiddleDb.Save(&data).Error
  911. if ierr != nil {
  912. tx.Rollback()
  913. }
  914. }
  915. tx.Commit()
  916. return err
  917. }