123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- package controllers
- import (
- "FollowUp_Notice/Nats/NatsServer"
- "FollowUp_Notice/conf"
- "FollowUp_Notice/http"
- "FollowUp_Notice/logs"
- "FollowUp_Notice/models/Account"
- "FollowUp_Notice/models/Patient"
- "FollowUp_Notice/models/System"
- "fmt"
- "git.baozhida.cn/ERP_libs/lib"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/robfig/cron/v3"
- "github.com/shopspring/decimal"
- "github.com/xuri/excelize/v2"
- "math"
- "os"
- "strings"
- "time"
- )
- type UserController struct {
- beego.Controller
- User Account.User
- }
- func (c *UserController) Prepare() {
- if Account.User_r != nil {
- c.User = *Account.User_r
- }
- }
- // 验证登录
- func (c *UserController) Login_verification() {
- Admin_user := c.GetString("username")
- Admin_pass := c.GetString("password")
- type JSONS struct {
- //必须的大写开头
- Code int16
- Msg string
- Data interface{} // 泛型
- UserId int
- }
- err, user_r := Account.Read_User_verification(Admin_user, Admin_pass)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "用户名或密码错误!"}
- } else {
- User_tokey := Account.Add_Tokey(user_r.T_uuid)
- c.Ctx.SetCookie("User_tokey", User_tokey, time.Second*60*60)
- c.Data["json"] = JSONS{Code: 200, Msg: "OK!", Data: User_tokey, UserId: user_r.Id}
- System.Add_UserLogs_T(user_r.T_uuid, "用户", "用户登陆", lib.GetUserLoginInfo(c.Ctx))
- }
- c.ServeJSON()
- return
- }
- // --------------------------------------------------------------------------------------------------------------
- // 用户列表
- func (c *UserController) List() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- // 查询
- T_name := c.GetString("T_name")
- R_List, R_cnt := Account.Read_User_List(T_name, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = R_cnt
- r_jsons.Data = R_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(R_cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- func (c *UserController) Get() {
- T_uuid := c.GetString("T_uuid")
- user, err := Account.Read_User_ByT_uuid(T_uuid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- var r_jsons lib.R_JSONS
- r_jsons.Data = Account.UserToUser_R(user)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 个人信息
- func (c *UserController) Info() {
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Account.UserToUser_R(c.User)}
- c.ServeJSON()
- return
- }
- // 添加用户信息
- func (c *UserController) Add() {
- T_user := c.GetString("T_user")
- T_pass := c.GetString("T_pass")
- T_phone := c.GetString("T_phone")
- T_arrears_notice, _ := c.GetInt("T_arrears_notice")
- T_State, _ := c.GetInt("T_State")
- if len(T_user) < 3 {
- c.Data["json"] = lib.JSONS{Code: 207, Msg: "用户名长度不足!"}
- c.ServeJSON()
- return
- }
- if len(T_pass) < 6 {
- c.Data["json"] = lib.JSONS{Code: 208, Msg: "密码异常!"}
- c.ServeJSON()
- return
- }
- temp, err := http.SmsTemplate_Post(T_user)
- if err != nil || temp.Status != "success" {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "创建短信模板失败"}
- c.ServeJSON()
- return
- }
- var_ := Account.User{
- T_user: T_user,
- T_pass: T_pass,
- T_phone: T_phone,
- T_template_id: temp.Template_id,
- T_arrears_notice: T_arrears_notice,
- T_State: T_State,
- }
- _, err = Account.Add_User(var_)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 209, Msg: "添加失败!"}
- c.ServeJSON()
- return
- }
- var_.T_pass = "******"
- System.Add_UserLogs_T(c.User.T_uuid, "用户", "新增", var_)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 修改个人信息
- func (c *UserController) Post() {
- T_pass := c.GetString("T_pass")
- user := c.User
- if len(T_pass) > 0 {
- if len(T_pass) < 8 {
- c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
- c.ServeJSON()
- return
- }
- user.T_pass = T_pass
- }
- if err := Account.Update_User(user, "T_pass"); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改登录密码", "")
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 修改用户信息
- func (c *UserController) Edit() {
- T_uuid := c.GetString("T_uuid")
- T_pass := c.GetString("T_pass")
- T_phone := c.GetString("T_phone")
- T_arrears_notice, _ := c.GetInt("T_arrears_notice")
- T_State, _ := c.GetInt("T_State")
- var err error
- var user Account.User
- var cols []string
- if len(T_uuid) > 0 {
- user, err = Account.Read_User_ByT_uuid(T_uuid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- }
- if len(T_pass) > 0 {
- if len(T_pass) < 6 {
- c.Data["json"] = lib.JSONS{Code: 206, Msg: "密码格式不正确!"}
- c.ServeJSON()
- return
- }
- user.T_pass = T_pass
- cols = append(cols, "T_pass")
- }
- if len(T_phone) > 0 {
- user.T_phone = T_phone
- cols = append(cols, "T_phone")
- }
- if T_arrears_notice > 0 {
- user.T_arrears_notice = T_arrears_notice
- cols = append(cols, "T_arrears_notice")
- }
- if T_State > 0 {
- user.T_State = T_State
- cols = append(cols, "T_State")
- }
- if err = Account.Update_User(user, cols...); err != nil {
- c.Data["json"] = lib.JSONS{Code: 208, Msg: "修改失败!"}
- c.ServeJSON()
- return
- }
- user.T_pass = "******"
- System.Add_UserLogs_T(c.User.T_uuid, "用户", "修改个人信息", user)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 删除用户信息
- func (c *UserController) Del() {
- T_uuid := c.GetString("T_uuid")
- if len(T_uuid) == 0 {
- c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- user, err := Account.Read_User_ByT_uuid(T_uuid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- if user.Id == 1 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "禁止删除超级管理员!"}
- c.ServeJSON()
- return
- }
- temp, err := http.SmsTemplate_Delete(user.T_template_id)
- if err != nil || temp.Status != "success" {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除短信模板失败"}
- c.ServeJSON()
- return
- }
- if err = Account.Delete_User(user); err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败!"}
- c.ServeJSON()
- return
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 充值
- func (c *UserController) Pay() {
- T_uuid := c.GetString("T_uuid")
- T_balance, _ := c.GetFloat("T_balance")
- if len(T_uuid) == 0 {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- user, err := Account.Read_User_ByT_uuid(T_uuid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- T_money64, _ := decimal.NewFromFloat(float64(user.T_money) + T_balance).Round(2).Float64()
- user.T_money = float32(T_money64)
- err = Account.Update_User(user, "T_money")
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
- c.ServeJSON()
- return
- }
- // 添加充值记录
- bill := Account.UserBill{
- T_uid: user.Id,
- T_type: Account.Pay,
- T_bill: "充值",
- T_charging: float32(T_balance),
- T_balance: float32(T_money64),
- }
- _, err = Account.Add_UserBill(bill)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "充值失败!"}
- c.ServeJSON()
- return
- }
- System.Add_UserLogs_T(c.User.T_uuid, "用户管理", "充值", user)
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
- c.ServeJSON()
- return
- }
- // 账单下载
- func (c *UserController) Bill() {
- // 分页参数 初始化
- page, _ := c.GetInt("page")
- if page < 1 {
- page = 1
- }
- page_z, _ := c.GetInt("page_z")
- if page_z < 1 {
- page_z = conf.Page_size
- }
- T_uuid := c.GetString("T_uuid")
- //T_type 1 充值 2扣费
- T_type, _ := c.GetInt("T_type")
- user, err := Account.Read_User_ByT_uuid(T_uuid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- CompanyBill_List, cnt := Account.Read_UserBill_List(user.Id, "", T_type, page, page_z)
- var r_jsons lib.R_JSONS
- r_jsons.Num = cnt
- r_jsons.Data = CompanyBill_List
- r_jsons.Page = page
- r_jsons.Page_size = int(math.Ceil(float64(cnt) / float64(page_z)))
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
- c.ServeJSON()
- return
- }
- // 账单下载
- func (c *UserController) Bill_Excel() {
- T_month := c.GetString("T_month")
- T_uuid := c.GetString("T_uuid")
- user, err := Account.Read_User_ByT_uuid(T_uuid)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_uuid Err!"}
- c.ServeJSON()
- return
- }
- filename := fmt.Sprintf("%s账单", user.T_user)
- if len(T_month) > 0 {
- _, err := time.Parse("2006-01", T_month)
- if err != nil {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "日期格式错误!"}
- c.ServeJSON()
- return
- }
- year, month := strings.Split(T_month, "-")[0], strings.Split(T_month, "-")[1]
- filename = fmt.Sprintf("%s%s年%s月账单", c.User.T_user, year, month)
- }
- f := excelize.NewFile()
- Style1, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 16, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- })
- Style2, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Bold: true, Size: 14, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- f.MergeCell("Sheet1", "A1", "G1")
- f.SetRowStyle("Sheet1", 1, 1, Style1)
- f.SetCellValue("Sheet1", "A1", filename)
- f.SetRowHeight("Sheet1", 1, 30)
- f.SetCellStyle("Sheet1", "A2", "F2", Style2)
- f.SetRowHeight("Sheet1", 2, 25)
- // 这里设置表头
- f.SetCellValue("Sheet1", "A2", "编号")
- f.SetCellValue("Sheet1", "B2", "说明")
- f.SetCellValue("Sheet1", "C2", "扣费/充值")
- f.SetCellValue("Sheet1", "D2", "金额(元)")
- f.SetCellValue("Sheet1", "E2", "余额(元)")
- f.SetCellValue("Sheet1", "F2", "时间")
- // 设置列宽
- f.SetColWidth("Sheet1", "A", "A", 10)
- f.SetColWidth("Sheet1", "B", "B", 15)
- f.SetColWidth("Sheet1", "C", "C", 12)
- f.SetColWidth("Sheet1", "D", "D", 15)
- f.SetColWidth("Sheet1", "E", "E", 15)
- f.SetColWidth("Sheet1", "F", "F", 20)
- line := 2
- //T_type 1 充值 2扣费
- CompanyBill_List, _ := Account.Read_UserBill_List(user.Id, T_month, 0, 0, 9999)
- // 循环写入数据
- for i, v := range CompanyBill_List {
- line++
- f.SetCellValue("Sheet1", fmt.Sprintf("A%d", line), i+1)
- f.SetCellValue("Sheet1", fmt.Sprintf("B%d", line), v.T_bill)
- f.SetCellValue("Sheet1", fmt.Sprintf("C%d", line), v.T_type)
- f.SetCellValue("Sheet1", fmt.Sprintf("D%d", line), v.T_charging)
- f.SetCellValue("Sheet1", fmt.Sprintf("E%d", line), v.T_balance)
- f.SetCellValue("Sheet1", fmt.Sprintf("F%d", line), v.CreateTime)
- }
- Style4, _ := f.NewStyle(
- &excelize.Style{
- Font: &excelize.Font{Size: 12, Family: "宋体"},
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
- Border: []excelize.Border{
- {Type: "left", Color: "000000", Style: 1},
- {Type: "top", Color: "000000", Style: 1},
- {Type: "bottom", Color: "000000", Style: 1},
- {Type: "right", Color: "000000", Style: 1},
- },
- })
- f.SetCellStyle("Sheet1", "A2", fmt.Sprintf("F%d", line), Style4)
- timeStr := filename + fmt.Sprintf("(%s)", lib.GetRandstring(8, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 0))
- // 保存文件
- if err = f.SaveAs("ofile/" + timeStr + ".xlsx"); err != nil {
- fmt.Println(err)
- }
- var url string
- //// 上传 OSS
- url, is := NatsServer.Qiniu_UploadFile(lib.GetCurrentDirectory()+"/ofile/"+timeStr+".xlsx", "ofile/"+timeStr+".xlsx")
- if !is {
- c.Data["json"] = lib.JSONS{Code: 202, Msg: "oss!"}
- c.ServeJSON()
- return
- }
- //删除目录
- err = os.Remove("ofile/" + timeStr + ".xlsx")
- if err != nil {
- fmt.Println(err)
- }
- c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: url}
- c.ServeJSON()
- return
- }
- // 用户定时任务
- func Cron_User() {
- //创建一个定时任务对象
- c := cron.New(cron.WithSeconds())
- //给对象增加定时任务
- //c.AddFunc("0 */1 * * * ?", Cron_User_Money_Bill)
- c.AddFunc("0 0 7 * * *", Cron_User_Money_Bill)
- //启动定时任务
- c.Start()
- defer c.Stop()
- //查询语句,阻塞,让main函数不退出,保持程序运行
- select {}
- }
- // 用户扣费,并生成账单,每天2点执行
- func Cron_User_Money_Bill() {
- yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
- logs.Info("开始进行" + yesterday + "用户账单扣费统计")
- userList, _ := Account.Read_User_List("", 0, 9999)
- for _, user := range userList {
- // 1 短信 2 电话
- smsCount := Patient.Read_PatientSend_Count(user.Id, 0, 1, yesterday)
- voiceCallCount := Patient.Read_PatientSend_Count(user.Id, 0, 2, yesterday)
- money := float64(smsCount)*conf.Sms_Fee + float64(voiceCallCount)*conf.VoiceCall_Fee
- if money == 0 {
- continue
- }
- var_ := Account.User{Id: user.Id, T_money: user.T_money - float32(money)}
- err := Account.Update_User(var_, "T_money")
- if err != nil {
- System.Add_SysLogs_T("用户扣费", "扣费失败", var_)
- }
- // 添加扣费记录
- bill := Account.UserBill{
- T_uid: user.Id,
- T_type: Account.FeeDeduction,
- T_bill: "扣费",
- T_charging: float32(money),
- T_balance: user.T_money - float32(money),
- }
- _, err = Account.Add_UserBill(bill)
- if err != nil {
- System.Add_SysLogs_T("用户扣费", "添加扣费记录失败", bill)
- }
- }
- logs.Info("用户账单扣费统计结束")
- }
|