package svc import ( "applet/app/md" "applet/app/utils" db "code.fnuoos.com/zhimeng/model.git/src" "code.fnuoos.com/zhimeng/model.git/src/super/implement" "code.fnuoos.com/zhimeng/model.git/src/super/model" "github.com/gin-gonic/gin" "time" ) func DivisionStrategyList(c *gin.Context, req md.DivisionStrategyReq) md.DivisionStrategyRes { engine := db.Db NewMediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(engine) mediumId := GetMediumId(c, req.Name) MediumList, total, _ := NewMediumDivisionStrategyDb.FindMediumDivisionStrategyList(c.GetString("mid"), mediumId, utils.StrToInt(req.Page), utils.StrToInt(req.Limit)) data := make([]md.DivisionStrategyData, 0) if len(MediumList) > 0 { for _, v := range MediumList { var tmp = md.DivisionStrategyData{ Id: utils.IntToStr(v.Id), MediumId: utils.IntToStr(v.MediumId), Account: "", Name: "", PlatformRetentionRate: utils.IntToStr(v.PlatformRetentionRate), CommissionRetentionRate: utils.IntToStr(v.CommissionRetentionRate), MediaRevenueRate: utils.IntToStr(v.MediaRevenueRate), AgentRevenueRate: utils.IntToStr(v.AgentRevenueRate), ExtraRevenueRate: utils.IntToStr(v.ExtraRevenueRate), AgreementSharingRate: utils.IntToStr(v.AgreementSharingRate), } tmpMedium := GetMediumInfo(c, v.MediumId) if tmpMedium["account"] != "" { tmp.Account = tmpMedium["account"] } if tmpMedium["name"] != "" { tmp.Name = tmpMedium["name"] } data = append(data, tmp) } } res := md.DivisionStrategyRes{ List: data, Total: total, } return res } func DivisionStrategyDetail(c *gin.Context, req md.DivisionStrategyDetailReq) md.DivisionStrategyDetailRes { eg := db.Db NewMediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(eg) mediumData, _ := NewMediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(utils.StrToInt(req.MediumId)) tmpMedium := GetMediumInfo(c, mediumData.MediumId) agentList := make([]md.DivisionStrategyDetailByAgent, 0) res := md.DivisionStrategyDetailRes{ MediumId: req.MediumId, Account: "", Name: "", PlatformRetentionRate: utils.IntToStr(mediumData.PlatformRetentionRate), CommissionRetentionRate: utils.IntToStr(mediumData.CommissionRetentionRate), MediaRevenueRate: utils.IntToStr(mediumData.MediaRevenueRate), AgentRevenueRate: utils.IntToStr(mediumData.AgentRevenueRate), ExtraRevenueRate: utils.IntToStr(mediumData.ExtraRevenueRate), AgreementSharingRate: utils.IntToStr(mediumData.AgreementSharingRate), AgentList: agentList, } if tmpMedium["account"] != "" { res.Account = tmpMedium["account"] } if tmpMedium["name"] != "" { res.Name = tmpMedium["name"] } NewAgentWithMediumDb := implement.NewAgentWithMediumDb(eg) agent, _ := NewAgentWithMediumDb.FindAgentWithMediumListByMediumIdAll(mediumData.MediumId) if agent != nil { NewMediumDivisionStrategyWithAgentFlowDb := implement.NewMediumDivisionStrategyWithAgentFlowDb(eg) strategy, _ := NewMediumDivisionStrategyWithAgentFlowDb.FindMediumDivisionStrategyWithAgentFlowByStrategyId(mediumData.Id) strategyMap := make(map[int]model.MediumDivisionStrategyWithAgentFlow) if strategy != nil { for _, v := range *strategy { strategyMap[v.AgentId] = v } } for _, v := range agent { tmp := md.DivisionStrategyDetailByAgent{ AgentId: utils.IntToStr(v.AgentId), AgentRevenueRate: utils.IntToStr(strategyMap[v.AgentId].AgentRevenueRate), ExtraRevenueRate: utils.IntToStr(strategyMap[v.AgentId].ExtraRevenueRate), } tmpAgent := GetAgentInfo(c, v.AgentId) if tmpAgent["account"] != "" { tmp.Account = tmpAgent["account"] } if tmpAgent["name"] != "" { tmp.Name = tmpAgent["name"] } agentList = append(agentList, tmp) } } res.AgentList = agentList return res } func DivisionStrategySave(c *gin.Context, req md.DivisionStrategyDetailRes) error { eg := db.Db sess := eg.NewSession() defer sess.Close() sess.Begin() NewMediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(eg) mediumData, _ := NewMediumDivisionStrategyDb.GetOriginalWxAdDataByMediumIdSess(sess, utils.StrToInt(req.MediumId)) mediumData.PlatformRetentionRate = utils.StrToInt(req.PlatformRetentionRate) mediumData.CommissionRetentionRate = utils.StrToInt(req.CommissionRetentionRate) mediumData.MediaRevenueRate = utils.StrToInt(req.MediaRevenueRate) mediumData.AgentRevenueRate = utils.StrToInt(req.AgentRevenueRate) mediumData.ExtraRevenueRate = utils.StrToInt(req.ExtraRevenueRate) mediumData.AgreementSharingRate = utils.StrToInt(req.AgreementSharingRate) _, err := sess.Where("id=?", mediumData.Id).Update(mediumData) if err != nil { sess.Rollback() return err } NewMediumDivisionStrategyWithAgentFlowDb := implement.NewMediumDivisionStrategyWithAgentFlowDb(eg) for _, v := range req.AgentList { flow, _ := NewMediumDivisionStrategyWithAgentFlowDb.GetMediumDivisionStrategyWithAgentFlowSess(sess, mediumData.Id, utils.StrToInt(v.AgentId)) if flow == nil { flow = &model.MediumDivisionStrategyWithAgentFlow{ StrategyId: mediumData.Id, AgentId: utils.StrToInt(v.AgentId), CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), } _, err = sess.Insert(flow) if err != nil { sess.Rollback() return err } } flow.ExtraRevenueRate = utils.StrToInt(v.ExtraRevenueRate) flow.AgentRevenueRate = utils.StrToInt(v.AgentRevenueRate) flow.UpdateAt = time.Now().Format("2006-01-02 15:04:05") _, err = sess.Where("id=?", flow.Id).Update(flow) if err != nil { sess.Rollback() return err } } sess.Commit() return nil }