auto_create_week_schedules_service.go 28KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. package city
  2. //
  3. import (
  4. "Data_Upload_Api/models/sz"
  5. "Data_Upload_Api/service"
  6. "XT/utils"
  7. "fmt"
  8. "github.com/astaxie/beego"
  9. "github.com/jinzhu/gorm"
  10. "github.com/robfig/cron"
  11. "math"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var createUploadDataCronJob *cron.Cron
  17. func init() {
  18. utils.InfoLog("开启定时任务")
  19. createUploadDataCronJob = cron.New()
  20. spec := "0 0 0 * * ?" // 每天凌晨0点检测数据上报情况
  21. createUploadDataCronJob.AddFunc(spec, func() {
  22. SyncToBX() //深圳市数据上报
  23. })
  24. }
  25. //
  26. func BeginAutoJob() {
  27. createUploadDataCronJob.Start()
  28. }
  29. func SyncToBX() {
  30. // 第一步:到上报配置表中找到深圳需要上报的机构
  31. //sz_province, _ := beego.AppConfig.Int64("sz_province")
  32. //sz_city, _ := beego.AppConfig.Int64("sz_city")
  33. //configs, _ := service.FindAllDataUploadConfigOrgInfo(sz_province, sz_city, 3)
  34. org := &sz.DataUploadConfig{
  35. OrgId: 3907,
  36. ProvinceId: 19,
  37. CityId: 291,
  38. DepartmentName: "深圳宝兴医院",
  39. HospitalId: "455835506",
  40. InstType: 1,
  41. DbHost: "172.16.173.151",
  42. DbPort: "1433",
  43. DbUser: "sa",
  44. DbName: "123.com",
  45. }
  46. //for _, org := range configs {
  47. // 第二步:跟进配置,创建数据库连接
  48. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  49. orgDb, _ := service.CreateSqlServiceDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  50. // 第三步:开始同步数据
  51. // 同步医院信息 t_hd_hospital
  52. SyncHospital(orgDb, org.OrgId, org.HospitalId, org.InstType, org.DepartmentName)
  53. // 同步水处理器 t_hd_wm
  54. SyncWMS(orgDb, org.OrgId, org.HospitalId)
  55. // 同步员工 t_hd_staff
  56. SyncStaff(orgDb, org.OrgId, org.HospitalId)
  57. // 同步病人 t_hd_patient
  58. SyncPatient(orgDb, org.OrgId, org.HospitalId)
  59. // 同步排班 t_hd_shift
  60. SyncShift(orgDb, org.OrgId, org.HospitalId)
  61. // 同步处方 t_hd_ps
  62. SyncPs(orgDb, org.OrgId, org.HospitalId)
  63. // 同步处方药品 t_hd_ps
  64. SyncPsMedicine(orgDb, org.OrgId, org.HospitalId)
  65. // 同步转院信息 t_hd_patient_out
  66. SyncPatientOut(orgDb, org.OrgId, org.HospitalId)
  67. // 同步处方信息 t_hd_doctors_advice
  68. SyncDoctorAdvice(orgDb, org.OrgId, org.HospitalId)
  69. // 同步透中信息 t_hd_middle
  70. SyncMiddle(orgDb, org.OrgId, org.HospitalId)
  71. // // 同步设备 t_hd_other_machine
  72. SyncOtherMachine(orgDb, org.OrgId, org.HospitalId)
  73. // // 同步维修 t_hd_maintain
  74. SyncMachineRepair(orgDb, org.OrgId, org.HospitalId)
  75. // // 同步设备 t_hd_dm
  76. SyncDM(orgDb, org.OrgId, org.HospitalId)
  77. // // 同步患者透析记录 t_hd_dialysis
  78. SyncDialysis(orgDb, org.OrgId, org.HospitalId)
  79. // 第四步:关闭数据库连接
  80. service.CloseDB(orgDb)
  81. }
  82. }
  83. func SyncToSZIC() {
  84. // 第一步:到上报配置表中找到深圳需要上报的机构
  85. sz_province, _ := beego.AppConfig.Int64("sz_province")
  86. sz_city, _ := beego.AppConfig.Int64("sz_city")
  87. configs, _ := service.FindAllDataUploadConfigOrgInfo(sz_province, sz_city, 3)
  88. for _, org := range configs {
  89. // 第二步:跟进配置,创建数据库连接
  90. if len(org.DbHost) > 0 && len(org.DbUser) > 0 && len(org.DbPort) > 0 && len(org.DbPass) > 0 && len(org.DbName) > 0 {
  91. orgDb, err := service.CreateDB(org.DbHost, org.DbPort, org.DbUser, org.DbPass, org.DbName)
  92. if err != nil {
  93. utils.ErrorLog("创建数据库连接失败:%v", err)
  94. }
  95. // 第三步:开始同步数据
  96. // 同步医院信息 t_hd_hospital
  97. SyncHospital(orgDb, org.OrgId, org.HospitalId, org.InstType, org.DepartmentName)
  98. // 同步水处理器 t_hd_wm
  99. SyncWMS(orgDb, org.OrgId, org.HospitalId)
  100. // 同步员工 t_hd_staff
  101. SyncStaff(orgDb, org.OrgId, org.HospitalId)
  102. // 同步病人 t_hd_patient
  103. SyncPatient(orgDb, org.OrgId, org.HospitalId)
  104. // 同步排班 t_hd_shift
  105. SyncShift(orgDb, org.OrgId, org.HospitalId)
  106. // 同步处方 t_hd_ps
  107. SyncPs(orgDb, org.OrgId, org.HospitalId)
  108. // 同步处方药品 t_hd_ps
  109. SyncPsMedicine(orgDb, org.OrgId, org.HospitalId)
  110. // 同步转院信息 t_hd_patient_out
  111. SyncPatientOut(orgDb, org.OrgId, org.HospitalId)
  112. // 同步处方信息 t_hd_doctors_advice
  113. SyncDoctorAdvice(orgDb, org.OrgId, org.HospitalId)
  114. // 同步透中信息 t_hd_middle
  115. SyncMiddle(orgDb, org.OrgId, org.HospitalId)
  116. // // 同步设备 t_hd_other_machine
  117. SyncOtherMachine(orgDb, org.OrgId, org.HospitalId)
  118. // // 同步维修 t_hd_maintain
  119. SyncMachineRepair(orgDb, org.OrgId, org.HospitalId)
  120. // // 同步设备 t_hd_dm
  121. SyncDM(orgDb, org.OrgId, org.HospitalId)
  122. // // 同步患者透析记录 t_hd_dialysis
  123. SyncDialysis(orgDb, org.OrgId, org.HospitalId)
  124. // 第四步:关闭数据库连接
  125. service.CloseDB(orgDb)
  126. }
  127. }
  128. }
  129. // 同步医院信息
  130. func SyncHospital(rdb *gorm.DB, org_id int64, hospital_id string, inst_type int64, department_name string) {
  131. // 第一步:根据机构id获取上次同步时间
  132. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 1)
  133. var sync_time int64
  134. if syncLastInfo.ID > 0 {
  135. sync_time = syncLastInfo.SyncTime
  136. } else {
  137. sync_time = 0
  138. }
  139. // 第二步:跟进上次同步时间找出这个时间段内增加的数据
  140. org, _ := service.FindOrgData(org_id, sync_time)
  141. if org.ID > 0 {
  142. hospital := &sz.TempHdHospital{
  143. HospitalId: hospital_id,
  144. InstType: strconv.FormatInt(inst_type, 10),
  145. DepartmentName: department_name,
  146. AuthorizedBeds: int64(len(org.DeviceNumber)),
  147. CreateTime: time.Now(),
  148. UpdateTime: time.Now(),
  149. Sjscsj: time.Now(),
  150. Xgbz: 0,
  151. }
  152. err := service.CreateOrgRecord(rdb, hospital)
  153. if err == nil {
  154. // 第三步:同步成功后,添加同步记录
  155. upload := &sz.DataUpload{
  156. SyncType: 1,
  157. OrgId: org_id,
  158. SyncTime: time.Now().Unix(),
  159. SyncResultType: 1,
  160. CreateTime: time.Now().Unix(),
  161. UpdateTime: time.Now().Unix(),
  162. }
  163. err := service.CreateUploadRecord(upload)
  164. if err != nil {
  165. utils.ErrorLog("%v", err)
  166. }
  167. }
  168. }
  169. }
  170. // 同步水处理器
  171. func SyncWMS(rdb *gorm.DB, org_id int64, hospital_id string) {
  172. // 第一步:根据机构id获取上次同步时间
  173. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 22)
  174. var sync_time int64
  175. if syncLastInfo.ID > 0 {
  176. sync_time = syncLastInfo.SyncTime
  177. } else {
  178. sync_time = 0
  179. }
  180. //同步水处理机
  181. waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  182. var wms []*sz.TempHdWm
  183. for _, item := range waterMachine {
  184. wm := &sz.TempHdWm{
  185. HospitalId: hospital_id,
  186. EquipmentId: strconv.FormatInt(item.ID, 10),
  187. EquipmentBrand: item.ManufactureFactory,
  188. EquipmentModel: strconv.FormatInt(item.UnitType, 10),
  189. EnableTime: time.Unix(item.StartDate, 0),
  190. DiscardedTime: time.Unix(item.RubbishDate, 0),
  191. EquipmentType: item.DeviceType,
  192. CreateTime: time.Now(),
  193. UpdateTime: time.Now(),
  194. Sjscsj: time.Now(),
  195. Xgbz: 0,
  196. }
  197. wms = append(wms, wm)
  198. }
  199. err := service.BatchCreateWMsRecord(wms, rdb)
  200. if err == nil {
  201. // 第三步:同步成功后,添加同步记录
  202. upload := &sz.DataUpload{
  203. SyncType: 22,
  204. OrgId: org_id,
  205. SyncTime: time.Now().Unix(),
  206. SyncResultType: 1,
  207. CreateTime: time.Now().Unix(),
  208. UpdateTime: time.Now().Unix(),
  209. }
  210. err := service.CreateUploadRecord(upload)
  211. if err != nil {
  212. utils.ErrorLog("%v", err)
  213. }
  214. } else {
  215. //错误处理
  216. }
  217. }
  218. // 同步员工
  219. func SyncStaff(rdb *gorm.DB, org_id int64, hospital_id string) {
  220. // 第一步:根据机构id获取上次同步时间
  221. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 21)
  222. var sync_time int64
  223. if syncLastInfo.ID > 0 {
  224. sync_time = syncLastInfo.SyncTime
  225. } else {
  226. sync_time = 0
  227. }
  228. //同步员工信息
  229. roles, _ := service.FindOrgRolesData(org_id, sync_time, time.Now().Unix())
  230. //waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  231. var staffs []*sz.TempHdStaff
  232. for _, role := range roles {
  233. var title int
  234. switch role.UserType {
  235. case 2:
  236. title = 1
  237. break
  238. case 3:
  239. title = 2
  240. break
  241. }
  242. staff := &sz.TempHdStaff{
  243. HospitalId: hospital_id,
  244. StaffId: strconv.FormatInt(role.ID, 10),
  245. StaffName: role.UserName,
  246. Position: int64(title),
  247. PermanentType: 1,
  248. CreateTime: time.Now(),
  249. UpdateTime: time.Now(),
  250. Sjscsj: time.Now(),
  251. Xgbz: 0,
  252. }
  253. staffs = append(staffs, staff)
  254. }
  255. err := service.BatchCreateStaffRecord(staffs, rdb)
  256. if err == nil {
  257. // 第三步:同步成功后,添加同步记录
  258. upload := &sz.DataUpload{
  259. SyncType: 21,
  260. OrgId: org_id,
  261. SyncTime: time.Now().Unix(),
  262. SyncResultType: 1,
  263. CreateTime: time.Now().Unix(),
  264. UpdateTime: time.Now().Unix(),
  265. }
  266. err := service.CreateUploadRecord(upload)
  267. if err != nil {
  268. utils.ErrorLog("%v", err)
  269. }
  270. } else {
  271. //错误处理
  272. }
  273. }
  274. // 同步病人
  275. func SyncPatient(rdb *gorm.DB, org_id int64, hospital_id string) {
  276. // 第一步:根据机构id获取上次同步时间
  277. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 15)
  278. var sync_time int64
  279. if syncLastInfo.ID > 0 {
  280. sync_time = syncLastInfo.SyncTime
  281. } else {
  282. sync_time = 0
  283. }
  284. //同步员工信息
  285. patients, _ := service.FindOrgPatientData(org_id, sync_time, time.Now().Unix())
  286. //waterMachine, _ := service.FindOrgWaterMachineData(org_id, sync_time, time.Now().Unix())
  287. var hdPatients []*sz.TempHdPatient
  288. for _, patient := range patients {
  289. idcard_year, _ := strconv.Atoi(Substr(patient.IdCardNo, 6, 4)) // 年
  290. idcard_mo, _ := strconv.Atoi(Substr(patient.IdCardNo, 10, 2)) // 月
  291. idcard_day, _ := strconv.Atoi(Substr(patient.IdCardNo, 12, 2)) // 日
  292. date := strconv.Itoa(idcard_year)
  293. if idcard_mo < 10 {
  294. date = date + "-0" + strconv.Itoa(idcard_mo)
  295. } else {
  296. date = date + "-" + strconv.Itoa(idcard_mo)
  297. }
  298. if idcard_day < 10 {
  299. date = date + "-0" + strconv.Itoa(idcard_day)
  300. } else {
  301. date = date + "-" + strconv.Itoa(idcard_day)
  302. }
  303. local, _ := time.LoadLocation("Local")
  304. birthday, _ := time.ParseInLocation("2006-01-02", date, local)
  305. fmt.Println(birthday)
  306. var isCKD int
  307. value := strings.Index(patient.Diagnose, "慢性肾脏病")
  308. value2 := strings.Index(patient.Diagnose, "CKD")
  309. value3 := strings.Index(patient.Diagnose, "慢性肾衰竭")
  310. if value != -1 || value2 != -1 || value3 != -1 {
  311. isCKD = 1
  312. } else {
  313. isCKD = 0
  314. }
  315. p := &sz.TempHdPatient{
  316. HospitalId: hospital_id,
  317. PatientNk: patient.ID,
  318. CardNo: "000000000000000000",
  319. CardType: 1,
  320. IdNo: patient.IdCardNo,
  321. IdType: "01",
  322. PatientName: patient.Name,
  323. Gender: strconv.FormatInt(patient.Gender, 10),
  324. BornDate: birthday,
  325. DiagnosisSummary: patient.Diagnose,
  326. IsCrf: strconv.Itoa(isCKD),
  327. CreateTime: time.Now(),
  328. UpdateTime: time.Now(),
  329. Sjscsj: time.Now(),
  330. Xgbz: 0,
  331. }
  332. if patient.FirstDialysisDate != 0 {
  333. p.DialysisStartTime = time.Unix(patient.FirstDialysisDate, 0)
  334. }
  335. if patient.HospitalFirstDialysisDate != 0 {
  336. p.LocalStartTime = time.Unix(patient.HospitalFirstDialysisDate, 0)
  337. }
  338. hdPatients = append(hdPatients, p)
  339. }
  340. err := service.BatchCreatePatinet(hdPatients, rdb)
  341. if err == nil {
  342. // 第三步:同步成功后,添加同步记录
  343. upload := &sz.DataUpload{
  344. SyncType: 15,
  345. OrgId: org_id,
  346. SyncTime: time.Now().Unix(),
  347. SyncResultType: 1,
  348. CreateTime: time.Now().Unix(),
  349. UpdateTime: time.Now().Unix(),
  350. }
  351. err := service.CreateUploadRecord(upload)
  352. if err != nil {
  353. utils.ErrorLog("%v", err)
  354. }
  355. } else {
  356. //错误处理
  357. }
  358. }
  359. func Substr(str string, start, length int) string {
  360. rs := []rune(str)
  361. rl := len(rs)
  362. end := 0
  363. if start < 0 {
  364. start = rl - 1 + start
  365. }
  366. end = start + length
  367. if start > end {
  368. start, end = end, start
  369. }
  370. if start < 0 {
  371. start = 0
  372. }
  373. if start > rl {
  374. start = rl
  375. }
  376. if end < 0 {
  377. end = 0
  378. }
  379. if end > rl {
  380. end = rl
  381. }
  382. return string(rs[start:end])
  383. }
  384. // 同步排班
  385. func SyncShift(rdb *gorm.DB, org_id int64, hospital_id string) {
  386. // 第一步:根据机构id获取上次同步时间
  387. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 19)
  388. var sync_time int64
  389. if syncLastInfo.ID > 0 {
  390. sync_time = syncLastInfo.SyncTime
  391. } else {
  392. sync_time = 0
  393. }
  394. var hdShift []*sz.TempHdShift
  395. schs, _ := service.FindOrgScheduleData(org_id, sync_time, time.Now().Unix())
  396. for _, sch := range schs {
  397. shift := &sz.TempHdShift{
  398. PsId: strconv.FormatInt(sch.ID, 10),
  399. HospitalId: strconv.FormatInt(sch.UserOrgId, 10),
  400. PatientNk: strconv.FormatInt(sch.PatientId, 10),
  401. ScheduleDate: time.Unix(sch.ScheduleDate, 0),
  402. ShiftType: strconv.FormatInt(sch.ScheduleType, 10),
  403. SickbedNo: sch.DeviceNumber.Number,
  404. ScheduleStatus: "1",
  405. CreateTime: time.Now(),
  406. Sjscsj: time.Now(),
  407. Xgbz: 0,
  408. }
  409. hdShift = append(hdShift, shift)
  410. }
  411. err := service.BatchCreateSchedual(hdShift, rdb)
  412. if err == nil {
  413. // 第三步:同步成功后,添加同步记录
  414. upload := &sz.DataUpload{
  415. SyncType: 19,
  416. OrgId: org_id,
  417. SyncTime: time.Now().Unix(),
  418. SyncResultType: 1,
  419. CreateTime: time.Now().Unix(),
  420. UpdateTime: time.Now().Unix(),
  421. }
  422. err := service.CreateUploadRecord(upload)
  423. if err != nil {
  424. utils.ErrorLog("%v", err)
  425. }
  426. } else {
  427. //错误处理
  428. }
  429. }
  430. // 同步处方
  431. func SyncPs(rdb *gorm.DB, org_id int64, hospital_id string) {
  432. // 第一步:根据机构id获取上次同步时间
  433. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 17)
  434. var sync_time int64
  435. if syncLastInfo.ID > 0 {
  436. sync_time = syncLastInfo.SyncTime
  437. } else {
  438. sync_time = 0
  439. }
  440. var hdPrescription []*sz.TempHdPs
  441. prescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  442. for _, item := range prescriptions {
  443. ps := &sz.TempHdPs{
  444. PrescribeId: strconv.FormatInt(item.ID, 10),
  445. HospitalId: hospital_id,
  446. PatientNk: strconv.FormatInt(item.PatientId, 10),
  447. K: item.Kalium,
  448. Ca: item.Calcium,
  449. Na: item.Sodium,
  450. CreateTime: time.Now(),
  451. Sjscsj: time.Now(),
  452. Xgbz: 0,
  453. PrescribeTime: time.Unix(item.RecordDate, 0),
  454. }
  455. switch item.ModeId {
  456. case 1:
  457. ps.DialysisFrequency = item.AssessmentBeforeDislysis.DialysisCount
  458. ps.FrequencyUnit = 1
  459. ps.DialysisDuration = item.DialysisDuration
  460. ps.Hdf = "0"
  461. ps.Hp = "0"
  462. break
  463. case 2:
  464. ps.Hdf = "1"
  465. ps.Hp = "0"
  466. ps.HdfFrequency = item.AssessmentBeforeDislysis.DialysisCount
  467. ps.HdfFrequencyUnit = 1
  468. ps.HpDuration = item.DialysisDuration
  469. break
  470. case 4:
  471. ps.Hdf = "0"
  472. ps.Hp = "1"
  473. ps.HpFrequency = item.AssessmentBeforeDislysis.DialysisCount
  474. ps.HpFrequencyUnit = 1
  475. ps.HpDuration = item.DialysisDuration
  476. break
  477. }
  478. hdPrescription = append(hdPrescription, ps)
  479. }
  480. err := service.BatchCreatePs(hdPrescription, rdb)
  481. if err == nil {
  482. // 第三步:同步成功后,添加同步记录
  483. upload := &sz.DataUpload{
  484. SyncType: 17,
  485. OrgId: org_id,
  486. SyncTime: time.Now().Unix(),
  487. SyncResultType: 1,
  488. CreateTime: time.Now().Unix(),
  489. UpdateTime: time.Now().Unix(),
  490. }
  491. err := service.CreateUploadRecord(upload)
  492. if err != nil {
  493. utils.ErrorLog("%v", err)
  494. }
  495. } else {
  496. //错误处理
  497. }
  498. }
  499. // 同步处方药物
  500. func SyncPsMedicine(rdb *gorm.DB, org_id int64, hospital_id string) {
  501. // 第一步:根据机构id获取上次同步时间
  502. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 18)
  503. var sync_time int64
  504. if syncLastInfo.ID > 0 {
  505. sync_time = syncLastInfo.SyncTime
  506. } else {
  507. sync_time = 0
  508. }
  509. var tempPsmedicine []*sz.TempHdPsMedicine
  510. prescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  511. for _, item := range prescriptions {
  512. psm := &sz.TempHdPsMedicine{
  513. PrescribeId: strconv.FormatInt(item.ID, 10),
  514. HospitalId: strconv.FormatInt(item.UserOrgId, 10),
  515. MedicineTypeId: "2",
  516. CreateTime: time.Now(),
  517. Sjscsj: time.Now(),
  518. Xgbz: 0,
  519. }
  520. switch item.Anticoagulant {
  521. case 1:
  522. psm.MedicineId = "3"
  523. break
  524. case 2:
  525. psm.MedicineId = "1"
  526. break
  527. case 3:
  528. psm.MedicineId = "2"
  529. break
  530. case 4:
  531. psm.MedicineId = "5"
  532. break
  533. case 5:
  534. psm.MedicineId = "4"
  535. break
  536. }
  537. tempPsmedicine = append(tempPsmedicine, psm)
  538. }
  539. err := service.BatchCreatePsm(tempPsmedicine, rdb)
  540. if err == nil {
  541. // 第三步:同步成功后,添加同步记录
  542. upload := &sz.DataUpload{
  543. SyncType: 18,
  544. OrgId: org_id,
  545. SyncTime: time.Now().Unix(),
  546. SyncResultType: 1,
  547. CreateTime: time.Now().Unix(),
  548. UpdateTime: time.Now().Unix(),
  549. }
  550. err := service.CreateUploadRecord(upload)
  551. if err != nil {
  552. utils.ErrorLog("%v", err)
  553. }
  554. } else {
  555. //错误处理
  556. }
  557. }
  558. // 同步转院信息
  559. func SyncPatientOut(rdb *gorm.DB, org_id int64, hospital_id string) {
  560. // 第一步:根据机构id获取上次同步时间
  561. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 16)
  562. var sync_time int64
  563. if syncLastInfo.ID > 0 {
  564. sync_time = syncLastInfo.SyncTime
  565. } else {
  566. sync_time = 0
  567. }
  568. var hdPatientOuts []*sz.TempHdPatientOut
  569. patients, _ := service.FindOrgPatientOutData(org_id, sync_time, time.Now().Unix())
  570. //
  571. for _, patient := range patients {
  572. p := &sz.TempHdPatientOut{
  573. HospitalId: strconv.FormatInt(patient.UserOrgId, 10),
  574. PatientNk: strconv.FormatInt(patient.ID, 10),
  575. SequelaeType: "99",
  576. CreateTime: time.Now(),
  577. Sjscsj: time.Now(),
  578. Xgbz: 0,
  579. }
  580. if len(patient.PatientLapseto) > 0 {
  581. p.SequelaeDate = time.Unix(patient.PatientLapseto[len(patient.PatientLapseto)-1].LapsetoTime, 0)
  582. }
  583. hdPatientOuts = append(hdPatientOuts, p)
  584. }
  585. err := service.BatchCreatePatientOut(hdPatientOuts, rdb)
  586. if err == nil {
  587. // 第三步:同步成功后,添加同步记录
  588. upload := &sz.DataUpload{
  589. SyncType: 16,
  590. OrgId: org_id,
  591. SyncTime: time.Now().Unix(),
  592. SyncResultType: 1,
  593. CreateTime: time.Now().Unix(),
  594. UpdateTime: time.Now().Unix(),
  595. }
  596. err := service.CreateUploadRecord(upload)
  597. if err != nil {
  598. utils.ErrorLog("%v", err)
  599. }
  600. } else {
  601. //错误处理
  602. }
  603. }
  604. // 同步处方信息
  605. func SyncDoctorAdvice(rdb *gorm.DB, org_id int64, hospital_id string) {
  606. // 第一步:根据机构id获取上次同步时间
  607. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 6)
  608. var sync_time int64
  609. if syncLastInfo.ID > 0 {
  610. sync_time = syncLastInfo.SyncTime
  611. } else {
  612. sync_time = 0
  613. }
  614. var hdDoctorAdvices []*sz.TempHdDoctorsAdvice
  615. dialysisPrescriptions, _ := service.FindOrgDialysisPrescriptionData(org_id, sync_time, time.Now().Unix())
  616. for _, dp := range dialysisPrescriptions {
  617. advice := &sz.TempHdDoctorsAdvice{
  618. MedicalOrderId: strconv.FormatInt(dp.ID, 10),
  619. HospitalId: strconv.FormatInt(dp.UserOrgId, 10),
  620. DialysisId: strconv.FormatInt(dp.DialysisOrder.ID, 10),
  621. OrderType: "1",
  622. PatientNk: strconv.FormatInt(dp.PatientId, 10),
  623. DialysisDuration: dp.DialysisDurationHour*60 + dp.DialysisDurationMinute,
  624. BloodVol: int64(math.Floor(dp.BloodFlowVolume + 0/5)),
  625. CreateTime: time.Now(),
  626. Sjscsj: time.Now(),
  627. Xgbz: 0,
  628. }
  629. hdDoctorAdvices = append(hdDoctorAdvices, advice)
  630. }
  631. err := service.BatchCreateDoctorsAdvice(hdDoctorAdvices, rdb)
  632. if err == nil {
  633. // 第三步:同步成功后,添加同步记录
  634. upload := &sz.DataUpload{
  635. SyncType: 6,
  636. OrgId: org_id,
  637. SyncTime: time.Now().Unix(),
  638. SyncResultType: 1,
  639. CreateTime: time.Now().Unix(),
  640. UpdateTime: time.Now().Unix(),
  641. }
  642. err := service.CreateUploadRecord(upload)
  643. if err != nil {
  644. utils.ErrorLog("%v", err)
  645. }
  646. } else {
  647. //错误处理
  648. }
  649. }
  650. //
  651. func SyncMiddle(rdb *gorm.DB, org_id int64, hospital_id string) {
  652. // 第一步:根据机构id获取上次同步时间
  653. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 36)
  654. var sync_time int64
  655. if syncLastInfo.ID > 0 {
  656. sync_time = syncLastInfo.SyncTime
  657. } else {
  658. sync_time = 0
  659. }
  660. var hdMiddle []*sz.TempHdMiddle
  661. monitors, _ := service.FindOrgMonitorRecordData(org_id, sync_time, time.Now().Unix())
  662. for _, item := range monitors {
  663. mid := &sz.TempHdMiddle{
  664. HospitalId: hospital_id,
  665. DialysisId: strconv.FormatInt(item.DialysisOrder.ID, 10),
  666. MonitorTime: time.Unix(item.OperateTime, 0),
  667. Sbp: int64(math.Floor(item.SystolicBloodPressure + 0/5)),
  668. Dbp: int64(math.Floor(item.DiastolicBloodPressure + 0/5)),
  669. PatientNk: strconv.FormatInt(item.PatientId, 10),
  670. CreateTime: time.Now(),
  671. Sjscsj: time.Now(),
  672. Xgbz: 0,
  673. }
  674. hdMiddle = append(hdMiddle, mid)
  675. }
  676. err := service.BatchCreateMonitor(hdMiddle, rdb)
  677. if err == nil {
  678. // 第三步:同步成功后,添加同步记录
  679. upload := &sz.DataUpload{
  680. SyncType: 36,
  681. OrgId: org_id,
  682. SyncTime: time.Now().Unix(),
  683. SyncResultType: 1,
  684. CreateTime: time.Now().Unix(),
  685. UpdateTime: time.Now().Unix(),
  686. }
  687. err := service.CreateUploadRecord(upload)
  688. if err != nil {
  689. utils.ErrorLog("%v", err)
  690. }
  691. } else {
  692. //错误处理
  693. }
  694. }
  695. // 同步设备
  696. func SyncOtherMachine(rdb *gorm.DB, org_id int64, hospital_id string) {
  697. // 第一步:根据机构id获取上次同步时间
  698. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 13)
  699. var sync_time int64
  700. if syncLastInfo.ID > 0 {
  701. sync_time = syncLastInfo.SyncTime
  702. } else {
  703. sync_time = 0
  704. }
  705. //同步设备
  706. waterMachine, _ := service.FindOrgOtherMachineData(org_id, sync_time, time.Now().Unix())
  707. var wms []*sz.TempHdOtherMachine
  708. for _, item := range waterMachine {
  709. wm := &sz.TempHdOtherMachine{
  710. HospitalId: hospital_id,
  711. EquipmentId: strconv.FormatInt(item.ID, 10),
  712. EquipmentBrand: item.ManufactureFactory,
  713. EquipmentModel: strconv.FormatInt(item.UnitType, 10),
  714. EnableTime: time.Unix(item.StartDate, 0),
  715. DiscardedTime: time.Unix(item.RubbishDate, 0),
  716. CreateTime: time.Now(),
  717. UpdateTime: time.Now(),
  718. Sjscsj: time.Now(),
  719. Xgbz: strconv.FormatInt(0, 10),
  720. }
  721. switch item.MachineStatus {
  722. case 1:
  723. wm.Status = 1
  724. break
  725. case 4:
  726. wm.Status = 3
  727. break
  728. }
  729. wms = append(wms, wm)
  730. }
  731. err := service.BatchCreateOtherMachineRecord(wms, rdb)
  732. if err == nil {
  733. // 第三步:同步成功后,添加同步记录
  734. upload := &sz.DataUpload{
  735. SyncType: 13,
  736. OrgId: org_id,
  737. SyncTime: time.Now().Unix(),
  738. SyncResultType: 1,
  739. CreateTime: time.Now().Unix(),
  740. UpdateTime: time.Now().Unix(),
  741. }
  742. err := service.CreateUploadRecord(upload)
  743. if err != nil {
  744. utils.ErrorLog("%v", err)
  745. }
  746. } else {
  747. //错误处理
  748. }
  749. }
  750. // 同步维修
  751. func SyncMachineRepair(rdb *gorm.DB, org_id int64, hospital_id string) {
  752. // 第一步:根据机构id获取上次同步时间
  753. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 11)
  754. var sync_time int64
  755. if syncLastInfo.ID > 0 {
  756. sync_time = syncLastInfo.SyncTime
  757. } else {
  758. sync_time = 0
  759. }
  760. //同步设备
  761. hdMaintain, _ := service.FindOrgMachineRepairData(org_id, sync_time, time.Now().Unix())
  762. var maintain []*sz.TempHdMaintain
  763. for _, item := range hdMaintain {
  764. mt := &sz.TempHdMaintain{
  765. HospitalId: hospital_id,
  766. EquipmentId: strconv.FormatInt(item.VMDeviceAddmacher.ID, 10),
  767. RepairTime: time.Unix(item.GuaranteeDate, 0),
  768. CreateTime: time.Now(),
  769. Sjscsj: time.Now(),
  770. Xgbz: 0,
  771. }
  772. switch item.VMDeviceAddmacher.DeviceType {
  773. case 1:
  774. mt.EquipmentType = "11"
  775. break
  776. case 2:
  777. mt.EquipmentType = "21"
  778. break
  779. case 3:
  780. mt.EquipmentType = "99"
  781. break
  782. }
  783. maintain = append(maintain, mt)
  784. }
  785. err := service.BatchCreateMainTain(maintain, rdb)
  786. if err == nil {
  787. // 第三步:同步成功后,添加同步记录
  788. upload := &sz.DataUpload{
  789. SyncType: 11,
  790. OrgId: org_id,
  791. SyncTime: time.Now().Unix(),
  792. SyncResultType: 1,
  793. CreateTime: time.Now().Unix(),
  794. UpdateTime: time.Now().Unix(),
  795. }
  796. err := service.CreateUploadRecord(upload)
  797. if err != nil {
  798. utils.ErrorLog("%v", err)
  799. }
  800. } else {
  801. //错误处理
  802. }
  803. }
  804. // 同步设备
  805. func SyncDM(rdb *gorm.DB, org_id int64, hospital_id string) {
  806. // 第一步:根据机构id获取上次同步时间
  807. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 5)
  808. var sync_time int64
  809. if syncLastInfo.ID > 0 {
  810. sync_time = syncLastInfo.SyncTime
  811. } else {
  812. sync_time = 0
  813. }
  814. //同步设备
  815. dm, _ := service.FindOrgMachineData(org_id, sync_time, time.Now().Unix())
  816. var hdms []*sz.TempHdDm
  817. for _, item := range dm {
  818. hdm := &sz.TempHdDm{
  819. HospitalId: hospital_id,
  820. EquipmentId: strconv.FormatInt(item.ID, 10),
  821. SickbedNo: item.DeviceNumber.Number,
  822. EquipmentBrand: item.ManufactureFactory,
  823. EquipmentModel: strconv.FormatInt(item.UnitType, 10),
  824. EnableTime: time.Unix(item.StartDate, 0),
  825. DiscardedTime: time.Unix(item.RubbishDate, 0),
  826. CreateTime: time.Now(),
  827. UpdateTime: time.Now(),
  828. Sjscsj: time.Now(),
  829. Xgbz: strconv.FormatInt(0, 10),
  830. }
  831. switch item.MachineStatus {
  832. case 1:
  833. hdm.Status = "1"
  834. break
  835. case 4:
  836. hdm.Status = "3"
  837. break
  838. }
  839. switch item.DeviceType {
  840. case 1:
  841. hdm.EquipmentType = "11"
  842. break
  843. case 3:
  844. hdm.EquipmentType = "99"
  845. break
  846. }
  847. hdms = append(hdms, hdm)
  848. }
  849. err := service.BatchCreateHDMRecord(hdms, rdb)
  850. if err == nil {
  851. // 第三步:同步成功后,添加同步记录
  852. upload := &sz.DataUpload{
  853. SyncType: 5,
  854. OrgId: org_id,
  855. SyncTime: time.Now().Unix(),
  856. SyncResultType: 1,
  857. CreateTime: time.Now().Unix(),
  858. UpdateTime: time.Now().Unix(),
  859. }
  860. err := service.CreateUploadRecord(upload)
  861. if err != nil {
  862. utils.ErrorLog("%v", err)
  863. }
  864. } else {
  865. //错误处理
  866. }
  867. }
  868. // 同步患者透析记录
  869. func SyncDialysis(rdb *gorm.DB, org_id int64, hospital_id string) {
  870. // 第一步:根据机构id获取上次同步时间
  871. syncLastInfo, _ := service.GetSyncTimeByOrgId(org_id, 2)
  872. var sync_time int64
  873. if syncLastInfo.ID > 0 {
  874. sync_time = syncLastInfo.SyncTime
  875. } else {
  876. sync_time = 0
  877. }
  878. var tempDialysis []*sz.TempHdDialysis
  879. dialysisData, _ := service.FindOrgDialysisData(org_id, sync_time, time.Now().Unix())
  880. for _, item := range dialysisData {
  881. psm := &sz.TempHdDialysis{
  882. DialysisId: strconv.FormatInt(item.ID, 10),
  883. HospitalId: strconv.FormatInt(item.UserOrgId, 10),
  884. PatientNk: strconv.FormatInt(item.PatientId, 10),
  885. PsId: strconv.FormatInt(item.Schedule.ID, 10),
  886. DialysisDate: time.Unix(item.DialysisDate, 0),
  887. SickbedNo: item.DeviceNumber.Number,
  888. DivisionId: strconv.FormatInt(item.Schedule.PartitionId, 10),
  889. EquipmentId: strconv.FormatInt(item.DeviceNumber.ID, 10),
  890. TotalTreatDuration: item.AssessmentAfterDislysis.ActualTreatmentHour*60 + item.AssessmentAfterDislysis.ActualTreatmentMinute,
  891. BeforeSbp: int64(item.AssessmentBeforeDislysis.SystolicBloodPressure),
  892. BeforeDbp: int64(item.AssessmentBeforeDislysis.DiastolicBloodPressure),
  893. AfterSbp: int64(item.AssessmentAfterDislysis.SystolicBloodPressure),
  894. AfterDbp: int64(item.AssessmentAfterDislysis.DiastolicBloodPressure),
  895. StartTime: time.Unix(item.StartTime, 0),
  896. EndTime: time.Unix(item.EndTime, 0),
  897. CreateTime: time.Unix(item.CreatedTime, 0),
  898. Sjscsj: time.Now(),
  899. Xgbz: 0,
  900. }
  901. if item.AssessmentAfterDislysis.ActualUltrafiltration < 10 {
  902. psm.ActualufMl = int64(item.AssessmentAfterDislysis.ActualUltrafiltration * 1000)
  903. } else {
  904. psm.ActualufMl = int64(item.AssessmentAfterDislysis.ActualUltrafiltration)
  905. }
  906. if item.VMDialysisPrescription.TargetUltrafiltration < 10 {
  907. psm.Ufv = int64(item.VMDialysisPrescription.TargetUltrafiltration * 1000)
  908. } else {
  909. psm.Ufv = int64(item.VMDialysisPrescription.TargetUltrafiltration)
  910. }
  911. if item.AssessmentAfterDislysis.ActualDisplacement < 10 {
  912. psm.TotalReplace = int64(item.AssessmentAfterDislysis.ActualDisplacement * 1000)
  913. } else {
  914. psm.TotalReplace = int64(item.AssessmentAfterDislysis.ActualDisplacement)
  915. }
  916. tempDialysis = append(tempDialysis, psm)
  917. }
  918. err := service.BatchCreateDialysis(tempDialysis, rdb)
  919. if err == nil {
  920. // 第三步:同步成功后,添加同步记录
  921. upload := &sz.DataUpload{
  922. SyncType: 2,
  923. OrgId: org_id,
  924. SyncTime: time.Now().Unix(),
  925. SyncResultType: 1,
  926. CreateTime: time.Now().Unix(),
  927. UpdateTime: time.Now().Unix(),
  928. }
  929. err := service.CreateUploadRecord(upload)
  930. if err != nil {
  931. utils.ErrorLog("%v", err)
  932. }
  933. } else {
  934. //错误处理
  935. }
  936. }