123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package TimeMonitor
- import (
- "Cold_DeductionNotice/WarningNotice"
- "Cold_DeductionNotice/logs"
- "Cold_DeductionNotice/models/Account"
- "Cold_DeductionNotice/models/Company"
- "Cold_DeductionNotice/models/Warning"
- "fmt"
- "github.com/robfig/cron/v3"
- "strconv"
- "time"
- )
- func DeductionNoticeInit() {
- //go DeductionNoticeStatus()
- crontab := cron.New(cron.WithSeconds())
- _, err := crontab.AddFunc("0 0 1 * * *", DeductionNoticeStatus)
- if err != nil {
- fmt.Printf("err: %v\n", err)
- fmt.Println("初始化成功")
- }
- crontab.Start()
- defer crontab.Stop()
- select {}
- }
- func DeductionNoticeStatus() {
- fmt.Println("==============定时 通知扣费================")
- now := time.Now()
- d, _ := time.ParseDuration("-24h")
- currentTime := now.Add(d)
- logs.Println(fmt.Sprintf("%d-%02d-%02d", currentTime.Year(), currentTime.Month(), currentTime.Day()))
- TABLE_name := fmt.Sprintf("warning_sand_%d%02d", currentTime.Year(), currentTime.Month())
- logs.Println("TABLE_name:", TABLE_name)
- StartTine := fmt.Sprintf("%d-%02d-%02d 00:00:00", currentTime.Year(), currentTime.Month(), currentTime.Day())
- EndTime := fmt.Sprintf("%d-%02d-%02d 23:59:59", currentTime.Year(), currentTime.Month(), currentTime.Day())
- for _,v := range Account.Read_Company_All(){
- logs.Println("=======",v.Id,v.T_name)
- var Money float32
- // 微信计费
- Ntype_0 := Warning.Get_WarningSandNum(TABLE_name, v.Id, 0, StartTine, EndTime)
- logs.Println("微信条数:",Ntype_0," 金额:",Ntype_0 * 0)
- // 短信计费
- Ntype_1 := Warning.Get_WarningSandNum(TABLE_name, v.Id, 1, StartTine, EndTime)
- logs.Println("短信条数:",Ntype_1," 金额:",float32(Ntype_1) * 0.1)
- Money += float32(Ntype_1) * 0.12
- // 电话计费
- Ntype_2 := Warning.Get_WarningSandNum(TABLE_name, v.Id, 2, StartTine, EndTime)
- logs.Println("电话条数:",Ntype_2," 金额:",float32(Ntype_2) * 0.1)
- Money += float32(Ntype_2) * 0.15
- if Money == 0 {
- // 没有费用,跳过
- continue
- }
- // 更新费用
- _, Company_r := Account.Read_Company_id(v.Id)
- logs.Println(Company_r.T_name," 扣费记录 -"," 当期余额:",Company_r.T_money," ",fmt.Sprintf("%d-%02d-%02d", currentTime.Year(), currentTime.Month(), currentTime.Day()),"扣除:",Money)
- Company_r.T_money = Company_r.T_money - Money
- Account.Update_Company(Company_r,"T_money")
- // 插入账单
- CompanyBill_r := Company.CompanyBill{
- T_pid : Company_r.Id,
- T_type : "扣费",
- T_project : "通知计费(天)",
- T_bill : fmt.Sprintf("%d-%02d-%02d", currentTime.Year(), currentTime.Month(), currentTime.Day())+"通知计费(天),短信条数:"+strconv.Itoa(Ntype_1)+"条,电话条数:"+strconv.Itoa(Ntype_2)+"条",
- T_charging : Money,
- T_balance : Company_r.T_money,
- }
- Company.Add_CompanyBill(CompanyBill_r)
- var Warning_r Warning.Warning
- Warning_r.T_pid = Company_r.Id // 报警类型
- Warning_r.T_tp = 1002 // 报警类型
- Warning_r.T_sn = ""
- Warning_r.T_id = 0
- Warning_r.T_Msid = 0
- Warning_r.T_D_name = Company_r.T_name
- Warning_r.T_DS_name = Company_r.T_name
- Warning_r.T_Ut = time.Now()
- Warning_r.T_State = 1
- // 余额预警
- if Company_r.T_money < 10{
- Warning_r.T_Remark = "您当前余额不足10元,请及时充值"
- }
- if Company_r.T_money <= 0{
- Warning_r.T_Remark = "您当前余额"+fmt.Sprintf("%.2f", Company_r.T_money)+"元,请及时充值"
- }
- // 发送给用户
- WarningNotice.WarningToUser(&Warning_r,Warning_r.T_pid)
- Warning.Add_Warning(Warning_r) // 添加报警
- }
- //
- }
|