123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633 |
- package controllers
-
- import (
- "XT_New/enums"
- "XT_New/models"
- "XT_New/service"
- "XT_New/utils"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego"
- "strconv"
- "strings"
- "time"
- )
-
- type InspectionApiController struct {
- BaseAuthAPIController
- }
-
- func InspectionApiRegistRouters() {
- beego.Router("/api/patient/inspection/list", &InspectionApiController{}, "Get:GetPatientInspections")
- beego.Router("/api/patient/inspection/reference", &InspectionApiController{}, "Get:PatientInspectionReference")
- beego.Router("/api/patient/inspection/create", &InspectionApiController{}, "Post:CreatePatientInspection")
- beego.Router("/api/patient/inspection/edit", &InspectionApiController{}, "Put:EditPatientInspection")
- beego.Router("/api/patient/inspection/delete", &InspectionApiController{}, "Delete:DeletePatientInspection")
-
- beego.Router("/api/patient/inspection/get", &InspectionApiController{}, "Get:GetAllPatientInspection")
- beego.Router("/api/patient/inspectioninit/get", &InspectionApiController{}, "Get:GetInitInsepction")
-
- }
-
- //PatientInspectionReference 请求检验检查大小项目
- //[get]: /api/patient/inspection/reference
- func (c *InspectionApiController) PatientInspectionReference() {
- patient, _ := c.GetInt64("patient")
- if patient <= 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- adminUserInfo := c.GetAdminUserInfo()
-
- references, err := service.GetInspectionReference(adminUserInfo.CurrentOrgId)
- if err != nil {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- patient_info, _ := service.GetPatientByID(adminUserInfo.CurrentOrgId, patient)
-
- counts, err := service.GetPatientInspectionProjectCount(adminUserInfo.CurrentOrgId, patient)
- if err != nil {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- countsMap := make(map[int64]int64, 0)
- for _, count := range counts {
- countsMap[count.ProjectId] = count.Count
- }
-
- referenceMap := make(map[string]*models.InspectionReferenceMap, 0)
-
- for _, reference := range references {
- if _, exist := referenceMap[reference.Project]; !exist {
- referenceMap[reference.Project] = new(models.InspectionReferenceMap)
- referenceMap[reference.Project].Project = reference.Project
- referenceMap[reference.Project].ProjectId = reference.ProjectId
- referenceMap[reference.Project].ProjectName = reference.ProjectName
- if _, cexit := countsMap[reference.ProjectId]; cexit {
- referenceMap[reference.Project].Count = countsMap[reference.ProjectId]
- }
- referenceMap[reference.Project].InspectionReference = make([]models.InspectionReference, 0)
- }
- referenceMap[reference.Project].InspectionReference = append(referenceMap[reference.Project].InspectionReference, *reference)
-
- }
-
- reference := make([]*models.InspectionReferenceMap, 0)
- for _, item := range referenceMap {
- reference = append(reference, item)
- }
-
- rl := len(reference)
- for index := 0; index < rl-1; index++ {
- for jndex := 0; jndex < rl-1-index; jndex++ {
- if reference[jndex].ProjectId > reference[jndex+1].ProjectId {
- var item models.InspectionReferenceMap
- item = *reference[jndex]
- reference[jndex] = reference[jndex+1]
- reference[jndex+1] = &item
- }
- }
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "reference": reference,
- "patient_info": patient_info,
- })
- return
-
- }
-
- func (c *InspectionApiController) CreatePatientInspection() {
- patient, _ := c.GetInt64("patient", 0)
- remind_cycle, _ := c.GetInt64("remind_cycle", 0)
-
- if patient <= 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- adminUserInfo := c.GetAdminUserInfo()
- patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patient)
- if patientInfo.ID == 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
- return
- }
-
- var from models.InepectionForm
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &from)
- if err != nil {
- utils.ErrorLog("%v", err)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- timeLayout := "2006-01-02 15:04"
- loc, _ := time.LoadLocation("Local")
-
- theTime, err := time.ParseInLocation(timeLayout, from.InspectDate, loc)
- if err != nil {
- utils.ErrorLog(err.Error())
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
- return
- }
-
- if len(from.FormItem) == 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "未填写项目")
- return
- }
-
- date := theTime.Unix()
- insp, err := service.GetPatientInspectionByDate(adminUserInfo.CurrentOrgId, patient, date, from.ProjectId)
- if err != nil {
- utils.ErrorLog("%v", err)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- if len(insp) > 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDateExit)
- return
- }
-
- inspections := make([]models.Inspection, 0)
- for _, item := range from.FormItem {
- var inspection models.Inspection
- inspection.OrgId = adminUserInfo.CurrentOrgId
- inspection.PatientId = patient
- inspection.ProjectId = from.ProjectId
- inspection.ItemId = item.ItemId
- inspection.ItemName = item.ItemName
- inspection.ProjectName = item.ProjectName
- inspection.InspectType = item.RangeType
- inspection.InspectValue = item.Value
- inspection.InspectDate = date
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- inspections = append(inspections, inspection)
- }
-
- err = service.CreatePatientInspection(inspections)
- if err != nil {
- utils.ErrorLog("%v", err)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionAddFail)
- return
- }
-
- if remind_cycle > 0 {
- infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, 14)
- fmt.Println(infectiousRecord.InspectDate)
- var record_time int64
- switch remind_cycle {
- case 1: //1个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 1, 0).Unix()
- fmt.Println(record_time)
- break
- case 2: //2个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 2, 0).Unix()
- fmt.Println(record_time)
-
- break
- case 3: //3个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 3, 0).Unix()
- fmt.Println(record_time)
-
- break
- case 4: //6个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 6, 0).Unix()
- fmt.Println(record_time)
-
- break
- case 5: //12个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 12, 0).Unix()
- fmt.Println(record_time)
-
- break
- }
-
- errs := service.UpDateInfectiousRecordTime(adminUserInfo.CurrentOrgId, patient, record_time, remind_cycle)
- if errs != nil {
- utils.ErrorLog("更新日期出错:%v", errs)
- }
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections": inspections,
- "remind_cycle": remind_cycle,
- })
- return
- }
-
- func (c *InspectionApiController) EditPatientInspection() {
- patient, _ := c.GetInt64("patient", 0)
- remind_cycle, _ := c.GetInt64("remind_cycle", 0)
- // dates := c.GetString("dates")
- // projectid := c.GetString("projectid")
-
- if patient <= 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- adminUserInfo := c.GetAdminUserInfo()
- patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patient)
- if patientInfo.ID == 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist)
- return
- }
-
- var from models.InepectionForm
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &from)
- if err != nil {
- utils.ErrorLog("%v", err)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- timeLayout := "2006-01-02 15:04"
- loc, _ := time.LoadLocation("Local")
-
- theTime, err := time.ParseInLocation(timeLayout, from.InspectDate , loc)
- if err != nil {
- utils.ErrorLog(err.Error())
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
- return
- }
-
- oldTime, olderr := time.ParseInLocation(timeLayout, from.OldInspectDate , loc)
- if olderr != nil {
- utils.ErrorLog(olderr.Error())
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
- return
- }
-
- if len(from.FormItem) == 0 {
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "未填写项目")
- return
- }
-
- date := theTime.Unix()
- oldDate := oldTime.Unix()
- insp, err := service.GetPatientInspectionByDate(adminUserInfo.CurrentOrgId, patient, oldDate, from.ProjectId)
- if err != nil {
- utils.ErrorLog("%v", err)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- if len(insp) == 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDateNotExit)
- return
- }
- inspMap := make(map[int64]*models.Inspection)
- for _, item := range insp {
- inspMap[item.ID] = item
- }
-
- addinsp := make([]models.Inspection, 0)
- editinsp := make([]models.Inspection, 0)
- noMap := make([]int64, 0)
- fmt.Println(from.FormItem)
-
- for _, item := range from.FormItem {
- fmt.Println(item.ID)
- if item.ID == 0 {
- var inspection models.Inspection
- inspection.OrgId = adminUserInfo.CurrentOrgId
- inspection.PatientId = patient
- inspection.ProjectId = from.ProjectId
- inspection.ItemId = item.ItemId
- inspection.ItemName = item.ItemName
- inspection.ProjectName = item.ProjectName
- inspection.InspectType = item.RangeType
- inspection.InspectValue = item.Value
- inspection.InspectDate = date
- inspection.Status = 1
- inspection.CreatedTime = time.Now().Unix()
- inspection.UpdatedTime = time.Now().Unix()
- addinsp = append(addinsp, inspection)
- } else {
- inspection := *inspMap[item.ID]
- inspection.InspectValue = item.Value
- inspection.InspectDate = date
- inspection.UpdatedTime = time.Now().Unix()
- editinsp = append(editinsp, inspection)
- noMap = append(noMap, item.ID)
- }
-
- }
-
- err = service.EditPatientInspection(addinsp, editinsp, noMap, patient, adminUserInfo.CurrentOrgId, from.ProjectId, date)
- if err != nil {
- utils.ErrorLog("%v", err)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionEditFail)
- return
- }
-
- inspections := make([]models.Inspection, 0)
- inspections = append(inspections, editinsp...)
- inspections = append(inspections, addinsp...)
-
- if remind_cycle > 0 {
- infectiousRecord, _ := service.FindLastRecordDate(adminUserInfo.CurrentOrgId, patient, 14)
- fmt.Println(infectiousRecord.InspectDate)
- var record_time int64
- switch remind_cycle {
- case 1: //1个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 1, 0).Unix()
-
- break
- case 2: //2个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 2, 0).Unix()
- break
- case 3: //3个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 3, 0).Unix()
- break
- case 4: //6个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 6, 0).Unix()
- break
- case 5: //12个月
- ts := time.Unix(infectiousRecord.InspectDate, 0)
- record_time = ts.AddDate(0, 12, 0).Unix()
- break
- }
- fmt.Println(record_time)
- errs := service.UpDateInfectiousRecordTime(adminUserInfo.CurrentOrgId, patient, record_time, remind_cycle)
- if errs != nil {
- utils.ErrorLog("更新日期出错:%v", errs)
- }
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections": inspections,
- "remind_cycle": remind_cycle,
- })
- return
- }
-
- func (c *InspectionApiController) DeletePatientInspection() {
- patient, _ := c.GetInt64("patient", 0)
- ProjectId, _ := c.GetInt64("project_id", 0)
- InspectDate := c.GetString("date")
- if patient <= 0 || ProjectId <= 0 || len(InspectDate) < 10 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- timeLayout := "2006-01-02 15:04"
- loc, _ := time.LoadLocation("Local")
-
- theTime, err := time.ParseInLocation(timeLayout, InspectDate, loc)
- if err != nil {
- utils.ErrorLog(err.Error())
- c.ServeFailJsonSend(enums.ErrorCodeParamWrong, "检验日期不正确")
- return
- }
-
- adminUserInfo := c.GetAdminUserInfo()
-
- date := theTime.Unix()
- insp, err := service.GetPatientInspectionByDate(adminUserInfo.CurrentOrgId, patient, date, ProjectId)
- if err != nil {
- utils.ErrorLog("%v", err)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- if len(insp) == 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDateNotExit)
- return
- }
-
- err = service.DeletePatientInspection(adminUserInfo.CurrentOrgId, patient, ProjectId, date)
- if err != nil {
- utils.ErrorLog("%v", err)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeInspectionDeleteFail)
- return
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "msg": "ok",
- })
- return
- }
-
- func (c *InspectionApiController) GetPatientInspections() {
- patient, _ := c.GetInt64("patient", 0)
- projectId, _ := c.GetInt64("project_id", 0)
- if patient <= 0 || projectId <= 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- page, _ := c.GetInt64("page", 0)
- if page <= 0 {
- page = 1
- }
-
- adminUserInfo := c.GetAdminUserInfo()
- inspections, total, dateTime, err := service.GetPatientInspections(adminUserInfo.CurrentOrgId, patient, projectId, page)
- if err != nil {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- date := ""
- if len(inspections) > 0 {
- date = time.Unix(dateTime, 0).Format("2006-01-02 15:04")
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections": inspections,
- "total": total,
- "date": date,
- })
- return
-
- }
-
- func (c *InspectionApiController) GetAllPatientInspection() {
- patient, _ := c.GetInt64("patient", 0)
- projectStr := c.GetString("project")
- start_time, _ := c.GetInt64("start_time")
- end_time, _ := c.GetInt64("end_time")
- upload_type, _ := c.GetInt64("type")
-
- ids := strings.Split(projectStr, "-")
-
- if patient <= 0 {
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- page, _ := c.GetInt64("page", 0)
- if page <= 0 {
- page = 1
- }
-
- adminUserInfo := c.GetAdminUserInfo()
-
- switch upload_type {
- case 1:
- var inspections []*models.Inspection
-
- for _, item := range ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
- //fmt.Println(err)
- //if err != nil {
- // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- // return
- //}
- for _, inspection_item := range inspection {
- inspections = append(inspections, inspection_item)
- }
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections": inspections,
- "type": upload_type,
- })
- break
-
- case 2:
- id_one, _ := strconv.ParseInt(ids[0], 10, 64)
- id_two, _ := strconv.ParseInt(ids[1], 10, 64)
- id_three, _ := strconv.ParseInt(ids[2], 10, 64)
- inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
- inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
- inspection_three, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_three, start_time, end_time)
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections_one": inspection_one,
- "inspections_two": inspection_two,
- "inspections_three": inspection_three,
- "type": upload_type,
- })
-
- break
- case 3:
- var inspections []*models.Inspection
-
- for _, item := range ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
- //fmt.Println(err)
- //if err != nil {
- // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- // return
- //}
- for _, inspection_item := range inspection {
- inspections = append(inspections, inspection_item)
- }
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections": inspections,
- "type": upload_type,
- })
-
- break
- case 4:
- id_one, _ := strconv.ParseInt(ids[0], 10, 64)
- id_two, _ := strconv.ParseInt(ids[1], 10, 64)
- id_three, _ := strconv.ParseInt(ids[2], 10, 64)
- id_four, _ := strconv.ParseInt(ids[3], 10, 64)
- id_five, _ := strconv.ParseInt(ids[4], 10, 64)
-
- inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
- inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
- inspection_three, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_three, start_time, end_time)
- inspection_four, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_four, start_time, end_time)
- inspection_five, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_five, start_time, end_time)
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections_one": inspection_one,
- "inspections_two": inspection_two,
- "inspections_three": inspection_three,
- "inspections_four": inspection_four,
- "inspections_five": inspection_five,
- "type": upload_type,
- })
-
- break
- case 5:
- id_one, _ := strconv.ParseInt(ids[0], 10, 64)
- id_two, _ := strconv.ParseInt(ids[1], 10, 64)
- inspection_one, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_one, start_time, end_time)
- inspection_two, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id_two, start_time, end_time)
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections_one": inspection_one,
- "inspections_two": inspection_two,
- "type": upload_type,
- })
-
- break
- case 6:
- var inspections []*models.Inspection
-
- for _, item := range ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
- //fmt.Println(err)
- //if err != nil {
- // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- // return
- //}
- for _, inspection_item := range inspection {
- inspections = append(inspections, inspection_item)
- }
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections": inspections,
- "type": upload_type,
- })
-
- break
- case 7:
- var inspections []*models.Inspection
-
- for _, item := range ids {
- id, _ := strconv.ParseInt(item, 10, 64)
- inspection, _, _ := service.GetAllPatientInspection(adminUserInfo.CurrentOrgId, patient, page, id, start_time, end_time)
- //fmt.Println(err)
- //if err != nil {
- // c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- // return
- //}
- for _, inspection_item := range inspection {
- inspections = append(inspections, inspection_item)
- }
- }
-
- c.ServeSuccessJSON(map[string]interface{}{
- "inspections": inspections,
- "type": upload_type,
- })
-
- break
-
- }
-
- return
-
- }
-
- func (c *InspectionApiController) GetInitInsepction() {
- references, _ := service.GetAllInspectionReference(0)
- c.ServeSuccessJSON(map[string]interface{}{
- "references": references,
- })
- return
-
- }
|