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 { tmp1 := 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, BusinessLicenseImg: v.AgentList.BusinessLicenseImgUrl, } agent := NewAgentDb.GetSuperAdmin(v.AgentWithMedium.AgentId) if agent != nil { tmp1.Account = agent.Username } list = append(list, tmp1) } } 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 db.Db.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 ?", "%"+account+"%") } 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(db.Db) 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 } func GetMediumIdStr(c *gin.Context, admId int, name, account string) string { appId := GetMediumByAccountId(c, name, account) appIds := []string{"-1"} NewAdminBindMediumDb := implement2.NewAdminBindMediumDb(MasterDb(c)) list := NewAdminBindMediumDb.FindAll(admId) ids := make([]string, 0) for _, v := range list { appIds = append(appIds, utils.IntToStr(v.MediumId)) ids = append(ids, utils.IntToStr(v.MediumId)) } NewAdminDb := implement2.NewAdminDb(MasterDb(c)) super, _ := NewAdminDb.GetSuperAdmin() if len(list) == 0 { user := GetUser(c) NewAppletApplicationDb := implement2.NewMediumDb(MasterDb(c)) appList := NewAppletApplicationDb.FindAllSuperAdmin() if appList != nil { for _, v := range *appList { if user.IsSuperAdministrator != 1 && user.ShowTime != "" { showTime := utils.TimeStdParseUnix(user.ShowTime + " 00:00:00") if utils.TimeStdParseUnix(v.CreateAt) >= showTime { appIds = append(appIds, utils.IntToStr(v.MediumId)) ids = append(ids, utils.IntToStr(v.MediumId)) } } } } } if admId == 0 || admId == super.AdmId { //为空就查全部 appIds = []string{} } if appId != "" { //不为空就判断 有没有在列表里面 appIds = []string{"-1"} ex := strings.Split(appId, ",") for _, v := range ex { if utils.InArr(v, ids) || admId == super.AdmId { appIds = append(appIds, v) } } } str := "" if len(appIds) > 0 { str = strings.Join(appIds, ",") } return str } func GetAgentIdStr(c *gin.Context, admId int, name, account string) string { appId := GetAgentByAccountId(c, name, account) appIds := []string{"-1"} NewAdminBindAgentDb := implement2.NewAdminBindAgentDb(MasterDb(c)) list := NewAdminBindAgentDb.FindAll(admId) ids := make([]string, 0) for _, v := range list { appIds = append(appIds, utils.IntToStr(v.AgentId)) ids = append(ids, utils.IntToStr(v.AgentId)) } NewAdminDb := implement2.NewAdminDb(MasterDb(c)) super, _ := NewAdminDb.GetSuperAdmin() if len(list) == 0 { user := GetUser(c) NewAppletApplicationDb := implement2.NewAgentDb(MasterDb(c)) appList := NewAppletApplicationDb.FindAllSuperAdmin() if appList != nil { for _, v := range *appList { if user.IsSuperAdministrator != 1 && user.ShowTime != "" { showTime := utils.TimeStdParseUnix(user.ShowTime + " 00:00:00") if utils.TimeStdParseUnix(v.CreateAt) >= showTime { appIds = append(appIds, utils.IntToStr(v.AgentId)) ids = append(ids, utils.IntToStr(v.AgentId)) } } } } } if admId == 0 || admId == super.AdmId { //为空就查全部 appIds = []string{} } if appId != "" { //不为空就判断 有没有在列表里面 appIds = []string{"-1"} ex := strings.Split(appId, ",") for _, v := range ex { if utils.InArr(v, ids) || admId == super.AdmId { appIds = append(appIds, v) } } } str := "" if len(appIds) > 0 { str = strings.Join(appIds, ",") } return str }