User.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. type Info struct {
  94. User Account.User_R
  95. Notice struct {
  96. Sms int64
  97. VoiceCall int64
  98. }
  99. }
  100. month := time.Now().Format("2006-01")
  101. var info Info
  102. info.User = Account.UserToUser_R(c.User)
  103. info.Notice.Sms = Patient.Read_PatientSend_Count_Month(c.User.Id, 0, 1, month)
  104. info.Notice.VoiceCall = Patient.Read_PatientSend_Count_Month(c.User.Id, 0, 2, month)
  105. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: info}
  106. c.ServeJSON()
  107. return
  108. }
  109. // 添加用户信息
  110. func (c *UserController) Add() {
  111. T_user := c.GetString("T_user")
  112. T_pass := c.GetString("T_pass")
  113. T_phone := c.GetString("T_phone")
  114. T_arrears_notice, _ := c.GetInt("T_arrears_notice")
  115. T_State, _ := c.GetInt("T_State")
  116. if len(T_user) < 3 {
  117. c.Data["json"] = lib.JSONS{Code: 207, Msg: "用户名长度不足!"}
  118. c.ServeJSON()
  119. return
  120. }
  121. if len(T_pass) < 6 {
  122. c.Data["json"] = lib.JSONS{Code: 208, Msg: "密码异常!"}
  123. c.ServeJSON()
  124. return
  125. }
  126. temp, err := http.SmsTemplate_Post(T_user)
  127. if err != nil || temp.Status != "success" {
  128. c.Data["json"] = lib.JSONS{Code: 202, Msg: "创建短信模板失败"}
  129. c.ServeJSON()
  130. return
  131. }
  132. var_ := Account.User{
  133. T_user: T_user,
  134. T_pass: T_pass,
  135. T_phone: T_phone,
  136. T_template_id: temp.Template_id,
  137. T_arrears_notice: T_arrears_notice,
  138. T_State: T_State,
  139. }
  140. _, err = Account.Add_User(var_)
  141. if err != nil {
  142. c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
  143. c.ServeJSON()
  144. return
  145. }
  146. var_.T_pass = "******"
  147. System.Add_UserLogs_T(c.User.T_uuid, "用户", "新增", var_)
  148. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  149. c.ServeJSON()
  150. return
  151. }
  152. // 修改个人信息
  153. func (c *UserController) Post() {
  154. T_pass := c.GetString("T_pass")
  155. user := c.User
  156. if len(T_pass) > 0 {
  157. if len(T_pass) < 8 {
  158. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  159. c.ServeJSON()
  160. return
  161. }
  162. user.T_pass = T_pass
  163. }
  164. if err := Account.Update_User(user, "T_pass"); err != nil {
  165. c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
  166. c.ServeJSON()
  167. return
  168. }
  169. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改登录密码", "")
  170. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  171. c.ServeJSON()
  172. return
  173. }
  174. // 修改用户信息
  175. func (c *UserController) Edit() {
  176. T_uuid := c.GetString("T_uuid")
  177. T_pass := c.GetString("T_pass")
  178. T_phone := c.GetString("T_phone")
  179. T_arrears_notice, _ := c.GetInt("T_arrears_notice")
  180. T_State, _ := c.GetInt("T_State")
  181. var err error
  182. var user Account.User
  183. var cols []string
  184. if len(T_uuid) > 0 {
  185. user, err = Account.Read_User_ByT_uuid(T_uuid)
  186. if err != nil {
  187. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  188. c.ServeJSON()
  189. return
  190. }
  191. }
  192. if len(T_pass) > 0 {
  193. if len(T_pass) < 6 {
  194. c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
  195. c.ServeJSON()
  196. return
  197. }
  198. user.T_pass = T_pass
  199. cols = append(cols, "T_pass")
  200. }
  201. if len(T_phone) > 0 {
  202. user.T_phone = T_phone
  203. cols = append(cols, "T_phone")
  204. }
  205. if T_arrears_notice > 0 {
  206. user.T_arrears_notice = T_arrears_notice
  207. cols = append(cols, "T_arrears_notice")
  208. }
  209. if T_State > 0 {
  210. user.T_State = T_State
  211. cols = append(cols, "T_State")
  212. }
  213. if err = Account.Update_User(user, cols...); err != nil {
  214. c.Data["json"] = lib.JSONS{Code: 208, Msg: "修改失败!"}
  215. c.ServeJSON()
  216. return
  217. }
  218. user.T_pass = "******"
  219. System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改个人信息", user)
  220. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  221. c.ServeJSON()
  222. return
  223. }
  224. // 删除用户信息
  225. func (c *UserController) Del() {
  226. T_uuid := c.GetString("T_uuid")
  227. if len(T_uuid) == 0 {
  228. c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
  229. c.ServeJSON()
  230. return
  231. }
  232. user, err := Account.Read_User_ByT_uuid(T_uuid)
  233. if err != nil {
  234. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  235. c.ServeJSON()
  236. return
  237. }
  238. if user.Id == 1 {
  239. c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除超级管理员!"}
  240. c.ServeJSON()
  241. return
  242. }
  243. temp, err := http.SmsTemplate_Delete(user.T_template_id)
  244. if err != nil || temp.Status != "success" {
  245. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除短信模板失败"}
  246. c.ServeJSON()
  247. return
  248. }
  249. if err = Account.Delete_User(user); err != nil {
  250. c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
  251. c.ServeJSON()
  252. return
  253. }
  254. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  255. c.ServeJSON()
  256. return
  257. }
  258. // 充值
  259. func (c *UserController) Pay() {
  260. T_uuid := c.GetString("T_uuid")
  261. T_balance, _ := c.GetFloat("T_balance")
  262. if len(T_uuid) == 0 {
  263. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  264. c.ServeJSON()
  265. return
  266. }
  267. user, err := Account.Read_User_ByT_uuid(T_uuid)
  268. if err != nil {
  269. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  270. c.ServeJSON()
  271. return
  272. }
  273. T_money64, _ := decimal.NewFromFloat(float64(user.T_money) + T_balance).Round(2).Float64()
  274. user.T_money = float32(T_money64)
  275. err = Account.Update_User(user, "T_money")
  276. if err != nil {
  277. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  278. c.ServeJSON()
  279. return
  280. }
  281. // 添加充值记录
  282. bill := Account.UserBill{
  283. T_uid: user.Id,
  284. T_type: Account.Pay,
  285. T_bill: "充值",
  286. T_charging: float32(T_balance),
  287. T_balance: float32(T_money64),
  288. }
  289. _, err = Account.Add_UserBill(bill)
  290. if err != nil {
  291. c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
  292. c.ServeJSON()
  293. return
  294. }
  295. System.Add_UserLogs_T(c.User.T_uuid, "用户管理", "充值", user)
  296. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
  297. c.ServeJSON()
  298. return
  299. }
  300. // 账单下载
  301. func (c *UserController) Bill() {
  302. // 分页参数 初始化
  303. page, _ := c.GetInt("page")
  304. if page < 1 {
  305. page = 1
  306. }
  307. page_z, _ := c.GetInt("page_z")
  308. if page_z < 1 {
  309. page_z = conf.Page_size
  310. }
  311. T_uuid := c.GetString("T_uuid")
  312. //T_type 1 充值 2扣费
  313. T_type, _ := c.GetInt("T_type")
  314. user, err := Account.Read_User_ByT_uuid(T_uuid)
  315. if err != nil {
  316. user = c.User
  317. }
  318. Bill_List, cnt := Account.Read_UserBill_List(user.Id, "", T_type, page, page_z)
  319. var r_jsons lib.R_JSONS
  320. r_jsons.Num = cnt
  321. r_jsons.Data = Bill_List
  322. r_jsons.Page = page
  323. r_jsons.Page_size = int(math.Ceil(float64(cnt) / float64(page_z)))
  324. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  325. c.ServeJSON()
  326. return
  327. }
  328. // 账单下载
  329. func (c *UserController) Bill_Excel() {
  330. T_month := c.GetString("T_month")
  331. T_uuid := c.GetString("T_uuid")
  332. user, err := Account.Read_User_ByT_uuid(T_uuid)
  333. if err != nil {
  334. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
  335. c.ServeJSON()
  336. return
  337. }
  338. filename := fmt.Sprintf("%s账单", user.T_user)
  339. if len(T_month) > 0 {
  340. _, err := time.Parse("2006-01", T_month)
  341. if err != nil {
  342. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  343. c.ServeJSON()
  344. return
  345. }
  346. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  347. filename = fmt.Sprintf("%s%s年%s月账单", c.User.T_user, year, month)
  348. }
  349. f := excelize.NewFile()
  350. Style1, _ := f.NewStyle(
  351. &excelize.Style{
  352. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  353. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  354. })
  355. Style2, _ := f.NewStyle(
  356. &excelize.Style{
  357. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  358. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  359. Border: []excelize.Border{
  360. {Type: "left", Color: "000000", Style: 1},
  361. {Type: "top", Color: "000000", Style: 1},
  362. {Type: "bottom", Color: "000000", Style: 1},
  363. {Type: "right", Color: "000000", Style: 1},
  364. },
  365. })
  366. f.MergeCell("Sheet1", "A1", "G1")
  367. f.SetRowStyle("Sheet1", 1, 1, Style1)
  368. f.SetCellValue("Sheet1", "A1", filename)
  369. f.SetRowHeight("Sheet1", 1, 30)
  370. f.SetCellStyle("Sheet1", "A2", "F2", Style2)
  371. f.SetRowHeight("Sheet1", 2, 25)
  372. // 这里设置表头
  373. f.SetCellValue("Sheet1", "A2", "编号")
  374. f.SetCellValue("Sheet1", "B2", "消费项目")
  375. f.SetCellValue("Sheet1", "C2", "扣费/充值")
  376. f.SetCellValue("Sheet1", "D2", "金额(元)")
  377. f.SetCellValue("Sheet1", "E2", "余额(元)")
  378. f.SetCellValue("Sheet1", "F2", "时间")
  379. // 设置列宽
  380. f.SetColWidth("Sheet1", "A", "A", 10)
  381. f.SetColWidth("Sheet1", "B", "B", 15)
  382. f.SetColWidth("Sheet1", "C", "C", 12)
  383. f.SetColWidth("Sheet1", "D", "D", 15)
  384. f.SetColWidth("Sheet1", "E", "E", 15)
  385. f.SetColWidth("Sheet1", "F", "F", 20)
  386. line := 2
  387. //T_type 1 充值 2扣费
  388. Bill_List, _ := Account.Read_UserBill_List(user.Id, T_month, 0, 0, 9999)
  389. // 循环写入数据
  390. for i, v := range Bill_List {
  391. line++
  392. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  393. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_bill)
  394. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_type)
  395. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_charging)
  396. f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_balance)
  397. f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CreateTime)
  398. }
  399. Style4, _ := f.NewStyle(
  400. &excelize.Style{
  401. Font: &excelize.Font{Size: 12, Family: "宋体"},
  402. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  403. Border: []excelize.Border{
  404. {Type: "left", Color: "000000", Style: 1},
  405. {Type: "top", Color: "000000", Style: 1},
  406. {Type: "bottom", Color: "000000", Style: 1},
  407. {Type: "right", Color: "000000", Style: 1},
  408. },
  409. })
  410. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("F%d", line), Style4)
  411. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  412. // 保存文件
  413. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  414. fmt.Println(err)
  415. }
  416. var url string
  417. //// 上传 OSS
  418. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  419. if !is {
  420. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  421. c.ServeJSON()
  422. return
  423. }
  424. //删除目录
  425. err = os.Remove("ofile/" + timeStr + ".xlsx")
  426. if err != nil {
  427. fmt.Println(err)
  428. }
  429. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  430. c.ServeJSON()
  431. return
  432. }
  433. // 通知记录
  434. func (c *UserController) Send() {
  435. // 分页参数 初始化
  436. page, _ := c.GetInt("page")
  437. T_month := c.GetString("T_month")
  438. if page < 1 {
  439. page = 1
  440. }
  441. page_z, _ := c.GetInt("page_z")
  442. if page_z < 1 {
  443. page_z = conf.Page_size
  444. }
  445. T_uuid := c.GetString("T_uuid")
  446. user, err := Account.Read_User_ByT_uuid(T_uuid)
  447. if err != nil {
  448. user = c.User
  449. }
  450. T_patient_uuid := c.GetString("T_patient_uuid")
  451. T_patient_Id := 0
  452. if len(T_patient_uuid) > 0 {
  453. patient, err := Patient.Read_Patient_ByT_uuid(T_patient_uuid)
  454. if err != nil {
  455. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_patient_uuid Err!"}
  456. c.ServeJSON()
  457. return
  458. }
  459. T_patient_Id = patient.Id
  460. }
  461. // 1 短信 2 电话
  462. T_type, _ := c.GetInt("T_type")
  463. send_List, cnt := Patient.Read_PatientSend_List(user.Id, T_patient_Id, T_type, T_month, page, page_z)
  464. var r_jsons lib.R_JSONS
  465. r_jsons.Num = cnt
  466. r_jsons.Data = send_List
  467. r_jsons.Page = page
  468. r_jsons.Page_size = int(math.Ceil(float64(cnt) / float64(page_z)))
  469. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
  470. c.ServeJSON()
  471. return
  472. }
  473. // 通知记录下载
  474. func (c *UserController) Send_Excel() {
  475. T_month := c.GetString("T_month")
  476. T_uuid := c.GetString("T_uuid")
  477. T_type, _ := c.GetInt("T_type")
  478. user, err := Account.Read_User_ByT_uuid(T_uuid)
  479. if err != nil {
  480. user = c.User
  481. }
  482. T_patient_uuid := c.GetString("T_patient_uuid")
  483. T_patient_Id := 0
  484. if len(T_patient_uuid) > 0 {
  485. patient, err := Patient.Read_Patient_ByT_uuid(T_patient_uuid)
  486. if err != nil {
  487. c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_patient_uuid Err!"}
  488. c.ServeJSON()
  489. return
  490. }
  491. T_patient_Id = patient.Id
  492. }
  493. var T_type_str string
  494. if T_type == 1 {
  495. T_type_str = "短信"
  496. }
  497. if T_type == 2 {
  498. T_type_str = "电话"
  499. }
  500. filename := fmt.Sprintf("%s%s通知记录", user.T_user, T_type_str)
  501. if len(T_month) > 0 {
  502. _, err := time.Parse("2006-01", T_month)
  503. if err != nil {
  504. c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
  505. c.ServeJSON()
  506. return
  507. }
  508. year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
  509. filename = fmt.Sprintf("%s%s%s年%s月通知记录", user.T_user, T_type_str, year, month)
  510. }
  511. f := excelize.NewFile()
  512. Style1, _ := f.NewStyle(
  513. &excelize.Style{
  514. Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
  515. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  516. })
  517. Style2, _ := f.NewStyle(
  518. &excelize.Style{
  519. Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
  520. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  521. Border: []excelize.Border{
  522. {Type: "left", Color: "000000", Style: 1},
  523. {Type: "top", Color: "000000", Style: 1},
  524. {Type: "bottom", Color: "000000", Style: 1},
  525. {Type: "right", Color: "000000", Style: 1},
  526. },
  527. })
  528. f.MergeCell("Sheet1", "A1", "D1")
  529. f.SetRowStyle("Sheet1", 1, 1, Style1)
  530. f.SetCellValue("Sheet1", "A1", filename)
  531. f.SetRowHeight("Sheet1", 1, 30)
  532. f.SetCellStyle("Sheet1", "A2", "D2", Style2)
  533. f.SetRowHeight("Sheet1", 2, 25)
  534. // 这里设置表头
  535. f.SetCellValue("Sheet1", "A2", "编号")
  536. f.SetCellValue("Sheet1", "B2", "通知电话")
  537. f.SetCellValue("Sheet1", "C2", "通知类型")
  538. f.SetCellValue("Sheet1", "D2", "时间")
  539. // 设置列宽
  540. f.SetColWidth("Sheet1", "A", "A", 10)
  541. f.SetColWidth("Sheet1", "B", "B", 15)
  542. f.SetColWidth("Sheet1", "C", "C", 12)
  543. f.SetColWidth("Sheet1", "D", "D", 30)
  544. line := 2
  545. //T_type 1 充值 2扣费
  546. send_List, _ := Patient.Read_PatientSend_List(user.Id, T_patient_Id, T_type, T_month, 0, 9999)
  547. // 循环写入数据
  548. for i, v := range send_List {
  549. line++
  550. f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
  551. f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_phone)
  552. if v.T_type == 1 {
  553. T_type_str = "短信"
  554. }
  555. if v.T_type == 2 {
  556. T_type_str = "电话"
  557. }
  558. f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), T_type_str)
  559. f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.CreateTime)
  560. }
  561. Style4, _ := f.NewStyle(
  562. &excelize.Style{
  563. Font: &excelize.Font{Size: 12, Family: "宋体"},
  564. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  565. Border: []excelize.Border{
  566. {Type: "left", Color: "000000", Style: 1},
  567. {Type: "top", Color: "000000", Style: 1},
  568. {Type: "bottom", Color: "000000", Style: 1},
  569. {Type: "right", Color: "000000", Style: 1},
  570. },
  571. })
  572. f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("D%d", line), Style4)
  573. timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
  574. // 保存文件
  575. if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
  576. fmt.Println(err)
  577. }
  578. var url string
  579. //// 上传 OSS
  580. url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
  581. if !is {
  582. c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
  583. c.ServeJSON()
  584. return
  585. }
  586. //删除目录
  587. err = os.Remove("ofile/" + timeStr + ".xlsx")
  588. if err != nil {
  589. fmt.Println(err)
  590. }
  591. c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
  592. c.ServeJSON()
  593. return
  594. }
  595. // 用户定时任务
  596. func Cron_User() {
  597. //创建一个定时任务对象
  598. c := cron.New(cron.WithSeconds())
  599. //给对象增加定时任务
  600. //c.AddFunc("0 */1 * * * ?", Cron_User_Money_Bill)
  601. c.AddFunc("0 0 7 * * *", Cron_User_Money_Bill)
  602. //启动定时任务
  603. c.Start()
  604. defer c.Stop()
  605. //查询语句,阻塞,让main函数不退出,保持程序运行
  606. select {}
  607. }
  608. // 用户扣费,并生成账单,每天2点执行
  609. func Cron_User_Money_Bill() {
  610. yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  611. logs.Info("开始进行" + yesterday + "用户账单扣费统计")
  612. userList, _ := Account.Read_User_List("", 0, 9999)
  613. for _, user := range userList {
  614. // 1 短信 2 电话
  615. smsCount := Patient.Read_PatientSend_Count_Yesterday(user.Id, 0, 1, yesterday)
  616. voiceCallCount := Patient.Read_PatientSend_Count_Yesterday(user.Id, 0, 2, yesterday)
  617. money := float64(smsCount)*conf.Sms_Fee + float64(voiceCallCount)*conf.VoiceCall_Fee
  618. if money == 0 {
  619. continue
  620. }
  621. var_ := Account.User{Id: user.Id, T_money: user.T_money - float32(money)}
  622. err := Account.Update_User(var_, "T_money")
  623. if err != nil {
  624. System.Add_SysLogs_T("用户扣费", "扣费失败", var_)
  625. }
  626. // 添加扣费记录
  627. bill := Account.UserBill{
  628. T_uid: user.Id,
  629. T_type: Account.FeeDeduction,
  630. T_bill: yesterday + "通知自动扣除",
  631. T_charging: float32(money),
  632. T_balance: user.T_money - float32(money),
  633. }
  634. _, err = Account.Add_UserBill(bill)
  635. if err != nil {
  636. System.Add_SysLogs_T("用户扣费", "添加扣费记录失败", bill)
  637. }
  638. }
  639. logs.Info("用户账单扣费统计结束")
  640. }