Files
2022-03-17 12:11:38 +08:00

176 lines
4.6 KiB
Go

package manage
import (
"SciencesServer/app/api/admin/model"
"SciencesServer/app/basic/config"
"SciencesServer/app/basic/controller"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/session"
config2 "SciencesServer/config"
"SciencesServer/utils"
"errors"
"strings"
"time"
)
type Agent struct {
*session.Admin
}
type AgentHandle func(session *session.Admin) *Agent
type (
// AgentInfo 经纪人信息
AgentInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Mobile string `json:"mobile"`
Industrys []string `json:"industrys"`
CredentialImage string `json:"credential_image"`
CreatedAt time.Time `json:"created_at"`
Area string `json:"area"`
Demand struct {
Total int `json:"total"`
Complete int `json:"complete"`
} `json:"detail"`
model2.Examine
}
// AgentDetail 经纪人详细信息
AgentDetail struct {
ID string `json:"id"`
TenantID string `json:"tenant_id"`
*model2.ManageAgent
IDImage *model2.ManageAgentIDImage `json:"id_image"`
}
// AgentSelect 经纪人筛选信息
AgentSelect struct {
ID string `json:"id"`
Name string `json:"name"`
Mobile string `json:"mobile"`
}
// AgentParams 经纪人参数信息
AgentParams struct {
ID, TenantID uint64
}
)
// Instance 首页信息
func (c *Agent) Instance(tenantID uint64, name string, status int, page, pageSize int) (*controller.ReturnPages, error) {
mManageAgent := model.NewManageAgent()
where := make([]*model2.ModelWhere, 0)
if c.TenantID > 0 {
where = append(where, model2.NewWhere("a.tenant_id", c.TenantID))
}
if tenantID > 0 {
where = append(where, model2.NewWhere("a.tenant_id", tenantID))
}
if name != "" {
where = append(where, model2.NewWhereLike("a.name", name))
}
if status > 0 {
where = append(where, model2.NewWhere("a.examine_status", status))
}
var count int64
out, err := mManageAgent.Agents(page, pageSize, &count, where...)
if err != nil {
return nil, err
}
list := make([]*AgentInfo, 0)
for _, v := range out {
industrys := make([]string, 0)
for _, v := range v.GetIndustryAttribute() {
industrys = append(industrys, config.GetIndustryInfo(v, "-", ">").Value)
}
demand := struct {
Total int `json:"total"`
Complete int `json:"complete"`
}{}
if v.Demand != "" {
// 筛选处理需求信息
for _, val := range strings.Split(v.Demand, ";") {
objs := strings.Split(val, ":")
count := utils.StringToInt(objs[1])
if model2.TechnologyDemandServiceStatus(utils.StringToInt(objs[0])) == model2.TechnologyDemandServiceStatusForClosedQuestions {
demand.Complete = count
}
demand.Total += count
}
}
list = append(list, &AgentInfo{
ID: v.GetEncodeID(), Name: v.Name, Mobile: v.Mobile, Industrys: industrys,
Examine: v.Examine, CreatedAt: v.CreatedAt, Area: v.FormatBasic(),
Demand: demand,
})
}
return &controller.ReturnPages{Data: list, Count: count}, nil
}
// Select 筛选信息
func (c *Agent) Select(tenantID uint64, keyword string) ([]*AgentSelect, error) {
mManageAgent := model.NewManageAgent()
where := make([]*model2.ModelWhere, 0)
if tenantID > 0 {
where = append(where, model2.NewWhere("tenant_id", tenantID))
}
if keyword != "" {
where = append(where, model2.NewWhereValue("(name LIKE '%"+keyword+"%') OR (mobile LIKE '%"+keyword+"%')"))
}
out, err := mManageAgent.Agent(where...)
if err != nil {
return nil, err
}
list := make([]*AgentSelect, len(out))
for k, v := range out {
list[k] = &AgentSelect{
ID: v.GetEncodeID(),
Name: v.Name,
Mobile: v.Mobile,
}
}
return list, nil
}
// Detail 详细信息
func (c *Agent) Detail(id uint64) (*AgentDetail, error) {
mManageAgent := model.NewManageAgent()
mManageAgent.ID = id
isExist, err := model2.First(mManageAgent)
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("操作错误,经纪人信息不存在或已被删除")
}
idImage := mManageAgent.GetIDImageAttribute()
idImage.Front = (&model2.Image{Image: idImage.Front}).Analysis(config2.SystemConfig[config2.SysImageDomain])
idImage.Behind = (&model2.Image{Image: idImage.Behind}).Analysis(config2.SystemConfig[config2.SysImageDomain])
idImage.Hold = (&model2.Image{Image: idImage.Hold}).Analysis(config2.SystemConfig[config2.SysImageDomain])
return &AgentDetail{
ID: mManageAgent.GetEncodeID(),
TenantID: mManageAgent.GetEncodeTenantID(),
ManageAgent: mManageAgent.ManageAgent,
IDImage: idImage,
}, nil
}
func NewAgent() AgentHandle {
return func(session *session.Admin) *Agent {
return &Agent{session}
}
}