package svc import ( "applet/app/utils/cache" "applet/app/utils/logx" model2 "code.fnuoos.com/zhimeng/model.git/src/super/model" "errors" "fmt" "time" "xorm.io/xorm" ) const AgentSettlementStateLockKey = "agent_settlement_state_lock_key" const AgentSettlementStateLockKeyValue = "running" func AgentSettlementState(eg *xorm.Engine) { now := time.Now() if now.Hour() > 1 && now.Hour() > 8 { //TODO::只在凌晨 1 点 ~ 凌晨 8 点运行 fmt.Println("err>>>>>>>", errors.New("非运行时间")) logx.Warn(errors.New("非运行时间")) return } //TODO::增加“悲观锁”防止串行 getString, _ := cache.GetString(AgentSettlementStateLockKey) //if err != nil { // return err //} if getString == AgentSettlementStateLockKeyValue { fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完") logx.Warn(errors.New("上一次结算未执行完")) return } cache.SetEx(AgentSettlementStateLockKey, AgentSettlementStateLockKeyValue, 3600*8) //8小时 fmt.Println("calc master data statistics...") defer func() { if err := recover(); err != nil { _ = logx.Error(err) } }() today := now.Format("2006-01-02") //1、查询"日结"结算单 agent_settlement 数据 _, err := eg.Where("kind = ?", 1).And("start_date != ?", today).Update(&model2.AgentSettlement{ State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订) EndDate: today, }) if err != nil { fmt.Println("err>>>>>>>", err.Error()) logx.Warn(err) } //2、查询"周结"结算单 agent_settlement 数据 weekday := now.Weekday() if weekday == time.Monday { _, err = eg.Where("kind = ?", 2).And("start_date != ?", today).Update(&model2.AgentSettlement{ State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订) EndDate: today, }) if err != nil { fmt.Println("err>>>>>>>", err.Error()) logx.Warn(err) } } //3、查询"月结"结算单 agent_settlement 数据 if now.Day() == 1 { _, err = eg.Where("kind = ?", 3).And("start_date != ?", today).Update(&model2.AgentSettlement{ State: 1, //结算单状态(0:未开始 1:核算中 2:待签订 3:完成签订) EndDate: today, }) if err != nil { fmt.Println("err>>>>>>>", err.Error()) logx.Warn(err) } } return }