User.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. package controllers
  2. import (
  3. "FollowUp_Notice/Nats/NatsServer"
  4. "FollowUp_Notice/conf"
  5. "FollowUp_Notice/http"
  6. "FollowUp_Notice/http/submail"
  7. "FollowUp_Notice/lib"
  8. "FollowUp_Notice/logs"
  9. "FollowUp_Notice/models/Account"
  10. "FollowUp_Notice/models/Illness"
  11. "FollowUp_Notice/models/Patient"
  12. "FollowUp_Notice/models/System"
  13. "encoding/json"
  14. "fmt"
  15. beego "github.com/beego/beego/v2/server/web"
  16. "github.com/robfig/cron/v3"
  17. "github.com/shopspring/decimal"
  18. "github.com/xuri/excelize/v2"
  19. "math"
  20. "os"
  21. "strings"
  22. "time"
  23. )
  24. type UserController struct {
  25. beego.Controller
  26. User Account.User
  27. }
  28. func (c *UserController) Prepare() {
  29. if Account.User_r != nil {
  30. c.User = *Account.User_r
  31. }
  32. }
  33. // 验证登录
  34. func (c *UserController) Login_verification() {
  35. Admin_user := c.GetString("username")
  36. Admin_pass := c.GetString("password")
  37. type JSONS struct {
  38. //必须的大写开头
  39. Code int16
  40. Msg string
  41. Data interface{} // 泛型
  42. UserId int
  43. }
  44. err, user_r := Account.Read_User_verification(Admin_user, Admin_pass)
  45. if err != nil {
  46. c.Data["json"] = lib.JSONS{Code: 202, Msg: "用户名或密码错误!"}
  47. } else {
  48. User_tokey := Account.Add_Tokey(user_r.T_uuid)
  49. c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
  50. c.Data["json"] = JSONS{Code: 200, Msg: "OK!", Data: User_tokey, UserId: user_r.Id}
  51. System.Add_UserLogs_T(user_r.T_uuid, "用户", "用户登陆", lib.GetUserLoginInfo(c.Ctx))
  52. }
  53. c.ServeJSON()
  54. return
  55. }
  56. // --------------------------------------------------------------------------------------------------------------
  57. // 用户列表
  58. func (c *UserController) List() {
  59. // 分页参数 初始化
  60. page, _ := c.GetInt("page")
  61. if page < 1 {
  62. page = 1
  63. }
  64. page_z, _ := c.GetInt("page_z")
  65. if page_z < 1 {
  66. page_z = conf.Page_size
  67. }
  68. // 查询
  69. T_name := c.GetString("T_name")
  70. R_List, R_cnt := Account.Read_User_List(T_name, page, page_z)
  71. var r_jsons lib.R_JSONS
  72. r_jsons.Num = R_cnt
  73. r_jsons.Data = R_List
  74. r_jsons.Page = page
  75. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  76. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  77. c.ServeJSON()
  78. return
  79. }
  80. func (c *UserController) Get() {
  81. T_uuid := c.GetString("T_uuid")
  82. user, err := Account.Read_User_ByT_uuid(T_uuid)
  83. if err != nil {
  84. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  85. c.ServeJSON()
  86. return
  87. }
  88. var r_jsons lib.R_JSONS
  89. r_jsons.Data = Account.UserToUser_R(user)
  90. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  91. c.ServeJSON()
  92. return
  93. }
  94. // 个人信息
  95. func (c *UserController) Info() {
  96. type Info struct {
  97. User Account.User_R
  98. UserPayMoney float32
  99. Notice struct {
  100. Sms struct {
  101. Success int64
  102. Fail int64
  103. }
  104. VoiceCall struct {
  105. Success int64
  106. Fail int64
  107. }
  108. Satisfaction struct {
  109. Success int64
  110. Fail int64
  111. }
  112. }
  113. }
  114. month := time.Now().Format("2006-01")
  115. var info Info
  116. info.User = Account.UserToUser_R(c.User)
  117. UserBill, _ := Account.Read_UserBill_Pay(c.User.Id)
  118. info.UserPayMoney = UserBill.T_balance
  119. info.Notice.Sms.Success = Patient.Read_PatientSend_Count_Month(c.User.Id, 0, 1, month, 1)
  120. info.Notice.Sms.Fail = Patient.Read_PatientSend_Count_Month(c.User.Id, 0, 1, month, 0)
  121. info.Notice.VoiceCall.Success = Patient.Read_PatientSend_Count_Month(c.User.Id, 0, 2, month, 1)
  122. info.Notice.VoiceCall.Fail = Patient.Read_PatientSend_Count_Month(c.User.Id, 0, 2, month, 0)
  123. info.Notice.Satisfaction.Success = Patient.Read_PatientSend_Count_Month(c.User.Id, 0, 3, month, 1)
  124. info.Notice.Satisfaction.Fail = Patient.Read_PatientSend_Count_Month(c.User.Id, 0, 3, month, 0)
  125. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: info}
  126. c.ServeJSON()
  127. return
  128. }
  129. // 添加用户信息
  130. func (c *UserController) Add() {
  131. T_user := c.GetString("T_user")
  132. T_pass := c.GetString("T_pass")
  133. T_phone := c.GetString("T_phone")
  134. T_arrears_notice, _ := c.GetInt("T_arrears_notice")
  135. T_State, _ := c.GetInt("T_State")
  136. if len(T_user) < 3 {
  137. c.Data["json"] = lib.JSONS{Code: 207, Msg: "用户名长度不足!"}
  138. c.ServeJSON()
  139. return
  140. }
  141. if len(T_pass) < 6 {
  142. c.Data["json"] = lib.JSONS{Code: 202, Msg: "密码异常!"}
  143. c.ServeJSON()
  144. return
  145. }
  146. var_ := Account.User{
  147. T_user: T_user,
  148. T_pass: T_pass,
  149. T_phone: T_phone,
  150. T_arrears_notice: T_arrears_notice,
  151. T_State: T_State,
  152. }
  153. _, err := Account.Add_User(var_)
  154. if err != nil {
  155. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  156. c.ServeJSON()
  157. return
  158. }
  159. var_.T_pass = "******"
  160. System.Add_UserLogs_T(c.User.T_uuid, "用户", "新增", var_)
  161. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  162. c.ServeJSON()
  163. return
  164. }
  165. // 修改个人信息
  166. func (c *UserController) Post() {
  167. T_pass := c.GetString("T_pass")
  168. user := c.User
  169. if len(T_pass) > 0 {
  170. if len(T_pass) < 8 {
  171. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  172. c.ServeJSON()
  173. return
  174. }
  175. user.T_pass = T_pass
  176. }
  177. if err := Account.Update_User(user, "T_pass"); err != nil {
  178. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  179. c.ServeJSON()
  180. return
  181. }
  182. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改登录密码", "")
  183. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  184. c.ServeJSON()
  185. return
  186. }
  187. // 修改用户信息
  188. func (c *UserController) Edit() {
  189. T_uuid := c.GetString("T_uuid")
  190. T_pass := c.GetString("T_pass")
  191. T_phone := c.GetString("T_phone")
  192. T_arrears_notice, _ := c.GetInt("T_arrears_notice")
  193. T_State, _ := c.GetInt("T_State")
  194. var err error
  195. var user Account.User
  196. var cols []string
  197. if len(T_uuid) > 0 {
  198. user, err = Account.Read_User_ByT_uuid(T_uuid)
  199. if err != nil {
  200. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  201. c.ServeJSON()
  202. return
  203. }
  204. }
  205. if len(T_pass) > 0 {
  206. if len(T_pass) < 6 {
  207. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  208. c.ServeJSON()
  209. return
  210. }
  211. user.T_pass = T_pass
  212. cols = append(cols, "T_pass")
  213. }
  214. if len(T_phone) > 0 {
  215. user.T_phone = T_phone
  216. cols = append(cols, "T_phone")
  217. }
  218. if T_arrears_notice > 0 {
  219. user.T_arrears_notice = T_arrears_notice
  220. cols = append(cols, "T_arrears_notice")
  221. }
  222. if T_State > 0 {
  223. user.T_State = T_State
  224. cols = append(cols, "T_State")
  225. }
  226. if err = Account.Update_User(user, cols...); err != nil {
  227. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  228. c.ServeJSON()
  229. return
  230. }
  231. user.T_pass = "******"
  232. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改个人信息", user)
  233. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  234. c.ServeJSON()
  235. return
  236. }
  237. // 删除用户信息
  238. func (c *UserController) Del() {
  239. T_uuid := c.GetString("T_uuid")
  240. if len(T_uuid) == 0 {
  241. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
  242. c.ServeJSON()
  243. return
  244. }
  245. user, err := Account.Read_User_ByT_uuid(T_uuid)
  246. if err != nil {
  247. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  248. c.ServeJSON()
  249. return
  250. }
  251. if user.Id == 1 {
  252. c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除超级管理员!"}
  253. c.ServeJSON()
  254. return
  255. }
  256. cnt := Illness.Read_IllnessNotice_Count_ByT_uid(user.Id)
  257. if cnt > 0 {
  258. c.Data["json"] = lib.JSONS{Code: 202, Msg: "请先删除短信通知模版!"}
  259. c.ServeJSON()
  260. return
  261. }
  262. if err = Account.Delete_User(user); err != nil {
  263. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  268. c.ServeJSON()
  269. return
  270. }
  271. // 充值
  272. func (c *UserController) Pay() {
  273. T_uuid := c.GetString("T_uuid")
  274. T_balance, _ := c.GetFloat("T_balance")
  275. if len(T_uuid) == 0 {
  276. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  277. c.ServeJSON()
  278. return
  279. }
  280. user, err := Account.Read_User_ByT_uuid(T_uuid)
  281. if err != nil {
  282. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  283. c.ServeJSON()
  284. return
  285. }
  286. T_money64, _ := decimal.NewFromFloat(float64(user.T_money) + T_balance).Round(2).Float64()
  287. user.T_money = float32(T_money64)
  288. err = Account.Update_User(user, "T_money")
  289. if err != nil {
  290. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  291. c.ServeJSON()
  292. return
  293. }
  294. // 添加充值记录
  295. bill := Account.UserBill{
  296. T_uid: user.Id,
  297. T_type: Account.Pay,
  298. T_bill: "充值",
  299. T_charging: float32(T_balance),
  300. T_balance: float32(T_money64),
  301. }
  302. _, err = Account.Add_UserBill(bill)
  303. if err != nil {
  304. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  305. c.ServeJSON()
  306. return
  307. }
  308. user.T_pass = "******"
  309. System.Add_UserLogs_T(c.User.T_uuid, "用户管理", "充值", user)
  310. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  311. c.ServeJSON()
  312. return
  313. }
  314. // 账单下载
  315. func (c *UserController) Bill() {
  316. // 分页参数 初始化
  317. page, _ := c.GetInt("page")
  318. if page < 1 {
  319. page = 1
  320. }
  321. page_z, _ := c.GetInt("page_z")
  322. if page_z < 1 {
  323. page_z = conf.Page_size
  324. }
  325. T_uuid := c.GetString("T_uuid")
  326. //T_type 1 充值 2扣费
  327. T_type, _ := c.GetInt("T_type")
  328. user, err := Account.Read_User_ByT_uuid(T_uuid)
  329. if err != nil {
  330. user = c.User
  331. }
  332. Bill_List, cnt := Account.Read_UserBill_List(user.Id, "", T_type, page, page_z)
  333. var r_jsons lib.R_JSONS
  334. r_jsons.Num = cnt
  335. r_jsons.Data = Bill_List
  336. r_jsons.Page = page
  337. r_jsons.Page_size = int(math.Ceil(float64(cnt) / float64(page_z)))
  338. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  339. c.ServeJSON()
  340. return
  341. }
  342. // 账单下载
  343. func (c *UserController) Bill_Excel() {
  344. T_month := c.GetString("T_month")
  345. T_uuid := c.GetString("T_uuid")
  346. user, err := Account.Read_User_ByT_uuid(T_uuid)
  347. if err != nil {
  348. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  349. c.ServeJSON()
  350. return
  351. }
  352. filename := fmt.Sprintf("%s账单", user.T_user)
  353. if len(T_month) > 0 {
  354. _, err := time.Parse("2006-01", T_month)
  355. if err != nil {
  356. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  357. c.ServeJSON()
  358. return
  359. }
  360. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  361. filename = fmt.Sprintf("%s%s年%s月账单", c.User.T_user, year, month)
  362. }
  363. f := excelize.NewFile()
  364. Style1, _ := f.NewStyle(
  365. &excelize.Style{
  366. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  367. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  368. })
  369. Style2, _ := f.NewStyle(
  370. &excelize.Style{
  371. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  372. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  373. Border: []excelize.Border{
  374. {Type: "left", Color: "000000", Style: 1},
  375. {Type: "top", Color: "000000", Style: 1},
  376. {Type: "bottom", Color: "000000", Style: 1},
  377. {Type: "right", Color: "000000", Style: 1},
  378. },
  379. })
  380. f.MergeCell("Sheet1", "A1", "G1")
  381. f.SetRowStyle("Sheet1", 1, 1, Style1)
  382. f.SetCellValue("Sheet1", "A1", filename)
  383. f.SetRowHeight("Sheet1", 1, 30)
  384. f.SetCellStyle("Sheet1", "A2", "F2", Style2)
  385. f.SetRowHeight("Sheet1", 2, 25)
  386. // 这里设置表头
  387. f.SetCellValue("Sheet1", "A2", "编号")
  388. f.SetCellValue("Sheet1", "B2", "消费项目")
  389. f.SetCellValue("Sheet1", "C2", "扣费/充值")
  390. f.SetCellValue("Sheet1", "D2", "金额(元)")
  391. f.SetCellValue("Sheet1", "E2", "余额(元)")
  392. f.SetCellValue("Sheet1", "F2", "时间")
  393. // 设置列宽
  394. f.SetColWidth("Sheet1", "A", "A", 10)
  395. f.SetColWidth("Sheet1", "B", "B", 15)
  396. f.SetColWidth("Sheet1", "C", "C", 12)
  397. f.SetColWidth("Sheet1", "D", "D", 15)
  398. f.SetColWidth("Sheet1", "E", "E", 15)
  399. f.SetColWidth("Sheet1", "F", "F", 20)
  400. line := 2
  401. //T_type 1 充值 2扣费
  402. Bill_List, _ := Account.Read_UserBill_List(user.Id, T_month, 0, 0, 9999)
  403. // 循环写入数据
  404. for i, v := range Bill_List {
  405. line++
  406. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  407. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_bill)
  408. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_type)
  409. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_charging)
  410. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_balance)
  411. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CreateTime)
  412. }
  413. Style4, _ := f.NewStyle(
  414. &excelize.Style{
  415. Font: &excelize.Font{Size: 12, Family: "宋体"},
  416. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  417. Border: []excelize.Border{
  418. {Type: "left", Color: "000000", Style: 1},
  419. {Type: "top", Color: "000000", Style: 1},
  420. {Type: "bottom", Color: "000000", Style: 1},
  421. {Type: "right", Color: "000000", Style: 1},
  422. },
  423. })
  424. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("F%d", line), Style4)
  425. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  426. // 保存文件
  427. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  428. fmt.Println(err)
  429. }
  430. var url string
  431. //// 上传 OSS
  432. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  433. if !is {
  434. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  435. c.ServeJSON()
  436. return
  437. }
  438. //删除目录
  439. err = os.Remove("ofile/" + timeStr + ".xlsx")
  440. if err != nil {
  441. fmt.Println(err)
  442. }
  443. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  444. c.ServeJSON()
  445. return
  446. }
  447. // 通知记录
  448. func (c *UserController) Send() {
  449. // 分页参数 初始化
  450. page, _ := c.GetInt("page")
  451. T_month := c.GetString("T_month")
  452. if page < 1 {
  453. page = 1
  454. }
  455. page_z, _ := c.GetInt("page_z")
  456. if page_z < 1 {
  457. page_z = conf.Page_size
  458. }
  459. T_uuid := c.GetString("T_uuid")
  460. user, err := Account.Read_User_ByT_uuid(T_uuid)
  461. if err != nil {
  462. user = c.User
  463. }
  464. T_patient_uuid := c.GetString("T_patient_uuid")
  465. T_patient_Id := 0
  466. if len(T_patient_uuid) > 0 {
  467. patient, err := Patient.Read_Patient_ByT_uuid(T_patient_uuid)
  468. if err != nil {
  469. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_patient_uuid Err!"}
  470. c.ServeJSON()
  471. return
  472. }
  473. T_patient_Id = patient.Id
  474. }
  475. // 1 短信 2 电话 3 满意度调查
  476. T_type, _ := c.GetInt("T_type")
  477. send_List, cnt := Patient.Read_PatientSend_List(user.Id, T_patient_Id, T_type, T_month, page, page_z)
  478. var r_jsons lib.R_JSONS
  479. r_jsons.Num = cnt
  480. r_jsons.Data = send_List
  481. r_jsons.Page = page
  482. r_jsons.Page_size = int(math.Ceil(float64(cnt) / float64(page_z)))
  483. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  484. c.ServeJSON()
  485. return
  486. }
  487. // 通知记录下载
  488. func (c *UserController) Send_Excel() {
  489. T_month := c.GetString("T_month")
  490. T_uuid := c.GetString("T_uuid")
  491. T_type, _ := c.GetInt("T_type")
  492. user, err := Account.Read_User_ByT_uuid(T_uuid)
  493. if err != nil {
  494. user = c.User
  495. }
  496. T_patient_uuid := c.GetString("T_patient_uuid")
  497. T_patient_Id := 0
  498. if len(T_patient_uuid) > 0 {
  499. patient, err := Patient.Read_Patient_ByT_uuid(T_patient_uuid)
  500. if err != nil {
  501. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_patient_uuid Err!"}
  502. c.ServeJSON()
  503. return
  504. }
  505. T_patient_Id = patient.Id
  506. }
  507. var T_type_str, unit string
  508. if T_type == 1 {
  509. T_type_str = "短信"
  510. unit = "条"
  511. }
  512. if T_type == 2 {
  513. T_type_str = "电话"
  514. unit = "分钟"
  515. }
  516. filename := fmt.Sprintf("%s%s通知记录", user.T_user, T_type_str)
  517. if len(T_month) > 0 {
  518. _, err := time.Parse("2006-01", T_month)
  519. if err != nil {
  520. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  521. c.ServeJSON()
  522. return
  523. }
  524. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  525. filename = fmt.Sprintf("%s%s年%s月%s通知记录", user.T_user, T_type_str, year, month)
  526. }
  527. f := excelize.NewFile()
  528. Style1, _ := f.NewStyle(
  529. &excelize.Style{
  530. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  531. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  532. })
  533. Style2, _ := f.NewStyle(
  534. &excelize.Style{
  535. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  536. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  537. Border: []excelize.Border{
  538. {Type: "left", Color: "000000", Style: 1},
  539. {Type: "top", Color: "000000", Style: 1},
  540. {Type: "bottom", Color: "000000", Style: 1},
  541. {Type: "right", Color: "000000", Style: 1},
  542. },
  543. })
  544. f.MergeCell("Sheet1", "A1", "F1")
  545. f.SetRowStyle("Sheet1", 1, 1, Style1)
  546. f.SetCellValue("Sheet1", "A1", filename)
  547. f.SetRowHeight("Sheet1", 1, 30)
  548. f.SetCellStyle("Sheet1", "A2", "F2", Style2)
  549. f.SetRowHeight("Sheet1", 2, 25)
  550. // 这里设置表头
  551. f.SetCellValue("Sheet1", "A2", "编号")
  552. f.SetCellValue("Sheet1", "B2", "通知电话")
  553. f.SetCellValue("Sheet1", "C2", "通知方式")
  554. f.SetCellValue("Sheet1", "D2", "通知状态")
  555. f.SetCellValue("Sheet1", "E2", fmt.Sprintf("计费条数(%s)", unit))
  556. f.SetCellValue("Sheet1", "F2", "时间")
  557. // 设置列宽
  558. f.SetColWidth("Sheet1", "A", "A", 10)
  559. f.SetColWidth("Sheet1", "B", "B", 15)
  560. f.SetColWidth("Sheet1", "C", "C", 12)
  561. f.SetColWidth("Sheet1", "D", "D", 12)
  562. f.SetColWidth("Sheet1", "E", "E", 15)
  563. f.SetColWidth("Sheet1", "F", "F", 30)
  564. line := 2
  565. //T_type 1 充值 2扣费
  566. send_List, _ := Patient.Read_PatientSend_List(user.Id, T_patient_Id, T_type, T_month, 0, 9999)
  567. var T_State_str string
  568. // 循环写入数据
  569. for i, v := range send_List {
  570. line++
  571. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  572. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_phone)
  573. if v.T_type == 1 {
  574. T_type_str = "短信"
  575. }
  576. if v.T_type == 2 {
  577. T_type_str = "电话"
  578. }
  579. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), T_type_str)
  580. if v.T_State == 1 {
  581. T_State_str = "成功"
  582. }
  583. if v.T_type == 0 {
  584. T_State_str = "失败"
  585. }
  586. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), T_State_str)
  587. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_fee_num)
  588. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CreateTime)
  589. }
  590. Style4, _ := f.NewStyle(
  591. &excelize.Style{
  592. Font: &excelize.Font{Size: 12, Family: "宋体"},
  593. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  594. Border: []excelize.Border{
  595. {Type: "left", Color: "000000", Style: 1},
  596. {Type: "top", Color: "000000", Style: 1},
  597. {Type: "bottom", Color: "000000", Style: 1},
  598. {Type: "right", Color: "000000", Style: 1},
  599. },
  600. })
  601. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("F%d", line), Style4)
  602. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  603. // 保存文件
  604. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  605. fmt.Println(err)
  606. }
  607. var url string
  608. //// 上传 OSS
  609. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  610. if !is {
  611. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  612. c.ServeJSON()
  613. return
  614. }
  615. //删除目录
  616. err = os.Remove("ofile/" + timeStr + ".xlsx")
  617. if err != nil {
  618. fmt.Println(err)
  619. }
  620. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  621. c.ServeJSON()
  622. return
  623. }
  624. // 通知记录
  625. func (c *UserController) Send_Test() {
  626. // 1 短信 2 电话
  627. T_type, _ := c.GetInt("T_type")
  628. T_phone := c.GetString("T_phone")
  629. if c.User.Id != 1 {
  630. c.Data["json"] = lib.JSONS{Code: 202, Msg: "没有权限!"}
  631. c.ServeJSON()
  632. return
  633. }
  634. //发送短信通知
  635. if T_type == 1 {
  636. res, err := submail.SmsXSend("mDZSZ3", T_phone, "张三", time.Now().AddDate(0, 0, 1).Format("2006年01月02日"))
  637. if err != nil {
  638. c.Data["json"] = lib.JSONS{Code: 200, Msg: "短信发送失败!"}
  639. c.ServeJSON()
  640. return
  641. }
  642. // 保存短信发送记录
  643. smsSend := Patient.PatientSend{
  644. T_uid: c.User.Id,
  645. T_pid: 0,
  646. T_phone: T_phone,
  647. T_type: 1,
  648. T_id: res.Send_id,
  649. T_remark: res.Status,
  650. T_fee: res.Fee,
  651. T_code: res.Code,
  652. T_State: 1,
  653. }
  654. if res.Status == "error" {
  655. smsSend.T_State = 0
  656. }
  657. _, err = Patient.Add_PatientSend(smsSend)
  658. if err != nil {
  659. System.Add_SysLogs_T("复诊通知", "添加短信发送记录失败", smsSend)
  660. }
  661. if res.Status == "error" {
  662. c.Data["json"] = lib.JSONS{Code: 200, Msg: "短信发送失败!"}
  663. c.ServeJSON()
  664. return
  665. }
  666. }
  667. if T_type == 2 {
  668. // 华为语音通知
  669. //playInfoList := http.GetPlayInfoList(conf.VoiceCall_Template, []string{"某某医院消化内科", "张三", time.Now().AddDate(0, 0, 1).Format("2006/01/02")})
  670. //res, err := http.VoiceNotifyAPI(conf.VoiceCall_Phone, "+86"+T_phone, playInfoList)
  671. //if err != nil {
  672. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "电话通知失败!"}
  673. // c.ServeJSON()
  674. // return
  675. //}
  676. // 保存短信发送记录
  677. //send := Patient.PatientSend{
  678. // T_uid: c.User.Id,
  679. // T_pid: 0,
  680. // T_phone: T_phone,
  681. // T_type: 2,
  682. // T_id: res.SessionId,
  683. // T_code: -1,
  684. // T_State: 0,
  685. //}
  686. //
  687. //_, err = Patient.Add_PatientSend(send)
  688. //if err != nil {
  689. // System.Add_SysLogs_T("复诊通知", "添加发送记录失败", send)
  690. //}
  691. //
  692. //if res.Resultcode != "0" {
  693. // c.Data["json"] = lib.JSONS{Code: 202, Msg: "电话通知失败!"}
  694. // c.ServeJSON()
  695. // return
  696. //}
  697. res, err := submail.VoiceXSend(T_phone, "某某医院某某科室", "张三", time.Now().AddDate(0, 0, 1).Format("2006年01月02日"))
  698. if err != nil {
  699. c.Data["json"] = lib.JSONS{Code: 202, Msg: "电话通知失败!"}
  700. c.ServeJSON()
  701. return
  702. }
  703. // 保存短信发送记录
  704. send := Patient.PatientSend{
  705. T_uid: c.User.Id,
  706. T_pid: 0,
  707. T_phone: T_phone,
  708. T_type: 2,
  709. T_id: res.Send_id,
  710. T_fee: res.Fee,
  711. T_code: res.Code,
  712. T_State: 1,
  713. }
  714. if res.Status == "error" {
  715. send.T_State = 0
  716. }
  717. _, err = Patient.Add_PatientSend(send)
  718. if err != nil {
  719. System.Add_SysLogs_T("复诊通知", "添加语音发送记录失败", send)
  720. }
  721. if res.Status == "error" {
  722. c.Data["json"] = lib.JSONS{Code: 202, Msg: "语音通知失败!"}
  723. c.ServeJSON()
  724. return
  725. }
  726. }
  727. // 发送满意度调查短信
  728. if T_type == 3 {
  729. playInfoList := http.GetSatisfactionPlayInfoList([]string{"张三", "某某医院消化内科"})
  730. res, err := http.VoiceNotifyAPI(conf.VoiceCall_Phone, "+86"+T_phone, playInfoList)
  731. if err != nil {
  732. c.Data["json"] = lib.JSONS{Code: 202, Msg: "电话通知失败!"}
  733. c.ServeJSON()
  734. return
  735. }
  736. // 保存电话通知发送记录
  737. send := Patient.PatientSend{
  738. T_uid: c.User.Id,
  739. T_pid: 0,
  740. T_phone: T_phone,
  741. T_type: 3,
  742. T_id: res.SessionId,
  743. T_code: res.Resultcode,
  744. T_State: 0,
  745. }
  746. _, err = Patient.Add_PatientSend(send)
  747. if err != nil {
  748. System.Add_SysLogs_T("复诊通知", "添加发送记录失败", send)
  749. }
  750. if res.Resultcode != "0" {
  751. c.Data["json"] = lib.JSONS{Code: 202, Msg: "满意度调查失败!"}
  752. c.ServeJSON()
  753. return
  754. }
  755. }
  756. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  757. c.ServeJSON()
  758. return
  759. }
  760. // 获取微信支付二维码
  761. func (c *UserController) Get_Weixin_QR_Code() {
  762. Total, _ := c.GetFloat("Total")
  763. Title := "复诊通知" + c.User.T_user
  764. res, err := http.PayTransactionNative(Title, Total)
  765. if err != nil {
  766. c.Data["json"] = lib.JSONS{Code: 202, Msg: err.Error()}
  767. c.ServeJSON()
  768. return
  769. }
  770. if res.Code != 200 {
  771. c.Data["json"] = lib.JSONS{Code: 202, Msg: res.Message}
  772. c.ServeJSON()
  773. return
  774. }
  775. var_ := Account.UserPayOrder{
  776. T_uid: c.User.Id,
  777. T_type: "微信",
  778. T_title: Title,
  779. T_total: Total,
  780. T_order_no: res.OrderNo,
  781. }
  782. _, err = Account.Add_UserPayOrder(var_)
  783. if err != nil {
  784. c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
  785. c.ServeJSON()
  786. return
  787. }
  788. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: res}
  789. c.ServeJSON()
  790. return
  791. }
  792. // 微信支付回调
  793. func (c *UserController) Weixin_Notify() {
  794. type RequestBody struct {
  795. TradeNo string
  796. Status string
  797. }
  798. type JSON struct {
  799. Code int
  800. Message string
  801. }
  802. logs.Info("Body:", string(c.Ctx.Input.RequestBody))
  803. //解密
  804. adc_str, err := lib.AesDeCrypt(c.Ctx.Input.RequestBody, []byte(conf.Weixin_PwdKey))
  805. //adc_str, _ := lib.AesDeCrypt(buf[0:n], []byte(conf.Weixin_PwdKey))
  806. var body RequestBody
  807. logs.Info("data================ ", string(adc_str))
  808. err = json.Unmarshal(adc_str, &body)
  809. if err != nil {
  810. c.Data["json"] = JSON{Code: 202, Message: "json.Unmarshal is err:" + err.Error()}
  811. c.ServeJSON()
  812. }
  813. UserPayOrder, err := Account.Get_UserPayOrder_ByT_order_no(body.TradeNo)
  814. if err != nil {
  815. c.Data["json"] = JSON{Code: 202, Message: "查询失败!"}
  816. c.ServeJSON()
  817. return
  818. }
  819. //交易状态
  820. //SUCCESS:支付成功
  821. //REFUND:转入退款
  822. //NOTPAY:未支付
  823. //CLOSED:已关闭
  824. //REVOKED:已撤销(付款码支付)
  825. //USERPAYING:用户支付中(付款码支付)
  826. //PAYERROR:支付失败(其他原因,如银行返回失败)
  827. UserPayOrder.T_status = body.Status
  828. err = Account.Update_UserPayOrder(UserPayOrder, "T_status")
  829. if err != nil {
  830. c.Data["json"] = JSON{Code: 202, Message: "更新状态失败!"}
  831. c.ServeJSON()
  832. return
  833. }
  834. // 添加充值记录
  835. // 余额加上充值金额
  836. if body.Status == "SUCCESS" {
  837. T_money64, _ := decimal.NewFromFloat(float64(c.User.T_money) + UserPayOrder.T_total).Round(2).Float64()
  838. c.User.T_money = float32(T_money64)
  839. // 修改用户余额
  840. err = Account.Update_User(c.User, "T_money")
  841. if err != nil {
  842. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  843. c.ServeJSON()
  844. return
  845. }
  846. // 添加充值记录
  847. bill := Account.UserBill{
  848. T_uid: c.User.Id,
  849. T_type: Account.Pay,
  850. T_bill: "充值(微信)",
  851. T_charging: float32(UserPayOrder.T_total),
  852. T_balance: float32(T_money64),
  853. }
  854. _, err = Account.Add_UserBill(bill)
  855. if err != nil {
  856. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  857. c.ServeJSON()
  858. return
  859. }
  860. user := c.User
  861. user.T_pass = "******"
  862. System.Add_UserLogs_T(c.User.T_uuid, "用户管理", "充值", user)
  863. }
  864. c.Data["json"] = JSON{Code: 200, Message: "成功"}
  865. c.ServeJSON()
  866. return
  867. }
  868. // 获取微信支付订单状态
  869. func (c *UserController) GetWxOrderState() {
  870. OrderNo := c.GetString("OrderNo")
  871. UserPayOrder, err := Account.Get_UserPayOrder_ByT_order_no(OrderNo)
  872. if err != nil {
  873. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  874. c.ServeJSON()
  875. return
  876. }
  877. c.Data["json"] = lib.JSONS{Code: 200, Msg: "成功", Data: UserPayOrder}
  878. c.ServeJSON()
  879. return
  880. }
  881. // 满意度统计
  882. func (c *UserController) Satisfaction_Stats() {
  883. list, err := Patient.Read_PatientSatisfaction_ByT_uid(c.User.Id)
  884. if err != nil {
  885. c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
  886. c.ServeJSON()
  887. return
  888. }
  889. type Json struct {
  890. Count map[string]map[string]int
  891. Title map[string]string
  892. }
  893. countMap := make(map[string]map[string]int)
  894. for _, v := range list {
  895. updateCountMap(countMap, "question1", v.T_question1)
  896. updateCountMap(countMap, "question2", v.T_question2)
  897. updateCountMap(countMap, "question3", v.T_question3)
  898. updateCountMap(countMap, "question4", v.T_question4)
  899. updateCountMap(countMap, "question5", v.T_question5)
  900. }
  901. title := make(map[string]string)
  902. title["question1"] = Patient.Question1
  903. title["question2"] = Patient.Question2
  904. title["question3"] = Patient.Question3
  905. title["question4"] = Patient.Question4
  906. title["question5"] = Patient.Question5
  907. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Json{Count: countMap, Title: title}}
  908. c.ServeJSON()
  909. return
  910. }
  911. // 辅助函数:更新选项计数
  912. func updateCountMap(countMap map[string]map[string]int, question string, value string) {
  913. if question == "question2" {
  914. if strings.Contains("12", value) {
  915. if countMap[question] == nil {
  916. countMap[question] = map[string]int{
  917. "count": 0,
  918. "1": 0,
  919. "2": 0,
  920. }
  921. }
  922. countMap[question]["count"]++
  923. countMap[question][value]++
  924. }
  925. } else {
  926. if strings.Contains("123", value) {
  927. if countMap[question] == nil {
  928. countMap[question] = map[string]int{
  929. "count": 0,
  930. "1": 0,
  931. "2": 0,
  932. "3": 0,
  933. }
  934. }
  935. countMap[question]["count"]++
  936. countMap[question][value]++
  937. }
  938. }
  939. }
  940. // 满意度列表
  941. func (c *UserController) Satisfaction_List() {
  942. // 分页参数 初始化
  943. page, _ := c.GetInt("page")
  944. if page < 1 {
  945. page = 1
  946. }
  947. page_z, _ := c.GetInt("page_z")
  948. if page_z < 1 {
  949. page_z = conf.Page_size
  950. }
  951. Patient.Read_Patient_All_Map(c.User.Id)
  952. list, cnt := Patient.Read_PatientSatisfaction_List(c.User.Id, page, page_z)
  953. var r_jsons lib.R_JSONS
  954. r_jsons.Num = cnt
  955. r_jsons.Data = list
  956. r_jsons.Page = page
  957. r_jsons.Page_size = int(math.Ceil(float64(cnt) / float64(page_z)))
  958. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  959. c.ServeJSON()
  960. return
  961. }
  962. // 用户定时任务
  963. func Cron_User() {
  964. //创建一个定时任务对象
  965. c := cron.New(cron.WithSeconds())
  966. //给对象增加定时任务
  967. //c.AddFunc("0 */1 * * * ?", Cron_User_Money_Bill)
  968. c.AddFunc("0 0 7 * * *", Cron_User_Money_Bill)
  969. //启动定时任务
  970. c.Start()
  971. defer c.Stop()
  972. //查询语句,阻塞,让main函数不退出,保持程序运行
  973. select {}
  974. }
  975. // 用户扣费,并生成账单,每天7点执行
  976. func Cron_User_Money_Bill() {
  977. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  978. logs.Info("开始进行" + yesterday + "用户账单扣费统计")
  979. userList, _ := Account.Read_User_List("", 0, 9999)
  980. for _, user := range userList {
  981. // 1 短信(条) 2 电话
  982. if user.Id != 2 {
  983. continue
  984. }
  985. smsCount := Patient.Read_PatientSend_Count_Yesterday(user.Id, 0, 1, yesterday)
  986. voiceCallCount := Patient.Read_PatientSend_Count_Yesterday(user.Id, 0, 2, yesterday)
  987. satisfactionCount := Patient.Read_PatientSend_Satisfaction_Count_Yesterday(user.Id, 0, yesterday)
  988. money := smsCount*conf.SUBMAIL_Sms_Fee + voiceCallCount/0.09*conf.SUBMAIL_VoiceCall_Fee + float64(satisfactionCount)*conf.VoiceCall_Fee
  989. if money == 0 {
  990. continue
  991. }
  992. sendMsg := false
  993. moneyFlag := ""
  994. var_ := Account.User{Id: user.Id, T_money: user.T_money - float32(money)}
  995. if user.T_money >= 100 && var_.T_money < 100 {
  996. sendMsg = true
  997. moneyFlag = "不足100元"
  998. } else if user.T_money >= 10 && var_.T_money < 10 {
  999. sendMsg = true
  1000. moneyFlag = "不足10元"
  1001. } else if user.T_money >= 0 && var_.T_money < 0 {
  1002. sendMsg = true
  1003. moneyFlag = "欠费"
  1004. }
  1005. err := Account.Update_User(var_, "T_money")
  1006. if err != nil {
  1007. System.Add_SysLogs_T("用户扣费", "扣费失败", var_)
  1008. }
  1009. // 添加扣费记录
  1010. bill := Account.UserBill{
  1011. T_uid: user.Id,
  1012. T_type: Account.FeeDeduction,
  1013. T_bill: yesterday + "通知自动扣除",
  1014. T_charging: float32(money),
  1015. T_balance: user.T_money - float32(money),
  1016. }
  1017. _, err = Account.Add_UserBill(bill)
  1018. if err != nil {
  1019. System.Add_SysLogs_T("用户扣费", "添加扣费记录失败", bill)
  1020. }
  1021. if sendMsg {
  1022. _, err = submail.SmsXSendBill(user.T_phone, user.T_user, moneyFlag)
  1023. info := fmt.Sprintf("T_uid:%v, T_phone:%v, T_user:%v, moneyFlag:%v", user.Id, user.T_phone, user.T_user, moneyFlag)
  1024. if err != nil {
  1025. System.Add_SysLogs_T("复诊通知-账单", "短信通知失败", info)
  1026. } else {
  1027. System.Add_SysLogs_T("复诊通知-账单", "短信通知成功", info)
  1028. }
  1029. }
  1030. }
  1031. logs.Info("用户账单扣费统计结束")
  1032. }