inspection_api_controller.go 33KB

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