User.go 24 KB

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