sssy_service.go 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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 := "2024-04-20"
  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*3
  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. if inspection.SysProjectId > 0 && inspection.SysItemId > 0 {
  784. SyncSssyInspection(&inspection)
  785. }
  786. }
  787. }
  788. utils.SuccessLog("检验检查同步任务完成")
  789. }
  790. func UpdateJg() error {
  791. var inspections []models.Inspection
  792. 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
  793. for _, info := range inspections {
  794. var inspectionByOne []models.Inspection
  795. 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
  796. if len(inspectionByOne) == 2 {
  797. for _, tinfo := range inspectionByOne {
  798. utils.InfoLog("date1: %v", tinfo.ID)
  799. if tinfo.ItemId != 115 && tinfo.ItemId != 37 {
  800. // 报告单里只有肌酐和尿素这两个项目要上传到“透后肾功能”里
  801. continue
  802. }
  803. if tinfo.ItemId == 115 {
  804. tinfo.ProjectId = 8060
  805. tinfo.ItemId = 8060
  806. }
  807. if tinfo.ItemId == 37 {
  808. tinfo.ProjectId = 8060
  809. tinfo.ItemId = 8061
  810. }
  811. writeDb.Save(&tinfo)
  812. }
  813. }
  814. }
  815. return err
  816. }
  817. // 从机构将数据同步到中间库
  818. func SyncSssyInspection(data *models.MiddleInspection) error {
  819. tx := writeDb.Begin()
  820. var inspection models.Inspection
  821. var total int
  822. 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
  823. if total <= 0 {
  824. inspection.OrgId = data.OrgId
  825. inspection.PatientId = data.PatientId
  826. inspection.ProjectName = data.ProjectName
  827. // inspection.Project = data.ProjectName
  828. inspection.ProjectId = data.SysProjectId
  829. inspection.ItemName = data.ItemName
  830. inspection.ItemId = data.SysItemId
  831. inspection.InspectType = int64(data.InspectType)
  832. inspection.InspectValue = data.InspectValue
  833. inspection.InspectTips = data.InspectTips
  834. inspection.InspectDate = data.RecordDate
  835. inspection.Status = 1
  836. inspection.CreatedTime = time.Now().Unix()
  837. inspection.UpdatedTime = time.Now().Unix()
  838. err := tx.Model(&models.Inspection{}).Create(&inspection).Error
  839. if err != nil {
  840. tx.Rollback()
  841. }
  842. data.IsSync = 1
  843. data.SyncId = inspection.ID
  844. data.UpdatedTime = time.Now().Unix()
  845. ierr := writeMiddleDb.Save(&data).Error
  846. if ierr != nil {
  847. tx.Rollback()
  848. }
  849. }
  850. tx.Commit()
  851. return err
  852. }
  853. func GetAge(idCard string) (int, error) {
  854. birthDate, err := getBirthDateFromIDCard(idCard)
  855. if err != nil {
  856. return 0, err
  857. }
  858. age := calculateAge(birthDate)
  859. return age, nil
  860. }
  861. func getBirthDateFromIDCard(idCard string) (time.Time, error) {
  862. birthDateStr := idCard[6:14] // 身份证号码中生日部分的字符
  863. birthDate, err := time.Parse("20060102", birthDateStr)
  864. if err != nil {
  865. return time.Time{}, err
  866. }
  867. return birthDate, nil
  868. }
  869. func calculateAge(birthDate time.Time) int {
  870. now := time.Now()
  871. age := now.Year() - birthDate.Year()
  872. // 如果当前月份小于出生月份,或者当前月份等于出生月份但当前日期小于出生日期,则年龄减一
  873. if now.Month() < birthDate.Month() || (now.Month() == birthDate.Month() && now.Day() < birthDate.Day()) {
  874. age--
  875. }
  876. return age
  877. }
  878. func GetHisLabelPrintStatusInfoById(id string) (info models.HisLabelPrintStatusInfo, err error) {
  879. err = readDb.Model(&models.HisLabelPrintStatusInfo{}).Where("id = ?", id).First(&info).Error
  880. return
  881. }
  882. // 根据机构ID和检验检查名称获取该检查的project_id,如没有,则创建一个
  883. func GetSyjhProjectID(org_id int64, project_name string) (project_id int64, sys_project_id int64, err error) {
  884. var inspection_reference models.MiddleInspectionReference
  885. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and project_name = ?", org_id, project_name).First(&inspection_reference).Error
  886. if inspection_reference.ID > 0 {
  887. return inspection_reference.ProjectId, inspection_reference.XtProjectId, err
  888. } else {
  889. err = readMiddleDb.Table("xt_middle_inspection_reference").Where("org_id=? ", org_id).Select("max(project_id) as project_id").Scan(&inspection_reference).Error
  890. if inspection_reference.ProjectId > 0 {
  891. return inspection_reference.ProjectId + 1, 0, err
  892. } else {
  893. return 10191000, 0, err
  894. }
  895. }
  896. }
  897. // 根据机构ID和检验检查小项名称获取该检查的item_id,如没有,则创建一个
  898. func GetSyItemID(org_id int64, project_name string, item_name string, project_id int64) (item_id int64, err error) {
  899. var inspection_reference models.MiddleInspectionReference
  900. err = readMiddleDb.Model(&models.MiddleInspectionReference{}).Where("org_id = ? and status = 1 and item_name = ?", org_id, project_name, item_name).First(&inspection_reference).Error
  901. if inspection_reference.ID > 0 {
  902. return inspection_reference.ItemId, err
  903. } else {
  904. 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
  905. utils.InfoLog("inspection_reference: %v", inspection_reference)
  906. if inspection_reference.ItemId > 0 {
  907. return inspection_reference.ItemId + 1, err
  908. } else {
  909. return project_id*10 + 1, err
  910. }
  911. }
  912. }
  913. // 从机构将数据同步到中间库
  914. func SyncInspectionReferenceThree(data *models.MiddleInspectionReference) error {
  915. tx := writeDb.Begin()
  916. var inspection_reference models.InspectionReference
  917. var total int
  918. err = readDb.Model(&models.InspectionReference{}).Where("org_id = ? and item_id = ? and status = 1", data.OrgId, data.ItemId).Find(&inspection_reference).Count(&total).Error
  919. if total <= 0 {
  920. inspection_reference.OrgId = data.OrgId
  921. inspection_reference.ProjectName = data.ProjectName
  922. inspection_reference.Project = data.ProjectName
  923. inspection_reference.ProjectId = data.ProjectId
  924. inspection_reference.ItemName = data.ItemName
  925. inspection_reference.ItemId = data.ItemId
  926. inspection_reference.RangeType = data.RangeType
  927. inspection_reference.RangeMin = data.RangeMin
  928. inspection_reference.RangeMax = data.RangeMax
  929. inspection_reference.RangeValue = data.RangeValue
  930. inspection_reference.RangeOptions = data.RangeOptions
  931. inspection_reference.Unit = data.Unit
  932. inspection_reference.Status = 1
  933. inspection_reference.CreatedTime = time.Now().Unix()
  934. inspection_reference.UpdatedTime = time.Now().Unix()
  935. err := tx.Model(&models.InspectionReference{}).Create(&inspection_reference).Error
  936. if err != nil {
  937. tx.Rollback()
  938. }
  939. data.IsSync = 1
  940. data.SyncId = inspection_reference.ID
  941. data.UpdatedTime = time.Now().Unix()
  942. ierr := writeMiddleDb.Save(&data).Error
  943. if ierr != nil {
  944. tx.Rollback()
  945. }
  946. }
  947. tx.Commit()
  948. return err
  949. }