inspection_api_controller.go 32KB

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