inspection_api_controller.go 32KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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[string]models.Inspection)
  569. for _, item := range insp {
  570. a := strconv.FormatInt(item.ProjectId, 10) + "-" + strconv.FormatInt(item.ItemId, 10)
  571. inspMap[a] = item
  572. }
  573. addinsp := make([]models.Inspection, 0)
  574. editinsp := make([]models.Inspection, 0)
  575. noMap := make([]int64, 0)
  576. fmt.Println("00000000000000000000000000000000000000000000000000000")
  577. for _, item := range from.FormItem {
  578. fmt.Println("name:", item.ItemId, "value:", item.Value)
  579. ref, _ := service.GetInspectionReferenceById(item.ItemId)
  580. //if item.ID == 0 {
  581. // var inspection models.Inspection
  582. // inspection.OrgId = adminUserInfo.CurrentOrgId
  583. // inspection.PatientId = patient
  584. // inspection.ProjectId = from.ProjectId
  585. // inspection.ItemId = item.ItemId
  586. // inspection.ItemName = item.ItemName
  587. // inspection.ProjectName = item.ProjectName
  588. // inspection.InspectType = item.RangeType
  589. // inspection.InspectValue = item.Value
  590. // inspection.InspectDate = date
  591. // inspection.Status = 1
  592. // inspection.CreatedTime = time.Now().Unix()
  593. // inspection.UpdatedTime = time.Now().Unix()
  594. // addinsp = append(addinsp, inspection)
  595. //} else {
  596. //fmt.Println(item.ID)
  597. a := strconv.FormatInt(item.ProjectId, 10) + "-" + strconv.FormatInt(ref.ItemId, 10)
  598. fmt.Println(a)
  599. fmt.Println(inspMap)
  600. inspection := inspMap[a]
  601. inspection.ID = item.ID
  602. inspection.OrgId = adminUserInfo.CurrentOrgId
  603. inspection.PatientId = patient
  604. inspection.ProjectId = from.ProjectId
  605. inspection.ItemId = item.ItemId
  606. inspection.ItemName = item.ItemName
  607. inspection.ProjectName = item.ProjectName
  608. inspection.InspectType = item.RangeType
  609. inspection.InspectValue = item.Value
  610. inspection.InspectDate = date
  611. inspection.Status = 1
  612. inspection.InspectValue = item.Value
  613. inspection.InspectDate = date
  614. inspection.UpdatedTime = time.Now().Unix()
  615. editinsp = append(editinsp, inspection)
  616. noMap = append(noMap, item.ID)
  617. //}
  618. }
  619. //fmt.Println(editinsp)
  620. //return
  621. err = service.EditPatientInspection(addinsp, editinsp, noMap, patient, adminUserInfo.CurrentOrgId, from.ProjectId, date)
  622. if err != nil {
  623. utils.ErrorLog("%v", err)
  624. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionEditFail)
  625. return
  626. }
  627. inspections := make([]models.Inspection, 0)
  628. inspections = append(inspections, editinsp...)
  629. inspections = append(inspections, addinsp...)
  630. if remind_cycle > 0 {
  631. infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, 14)
  632. fmt.Println(infectiousRecord.InspectDate)
  633. var record_time int64
  634. switch remind_cycle {
  635. case 1: //1个月
  636. ts := time.Unix(infectiousRecord.InspectDate, 0)
  637. record_time = ts.AddDate(0, 1, 0).Unix()
  638. break
  639. case 2: //2个月
  640. ts := time.Unix(infectiousRecord.InspectDate, 0)
  641. record_time = ts.AddDate(0, 2, 0).Unix()
  642. break
  643. case 3: //3个月
  644. ts := time.Unix(infectiousRecord.InspectDate, 0)
  645. record_time = ts.AddDate(0, 3, 0).Unix()
  646. break
  647. case 4: //6个月
  648. ts := time.Unix(infectiousRecord.InspectDate, 0)
  649. record_time = ts.AddDate(0, 6, 0).Unix()
  650. break
  651. case 5: //12个月
  652. ts := time.Unix(infectiousRecord.InspectDate, 0)
  653. record_time = ts.AddDate(0, 12, 0).Unix()
  654. break
  655. }
  656. fmt.Println(record_time)
  657. errs := service.UpDateInfectiousRecordTime(adminUserInfo.CurrentOrgId, patient, record_time, remind_cycle)
  658. if errs != nil {
  659. utils.ErrorLog("更新日期出错:%v", errs)
  660. }
  661. }
  662. c.ServeSuccessJSON(map[string]interface{}{
  663. "inspections": inspections,
  664. "remind_cycle": remind_cycle,
  665. })
  666. return
  667. }
  668. func (c *InspectionApiController) DeletePatientInspection() {
  669. patient, _ := c.GetInt64("patient", 0)
  670. ProjectId, _ := c.GetInt64("project_id", 0)
  671. InspectDate := c.GetString("date")
  672. if patient <= 0 || ProjectId <= 0 || len(InspectDate) < 10 {
  673. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  674. return
  675. }
  676. timeLayout := "2006-01-02 15:04"
  677. loc, _ := time.LoadLocation("Local")
  678. theTime, err := time.ParseInLocation(timeLayout, InspectDate, loc)
  679. if err != nil {
  680. utils.ErrorLog(err.Error())
  681. c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
  682. return
  683. }
  684. adminUserInfo := c.GetAdminUserInfo()
  685. date := theTime.Unix()
  686. insp, err := service.GetPatientInspectionByDate(adminUserInfo.CurrentOrgId, patient, date, ProjectId)
  687. if err != nil {
  688. utils.ErrorLog("%v", err)
  689. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  690. return
  691. }
  692. if len(insp) == 0 {
  693. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDateNotExit)
  694. return
  695. }
  696. err = service.DeletePatientInspection(adminUserInfo.CurrentOrgId, patient, ProjectId, date)
  697. if err != nil {
  698. utils.ErrorLog("%v", err)
  699. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDeleteFail)
  700. return
  701. }
  702. //更新提醒时间
  703. reminds, _ := service.GetCheckRemindRecordTime(adminUserInfo.CurrentOrgId, patient, ProjectId)
  704. if reminds.ID > 0 {
  705. infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, ProjectId)
  706. fmt.Println(infectiousRecord)
  707. if infectiousRecord.ID > 0 {
  708. var record_time int64
  709. switch reminds.RemindCycle {
  710. case 1: //1个月
  711. ts := time.Unix(infectiousRecord.InspectDate, 0)
  712. record_time = ts.AddDate(0, 1, 0).Unix()
  713. fmt.Println(record_time)
  714. break
  715. case 2: //2个月
  716. ts := time.Unix(infectiousRecord.InspectDate, 0)
  717. record_time = ts.AddDate(0, 2, 0).Unix()
  718. fmt.Println(record_time)
  719. break
  720. case 3: //3个月
  721. ts := time.Unix(infectiousRecord.InspectDate, 0)
  722. record_time = ts.AddDate(0, 3, 0).Unix()
  723. fmt.Println(record_time)
  724. break
  725. case 4: //6个月
  726. ts := time.Unix(infectiousRecord.InspectDate, 0)
  727. record_time = ts.AddDate(0, 6, 0).Unix()
  728. fmt.Println(record_time)
  729. break
  730. case 5: //12个月
  731. ts := time.Unix(infectiousRecord.InspectDate, 0)
  732. record_time = ts.AddDate(0, 12, 0).Unix()
  733. fmt.Println(record_time)
  734. break
  735. }
  736. errs := service.UpDateCheckRemindRecordTimeTwo(adminUserInfo.CurrentOrgId, patient, record_time, ProjectId)
  737. if errs != nil {
  738. utils.ErrorLog("更新日期出错:%v", errs)
  739. }
  740. } else {
  741. errs := service.UpDateCheckRemindRecordTimeTwo(adminUserInfo.CurrentOrgId, patient, 0, ProjectId)
  742. if errs != nil {
  743. utils.ErrorLog("更新日期出错:%v", errs)
  744. }
  745. }
  746. }
  747. c.ServeSuccessJSON(map[string]interface{}{
  748. "msg": "ok",
  749. })
  750. return
  751. }
  752. func (c *InspectionApiController) GetPatientInspections() {
  753. patient, _ := c.GetInt64("patient", 0)
  754. projectId, _ := c.GetInt64("project_id", 0)
  755. if patient <= 0 || projectId <= 0 {
  756. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  757. return
  758. }
  759. page, _ := c.GetInt64("page", 0)
  760. if page <= 0 {
  761. page = 1
  762. }
  763. adminUserInfo := c.GetAdminUserInfo()
  764. inspections, total, dateTime, err := service.GetPatientInspections(adminUserInfo.CurrentOrgId, patient, projectId, page)
  765. fmt.Println("inspections", inspections)
  766. if err != nil {
  767. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  768. return
  769. }
  770. date := ""
  771. if len(inspections) > 0 {
  772. date = time.Unix(dateTime, 0).Format("2006-01-02 15:04")
  773. }
  774. remind, _ := service.GetCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, projectId)
  775. //remind, _ := service.GetCheckRemindRecordTime(c.GetAdminUserInfo().CurrentOrgId, patient, projectId)
  776. config, _ := service.GetCheckRemindConfigById(c.GetAdminUserInfo().CurrentOrgId)
  777. c.ServeSuccessJSON(map[string]interface{}{
  778. "inspections": inspections,
  779. "total": total,
  780. "date": date,
  781. "remind": remind,
  782. "config": config,
  783. })
  784. return
  785. }
  786. func (c *InspectionApiController) GetAllPatientInspection() {
  787. patient, _ := c.GetInt64("patient", 0)
  788. projectStr := c.GetString("project")
  789. start_time, _ := c.GetInt64("start_time")
  790. end_time, _ := c.GetInt64("end_time")
  791. upload_type, _ := c.GetInt64("type")
  792. ids := strings.Split(projectStr, "-")
  793. if patient <= 0 {
  794. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  795. return
  796. }
  797. page, _ := c.GetInt64("page", 0)
  798. if page <= 0 {
  799. page = 1
  800. }
  801. adminUserInfo := c.GetAdminUserInfo()
  802. switch upload_type {
  803. case 1:
  804. var inspections []models.Inspection
  805. for _, item := range ids {
  806. id, _ := strconv.ParseInt(item, 10, 64)
  807. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  808. // fmt.Println(err)
  809. //
  810. // if err != nil {
  811. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  812. // return
  813. // }
  814. for _, inspection_item := range inspection {
  815. inspections = append(inspections, inspection_item)
  816. }
  817. }
  818. c.ServeSuccessJSON(map[string]interface{}{
  819. "inspections": inspections,
  820. "type": upload_type,
  821. })
  822. break
  823. case 2:
  824. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  825. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  826. id_three, _ := strconv.ParseInt(ids[2], 10, 64)
  827. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  828. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  829. inspection_three, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_three, start_time, end_time)
  830. c.ServeSuccessJSON(map[string]interface{}{
  831. "inspections_one": inspection_one,
  832. "inspections_two": inspection_two,
  833. "inspections_three": inspection_three,
  834. "type": upload_type,
  835. })
  836. break
  837. case 3:
  838. var inspections []models.Inspection
  839. for _, item := range ids {
  840. id, _ := strconv.ParseInt(item, 10, 64)
  841. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  842. // fmt.Println(err)
  843. //
  844. // if err != nil {
  845. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  846. // return
  847. // }
  848. for _, inspection_item := range inspection {
  849. inspections = append(inspections, inspection_item)
  850. }
  851. }
  852. c.ServeSuccessJSON(map[string]interface{}{
  853. "inspections": inspections,
  854. "type": upload_type,
  855. })
  856. break
  857. case 4:
  858. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  859. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  860. id_three, _ := strconv.ParseInt(ids[2], 10, 64)
  861. id_four, _ := strconv.ParseInt(ids[3], 10, 64)
  862. id_five, _ := strconv.ParseInt(ids[4], 10, 64)
  863. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  864. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  865. inspection_three, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_three, start_time, end_time)
  866. inspection_four, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_four, start_time, end_time)
  867. inspection_five, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_five, start_time, end_time)
  868. c.ServeSuccessJSON(map[string]interface{}{
  869. "inspections_one": inspection_one,
  870. "inspections_two": inspection_two,
  871. "inspections_three": inspection_three,
  872. "inspections_four": inspection_four,
  873. "inspections_five": inspection_five,
  874. "type": upload_type,
  875. })
  876. break
  877. case 5:
  878. id_one, _ := strconv.ParseInt(ids[0], 10, 64)
  879. id_two, _ := strconv.ParseInt(ids[1], 10, 64)
  880. inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
  881. inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
  882. c.ServeSuccessJSON(map[string]interface{}{
  883. "inspections_one": inspection_one,
  884. "inspections_two": inspection_two,
  885. "type": upload_type,
  886. })
  887. break
  888. case 6:
  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. case 7:
  909. var inspections []models.Inspection
  910. for _, item := range ids {
  911. id, _ := strconv.ParseInt(item, 10, 64)
  912. inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
  913. // fmt.Println(err)
  914. //
  915. // if err != nil {
  916. // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  917. // return
  918. // }
  919. for _, inspection_item := range inspection {
  920. inspections = append(inspections, inspection_item)
  921. }
  922. }
  923. c.ServeSuccessJSON(map[string]interface{}{
  924. "inspections": inspections,
  925. "type": upload_type,
  926. })
  927. break
  928. }
  929. return
  930. }
  931. func (c *InspectionApiController) GetInitInsepction() {
  932. references, _ := service.GetAllInspectionReference(0)
  933. c.ServeSuccessJSON(map[string]interface{}{
  934. "references": references,
  935. })
  936. return
  937. }