sync.go 29KB

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