package svc import ( "applet/app/e" "applet/app/lib/validate" "applet/app/md" "applet/app/utils" db "code.fnuoos.com/zhimeng/model.git/src" implement2 "code.fnuoos.com/zhimeng/model.git/src/implement" model2 "code.fnuoos.com/zhimeng/model.git/src/model" "code.fnuoos.com/zhimeng/model.git/src/super/implement" "code.fnuoos.com/zhimeng/model.git/src/super/model" "github.com/gin-gonic/gin" "strings" "time" ) func AgentBindMediumList(c *gin.Context) { //代理 查旗下 媒体 var req md.MediumListReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } var tmp model.AgentList MasterDb(c).Where("id=?", req.Id).Get(&tmp) engine := db.Db agentWithMediumDb := implement.NewAgentWithMediumDb(engine) data, total, _ := agentWithMediumDb.FindAgentWithMediumList(req.Name, req.State, tmp.AgentId, 0, utils.StrToInt(req.Page), utils.StrToInt(req.Limit)) list := make([]md.MediumListData, 0) NewMediumDb := implement2.NewMediumDb(MasterDb(c)) if data != nil { for _, v := range data { tmp := md.MediumListData{ Id: utils.IntToStr(v.AgentWithMedium.Id), MediumId: utils.IntToStr(v.AgentWithMedium.MediumId), CompanyName: v.MediumList.CompanyName, UnifiedSocialCreditCode: v.MediumList.UnifiedSocialCreditCode, State: utils.IntToStr(v.MediumList.State), Memo: v.MediumList.Memo, LegalRepresentative: v.MediumList.LegalRepresentative, BusinessLicenseAddress: v.MediumList.BusinessLicenseAddress, } medium := NewMediumDb.GetSuperAdmin(v.AgentWithMedium.MediumId) if medium != nil { tmp.Account = medium.Username } list = append(list, tmp) } } res := md.MediumListRes{ List: list, Total: total, State: []md.SelectData{ {Name: "待提交", Value: "0"}, {Name: "待审核", Value: "1"}, {Name: "审核通过", Value: "2"}, {Name: "审核拒绝", Value: "3"}, }, } e.OutSuc(c, res, nil) return } func MediumBindAgentList(c *gin.Context) { var req md.MediumListReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } var tmp model.MediumList MasterDb(c).Where("id=?", req.Id).Get(&tmp) engine := db.Db agentWithMediumDb := implement.NewAgentWithMediumDb(engine) data, total, _ := agentWithMediumDb.FindAgentWithMediumList(req.Name, req.State, 0, tmp.MediumId, utils.StrToInt(req.Page), utils.StrToInt(req.Limit)) list := make([]md.MediumListData, 0) NewAgentDb := implement2.NewAgentDb(MasterDb(c)) if data != nil { for _, v := range data { tmp := md.MediumListData{ Id: utils.IntToStr(v.AgentWithMedium.Id), MediumId: utils.IntToStr(v.AgentWithMedium.AgentId), CompanyName: v.AgentList.CompanyName, UnifiedSocialCreditCode: v.AgentList.UnifiedSocialCreditCode, State: utils.IntToStr(v.AgentList.State), Memo: v.AgentList.Memo, LegalRepresentative: v.AgentList.LegalRepresentative, BusinessLicenseAddress: v.AgentList.BusinessLicenseAddress, } agent := NewAgentDb.GetSuperAdmin(v.AgentWithMedium.AgentId) if agent != nil { tmp.Account = agent.Username } list = append(list, tmp) } } res := md.MediumListRes{ List: list, Total: total, State: []md.SelectData{ {Name: "待提交", Value: "0"}, {Name: "待审核", Value: "1"}, {Name: "审核通过", Value: "2"}, {Name: "审核拒绝", Value: "3"}, }, } e.OutSuc(c, res, nil) return } func MediumBindAgentSave(c *gin.Context) { var req md.MediumListSaveReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } agentDb := implement2.NewAgentDb(MasterDb(c)) req.Username = strings.ReplaceAll(req.Username, " ", "") username, _ := agentDb.GetAgent(utils.StrToInt(req.Username)) if username == nil { e.OutErr(c, 400, e.NewErr(400, "渠道代理不存在")) return } mediumDb := implement.NewAgentWithMediumDb(db.Db) data := mediumDb.GetAgentWithMediumByMediumIdAndAgentId(utils.StrToInt(req.MediumId), username.AgentId) if data != nil { e.OutErr(c, 400, e.NewErr(400, "已绑定过该渠道代理")) return } var tmp = model.AgentWithMedium{ AgentId: username.AgentId, MediumId: utils.StrToInt(req.MediumId), CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), } db.Db.InsertOne(&tmp) e.OutSuc(c, "success", nil) return } func MediumBindAgentDel(c *gin.Context) { var req md.MediumListDelReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } db.Db.In("id", strings.Split(req.Id, ",")).Delete(&model.AgentWithMedium{}) e.OutSuc(c, "success", nil) return } func GetMediumId(c *gin.Context, name string) string { mediumId := "" if name != "" { ids := []string{"-1"} var tmp []model2.Medium MasterDb(c).Where("memo like ? or username like ?", "%"+name+"%", "%"+name+"%").Find(&tmp) for _, v := range tmp { ids = append(ids, utils.IntToStr(v.MediumId)) } var tmp1 []model.MediumList MasterDb(c).Where("company_name like ? or company_abbreviation like ?", "%"+name+"%", "%"+name+"%").Find(&tmp1) for _, v := range tmp1 { ids = append(ids, utils.IntToStr(v.MediumId)) } mediumId = strings.Join(ids, ",") } return mediumId } func GetMediumByAccountId(c *gin.Context, name, account string) string { mediumId := "" if name != "" || account != "" { ids := []string{"-1"} var tmp []model2.Medium sess := MasterDb(c).Where("1=1") if name != "" { sess.And("memo like ?", "%"+name+"%") var tmp1 []model.MediumList MasterDb(c).Where("company_name like ? or company_abbreviation like ?", "%"+name+"%", "%"+name+"%").Find(&tmp1) for _, v := range tmp1 { ids = append(ids, utils.IntToStr(v.MediumId)) } } if account != "" { sess.And(" username like ?", "%"+name+"%", "%"+name+"%") } sess.Find(&tmp) for _, v := range tmp { ids = append(ids, utils.IntToStr(v.MediumId)) } mediumId = strings.Join(ids, ",") } return mediumId } func GetMediumInfo(c *gin.Context, mediumId int) map[string]string { var res = map[string]string{ "account": "", "name": "", } NewMediumDb := implement2.NewMediumDb(MasterDb(c)) NewMediumListDb := implement.NewMediumListDb(db.Db) medium := NewMediumDb.GetSuperAdmin(mediumId) if medium != nil { res["account"] = medium.Username res["name"] = medium.Memo } NewMediumList, _ := NewMediumListDb.GetMediumList(mediumId) if NewMediumList != nil { if NewMediumList.CompanyName != "" { res["name"] = NewMediumList.CompanyName } if NewMediumList.CompanyAbbreviation != "" { res["name"] = NewMediumList.CompanyAbbreviation } } return res } func GetAgentId(c *gin.Context, name string) string { mediumId := "" if name != "" { ids := []string{"-1"} var tmp []model2.Agent MasterDb(c).Where("memo like ? or username like ?", "%"+name+"%", "%"+name+"%").Find(&tmp) for _, v := range tmp { ids = append(ids, utils.IntToStr(v.AgentId)) } var tmp1 []model.AgentList MasterDb(c).Where("company_name like ? or company_abbreviation like ?", "%"+name+"%", "%"+name+"%").Find(&tmp1) for _, v := range tmp1 { ids = append(ids, utils.IntToStr(v.AgentId)) } mediumId = strings.Join(ids, ",") } return mediumId } func GetAgentByAccountId(c *gin.Context, name, account string) string { mediumId := "" if name != "" || account != "" { ids := []string{"-1"} var tmp []model2.Agent sess := MasterDb(c).Where("1=1") if name != "" { sess.And("memo like ?", "%"+name+"%") var tmp1 []model.AgentList MasterDb(c).Where("company_name like ? or company_abbreviation like ?", "%"+name+"%", "%"+name+"%").Find(&tmp1) for _, v := range tmp1 { ids = append(ids, utils.IntToStr(v.AgentId)) } } if account != "" { sess.And(" username like ?", "%"+name+"%", "%"+name+"%") } sess.Find(&tmp) for _, v := range tmp { ids = append(ids, utils.IntToStr(v.AgentId)) } mediumId = strings.Join(ids, ",") } return mediumId } func GetAgentInfo(c *gin.Context, mediumId int) map[string]string { var res = map[string]string{ "account": "", "name": "", } NewAgentDb := implement2.NewAgentDb(MasterDb(c)) NewAgentListDb := implement.NewAgentListDb(MasterDb(c)) medium := NewAgentDb.GetSuperAdmin(mediumId) if medium != nil { res["account"] = medium.Username res["name"] = medium.Memo } NewAgentList, _ := NewAgentListDb.GetAgentList(mediumId) if NewAgentList != nil { if NewAgentList.CompanyName != "" { res["name"] = NewAgentList.CompanyName } if NewAgentList.CompanyAbbreviation != "" { res["name"] = NewAgentList.CompanyAbbreviation } } return res }