inspection_api_controller.go 32KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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.GetFaPiaoPatientByID(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.ProjectName]; !exist {
  131. referenceMap[reference.ProjectName] = new(models.InspectionReferenceMap)
  132. referenceMap[reference.ProjectName].Project = reference.Project
  133. referenceMap[reference.ProjectName].ProjectId = reference.ProjectId
  134. referenceMap[reference.ProjectName].ProjectName = reference.ProjectName
  135. if _, cexit := countsMap[reference.ProjectId]; cexit {
  136. referenceMap[reference.ProjectName].Count = countsMap[reference.ProjectId]
  137. }
  138. referenceMap[reference.ProjectName].InspectionReference = make([]models.InspectionReference, 0)
  139. }
  140. referenceMap[reference.ProjectName].InspectionReference = append(referenceMap[reference.ProjectName].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. fmt.Println("inspections", inspections)
  747. if err != nil {
  748. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  749. return
  750. }
  751. date := ""
  752. if len(inspections) > 0 {
  753. date = time.Unix(dateTime, 0).Format("2006-01-02 15:04")
  754. }
  755. remind, _ := service.GetCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, projectId)
  756. //remind, _ := service.GetCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, projectId)
  757. config, _ := service.GetCheckRemindConfigById(c.GetAdminUserInfo().CurrentOrgId)
  758. c.ServeSuccessJSON(map[string]interface{}{
  759. "inspections": inspections,
  760. "total": total,
  761. "date": date,
  762. "remind": remind,
  763. "config": config,
  764. })
  765. return
  766. }
  767. func (c *InspectionApiController) GetAllPatientInspection() {
  768. patient, _ := c.GetInt64("patient", 0)
  769. projectStr := c.GetString("project")
  770. start_time, _ := c.GetInt64("start_time")
  771. end_time, _ := c.GetInt64("end_time")
  772. upload_type, _ := c.GetInt64("type")
  773. ids := strings.Split(projectStr, "-")
  774. if patient <= 0 {
  775. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  776. return
  777. }
  778. page, _ := c.GetInt64("page", 0)
  779. if page <= 0 {
  780. page = 1
  781. }
  782. adminUserInfo := c.GetAdminUserInfo()
  783. switch upload_type {
  784. case 1:
  785. var inspections []*models.Inspection
  786. for _, item := range ids {
  787. id, _ := strconv.ParseInt(item, 10, 64)
  788. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  789. // fmt.Println(err)
  790. //
  791. // if err != nil {
  792. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  793. // return
  794. // }
  795. for _, inspection_item := range inspection {
  796. inspections = append(inspections, inspection_item)
  797. }
  798. }
  799. c.ServeSuccessJSON(map[string]interface{}{
  800. "inspections": inspections,
  801. "type": upload_type,
  802. })
  803. break
  804. case 2:
  805. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  806. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  807. id_three, _ := strconv.ParseInt(ids[2], 10, 64)
  808. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  809. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  810. inspection_three, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_three, start_time, end_time)
  811. c.ServeSuccessJSON(map[string]interface{}{
  812. "inspections_one": inspection_one,
  813. "inspections_two": inspection_two,
  814. "inspections_three": inspection_three,
  815. "type": upload_type,
  816. })
  817. break
  818. case 3:
  819. var inspections []*models.Inspection
  820. for _, item := range ids {
  821. id, _ := strconv.ParseInt(item, 10, 64)
  822. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  823. // fmt.Println(err)
  824. //
  825. // if err != nil {
  826. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  827. // return
  828. // }
  829. for _, inspection_item := range inspection {
  830. inspections = append(inspections, inspection_item)
  831. }
  832. }
  833. c.ServeSuccessJSON(map[string]interface{}{
  834. "inspections": inspections,
  835. "type": upload_type,
  836. })
  837. break
  838. case 4:
  839. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  840. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  841. id_three, _ := strconv.ParseInt(ids[2], 10, 64)
  842. id_four, _ := strconv.ParseInt(ids[3], 10, 64)
  843. id_five, _ := strconv.ParseInt(ids[4], 10, 64)
  844. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  845. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  846. inspection_three, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_three, start_time, end_time)
  847. inspection_four, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_four, start_time, end_time)
  848. inspection_five, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_five, start_time, end_time)
  849. c.ServeSuccessJSON(map[string]interface{}{
  850. "inspections_one": inspection_one,
  851. "inspections_two": inspection_two,
  852. "inspections_three": inspection_three,
  853. "inspections_four": inspection_four,
  854. "inspections_five": inspection_five,
  855. "type": upload_type,
  856. })
  857. break
  858. case 5:
  859. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  860. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  861. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  862. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  863. c.ServeSuccessJSON(map[string]interface{}{
  864. "inspections_one": inspection_one,
  865. "inspections_two": inspection_two,
  866. "type": upload_type,
  867. })
  868. break
  869. case 6:
  870. var inspections []*models.Inspection
  871. for _, item := range ids {
  872. id, _ := strconv.ParseInt(item, 10, 64)
  873. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  874. // fmt.Println(err)
  875. //
  876. // if err != nil {
  877. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  878. // return
  879. // }
  880. for _, inspection_item := range inspection {
  881. inspections = append(inspections, inspection_item)
  882. }
  883. }
  884. c.ServeSuccessJSON(map[string]interface{}{
  885. "inspections": inspections,
  886. "type": upload_type,
  887. })
  888. break
  889. case 7:
  890. var inspections []*models.Inspection
  891. for _, item := range ids {
  892. id, _ := strconv.ParseInt(item, 10, 64)
  893. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  894. // fmt.Println(err)
  895. //
  896. // if err != nil {
  897. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  898. // return
  899. // }
  900. for _, inspection_item := range inspection {
  901. inspections = append(inspections, inspection_item)
  902. }
  903. }
  904. c.ServeSuccessJSON(map[string]interface{}{
  905. "inspections": inspections,
  906. "type": upload_type,
  907. })
  908. break
  909. }
  910. return
  911. }
  912. func (c *InspectionApiController) GetInitInsepction() {
  913. references, _ := service.GetAllInspectionReference(0)
  914. c.ServeSuccessJSON(map[string]interface{}{
  915. "references": references,
  916. })
  917. return
  918. }