inspection_api_controller.go 31KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_New/utils"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/astaxie/beego"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. type InspectionApiController struct {
  15. BaseAuthAPIController
  16. }
  17. func InspectionApiRegistRouters() {
  18. beego.Router("/api/patient/inspection/list", &InspectionApiController{}, "Get:GetPatientInspections")
  19. beego.Router("/api/patient/inspection/reference", &InspectionApiController{}, "Get:PatientInspectionReference")
  20. beego.Router("/api/patient/inspection/create", &InspectionApiController{}, "Post:CreatePatientInspection")
  21. beego.Router("/api/patient/inspection/edit", &InspectionApiController{}, "Put:EditPatientInspection")
  22. beego.Router("/api/patient/inspection/delete", &InspectionApiController{}, "Delete:DeletePatientInspection")
  23. beego.Router("/api/patient/inspection/get", &InspectionApiController{}, "Get:GetAllPatientInspection")
  24. beego.Router("/api/patient/inspectioninit/get", &InspectionApiController{}, "Get:GetInitInsepction")
  25. beego.Router("/api/patient/pic_inspection/create", &InspectionApiController{}, "Post:CreatePatientPicInspection")
  26. beego.Router("/api/patient/pic_inspection/edit", &InspectionApiController{}, "Post:EditPatientPicInspection")
  27. beego.Router("/api/patient/inspection_remind/get", &InspectionApiController{}, "Get:GetInspectionRemind")
  28. beego.Router("/api/patient/inspection_remind/set", &InspectionApiController{}, "Get:SetInspectionRemind")
  29. }
  30. func (c *InspectionApiController) SetInspectionRemind() {
  31. remind_cycle, _ := c.GetInt64("remind_cycle", 0)
  32. patient, _ := c.GetInt64("patient", 0)
  33. project_id, _ := c.GetInt64("project_id", 0)
  34. is_open, _ := c.GetInt64("is_open", 0)
  35. if remind_cycle > 0 {
  36. infectiousRecord, _ := service.FindLastRecordDate(c.GetAdminUserInfo().CurrentOrgId, patient, project_id)
  37. fmt.Println(infectiousRecord.InspectDate)
  38. var record_time int64
  39. switch remind_cycle {
  40. case 1: //1个月
  41. ts := time.Unix(infectiousRecord.InspectDate, 0)
  42. record_time = ts.AddDate(0, 1, 0).Unix()
  43. fmt.Println(record_time)
  44. break
  45. case 2: //2个月
  46. ts := time.Unix(infectiousRecord.InspectDate, 0)
  47. record_time = ts.AddDate(0, 2, 0).Unix()
  48. fmt.Println(record_time)
  49. break
  50. case 3: //3个月
  51. ts := time.Unix(infectiousRecord.InspectDate, 0)
  52. record_time = ts.AddDate(0, 3, 0).Unix()
  53. fmt.Println(record_time)
  54. break
  55. case 4: //6个月
  56. ts := time.Unix(infectiousRecord.InspectDate, 0)
  57. record_time = ts.AddDate(0, 6, 0).Unix()
  58. fmt.Println(record_time)
  59. break
  60. case 5: //12个月
  61. ts := time.Unix(infectiousRecord.InspectDate, 0)
  62. record_time = ts.AddDate(0, 12, 0).Unix()
  63. fmt.Println(record_time)
  64. break
  65. }
  66. var errs error
  67. if infectiousRecord.ID == 0 {
  68. record_time = 0
  69. }
  70. //根据project_id查找是否存在提醒记录,没有的话新建,有的话更新提醒时间
  71. reminds, _ := service.GetCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, project_id)
  72. if reminds.ID == 0 {
  73. remind := models.XtCheckRemind{
  74. ProjectId: project_id,
  75. UserOrgId: c.GetAdminUserInfo().CurrentOrgId,
  76. Status: 1,
  77. LastRemindDate: record_time,
  78. PatientId: patient,
  79. RemindCycle: remind_cycle,
  80. IsOpen: is_open,
  81. }
  82. errs = service.CreateRemind(remind)
  83. } else {
  84. errs = service.UpDateCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, record_time, project_id, remind_cycle)
  85. }
  86. fmt.Println(errs)
  87. c.ServeSuccessJSON(map[string]interface{}{
  88. "remind_cycle": remind_cycle,
  89. })
  90. } else {
  91. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  92. return
  93. }
  94. }
  95. func (c *InspectionApiController) GetInspectionRemind() {
  96. patient, _ := c.GetInt64("patient")
  97. project_id, _ := c.GetInt64("project_id")
  98. remind, _ := service.GetCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, project_id)
  99. c.ServeSuccessJSON(map[string]interface{}{
  100. "remind": remind,
  101. })
  102. }
  103. // PatientInspectionReference 请求检验检查大小项目
  104. // [get]: /api/patient/inspection/reference
  105. func (c *InspectionApiController) PatientInspectionReference() {
  106. patient, _ := c.GetInt64("patient")
  107. inspect_type, _ := c.GetInt64("type")
  108. if patient <= 0 {
  109. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  110. return
  111. }
  112. adminUserInfo := c.GetAdminUserInfo()
  113. references, err := service.GetInspectionReference(adminUserInfo.CurrentOrgId, inspect_type)
  114. if err != nil {
  115. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  116. return
  117. }
  118. patient_info, _ := service.GetPatientByID(adminUserInfo.CurrentOrgId, patient)
  119. counts, err := service.GetPatientInspectionProjectCount(adminUserInfo.CurrentOrgId, patient)
  120. if err != nil {
  121. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  122. return
  123. }
  124. countsMap := make(map[int64]int64, 0)
  125. for _, count := range counts {
  126. countsMap[count.ProjectId] = count.Count
  127. }
  128. referenceMap := make(map[string]*models.InspectionReferenceMap, 0)
  129. for _, reference := range references {
  130. if _, exist := referenceMap[reference.Project]; !exist {
  131. referenceMap[reference.Project] = new(models.InspectionReferenceMap)
  132. referenceMap[reference.Project].Project = reference.Project
  133. referenceMap[reference.Project].ProjectId = reference.ProjectId
  134. referenceMap[reference.Project].ProjectName = reference.ProjectName
  135. if _, cexit := countsMap[reference.ProjectId]; cexit {
  136. referenceMap[reference.Project].Count = countsMap[reference.ProjectId]
  137. }
  138. referenceMap[reference.Project].InspectionReference = make([]models.InspectionReference, 0)
  139. }
  140. referenceMap[reference.Project].InspectionReference = append(referenceMap[reference.Project].InspectionReference, *reference)
  141. }
  142. reference := make([]*models.InspectionReferenceMap, 0)
  143. for _, item := range referenceMap {
  144. reference = append(reference, item)
  145. }
  146. rl := len(reference)
  147. for index := 0; index < rl-1; index++ {
  148. for jndex := 0; jndex < rl-1-index; jndex++ {
  149. if reference[jndex].ProjectId > reference[jndex+1].ProjectId {
  150. var item models.InspectionReferenceMap
  151. item = *reference[jndex]
  152. reference[jndex] = reference[jndex+1]
  153. reference[jndex+1] = &item
  154. }
  155. }
  156. }
  157. list, err := service.GetInspectionReferenceByOrgId(0)
  158. c.ServeSuccessJSON(map[string]interface{}{
  159. "reference": reference,
  160. "patient_info": patient_info,
  161. "list": list,
  162. })
  163. return
  164. }
  165. func (c *InspectionApiController) CreatePatientInspection() {
  166. patient, _ := c.GetInt64("patient", 0)
  167. remind_cycle, _ := c.GetInt64("remind_cycle", 0)
  168. if patient <= 0 {
  169. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  170. return
  171. }
  172. adminUserInfo := c.GetAdminUserInfo()
  173. patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patient)
  174. if patientInfo.ID == 0 {
  175. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  176. return
  177. }
  178. var from models.InepectionForm
  179. err := json.Unmarshal(c.Ctx.Input.RequestBody, &from)
  180. if err != nil {
  181. utils.ErrorLog("%v", err)
  182. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  183. return
  184. }
  185. timeLayout := "2006-01-02 15:04"
  186. loc, _ := time.LoadLocation("Local")
  187. theTime, err := time.ParseInLocation(timeLayout, from.InspectDate, loc)
  188. if err != nil {
  189. utils.ErrorLog(err.Error())
  190. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
  191. return
  192. }
  193. if len(from.FormItem) == 0 {
  194. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "未填写项目")
  195. return
  196. }
  197. date := theTime.Unix()
  198. insp, err := service.GetPatientInspectionByDate(adminUserInfo.CurrentOrgId, patient, date, from.ProjectId)
  199. if err != nil {
  200. utils.ErrorLog("%v", err)
  201. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  202. return
  203. }
  204. if len(insp) > 0 {
  205. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDateExit)
  206. return
  207. }
  208. inspections := make([]models.Inspection, 0)
  209. for _, item := range from.FormItem {
  210. var inspection models.Inspection
  211. inspection.OrgId = adminUserInfo.CurrentOrgId
  212. inspection.PatientId = patient
  213. inspection.ProjectId = from.ProjectId
  214. inspection.ItemId = item.ItemId
  215. inspection.ItemName = item.ItemName
  216. inspection.ProjectName = item.ProjectName
  217. inspection.InspectType = item.RangeType
  218. inspection.InspectValue = item.Value
  219. inspection.InspectDate = date
  220. inspection.Status = 1
  221. inspection.CreatedTime = time.Now().Unix()
  222. inspection.UpdatedTime = time.Now().Unix()
  223. inspections = append(inspections, inspection)
  224. }
  225. err = service.CreatePatientInspection(inspections)
  226. if err != nil {
  227. utils.ErrorLog("%v", err)
  228. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionAddFail)
  229. return
  230. }
  231. if inspections[0].ProjectId == 14 {
  232. if remind_cycle > 0 {
  233. infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, inspections[0].ProjectId)
  234. fmt.Println(infectiousRecord.InspectDate)
  235. var record_time int64
  236. switch remind_cycle {
  237. case 1: //1个月
  238. ts := time.Unix(infectiousRecord.InspectDate, 0)
  239. record_time = ts.AddDate(0, 1, 0).Unix()
  240. fmt.Println(record_time)
  241. break
  242. case 2: //2个月
  243. ts := time.Unix(infectiousRecord.InspectDate, 0)
  244. record_time = ts.AddDate(0, 2, 0).Unix()
  245. fmt.Println(record_time)
  246. break
  247. case 3: //3个月
  248. ts := time.Unix(infectiousRecord.InspectDate, 0)
  249. record_time = ts.AddDate(0, 3, 0).Unix()
  250. fmt.Println(record_time)
  251. break
  252. case 4: //6个月
  253. ts := time.Unix(infectiousRecord.InspectDate, 0)
  254. record_time = ts.AddDate(0, 6, 0).Unix()
  255. fmt.Println(record_time)
  256. break
  257. case 5: //12个月
  258. ts := time.Unix(infectiousRecord.InspectDate, 0)
  259. record_time = ts.AddDate(0, 12, 0).Unix()
  260. fmt.Println(record_time)
  261. break
  262. }
  263. var errs error
  264. if inspections[0].ProjectId == 14 {
  265. errs = service.UpDateInfectiousRecordTime(adminUserInfo.CurrentOrgId, patient, record_time, remind_cycle)
  266. }
  267. if errs != nil {
  268. utils.ErrorLog("更新日期出错:%v", errs)
  269. }
  270. }
  271. c.ServeSuccessJSON(map[string]interface{}{
  272. "inspections": inspections,
  273. "remind_cycle": remind_cycle,
  274. })
  275. } else {
  276. //更新提醒时间
  277. reminds, _ := service.GetCheckRemindRecordTime(adminUserInfo.CurrentOrgId, patient, inspections[0].ProjectId)
  278. if reminds.ID > 0 {
  279. infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, inspections[0].ProjectId)
  280. var record_time int64
  281. switch reminds.RemindCycle {
  282. case 1: //1个月
  283. ts := time.Unix(infectiousRecord.InspectDate, 0)
  284. record_time = ts.AddDate(0, 1, 0).Unix()
  285. fmt.Println(record_time)
  286. break
  287. case 2: //2个月
  288. ts := time.Unix(infectiousRecord.InspectDate, 0)
  289. record_time = ts.AddDate(0, 2, 0).Unix()
  290. fmt.Println(record_time)
  291. break
  292. case 3: //3个月
  293. ts := time.Unix(infectiousRecord.InspectDate, 0)
  294. record_time = ts.AddDate(0, 3, 0).Unix()
  295. fmt.Println(record_time)
  296. break
  297. case 4: //6个月
  298. ts := time.Unix(infectiousRecord.InspectDate, 0)
  299. record_time = ts.AddDate(0, 6, 0).Unix()
  300. fmt.Println(record_time)
  301. break
  302. case 5: //12个月
  303. ts := time.Unix(infectiousRecord.InspectDate, 0)
  304. record_time = ts.AddDate(0, 12, 0).Unix()
  305. fmt.Println(record_time)
  306. break
  307. }
  308. errs := service.UpDateCheckRemindRecordTimeTwo(adminUserInfo.CurrentOrgId, patient, record_time, inspections[0].ProjectId)
  309. if errs != nil {
  310. utils.ErrorLog("更新日期出错:%v", errs)
  311. }
  312. }
  313. c.ServeSuccessJSON(map[string]interface{}{
  314. "inspections": inspections,
  315. })
  316. }
  317. return
  318. }
  319. func (c *InspectionApiController) CreatePatientPicInspection() {
  320. patient, _ := c.GetInt64("patient", 0)
  321. if patient <= 0 {
  322. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  323. return
  324. }
  325. adminUserInfo := c.GetAdminUserInfo()
  326. patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patient)
  327. if patientInfo.ID == 0 {
  328. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  329. return
  330. }
  331. var from models.InepectionPICForm
  332. err := json.Unmarshal(c.Ctx.Input.RequestBody, &from)
  333. if err != nil {
  334. utils.ErrorLog("%v", err)
  335. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  336. return
  337. }
  338. timeLayout := "2006-01-02 15:04"
  339. loc, _ := time.LoadLocation("Local")
  340. theTime, err := time.ParseInLocation(timeLayout, from.InspectDate, loc)
  341. if err != nil {
  342. utils.ErrorLog(err.Error())
  343. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
  344. return
  345. }
  346. if len(from.Imags) == 0 {
  347. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "未上传图片")
  348. return
  349. }
  350. date := theTime.Unix()
  351. insp, err := service.GetPatientInspectionByDate(adminUserInfo.CurrentOrgId, patient, date, from.ProjectId)
  352. if err != nil {
  353. utils.ErrorLog("%v", err)
  354. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  355. return
  356. }
  357. if len(insp) > 0 {
  358. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDateExit)
  359. return
  360. }
  361. inspection_re, _ := service.GetInspectionReferenceTwo(from.ProjectId)
  362. inspections := make([]models.Inspection, 0)
  363. for index, item := range from.Imags {
  364. var inspection models.Inspection
  365. inspection.OrgId = adminUserInfo.CurrentOrgId
  366. inspection.PatientId = patient
  367. inspection.ProjectId = from.ProjectId
  368. inspection.ItemId = inspection_re[index].ID
  369. inspection.ItemName = item.Desc
  370. inspection.ProjectName = ""
  371. inspection.InspectType = 3
  372. inspection.InspectValue = item.ImgUrl
  373. inspection.InspectDesc = item.Desc
  374. inspection.InspectDate = date
  375. inspection.Status = 1
  376. inspection.CreatedTime = time.Now().Unix()
  377. inspection.UpdatedTime = time.Now().Unix()
  378. inspections = append(inspections, inspection)
  379. }
  380. err = service.CreatePatientInspection(inspections)
  381. if err != nil {
  382. utils.ErrorLog("%v", err)
  383. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionAddFail)
  384. return
  385. }
  386. if inspections[0].ProjectId != 14 {
  387. //更新提醒时间
  388. reminds, _ := service.GetCheckRemindRecordTime(adminUserInfo.CurrentOrgId, patient, inspections[0].ProjectId)
  389. if reminds.ID > 0 {
  390. infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, inspections[0].ProjectId)
  391. var record_time int64
  392. switch reminds.RemindCycle {
  393. case 1: //1个月
  394. ts := time.Unix(infectiousRecord.InspectDate, 0)
  395. record_time = ts.AddDate(0, 1, 0).Unix()
  396. fmt.Println(record_time)
  397. break
  398. case 2: //2个月
  399. ts := time.Unix(infectiousRecord.InspectDate, 0)
  400. record_time = ts.AddDate(0, 2, 0).Unix()
  401. fmt.Println(record_time)
  402. break
  403. case 3: //3个月
  404. ts := time.Unix(infectiousRecord.InspectDate, 0)
  405. record_time = ts.AddDate(0, 3, 0).Unix()
  406. fmt.Println(record_time)
  407. break
  408. case 4: //6个月
  409. ts := time.Unix(infectiousRecord.InspectDate, 0)
  410. record_time = ts.AddDate(0, 6, 0).Unix()
  411. fmt.Println(record_time)
  412. break
  413. case 5: //12个月
  414. ts := time.Unix(infectiousRecord.InspectDate, 0)
  415. record_time = ts.AddDate(0, 12, 0).Unix()
  416. fmt.Println(record_time)
  417. break
  418. }
  419. errs := service.UpDateCheckRemindRecordTimeTwo(adminUserInfo.CurrentOrgId, patient, record_time, inspections[0].ProjectId)
  420. if errs != nil {
  421. utils.ErrorLog("更新日期出错:%v", errs)
  422. }
  423. }
  424. }
  425. c.ServeSuccessJSON(map[string]interface{}{
  426. "inspections": inspections,
  427. })
  428. return
  429. }
  430. func (c *InspectionApiController) EditPatientPicInspection() {
  431. patient, _ := c.GetInt64("patient", 0)
  432. if patient <= 0 {
  433. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  434. return
  435. }
  436. adminUserInfo := c.GetAdminUserInfo()
  437. patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patient)
  438. if patientInfo.ID == 0 {
  439. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  440. return
  441. }
  442. var from models.InepectionPICForm
  443. err := json.Unmarshal(c.Ctx.Input.RequestBody, &from)
  444. if err != nil {
  445. utils.ErrorLog("%v", err)
  446. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  447. return
  448. }
  449. timeLayout := "2006-01-02 15:04"
  450. loc, _ := time.LoadLocation("Local")
  451. theTime, err := time.ParseInLocation(timeLayout, from.InspectDate, loc)
  452. if err != nil {
  453. utils.ErrorLog(err.Error())
  454. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
  455. return
  456. }
  457. if len(from.Imags) == 0 {
  458. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "未上传图片")
  459. return
  460. }
  461. date := theTime.Unix()
  462. inspection_re, _ := service.GetInspectionReferenceTwo(from.ProjectId)
  463. inspections := make([]models.Inspection, 0)
  464. for index, item := range from.Imags {
  465. if item.ID == 0 {
  466. var inspection models.Inspection
  467. inspection.OrgId = adminUserInfo.CurrentOrgId
  468. inspection.PatientId = patient
  469. inspection.ProjectId = from.ProjectId
  470. inspection.ItemId = inspection_re[index].ID
  471. inspection.ItemName = item.Desc
  472. inspection.ProjectName = ""
  473. inspection.InspectType = 3
  474. inspection.InspectValue = item.ImgUrl
  475. inspection.InspectDesc = item.Desc
  476. inspection.InspectDate = date
  477. inspection.Status = 1
  478. inspection.CreatedTime = time.Now().Unix()
  479. inspection.UpdatedTime = time.Now().Unix()
  480. inspections = append(inspections, inspection)
  481. } else {
  482. var inspection models.Inspection
  483. inspection.ID = item.ID
  484. inspection.OrgId = adminUserInfo.CurrentOrgId
  485. inspection.PatientId = patient
  486. inspection.ProjectId = from.ProjectId
  487. inspection.ItemId = inspection_re[index].ID
  488. inspection.ItemName = item.Desc
  489. inspection.ProjectName = ""
  490. inspection.InspectType = 3
  491. inspection.InspectValue = item.ImgUrl
  492. inspection.InspectDesc = item.Desc
  493. inspection.InspectDate = date
  494. inspection.Status = 1
  495. inspection.CreatedTime = time.Now().Unix()
  496. inspection.UpdatedTime = time.Now().Unix()
  497. err = service.SavePatientInspection(inspection)
  498. }
  499. }
  500. for _, item := range from.DeleteImg {
  501. service.DeleteInspectionTwo(item.ID)
  502. }
  503. if len(inspections) > 0 {
  504. err = service.CreatePatientInspection(inspections)
  505. }
  506. if err != nil {
  507. utils.ErrorLog("%v", err)
  508. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionAddFail)
  509. return
  510. }
  511. c.ServeSuccessJSON(map[string]interface{}{
  512. "inspections": inspections,
  513. })
  514. return
  515. }
  516. func (c *InspectionApiController) EditPatientInspection() {
  517. patient, _ := c.GetInt64("patient", 0)
  518. remind_cycle, _ := c.GetInt64("remind_cycle", 0)
  519. // dates := c.GetString("dates")
  520. // projectid := c.GetString("projectid")
  521. if patient <= 0 {
  522. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  523. return
  524. }
  525. adminUserInfo := c.GetAdminUserInfo()
  526. patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patient)
  527. if patientInfo.ID == 0 {
  528. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
  529. return
  530. }
  531. var from models.InepectionForm
  532. err := json.Unmarshal(c.Ctx.Input.RequestBody, &from)
  533. if err != nil {
  534. utils.ErrorLog("%v", err)
  535. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  536. return
  537. }
  538. timeLayout := "2006-01-02 15:04"
  539. loc, _ := time.LoadLocation("Local")
  540. theTime, err := time.ParseInLocation(timeLayout, from.InspectDate, loc)
  541. if err != nil {
  542. utils.ErrorLog(err.Error())
  543. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
  544. return
  545. }
  546. oldTime, olderr := time.ParseInLocation(timeLayout, from.OldInspectDate, loc)
  547. if olderr != nil {
  548. utils.ErrorLog(olderr.Error())
  549. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
  550. return
  551. }
  552. if len(from.FormItem) == 0 {
  553. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "未填写项目")
  554. return
  555. }
  556. date := theTime.Unix()
  557. oldDate := oldTime.Unix()
  558. insp, err := service.GetPatientInspectionByDate(adminUserInfo.CurrentOrgId, patient, oldDate, from.ProjectId)
  559. if err != nil {
  560. utils.ErrorLog("%v", err)
  561. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  562. return
  563. }
  564. if len(insp) == 0 {
  565. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDateNotExit)
  566. return
  567. }
  568. inspMap := make(map[int64]*models.Inspection)
  569. for _, item := range insp {
  570. inspMap[item.ID] = item
  571. }
  572. addinsp := make([]models.Inspection, 0)
  573. editinsp := make([]models.Inspection, 0)
  574. noMap := make([]int64, 0)
  575. fmt.Println(from.FormItem)
  576. for _, item := range from.FormItem {
  577. fmt.Println(item.ID)
  578. if item.ID == 0 {
  579. var inspection models.Inspection
  580. inspection.OrgId = adminUserInfo.CurrentOrgId
  581. inspection.PatientId = patient
  582. inspection.ProjectId = from.ProjectId
  583. inspection.ItemId = item.ItemId
  584. inspection.ItemName = item.ItemName
  585. inspection.ProjectName = item.ProjectName
  586. inspection.InspectType = item.RangeType
  587. inspection.InspectValue = item.Value
  588. inspection.InspectDate = date
  589. inspection.Status = 1
  590. inspection.CreatedTime = time.Now().Unix()
  591. inspection.UpdatedTime = time.Now().Unix()
  592. addinsp = append(addinsp, inspection)
  593. } else {
  594. inspection := *inspMap[item.ID]
  595. inspection.InspectValue = item.Value
  596. inspection.InspectDate = date
  597. inspection.UpdatedTime = time.Now().Unix()
  598. editinsp = append(editinsp, inspection)
  599. noMap = append(noMap, item.ID)
  600. }
  601. }
  602. err = service.EditPatientInspection(addinsp, editinsp, noMap, patient, adminUserInfo.CurrentOrgId, from.ProjectId, date)
  603. if err != nil {
  604. utils.ErrorLog("%v", err)
  605. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionEditFail)
  606. return
  607. }
  608. inspections := make([]models.Inspection, 0)
  609. inspections = append(inspections, editinsp...)
  610. inspections = append(inspections, addinsp...)
  611. if remind_cycle > 0 {
  612. infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, 14)
  613. fmt.Println(infectiousRecord.InspectDate)
  614. var record_time int64
  615. switch remind_cycle {
  616. case 1: //1个月
  617. ts := time.Unix(infectiousRecord.InspectDate, 0)
  618. record_time = ts.AddDate(0, 1, 0).Unix()
  619. break
  620. case 2: //2个月
  621. ts := time.Unix(infectiousRecord.InspectDate, 0)
  622. record_time = ts.AddDate(0, 2, 0).Unix()
  623. break
  624. case 3: //3个月
  625. ts := time.Unix(infectiousRecord.InspectDate, 0)
  626. record_time = ts.AddDate(0, 3, 0).Unix()
  627. break
  628. case 4: //6个月
  629. ts := time.Unix(infectiousRecord.InspectDate, 0)
  630. record_time = ts.AddDate(0, 6, 0).Unix()
  631. break
  632. case 5: //12个月
  633. ts := time.Unix(infectiousRecord.InspectDate, 0)
  634. record_time = ts.AddDate(0, 12, 0).Unix()
  635. break
  636. }
  637. fmt.Println(record_time)
  638. errs := service.UpDateInfectiousRecordTime(adminUserInfo.CurrentOrgId, patient, record_time, remind_cycle)
  639. if errs != nil {
  640. utils.ErrorLog("更新日期出错:%v", errs)
  641. }
  642. }
  643. c.ServeSuccessJSON(map[string]interface{}{
  644. "inspections": inspections,
  645. "remind_cycle": remind_cycle,
  646. })
  647. return
  648. }
  649. func (c *InspectionApiController) DeletePatientInspection() {
  650. patient, _ := c.GetInt64("patient", 0)
  651. ProjectId, _ := c.GetInt64("project_id", 0)
  652. InspectDate := c.GetString("date")
  653. if patient <= 0 || ProjectId <= 0 || len(InspectDate) < 10 {
  654. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  655. return
  656. }
  657. timeLayout := "2006-01-02 15:04"
  658. loc, _ := time.LoadLocation("Local")
  659. theTime, err := time.ParseInLocation(timeLayout, InspectDate, loc)
  660. if err != nil {
  661. utils.ErrorLog(err.Error())
  662. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
  663. return
  664. }
  665. adminUserInfo := c.GetAdminUserInfo()
  666. date := theTime.Unix()
  667. insp, err := service.GetPatientInspectionByDate(adminUserInfo.CurrentOrgId, patient, date, ProjectId)
  668. if err != nil {
  669. utils.ErrorLog("%v", err)
  670. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  671. return
  672. }
  673. if len(insp) == 0 {
  674. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDateNotExit)
  675. return
  676. }
  677. err = service.DeletePatientInspection(adminUserInfo.CurrentOrgId, patient, ProjectId, date)
  678. if err != nil {
  679. utils.ErrorLog("%v", err)
  680. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDeleteFail)
  681. return
  682. }
  683. //更新提醒时间
  684. reminds, _ := service.GetCheckRemindRecordTime(adminUserInfo.CurrentOrgId, patient, ProjectId)
  685. if reminds.ID > 0 {
  686. infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, ProjectId)
  687. fmt.Println(infectiousRecord)
  688. if infectiousRecord.ID > 0 {
  689. var record_time int64
  690. switch reminds.RemindCycle {
  691. case 1: //1个月
  692. ts := time.Unix(infectiousRecord.InspectDate, 0)
  693. record_time = ts.AddDate(0, 1, 0).Unix()
  694. fmt.Println(record_time)
  695. break
  696. case 2: //2个月
  697. ts := time.Unix(infectiousRecord.InspectDate, 0)
  698. record_time = ts.AddDate(0, 2, 0).Unix()
  699. fmt.Println(record_time)
  700. break
  701. case 3: //3个月
  702. ts := time.Unix(infectiousRecord.InspectDate, 0)
  703. record_time = ts.AddDate(0, 3, 0).Unix()
  704. fmt.Println(record_time)
  705. break
  706. case 4: //6个月
  707. ts := time.Unix(infectiousRecord.InspectDate, 0)
  708. record_time = ts.AddDate(0, 6, 0).Unix()
  709. fmt.Println(record_time)
  710. break
  711. case 5: //12个月
  712. ts := time.Unix(infectiousRecord.InspectDate, 0)
  713. record_time = ts.AddDate(0, 12, 0).Unix()
  714. fmt.Println(record_time)
  715. break
  716. }
  717. errs := service.UpDateCheckRemindRecordTimeTwo(adminUserInfo.CurrentOrgId, patient, record_time, ProjectId)
  718. if errs != nil {
  719. utils.ErrorLog("更新日期出错:%v", errs)
  720. }
  721. } else {
  722. errs := service.UpDateCheckRemindRecordTimeTwo(adminUserInfo.CurrentOrgId, patient, 0, ProjectId)
  723. if errs != nil {
  724. utils.ErrorLog("更新日期出错:%v", errs)
  725. }
  726. }
  727. }
  728. c.ServeSuccessJSON(map[string]interface{}{
  729. "msg": "ok",
  730. })
  731. return
  732. }
  733. func (c *InspectionApiController) GetPatientInspections() {
  734. patient, _ := c.GetInt64("patient", 0)
  735. projectId, _ := c.GetInt64("project_id", 0)
  736. if patient <= 0 || projectId <= 0 {
  737. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  738. return
  739. }
  740. page, _ := c.GetInt64("page", 0)
  741. if page <= 0 {
  742. page = 1
  743. }
  744. adminUserInfo := c.GetAdminUserInfo()
  745. inspections, total, dateTime, err := service.GetPatientInspections(adminUserInfo.CurrentOrgId, patient, projectId, page)
  746. if err != nil {
  747. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  748. return
  749. }
  750. date := ""
  751. if len(inspections) > 0 {
  752. date = time.Unix(dateTime, 0).Format("2006-01-02 15:04")
  753. }
  754. remind, _ := service.GetCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, projectId)
  755. //remind, _ := service.GetCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, projectId)
  756. config, _ := service.GetCheckRemindConfigById(c.GetAdminUserInfo().CurrentOrgId)
  757. c.ServeSuccessJSON(map[string]interface{}{
  758. "inspections": inspections,
  759. "total": total,
  760. "date": date,
  761. "remind": remind,
  762. "config": config,
  763. })
  764. return
  765. }
  766. func (c *InspectionApiController) GetAllPatientInspection() {
  767. patient, _ := c.GetInt64("patient", 0)
  768. projectStr := c.GetString("project")
  769. start_time, _ := c.GetInt64("start_time")
  770. end_time, _ := c.GetInt64("end_time")
  771. upload_type, _ := c.GetInt64("type")
  772. ids := strings.Split(projectStr, "-")
  773. if patient <= 0 {
  774. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  775. return
  776. }
  777. page, _ := c.GetInt64("page", 0)
  778. if page <= 0 {
  779. page = 1
  780. }
  781. adminUserInfo := c.GetAdminUserInfo()
  782. switch upload_type {
  783. case 1:
  784. var inspections []*models.Inspection
  785. for _, item := range ids {
  786. id, _ := strconv.ParseInt(item, 10, 64)
  787. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  788. // fmt.Println(err)
  789. //
  790. // if err != nil {
  791. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  792. // return
  793. // }
  794. for _, inspection_item := range inspection {
  795. inspections = append(inspections, inspection_item)
  796. }
  797. }
  798. c.ServeSuccessJSON(map[string]interface{}{
  799. "inspections": inspections,
  800. "type": upload_type,
  801. })
  802. break
  803. case 2:
  804. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  805. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  806. id_three, _ := strconv.ParseInt(ids[2], 10, 64)
  807. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  808. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  809. inspection_three, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_three, start_time, end_time)
  810. c.ServeSuccessJSON(map[string]interface{}{
  811. "inspections_one": inspection_one,
  812. "inspections_two": inspection_two,
  813. "inspections_three": inspection_three,
  814. "type": upload_type,
  815. })
  816. break
  817. case 3:
  818. var inspections []*models.Inspection
  819. for _, item := range ids {
  820. id, _ := strconv.ParseInt(item, 10, 64)
  821. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  822. // fmt.Println(err)
  823. //
  824. // if err != nil {
  825. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  826. // return
  827. // }
  828. for _, inspection_item := range inspection {
  829. inspections = append(inspections, inspection_item)
  830. }
  831. }
  832. c.ServeSuccessJSON(map[string]interface{}{
  833. "inspections": inspections,
  834. "type": upload_type,
  835. })
  836. break
  837. case 4:
  838. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  839. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  840. id_three, _ := strconv.ParseInt(ids[2], 10, 64)
  841. id_four, _ := strconv.ParseInt(ids[3], 10, 64)
  842. id_five, _ := strconv.ParseInt(ids[4], 10, 64)
  843. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  844. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  845. inspection_three, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_three, start_time, end_time)
  846. inspection_four, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_four, start_time, end_time)
  847. inspection_five, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_five, start_time, end_time)
  848. c.ServeSuccessJSON(map[string]interface{}{
  849. "inspections_one": inspection_one,
  850. "inspections_two": inspection_two,
  851. "inspections_three": inspection_three,
  852. "inspections_four": inspection_four,
  853. "inspections_five": inspection_five,
  854. "type": upload_type,
  855. })
  856. break
  857. case 5:
  858. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  859. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  860. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  861. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  862. c.ServeSuccessJSON(map[string]interface{}{
  863. "inspections_one": inspection_one,
  864. "inspections_two": inspection_two,
  865. "type": upload_type,
  866. })
  867. break
  868. case 6:
  869. var inspections []*models.Inspection
  870. for _, item := range ids {
  871. id, _ := strconv.ParseInt(item, 10, 64)
  872. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  873. // fmt.Println(err)
  874. //
  875. // if err != nil {
  876. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  877. // return
  878. // }
  879. for _, inspection_item := range inspection {
  880. inspections = append(inspections, inspection_item)
  881. }
  882. }
  883. c.ServeSuccessJSON(map[string]interface{}{
  884. "inspections": inspections,
  885. "type": upload_type,
  886. })
  887. break
  888. case 7:
  889. var inspections []*models.Inspection
  890. for _, item := range ids {
  891. id, _ := strconv.ParseInt(item, 10, 64)
  892. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  893. // fmt.Println(err)
  894. //
  895. // if err != nil {
  896. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  897. // return
  898. // }
  899. for _, inspection_item := range inspection {
  900. inspections = append(inspections, inspection_item)
  901. }
  902. }
  903. c.ServeSuccessJSON(map[string]interface{}{
  904. "inspections": inspections,
  905. "type": upload_type,
  906. })
  907. break
  908. }
  909. return
  910. }
  911. func (c *InspectionApiController) GetInitInsepction() {
  912. references, _ := service.GetAllInspectionReference(0)
  913. c.ServeSuccessJSON(map[string]interface{}{
  914. "references": references,
  915. })
  916. return
  917. }