User.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. package controllers
  2. import (
  3. "FollowUp_Notice/Nats/NatsServer"
  4. "FollowUp_Notice/conf"
  5. "FollowUp_Notice/http"
  6. "FollowUp_Notice/logs"
  7. "FollowUp_Notice/models/Account"
  8. "FollowUp_Notice/models/Patient"
  9. "FollowUp_Notice/models/System"
  10. "fmt"
  11. "git.baozhida.cn/ERP_libs/lib"
  12. beego "github.com/beego/beego/v2/server/web"
  13. "github.com/robfig/cron/v3"
  14. "github.com/shopspring/decimal"
  15. "github.com/xuri/excelize/v2"
  16. "math"
  17. "os"
  18. "strings"
  19. "time"
  20. )
  21. type UserController struct {
  22. beego.Controller
  23. User Account.User
  24. }
  25. func (c *UserController) Prepare() {
  26. if Account.User_r != nil {
  27. c.User = *Account.User_r
  28. }
  29. }
  30. // 验证登录
  31. func (c *UserController) Login_verification() {
  32. Admin_user := c.GetString("username")
  33. Admin_pass := c.GetString("password")
  34. type JSONS struct {
  35. //必须的大写开头
  36. Code int16
  37. Msg string
  38. Data interface{} // 泛型
  39. UserId int
  40. }
  41. err, user_r := Account.Read_User_verification(Admin_user, Admin_pass)
  42. if err != nil {
  43. c.Data["json"] = lib.JSONS{Code: 202, Msg: "用户名或密码错误!"}
  44. } else {
  45. User_tokey := Account.Add_Tokey(user_r.T_uuid)
  46. c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
  47. c.Data["json"] = JSONS{Code: 200, Msg: "OK!", Data: User_tokey, UserId: user_r.Id}
  48. System.Add_UserLogs_T(user_r.T_uuid, "用户", "用户登陆", lib.GetUserLoginInfo(c.Ctx))
  49. }
  50. c.ServeJSON()
  51. return
  52. }
  53. // --------------------------------------------------------------------------------------------------------------
  54. // 用户列表
  55. func (c *UserController) List() {
  56. // 分页参数 初始化
  57. page, _ := c.GetInt("page")
  58. if page < 1 {
  59. page = 1
  60. }
  61. page_z, _ := c.GetInt("page_z")
  62. if page_z < 1 {
  63. page_z = conf.Page_size
  64. }
  65. // 查询
  66. T_name := c.GetString("T_name")
  67. R_List, R_cnt := Account.Read_User_List(T_name, page, page_z)
  68. var r_jsons lib.R_JSONS
  69. r_jsons.Num = R_cnt
  70. r_jsons.Data = R_List
  71. r_jsons.Page = page
  72. r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
  73. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  74. c.ServeJSON()
  75. return
  76. }
  77. func (c *UserController) Get() {
  78. T_uuid := c.GetString("T_uuid")
  79. user, err := Account.Read_User_ByT_uuid(T_uuid)
  80. if err != nil {
  81. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  82. c.ServeJSON()
  83. return
  84. }
  85. var r_jsons lib.R_JSONS
  86. r_jsons.Data = Account.UserToUser_R(user)
  87. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  88. c.ServeJSON()
  89. return
  90. }
  91. // 个人信息
  92. func (c *UserController) Info() {
  93. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.UserToUser_R(c.User)}
  94. c.ServeJSON()
  95. return
  96. }
  97. // 添加用户信息
  98. func (c *UserController) Add() {
  99. T_user := c.GetString("T_user")
  100. T_pass := c.GetString("T_pass")
  101. T_phone := c.GetString("T_phone")
  102. T_arrears_notice, _ := c.GetInt("T_arrears_notice")
  103. T_State, _ := c.GetInt("T_State")
  104. if len(T_user) < 3 {
  105. c.Data["json"] = lib.JSONS{Code: 207, Msg: "用户名长度不足!"}
  106. c.ServeJSON()
  107. return
  108. }
  109. if len(T_pass) < 6 {
  110. c.Data["json"] = lib.JSONS{Code: 208, Msg: "密码异常!"}
  111. c.ServeJSON()
  112. return
  113. }
  114. temp, err := http.SmsTemplate_Post(T_user)
  115. if err != nil || temp.Status != "success" {
  116. c.Data["json"] = lib.JSONS{Code: 202, Msg: "创建短信模板失败"}
  117. c.ServeJSON()
  118. return
  119. }
  120. var_ := Account.User{
  121. T_user: T_user,
  122. T_pass: T_pass,
  123. T_phone: T_phone,
  124. T_template_id: temp.Template_id,
  125. T_arrears_notice: T_arrears_notice,
  126. T_State: T_State,
  127. }
  128. _, err = Account.Add_User(var_)
  129. if err != nil {
  130. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  131. c.ServeJSON()
  132. return
  133. }
  134. var_.T_pass = "******"
  135. System.Add_UserLogs_T(c.User.T_uuid, "用户", "新增", var_)
  136. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  137. c.ServeJSON()
  138. return
  139. }
  140. // 修改个人信息
  141. func (c *UserController) Post() {
  142. T_pass := c.GetString("T_pass")
  143. user := c.User
  144. if len(T_pass) > 0 {
  145. if len(T_pass) < 8 {
  146. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  147. c.ServeJSON()
  148. return
  149. }
  150. user.T_pass = T_pass
  151. }
  152. if err := Account.Update_User(user, "T_pass"); err != nil {
  153. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  154. c.ServeJSON()
  155. return
  156. }
  157. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改登录密码", "")
  158. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  159. c.ServeJSON()
  160. return
  161. }
  162. // 修改用户信息
  163. func (c *UserController) Edit() {
  164. T_uuid := c.GetString("T_uuid")
  165. T_pass := c.GetString("T_pass")
  166. T_phone := c.GetString("T_phone")
  167. T_arrears_notice, _ := c.GetInt("T_arrears_notice")
  168. T_State, _ := c.GetInt("T_State")
  169. var err error
  170. var user Account.User
  171. var cols []string
  172. if len(T_uuid) > 0 {
  173. user, err = Account.Read_User_ByT_uuid(T_uuid)
  174. if err != nil {
  175. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  176. c.ServeJSON()
  177. return
  178. }
  179. }
  180. if len(T_pass) > 0 {
  181. if len(T_pass) < 6 {
  182. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  183. c.ServeJSON()
  184. return
  185. }
  186. user.T_pass = T_pass
  187. cols = append(cols, "T_pass")
  188. }
  189. if len(T_phone) > 0 {
  190. user.T_phone = T_phone
  191. cols = append(cols, "T_phone")
  192. }
  193. if T_arrears_notice > 0 {
  194. user.T_arrears_notice = T_arrears_notice
  195. cols = append(cols, "T_arrears_notice")
  196. }
  197. if T_State > 0 {
  198. user.T_State = T_State
  199. cols = append(cols, "T_State")
  200. }
  201. if err = Account.Update_User(user, cols...); err != nil {
  202. c.Data["json"] = lib.JSONS{Code: 208, Msg: "修改失败!"}
  203. c.ServeJSON()
  204. return
  205. }
  206. user.T_pass = "******"
  207. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改个人信息", user)
  208. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  209. c.ServeJSON()
  210. return
  211. }
  212. // 删除用户信息
  213. func (c *UserController) Del() {
  214. T_uuid := c.GetString("T_uuid")
  215. if len(T_uuid) == 0 {
  216. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
  217. c.ServeJSON()
  218. return
  219. }
  220. user, err := Account.Read_User_ByT_uuid(T_uuid)
  221. if err != nil {
  222. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  223. c.ServeJSON()
  224. return
  225. }
  226. if user.Id == 1 {
  227. c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除超级管理员!"}
  228. c.ServeJSON()
  229. return
  230. }
  231. temp, err := http.SmsTemplate_Delete(user.T_template_id)
  232. if err != nil || temp.Status != "success" {
  233. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除短信模板失败"}
  234. c.ServeJSON()
  235. return
  236. }
  237. if err = Account.Delete_User(user); err != nil {
  238. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  239. c.ServeJSON()
  240. return
  241. }
  242. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  243. c.ServeJSON()
  244. return
  245. }
  246. // 充值
  247. func (c *UserController) Pay() {
  248. T_uuid := c.GetString("T_uuid")
  249. T_balance, _ := c.GetFloat("T_balance")
  250. if len(T_uuid) == 0 {
  251. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  252. c.ServeJSON()
  253. return
  254. }
  255. user, err := Account.Read_User_ByT_uuid(T_uuid)
  256. if err != nil {
  257. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  258. c.ServeJSON()
  259. return
  260. }
  261. T_money64, _ := decimal.NewFromFloat(float64(user.T_money) + T_balance).Round(2).Float64()
  262. user.T_money = float32(T_money64)
  263. err = Account.Update_User(user, "T_money")
  264. if err != nil {
  265. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  266. c.ServeJSON()
  267. return
  268. }
  269. // 添加充值记录
  270. bill := Account.UserBill{
  271. T_uid: user.Id,
  272. T_type: Account.Pay,
  273. T_bill: "充值",
  274. T_charging: float32(T_balance),
  275. T_balance: float32(T_money64),
  276. }
  277. _, err = Account.Add_UserBill(bill)
  278. if err != nil {
  279. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  280. c.ServeJSON()
  281. return
  282. }
  283. System.Add_UserLogs_T(c.User.T_uuid, "用户管理", "充值", user)
  284. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  285. c.ServeJSON()
  286. return
  287. }
  288. // 账单下载
  289. func (c *UserController) Bill() {
  290. // 分页参数 初始化
  291. page, _ := c.GetInt("page")
  292. if page < 1 {
  293. page = 1
  294. }
  295. page_z, _ := c.GetInt("page_z")
  296. if page_z < 1 {
  297. page_z = conf.Page_size
  298. }
  299. T_uuid := c.GetString("T_uuid")
  300. //T_type 1 充值 2扣费
  301. T_type, _ := c.GetInt("T_type")
  302. user, err := Account.Read_User_ByT_uuid(T_uuid)
  303. if err != nil {
  304. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  305. c.ServeJSON()
  306. return
  307. }
  308. CompanyBill_List, cnt := Account.Read_UserBill_List(user.Id, "", T_type, page, page_z)
  309. var r_jsons lib.R_JSONS
  310. r_jsons.Num = cnt
  311. r_jsons.Data = CompanyBill_List
  312. r_jsons.Page = page
  313. r_jsons.Page_size = int(math.Ceil(float64(cnt) / float64(page_z)))
  314. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  315. c.ServeJSON()
  316. return
  317. }
  318. // 账单下载
  319. func (c *UserController) Bill_Excel() {
  320. T_month := c.GetString("T_month")
  321. T_uuid := c.GetString("T_uuid")
  322. user, err := Account.Read_User_ByT_uuid(T_uuid)
  323. if err != nil {
  324. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  325. c.ServeJSON()
  326. return
  327. }
  328. filename := fmt.Sprintf("%s账单", user.T_user)
  329. if len(T_month) > 0 {
  330. _, err := time.Parse("2006-01", T_month)
  331. if err != nil {
  332. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  333. c.ServeJSON()
  334. return
  335. }
  336. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  337. filename = fmt.Sprintf("%s%s年%s月账单", c.User.T_user, year, month)
  338. }
  339. f := excelize.NewFile()
  340. Style1, _ := f.NewStyle(
  341. &excelize.Style{
  342. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  343. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  344. })
  345. Style2, _ := f.NewStyle(
  346. &excelize.Style{
  347. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  348. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  349. Border: []excelize.Border{
  350. {Type: "left", Color: "000000", Style: 1},
  351. {Type: "top", Color: "000000", Style: 1},
  352. {Type: "bottom", Color: "000000", Style: 1},
  353. {Type: "right", Color: "000000", Style: 1},
  354. },
  355. })
  356. f.MergeCell("Sheet1", "A1", "G1")
  357. f.SetRowStyle("Sheet1", 1, 1, Style1)
  358. f.SetCellValue("Sheet1", "A1", filename)
  359. f.SetRowHeight("Sheet1", 1, 30)
  360. f.SetCellStyle("Sheet1", "A2", "F2", Style2)
  361. f.SetRowHeight("Sheet1", 2, 25)
  362. // 这里设置表头
  363. f.SetCellValue("Sheet1", "A2", "编号")
  364. f.SetCellValue("Sheet1", "B2", "说明")
  365. f.SetCellValue("Sheet1", "C2", "扣费/充值")
  366. f.SetCellValue("Sheet1", "D2", "金额(元)")
  367. f.SetCellValue("Sheet1", "E2", "余额(元)")
  368. f.SetCellValue("Sheet1", "F2", "时间")
  369. // 设置列宽
  370. f.SetColWidth("Sheet1", "A", "A", 10)
  371. f.SetColWidth("Sheet1", "B", "B", 15)
  372. f.SetColWidth("Sheet1", "C", "C", 12)
  373. f.SetColWidth("Sheet1", "D", "D", 15)
  374. f.SetColWidth("Sheet1", "E", "E", 15)
  375. f.SetColWidth("Sheet1", "F", "F", 20)
  376. line := 2
  377. //T_type 1 充值 2扣费
  378. CompanyBill_List, _ := Account.Read_UserBill_List(user.Id, T_month, 0, 0, 9999)
  379. // 循环写入数据
  380. for i, v := range CompanyBill_List {
  381. line++
  382. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  383. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_bill)
  384. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_type)
  385. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_charging)
  386. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_balance)
  387. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CreateTime)
  388. }
  389. Style4, _ := f.NewStyle(
  390. &excelize.Style{
  391. Font: &excelize.Font{Size: 12, Family: "宋体"},
  392. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  393. Border: []excelize.Border{
  394. {Type: "left", Color: "000000", Style: 1},
  395. {Type: "top", Color: "000000", Style: 1},
  396. {Type: "bottom", Color: "000000", Style: 1},
  397. {Type: "right", Color: "000000", Style: 1},
  398. },
  399. })
  400. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("F%d", line), Style4)
  401. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  402. // 保存文件
  403. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  404. fmt.Println(err)
  405. }
  406. var url string
  407. //// 上传 OSS
  408. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  409. if !is {
  410. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  411. c.ServeJSON()
  412. return
  413. }
  414. //删除目录
  415. err = os.Remove("ofile/" + timeStr + ".xlsx")
  416. if err != nil {
  417. fmt.Println(err)
  418. }
  419. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  420. c.ServeJSON()
  421. return
  422. }
  423. // 用户定时任务
  424. func Cron_User() {
  425. //创建一个定时任务对象
  426. c := cron.New(cron.WithSeconds())
  427. //给对象增加定时任务
  428. //c.AddFunc("0 */1 * * * ?", Cron_User_Money_Bill)
  429. c.AddFunc("0 0 7 * * *", Cron_User_Money_Bill)
  430. //启动定时任务
  431. c.Start()
  432. defer c.Stop()
  433. //查询语句,阻塞,让main函数不退出,保持程序运行
  434. select {}
  435. }
  436. // 用户扣费,并生成账单,每天2点执行
  437. func Cron_User_Money_Bill() {
  438. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  439. logs.Info("开始进行" + yesterday + "用户账单扣费统计")
  440. userList, _ := Account.Read_User_List("", 0, 9999)
  441. for _, user := range userList {
  442. // 1 短信 2 电话
  443. smsCount := Patient.Read_PatientSend_Count(user.Id, 0, 1, yesterday)
  444. voiceCallCount := Patient.Read_PatientSend_Count(user.Id, 0, 2, yesterday)
  445. money := float64(smsCount)*conf.Sms_Fee + float64(voiceCallCount)*conf.VoiceCall_Fee
  446. if money == 0 {
  447. continue
  448. }
  449. var_ := Account.User{Id: user.Id, T_money: user.T_money - float32(money)}
  450. err := Account.Update_User(var_, "T_money")
  451. if err != nil {
  452. System.Add_SysLogs_T("用户扣费", "扣费失败", var_)
  453. }
  454. // 添加扣费记录
  455. bill := Account.UserBill{
  456. T_uid: user.Id,
  457. T_type: Account.FeeDeduction,
  458. T_bill: "扣费",
  459. T_charging: float32(money),
  460. T_balance: user.T_money - float32(money),
  461. }
  462. _, err = Account.Add_UserBill(bill)
  463. if err != nil {
  464. System.Add_SysLogs_T("用户扣费", "添加扣费记录失败", bill)
  465. }
  466. }
  467. logs.Info("用户账单扣费统计结束")
  468. }