package svc import ( "applet/app/db" "applet/app/db/model" "applet/app/utils" "applet/app/utils/cache" "fmt" "time" ) func PlayletOrderSettle() { day := time.Now().Day() if day < 5 { return } month := utils.GetTimeRange("current_month") fmt.Println(month) arg := map[string]string{ "status": "订单结算", "sort": "id desc", "is_to_settle": "1", "to_settle_time": time.Unix(month["start"], 0).Format("2006-01-02 15:04:05"), "p": "1", "size": "100", } playletSaleOrderDb := db.PlayletSaleOrderDb{} playletSaleOrderDb.Set() order := playletSaleOrderDb.GetPlayletVideoOrderList(arg) if len(order) == 0 { return } for _, v := range order { // 加锁 防止并发提取 mutexKey := fmt.Sprintf("playlet_order_settle:%s", v.CustomOid) withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 1800, "NX") if err != nil { continue } if withdrawAvailable != "OK" { continue } if v.SettleTime > 0 { continue } //加到上月结算 if v.Uid == "" || v.Uid == "0" { continue } masterDb := db.MasterDb{} masterDb.Set() master := masterDb.GetMaster(v.Uid) if master == nil { continue } masterAmountDb := db.MasterAmountDb{} masterAmountDb.Set() amount := masterAmountDb.GetMasterAmount(utils.IntToStr(master.Id), "playlet") if amount == nil { continue } if utils.StrToFloat64(v.Commission) <= 0 { v.SettleTime = int(time.Now().Unix()) playletSaleOrderDb.PlayletVideoOrderUpdate(v.Id, &v) continue } oldAmount := amount.LastMonthAmount amount.LastMonthAmount = utils.Float64ToStr(utils.StrToFloat64(amount.LastMonthAmount) + utils.StrToFloat64(v.Commission)) update := masterAmountDb.MasterAmountUpdate(master.Id, amount) if update == false { continue } var tmp = model.MasterLastMonthAmountFlow{ Uid: utils.IntToStr(master.Id), Time: time.Now(), BeforeAmount: oldAmount, Amount: v.Commission, AfterAmount: amount.LastMonthAmount, Platform: "playlet", Oid: v.Oid, Title: "短剧订单结算", FlowType: "playlet_settle", ExtendUid: master.ExtendUid, } masterLastMonthAmountFlowDb := db.MasterLastMonthAmountFlowDb{} masterLastMonthAmountFlowDb.Set() masterLastMonthAmountFlowDb.MasterLastMonthAmountFlowInsert(&tmp) v.SettleTime = int(time.Now().Unix()) playletSaleOrderDb.PlayletVideoOrderUpdate(v.Id, &v) } return }