PatientSend.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package Patient
  2. import (
  3. "FollowUp_Notice/conf"
  4. "FollowUp_Notice/lib"
  5. "fmt"
  6. "github.com/astaxie/beego/cache"
  7. _ "github.com/astaxie/beego/cache/redis"
  8. "github.com/astaxie/beego/logs"
  9. "github.com/beego/beego/v2/adapter/orm"
  10. orm2 "github.com/beego/beego/v2/client/orm"
  11. "time"
  12. )
  13. // 患者信息发送
  14. type PatientSend struct {
  15. Id int `orm:"column(ID);size(11);auto;pk"`
  16. T_uid int `orm:"index;size(100);null"` // 用户id
  17. T_pid int `orm:"index;size(100);null"` // 患者id
  18. T_phone string `orm:"size(256);null"` // 患者电话 18888888888
  19. T_type int `orm:"index;size(4);null"` // 1 短信 2 电话 3 电话满意度调查
  20. T_id string `orm:"size(256);null"` // 发送id
  21. T_code string `orm:"size(256);null"` // 错误码
  22. T_fee float64 `orm:"digits(12);decimals(2)"` // 错误码
  23. T_remark string `orm:"size(256);null"` // 备注
  24. T_State int `orm:"size(200);default(1)"` // 0失败 1正常
  25. T_digitInfo string `orm:"size(200)"` // 满意度调查 放音收号结果
  26. CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间
  27. }
  28. type PatientSend_R struct {
  29. Id int
  30. T_pid int // 患者电话
  31. T_phone string // 18888888888
  32. T_type int // 1 短信 2 电话
  33. T_id string
  34. T_code string
  35. T_remark string // 备注
  36. T_State int // 0失败 1正常
  37. T_fee_num int // 计费条数 赛邮云 短信 T_fee 电话 T_fee/0.09
  38. CreateTime string //auto_now_add 第一次保存时才设置时间
  39. }
  40. func (t *PatientSend) TableName() string {
  41. return "patient_send" // 数据库名称 // ************** 替换 FormulaList **************
  42. }
  43. var redisCache_PatientSend cache.Cache
  44. func init() {
  45. //注册模型
  46. orm.RegisterModel(new(PatientSend))
  47. config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
  48. "redis_PatientSend", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
  49. fmt.Println(config)
  50. var err error
  51. redisCache_PatientSend, err = cache.NewCache("redis", config)
  52. if err != nil || redisCache_PatientSend == nil {
  53. errMsg := "failed to init redis"
  54. logs.Error(errMsg, err)
  55. }
  56. }
  57. // ---------------- Redis -------------------
  58. func Redis_PatientSend_Set(key string, r string) (err error) {
  59. err = redisCache_PatientSend.Put(key, r, 10*time.Minute)
  60. if err != nil {
  61. logs.Error("set key:", key, ",value:", r, err)
  62. }
  63. return
  64. }
  65. func Redis_PatientSend_Get(key string) (r string, is bool) {
  66. if redisCache_PatientSend.IsExist(key) {
  67. //println("找到key:",key)
  68. v := redisCache_PatientSend.Get(key)
  69. value := string(v.([]byte))
  70. Redis_PatientSend_Set(key, value)
  71. return value, true
  72. }
  73. //println("没有 找到key:",key)
  74. return "", false
  75. }
  76. func Redis_PatientSend_DelK(key string) (err error) {
  77. err = redisCache_PatientSend.Delete(key)
  78. if err != nil {
  79. logs.Error(lib.FuncName(), err)
  80. }
  81. return
  82. }
  83. func PatientSendToPatientSend_R(r PatientSend) (m PatientSend_R) {
  84. m.Id = r.Id
  85. m.T_pid = r.T_pid
  86. m.T_phone = r.T_phone
  87. m.T_type = r.T_type
  88. m.T_id = r.T_id
  89. m.T_code = r.T_code
  90. m.T_remark = r.T_remark
  91. m.T_State = r.T_State
  92. if r.T_type == 1 {
  93. m.T_fee_num = int(r.T_fee)
  94. }
  95. if r.T_type == 2 {
  96. m.T_fee_num = int(r.T_fee / 0.09)
  97. }
  98. m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
  99. return
  100. }
  101. // 添加
  102. func Add_PatientSend(r PatientSend) (id int64, err error) {
  103. o := orm.NewOrm()
  104. id, err = o.Insert(&r)
  105. if err != nil {
  106. logs.Error(lib.FuncName(), err)
  107. }
  108. return id, err
  109. }
  110. // 获取 ById
  111. func Read_PatientSend_ByT_id(T_id string) (r PatientSend, err error) {
  112. o := orm.NewOrm()
  113. qs := o.QueryTable(new(PatientSend))
  114. err = qs.Filter("T_id", T_id).One(&r)
  115. if err != nil {
  116. logs.Error(lib.FuncName(), err)
  117. }
  118. return
  119. }
  120. // 修改
  121. func Update_PatientSend(m PatientSend, cols ...string) error {
  122. o := orm.NewOrm()
  123. num, err := o.Update(&m, cols...)
  124. if err != nil {
  125. logs.Error(lib.FuncName(), err)
  126. return err
  127. }
  128. fmt.Println("Number of records updated in database:", num)
  129. return nil
  130. }
  131. // 获取列表
  132. func Read_PatientSend_List(T_uid, T_pid, T_type int, T_date string, page, page_z int) (r_ []PatientSend_R, cnt int64) {
  133. o := orm.NewOrm()
  134. // 也可以直接使用 Model 结构体作为表名
  135. qs := o.QueryTable(new(PatientSend))
  136. var offset int64
  137. if page <= 1 {
  138. offset = 0
  139. } else {
  140. offset = int64((page - 1) * page_z)
  141. }
  142. // 过滤
  143. cond := orm.NewCondition()
  144. if T_uid > 0 {
  145. cond = cond.And("T_uid", T_uid)
  146. }
  147. if T_pid > 0 {
  148. cond = cond.And("T_pid", T_pid)
  149. }
  150. if T_type > 0 {
  151. cond = cond.And("T_type", T_type)
  152. }
  153. if len(T_date) > 0 {
  154. cond = cond.And("CreateTime__startswith", T_date)
  155. }
  156. // 查询
  157. var r []PatientSend
  158. var err error
  159. if page_z == 9999 {
  160. _, err = qs.SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
  161. } else {
  162. _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond)).OrderBy("-Id").All(&r)
  163. }
  164. if err != nil {
  165. logs.Error(lib.FuncName(), err)
  166. return
  167. }
  168. cnt, err = qs.SetCond((*orm2.Condition)(cond)).Count()
  169. if err != nil {
  170. logs.Error(lib.FuncName(), err)
  171. return
  172. }
  173. for _, v := range r {
  174. r_ = append(r_, PatientSendToPatientSend_R(v))
  175. }
  176. return r_, cnt
  177. }
  178. // 获取列表
  179. func Read_PatientSend_Count_Yesterday(T_uid, T_pid, T_type int, date string) (cnt float64) {
  180. o := orm.NewOrm()
  181. // 也可以直接使用 Model 结构体作为表名
  182. qs := o.QueryTable(new(PatientSend))
  183. // 过滤
  184. cond := orm.NewCondition()
  185. cond = cond.And("T_State", 1)
  186. if T_uid > 0 {
  187. cond = cond.And("T_uid", T_uid)
  188. }
  189. if T_pid > 0 {
  190. cond = cond.And("T_pid", T_pid)
  191. }
  192. if T_type > 0 {
  193. cond = cond.And("T_type", T_type)
  194. }
  195. if len(date) > 0 {
  196. cond = cond.And("CreateTime__gte", date+" 00:00:00").And("CreateTime__lte", date+" 23:59:59")
  197. }
  198. type result struct {
  199. Total float64 `json:"total"`
  200. }
  201. var res []result
  202. _, err := qs.SetCond((*orm2.Condition)(cond)).Aggregate("sum(t_fee) as total").All(&res)
  203. if err != nil {
  204. logs.Error(lib.FuncName(), err)
  205. return
  206. }
  207. //cnt, err := qs.SetCond((*orm2.Condition)(cond)).Count()
  208. //var total float64
  209. //_, err := qs.SetCond((*orm2.Condition)(cond)).Aggregate("sum(t_fee) as total").All(&total)
  210. //if err != nil {
  211. // logs.Error(lib.FuncName(), err)
  212. // return
  213. //}
  214. return res[0].Total
  215. }
  216. func Read_PatientSend_Satisfaction_Count_Yesterday(T_uid, T_pid int, date string) (cnt int64) {
  217. o := orm.NewOrm()
  218. // 也可以直接使用 Model 结构体作为表名
  219. qs := o.QueryTable(new(PatientSend))
  220. // 过滤
  221. cond := orm.NewCondition()
  222. cond = cond.And("T_type", 3)
  223. cond = cond.AndCond(cond.Or("T_State", 1).Or("T_code", "8008"))
  224. if T_uid > 0 {
  225. cond = cond.And("T_uid", T_uid)
  226. }
  227. if T_pid > 0 {
  228. cond = cond.And("T_pid", T_pid)
  229. }
  230. if len(date) > 0 {
  231. cond = cond.And("CreateTime__gte", date+" 00:00:00").And("CreateTime__lte", date+" 23:59:59")
  232. }
  233. cnt, err := qs.SetCond((*orm2.Condition)(cond)).Count()
  234. if err != nil {
  235. logs.Error(lib.FuncName(), err)
  236. return
  237. }
  238. return cnt
  239. }
  240. // 获取列表
  241. func Read_PatientSend_Count_Month(T_uid, T_pid, T_type int, month string, T_State int) (cnt int64) {
  242. o := orm.NewOrm()
  243. // 也可以直接使用 Model 结构体作为表名
  244. qs := o.QueryTable(new(PatientSend))
  245. // 过滤
  246. cond := orm.NewCondition()
  247. cond = cond.And("T_State", T_State)
  248. if T_uid > 0 {
  249. cond = cond.And("T_uid", T_uid)
  250. }
  251. if T_pid > 0 {
  252. cond = cond.And("T_pid", T_pid)
  253. }
  254. if T_type > 0 {
  255. cond = cond.And("T_type", T_type)
  256. }
  257. if len(month) > 0 {
  258. cond = cond.And("CreateTime__startswith", month)
  259. }
  260. cnt, err := qs.SetCond((*orm2.Condition)(cond)).Count()
  261. if err != nil {
  262. logs.Error(lib.FuncName(), err)
  263. return
  264. }
  265. return cnt
  266. }