feat: management && notify
This commit is contained in:
223
service/management/observation.go
Normal file
223
service/management/observation.go
Normal file
@@ -0,0 +1,223 @@
|
||||
package management
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"nCovTrack-Backend/global"
|
||||
"nCovTrack-Backend/models"
|
||||
"nCovTrack-Backend/utils"
|
||||
"time"
|
||||
)
|
||||
|
||||
func PullFromFaker(claims models.TokenClaims, patientId int) int64 {
|
||||
// Get patient's identification by id
|
||||
queryMap := []map[string]interface{}{{"id": patientId}}
|
||||
patient := models.GetField[models.BackObservation](queryMap, false, "identification")
|
||||
if patient == nil || patient["identification"] == nil {
|
||||
return -1
|
||||
}
|
||||
|
||||
// Pull contacts form faker
|
||||
observations := queryContacts(patient["identification"].(string))
|
||||
// Set ContactPerson
|
||||
for i := range observations {
|
||||
observations[i].ContactPerson = patientId
|
||||
}
|
||||
// Insert into db
|
||||
var observationsJMap []map[string]interface{}
|
||||
utils.Strcut2Map(observations, &observationsJMap)
|
||||
observationsCMap := models.MapsJ2c[models.BackObservation](observationsJMap, false)
|
||||
models.BeforeBatchSave(&observationsCMap, claims.ID)
|
||||
_, rowsAffected := models.BatchInsert[models.BackObservation](observationsCMap)
|
||||
// Generate situation record
|
||||
if rowsAffected != 0 {
|
||||
pullSituationRecord(claims, observations)
|
||||
}
|
||||
return rowsAffected
|
||||
}
|
||||
|
||||
func ListObservation(jsonMap map[string]interface{}) []models.ListObeservation {
|
||||
colMap := models.MapJ2c[models.BackObservation](jsonMap, true)
|
||||
queryMap := []map[string]interface{}{colMap}
|
||||
return listObservation(queryMap)
|
||||
}
|
||||
|
||||
func GetObservation(id int) map[string]interface{} {
|
||||
observationCMap := models.GetField[models.BackObservation]([]map[string]interface{}{{"id": id}}, true, "is_delete")
|
||||
observationJMap := models.MapC2j[models.BackObservation](observationCMap, true)
|
||||
pcrRecordCMap := models.ListField[models.BackPcr]([]map[string]interface{}{{"observation": id}}, true, "is_delete")
|
||||
pcrRecordJMap := models.MapsC2j[models.BackPcr](*pcrRecordCMap, true)
|
||||
situationRecordCMap := models.ListField[models.BackSituationRecord]([]map[string]interface{}{{"observation": id}}, true, "is_delete")
|
||||
situationRecordJMap := models.MapsC2j[models.BackSituationRecord](*situationRecordCMap, true)
|
||||
observationJMap["pcrRecord"] = pcrRecordJMap
|
||||
observationJMap["situationRecord"] = situationRecordJMap
|
||||
return observationJMap
|
||||
}
|
||||
|
||||
func InsertObservation(claims models.TokenClaims, jsonMap map[string]interface{}) bool {
|
||||
colMap := models.MapJ2c[models.BackObservation](jsonMap, true)
|
||||
models.BeforeSave(colMap, claims.ID)
|
||||
ok, rowsAffected := models.Upsert[models.BackObservation](colMap)
|
||||
queryMap := []map[string]interface{}{{"identification": jsonMap["identification"]}}
|
||||
// Get result after insert, assemble record will use it.
|
||||
newObservation := models.GetField[models.BackObservation](queryMap, true)
|
||||
recordOk := insertSituationRecord(claims, map[string]interface{}{}, newObservation)
|
||||
return (ok && recordOk && rowsAffected != 0)
|
||||
}
|
||||
|
||||
func UpdateObservation(claims models.TokenClaims, jsonMap map[string]any) bool {
|
||||
colMap := models.MapJ2c[models.BackObservation](jsonMap, true)
|
||||
models.BeforeSave(colMap, claims.ID)
|
||||
oldMap := models.Get[models.BackObservation]([]map[string]interface{}{{"id": jsonMap["id"]}})
|
||||
insertSituationRecord(claims, oldMap, colMap)
|
||||
return false
|
||||
}
|
||||
|
||||
// listObservation the queryMap need with table name
|
||||
// @Param queryMap the colMap of models.BackObservation
|
||||
func listObservation(queryMap []map[string]interface{}) []models.ListObeservation {
|
||||
var observations []models.ListObeservation
|
||||
tx := global.Db.Model(new((models.BackObservation))).
|
||||
Select("back_observation.*, back_pcr.detect_time AS pcr_time, detect_result AS pcr_result," +
|
||||
"back_situation_record.create_time AS record_time, back_situation_record.record AS record").
|
||||
Joins("LEFT JOIN back_pcr ON back_observation.id = back_pcr.observation").
|
||||
Joins("LEFT JOIN back_situation_record ON back_observation.id = back_situation_record.observation")
|
||||
for _, query := range queryMap {
|
||||
query["is_delete"] = 0
|
||||
// Add the table prefix
|
||||
fullQuery := map[string]interface{}{}
|
||||
for k := range query {
|
||||
fullQuery["back_observation."+k] = query[k]
|
||||
}
|
||||
tx = tx.Or(fullQuery)
|
||||
}
|
||||
tx.Find(&observations)
|
||||
return observations
|
||||
}
|
||||
|
||||
func pullSituationRecord(claims models.TokenClaims, observations []models.BackObservation) {
|
||||
// Query observations' id
|
||||
var identifications []string
|
||||
for _, observation := range observations {
|
||||
identifications = append(identifications, observation.Identification)
|
||||
}
|
||||
queryMap := []map[string]interface{}{{"identification": identifications}}
|
||||
observationMaps := models.ListField[models.BackObservation](queryMap, false, "id", "identification")
|
||||
// Assemble records
|
||||
var records []models.BackSituationRecord
|
||||
recordContent := record00_20()
|
||||
for _, observationMap := range *observationMaps {
|
||||
record := models.BackSituationRecord{Observation: observationMap["id"].(int), Record: recordContent}
|
||||
records = append(records, record)
|
||||
}
|
||||
// Insert into db
|
||||
var recordsJMaps []map[string]interface{}
|
||||
utils.Strcut2Map(records, &recordsJMaps)
|
||||
recordsCMaps := models.MapsJ2c[models.BackSituationRecord](recordsJMaps, true)
|
||||
models.BeforeBatchSave(&recordsCMaps, claims.ID)
|
||||
models.BatchInsert[models.BackSituationRecord](recordsCMaps)
|
||||
// Notify the admin of pull region
|
||||
// Send email to the region admin
|
||||
}
|
||||
|
||||
func updateObservationRegion(region string) {
|
||||
// Add to notify
|
||||
// Send email to the target region amdin
|
||||
}
|
||||
|
||||
func updateWhileDiagnosis(claims models.TokenClaims, patientId int) {
|
||||
// Update sub contact to contact and nomeasure
|
||||
// Add to notify
|
||||
}
|
||||
|
||||
func insertSituationRecord(claims models.TokenClaims, oldObservation, newObservation map[string]interface{}) bool {
|
||||
var oldSituation, newSituation string
|
||||
if oldObservation["id"] == nil {
|
||||
// insert observation
|
||||
oldSituation = "00"
|
||||
} else {
|
||||
// update observation
|
||||
oldSituation = fmt.Sprintf("%d%d", oldObservation["health_situation"], oldObservation["measure_situation"])
|
||||
}
|
||||
if newObservation["health_situation"] == nil || newObservation["measure_situation"] == nil {
|
||||
// Situation not update
|
||||
return true
|
||||
}
|
||||
newSituation = fmt.Sprintf("%d%d", newObservation["health_situation"], newObservation["measure_situation"])
|
||||
|
||||
// Situation not update
|
||||
if oldSituation == newSituation {
|
||||
return true
|
||||
}
|
||||
// Set record content accrodding the situation convertions
|
||||
var recordContent string
|
||||
situationConvert := oldSituation + "_" + newSituation
|
||||
switch situationConvert {
|
||||
case "00_10":
|
||||
recordContent = record00_10()
|
||||
case "00_20":
|
||||
recordContent = record00_20()
|
||||
case "00_30":
|
||||
recordContent = record00_30()
|
||||
case "00_12":
|
||||
recordContent = record00_12(newObservation["region"].(string), newObservation["address"].(string))
|
||||
case "10_12":
|
||||
recordContent = record10_12(newObservation["region"].(string), newObservation["address"].(string))
|
||||
case "12_01", "23_01", "24_01", "33_01", "34_01":
|
||||
recordContent = record___01()
|
||||
case "20_23", "30_33", "24_23", "34_33":
|
||||
recordContent = record____3(newObservation["region"].(string), newObservation["address"].(string))
|
||||
case "20_24", "30_34", "23_24", "33_34":
|
||||
recordContent = record____4(newObservation["region"].(string), newObservation["address"].(string))
|
||||
default:
|
||||
if oldSituation != "00" {
|
||||
models.Upsert[models.BackObservation](oldObservation)
|
||||
} else {
|
||||
models.DropById[models.BackObservation](newObservation["id"].(int))
|
||||
}
|
||||
return false
|
||||
}
|
||||
record := models.BackSituationRecord{Observation: newObservation["id"].(int), Record: recordContent}
|
||||
var recordJMap map[string]interface{}
|
||||
utils.Strcut2Map(record, &recordJMap)
|
||||
recordCMap := models.MapJ2c[models.BackSituationRecord](recordJMap, true)
|
||||
models.BeforeSave(recordCMap, claims.ID)
|
||||
models.Upsert[models.BackSituationRecord](recordCMap)
|
||||
return true
|
||||
}
|
||||
|
||||
func record00_10() string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return date + " 确诊为患者"
|
||||
}
|
||||
func record00_12(region, address string) string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return fmt.Sprintf("%s 于 %s %s 确诊为患者", date, region, address)
|
||||
}
|
||||
func record00_20() string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return date + " 转为密接"
|
||||
}
|
||||
func record00_30() string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return date + " 转为次密接"
|
||||
}
|
||||
func record___01() string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return fmt.Sprintf("%s 解除风险", date)
|
||||
}
|
||||
func record10_12(region, address string) string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return fmt.Sprintf("%s 转至医院:%s %s", date, region, address)
|
||||
}
|
||||
func record___10() string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return date + " 确诊为患者"
|
||||
}
|
||||
func record____3(region, address string) string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return fmt.Sprintf("%s 集中隔离至:%s %s", date, region, address)
|
||||
}
|
||||
func record____4(region, address string) string {
|
||||
date := utils.FormatDate(time.Now())
|
||||
return fmt.Sprintf("%s 进行居家隔离:%s %s", date, region, address)
|
||||
}
|
||||
Reference in New Issue
Block a user