123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429 |
- package controllers
- import (
- "FollowUp_Notice/conf"
- "FollowUp_Notice/http"
- "FollowUp_Notice/logs"
- "FollowUp_Notice/models/Account"
- "FollowUp_Notice/models/Illness"
- "FollowUp_Notice/models/Patient"
- "FollowUp_Notice/models/System"
- "FollowUp_Notice/models/Tag"
- "fmt"
- "git.baozhida.cn/ERP_libs/lib"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/robfig/cron/v3"
- "math"
- "strconv"
- "strings"
- "time"
- )
- type PatientController struct {
- beego.Controller
- User Account.User
- }
- func (c *PatientController) Prepare() {
- if Account.User_r != nil {
- c.User = *Account.User_r
- }
- }
- // 添加患者信息
- func (c *PatientController) Patient_List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 病历号
- T_number := c.GetString("T_number")
- // 姓名
- T_name := c.GetString("T_name")
- // 标签
- T_tag, _ := c.GetInt("T_tag")
- // 疾病
- T_illness, _ := c.GetInt("T_illness")
- // 通知状态 1待通知 2已通知
- T_notice, _ := c.GetInt("T_notice")
- // 复诊状态 1正常 2超时
- T_follow_up, _ := c.GetInt("T_follow_up")
- // 年龄排序 1-升序 2降序
- T_age_sort, _ := c.GetInt("T_age_sort")
- // 下次复诊时间 1-升序 2降序
- T_next_time_sort, _ := c.GetInt("T_next_time_sort")
- Tag.Read_Tag_All_Map()
- Illness.Read_Illness_All_Map()
- R_List, R_cnt := Patient.Read_Patient_List(c.User.Id, T_number, T_name, T_tag, T_illness, T_notice, T_follow_up, T_age_sort, T_next_time_sort, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 添加患者信息
- func (c *PatientController) Patient_Add() {
- T_number := c.GetString("T_number")
- T_name := c.GetString("T_name")
- T_age, _ := c.GetInt("T_age")
- T_tag := c.GetString("T_tag")
- T_illness, _ := c.GetInt("T_illness")
- T_phone := c.GetString("T_phone")
- T_notice_phone, _ := c.GetInt("T_notice_phone")
- T_notice_message, _ := c.GetInt("T_notice_message")
- T_notice_interval := c.GetString("T_notice_interval")
- T_record := c.GetString("T_record")
- var_ := Patient.Patient{
- T_uid: c.User.Id,
- T_number: T_number,
- T_name: T_name,
- T_age: T_age,
- T_tag: T_tag,
- T_illness: T_illness,
- T_phone: T_phone,
- T_notice_phone: T_notice_phone,
- T_notice_message: T_notice_message,
- T_notice_interval: T_notice_interval,
- T_record: T_record,
- T_State: 1,
- }
- T_record_list := strings.Split(strings.Trim(T_record, "|"), "|")
- var T_time string // 复诊时间
- var T_interval int // 复诊间隔
- if len(T_record_list) > 0 {
- temp := T_record_list[len(T_record_list)-1]
- T_time = strings.Split(temp, ",")[0]
- T_interval, _ = strconv.Atoi(strings.Split(temp, ",")[1])
- t, _ := lib.DateStrToTime(T_time)
- nextTime := t.AddDate(0, 0, T_interval)
- // 复诊状态 1正常 2超时 3结束
- var_.T_follow_up = 1
- if nextTime.Before(time.Now()) {
- var_.T_follow_up = 2
- }
- if T_interval == 0 {
- var_.T_follow_up = 3
- }
- var_.T_next_time = nextTime.Format("2006-01-02")
- var_.T_notice = 1
- }
- _, err := Patient.Add_Patient(var_)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.User.T_uuid, "患者", "新增", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 修改患者信息
- func (c *PatientController) Patient_Edit() {
- T_uuid := c.GetString("T_uuid")
- T_number := c.GetString("T_number")
- T_name := c.GetString("T_name")
- T_age, _ := c.GetInt("T_age")
- T_tag := c.GetString("T_tag")
- T_illness, _ := c.GetInt("T_illness")
- T_phone := c.GetString("T_phone")
- T_notice_phone, _ := c.GetInt("T_notice_phone")
- T_notice_message, _ := c.GetInt("T_notice_message")
- T_notice_interval := c.GetString("T_notice_interval")
- T_record := c.GetString("T_record")
- var err error
- var patient Patient.Patient
- if len(T_uuid) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- patient, err = Patient.Read_Patient_ByT_uuid(T_uuid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- var cols []string
- if len(T_number) > 0 {
- patient.T_number = T_number
- cols = append(cols, "T_number")
- }
- if len(T_name) > 0 {
- patient.T_name = T_name
- cols = append(cols, "T_name")
- }
- if T_age > 0 {
- patient.T_age = T_age
- cols = append(cols, "T_age")
- }
- if len(T_tag) > 0 {
- patient.T_tag = T_tag
- cols = append(cols, "T_tag")
- }
- if T_illness > 0 {
- patient.T_illness = T_illness
- cols = append(cols, "T_illness")
- }
- if len(T_phone) > 0 {
- patient.T_phone = T_phone
- cols = append(cols, "T_name")
- }
- if T_notice_phone > 0 {
- patient.T_notice_phone = T_notice_phone
- cols = append(cols, "T_notice_phone")
- }
- if T_notice_message > 0 {
- patient.T_notice_message = T_notice_message
- cols = append(cols, "T_notice_message")
- }
- if len(T_notice_interval) > 0 {
- patient.T_notice_interval = T_notice_interval
- cols = append(cols, "T_notice_interval")
- }
- if len(T_record) > 0 {
- patient.T_record = T_record
- cols = append(cols, "T_record")
- }
- T_record_list := strings.Split(strings.Trim(T_record, "|"), "|")
- var T_time string // 复诊时间
- var T_interval int // 复诊间隔
- if len(T_record_list) > 0 {
- temp := T_record_list[len(T_record_list)-1]
- T_time = strings.Split(temp, ",")[0]
- T_interval, _ = strconv.Atoi(strings.Split(temp, ",")[1])
- t, _ := lib.DateStrToTime(T_time)
- nextTime := t.AddDate(0, 0, T_interval)
- nextTimeStr := nextTime.Format("2006-01-02")
- if nextTimeStr != patient.T_next_time {
- patient.T_follow_up = 1
- if nextTime.Before(time.Now()) {
- patient.T_follow_up = 2
- }
- if T_interval == 0 {
- patient.T_follow_up = 3
- }
- patient.T_next_time = nextTimeStr
- patient.T_notice = 1
- }
- }
- cols = append(cols, "T_next_time", "T_notice", "T_follow_up")
- if err = Patient.Update_Patient(patient, cols...); err != nil {
- c.Data["json"] = lib.JSONS{Code: 208, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改个人信息", patient)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 删除患者信息
- func (c *PatientController) Patient_Del() {
- T_uuid := c.GetString("T_uuid")
- if len(T_uuid) == 0 {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- patient, err := Patient.Read_Patient_ByT_uuid(T_uuid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- if c.User.Id != 1 || c.User.Id != patient.T_uid {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "无权删除!"}
- c.ServeJSON()
- return
- }
- if err = Patient.Delete_Patient(patient); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- func Cron_Patient() {
- //创建一个定时任务对象
- c := cron.New(cron.WithSeconds())
- //给对象增加定时任务
- //c.AddFunc("0 */1 * * * ?", Cron_Patient_Notice)
- c.AddFunc("@daily", Cron_Patient_ChangeFollowUp) // 修改复诊状态
- c.AddFunc("0 0 8 * * *", Cron_Patient_Notice) // 消息通知
- //启动定时任务
- c.Start()
- defer c.Stop()
- //查询语句,阻塞,让main函数不退出,保持程序运行
- select {}
- }
- // 修改患者复诊状态 每晚0点统计
- // 已通知并且下次复诊时间超过当期时间 已过期
- func Cron_Patient_ChangeFollowUp() {
- T_date := time.Now().Format("2006-01-02")
- logs.Info("开始处理" + T_date + "患者通知状态")
- // T_notice 通知状态 1待通知 2已通知
- // T_follow_up 复诊状态 1正常 2超时
- list, _ := Patient.Read_Patient_List(0, "", "", 0, 0, 2, 1, 0, 0, 0, 9999)
- for _, v := range list {
- nextTime, _ := lib.DateStrToTime(v.T_next_time)
- expDate := nextTime.AddDate(0, 0, v.T_next_interval)
- if expDate.Before(time.Now()) {
- var_ := Patient.Patient{Id: v.Id, T_follow_up: 2}
- if err := Patient.Update_Patient(var_, "T_follow_up"); err != nil {
- System.Add_SysLogs_T("复诊状态", "修改失败", var_)
- }
- }
- }
- }
- // 给患者发送消息提醒 每天8点
- // 发送成功将通知状态修改未已通知H
- func Cron_Patient_Notice() {
- T_date := time.Now().Format("2006-01-02")
- logs.Info("开始发送" + T_date + "患者通知")
- // T_notice 通知状态 1待通知 2已通知
- // T_follow_up 复诊状态 1正常 2超时 3结束
- now := time.Now()
- nowDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
- userList, _ := Account.Read_User_List("", 0, 9999)
- for _, user := range userList {
- // T_arrears_notice 欠费通知 1继续通知 2终止通知
- // T_State 0删除 1启用 2停用
- // 欠费终止通知或者停用后,不再继续发送通知
- if (user.T_money <= 0 && user.T_arrears_notice == 2) || user.T_State == 2 {
- continue
- }
- patientList, _ := Patient.Read_Patient_List(user.Id, "", "", 0, 0, 0, 1, 0, 0, 0, 9999)
- for _, v := range patientList {
- nextTime, _ := lib.DateStrToTime(v.T_next_time)
- intervalList := strings.Split(strings.Trim(v.T_notice_interval, ","), ",")
- var success bool
- // 提前1天
- if intervalList[0] == "1" && nextTime.AddDate(0, 0, -1) == nowDate {
- success = Send_Notice(nextTime, user, v)
- }
- // 提前2天
- if intervalList[1] == "1" && nextTime.AddDate(0, 0, -2) == nowDate {
- success = Send_Notice(nextTime, user, v)
- }
- // 提前3天
- if intervalList[2] == "1" && nextTime.AddDate(0, 0, -3) == nowDate {
- success = Send_Notice(nextTime, user, v)
- }
- // 提前7天
- if intervalList[3] == "1" && nextTime.AddDate(0, 0, -7) == nowDate {
- success = Send_Notice(nextTime, user, v)
- }
- if success == true {
- // 通知状态 1待通知 2已通知
- var_ := Patient.Patient{Id: v.Id, T_notice: 2}
- if err := Patient.Update_Patient(var_, "T_notice"); err != nil {
- System.Add_SysLogs_T("复诊通知", "修改失败", var_)
- }
- }
- }
- }
- }
- // 发送通知(短信、电话)
- func Send_Notice(nextTime time.Time, user Account.User_R, patient Patient.Patient_R) (Success bool) {
- // 发送短信通知
- //if patient.T_notice_message == 1 {
- // res, err := http.SmsXSend(user.T_template_id, patient.T_phone, patient.T_name, nextTime.Format("2006年01月02日"))
- // if err != nil {
- // System.Add_SysLogs_T("复诊通知", "短信通知失败", patient)
- // }
- // // 保存短信发送记录
- // smsSend := Patient.PatientSend{
- // T_uid: user.Id,
- // T_pid: patient.Id,
- // T_phone: patient.T_phone,
- // T_type: 1,
- // T_Remark: "send_id:" + res.Send_id,
- // T_State: 1,
- // }
- // if res.Status == "error" {
- // smsSend.T_State = 0
- // }
- // if res.Status == "success" {
- // Success = true
- // }
- // _, err = Patient.Add_PatientSend(smsSend)
- // if err != nil {
- // System.Add_SysLogs_T("复诊通知", "添加发送记录失败", patient)
- // }
- //}
- if patient.T_notice_phone == 1 {
- playInfoList := http.GetPlayInfoList(conf.VoiceCall_Template, []string{user.T_user, patient.T_name, nextTime.Format("2006/01/02")})
- res, err := http.VoiceNotifyAPI(conf.VoiceCall_Phone, "+86"+patient.T_phone, playInfoList)
- if err != nil {
- System.Add_SysLogs_T("复诊通知", "电话通知失败", patient)
- }
- // 保存短信发送记录
- smsSend := Patient.PatientSend{
- T_uid: user.Id,
- T_pid: patient.Id,
- T_phone: patient.T_phone,
- T_type: 1,
- T_Remark: fmt.Sprintf("resultdesc:%s,sessionId:%s", res.Resultdesc, res.SessionId),
- T_State: 1,
- }
- if res.Resultcode != "0" {
- smsSend.T_State = 0
- }
- if res.Resultcode == "0" {
- Success = true
- }
- _, err = Patient.Add_PatientSend(smsSend)
- if err != nil {
- System.Add_SysLogs_T("复诊通知", "添加发送记录失败", patient)
- }
- }
- return Success
- }
|