410 lines
11 KiB
Go
410 lines
11 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/service"
|
||
"SciencesServer/app/session"
|
||
config2 "SciencesServer/config"
|
||
"SciencesServer/lib"
|
||
"SciencesServer/utils"
|
||
"errors"
|
||
"fmt"
|
||
"strings"
|
||
"time"
|
||
)
|
||
|
||
type Company struct {
|
||
*session.Admin
|
||
}
|
||
|
||
type CompanyHandle func(session *session.Admin) *Company
|
||
|
||
type (
|
||
// CompanyInstance 公司企业信息
|
||
CompanyInstance struct {
|
||
ID string `json:"id"`
|
||
*model.ManageCompanyInfo
|
||
Industrys []string `json:"industrys"`
|
||
Address string `json:"address"`
|
||
Area string `json:"area"`
|
||
}
|
||
// CompanyDetail 公司企业详细信息
|
||
CompanyDetail struct {
|
||
ID string `json:"id"`
|
||
TenantID string `json:"tenant_id"`
|
||
InvitedCode string `json:"invited_code"`
|
||
*model2.ManageCompany
|
||
Industrys []*config.Industry `json:"industrys"`
|
||
Keywords []string `json:"keywords"`
|
||
Directions []string `json:"directions"`
|
||
Area string `json:"area"`
|
||
}
|
||
// CompanyParams 公司企业参数信息
|
||
CompanyParams struct {
|
||
}
|
||
// CompanyExcel 公司企业表格信息
|
||
CompanyExcel struct {
|
||
Name string `json:"name" xlsx:"企业名称"`
|
||
Province string `json:"province" xlsx:"所在省"`
|
||
City string `json:"city" xlsx:"所在市"`
|
||
District string `json:"district" xlsx:"所在区"`
|
||
Address string `json:"address" xlsx:"详细地址"`
|
||
Code string `json:"code" xlsx:"信用代码"`
|
||
RegisterAt string `json:"register_at" xlsx:"注册时间"`
|
||
Industry string `json:"industry" xlsx:"所属领域"`
|
||
Url string `json:"url" xlsx:"企业网站"`
|
||
Introduce string `json:"introduce" xlsx:"企业简介"`
|
||
Kind string `json:"kind" xlsx:"企业类型"`
|
||
Product string `json:"product" xlsx:"核心技术与产品及应用场景"`
|
||
Keyword string `json:"keyword" xlsx:"关键词"`
|
||
Contact string `json:"contact" xlsx:"联系人"` // 联系人
|
||
ContactMobile string `json:"contact_mobile" xlsx:"联系电话"` // 联系方式
|
||
}
|
||
)
|
||
|
||
// Instance 首页信息
|
||
func (c *Company) Instance(tenantID uint64, name string, status int, page, pageSize int) (*controller.ReturnPages, error) {
|
||
mManageCompany := model.NewManageCompany()
|
||
|
||
where := make([]*model2.ModelWhere, 0)
|
||
|
||
if c.TenantID > 0 {
|
||
where = append(where, model2.NewWhere("c.tenant_id", c.TenantID))
|
||
}
|
||
if tenantID > 0 {
|
||
where = append(where, model2.NewWhere("c.tenant_id", tenantID))
|
||
}
|
||
if name != "" {
|
||
where = append(where, model2.NewWhereLike("c.name", name))
|
||
}
|
||
if status > 0 {
|
||
where = append(where, model2.NewWhere("c.examine_status", status))
|
||
}
|
||
var count int64
|
||
|
||
out, err := mManageCompany.Companys(page, pageSize, &count, where...)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
list := make([]*CompanyInstance, 0)
|
||
|
||
for _, v := range out {
|
||
mManageCompany.Industry = v.Industry
|
||
|
||
_industrys := make([]string, 0)
|
||
|
||
for _, v := range mManageCompany.GetIndustryAttribute() {
|
||
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", "/").Value)
|
||
}
|
||
list = append(list, &CompanyInstance{
|
||
ID: v.GetEncodeID(), ManageCompanyInfo: v, Industrys: _industrys,
|
||
Address: v.FormatBasic(), Area: (&model2.Area{
|
||
Province: v.TenantProvince,
|
||
City: v.TenantCity,
|
||
}).FormatBasic(),
|
||
})
|
||
}
|
||
return &controller.ReturnPages{Data: list, Count: count}, nil
|
||
}
|
||
|
||
// Form 数据操作
|
||
func (c *Company) Form(params *BasicParams, other *config.IdentityForCompany) error {
|
||
mManageCompany := model.NewManageCompany()
|
||
// 查询相应的企业入驻信息
|
||
if params.ID > 0 {
|
||
mManageCompany.ID = params.ID
|
||
|
||
isExist, err := model2.FirstField(mManageCompany.ManageCompany, []string{"id", "name", "code", "tenant_id", "examine_status", "created_at"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,公司企业信息不存在或已被删除")
|
||
}
|
||
if c.TenantID > 0 {
|
||
if mManageCompany.TenantID != c.TenantID {
|
||
return errors.New("操作错误,无权限操作")
|
||
}
|
||
} else {
|
||
if params.Code != mManageCompany.Code {
|
||
if isExist, err = params.isExist(mManageCompany.ManageCompany, model2.NewWhere("code", params.Code)); err != nil {
|
||
return err
|
||
} else if isExist {
|
||
return errors.New("操作错误,当前站点下已存在同一公司组织机构代码")
|
||
}
|
||
}
|
||
}
|
||
}
|
||
mManageCompany.TenantID = c.TenantID
|
||
mManageCompany.Kind = model2.ManageCompanyKind(other.Kind)
|
||
mManageCompany.Image = model2.Image{Image: params.Image}
|
||
mManageCompany.Area = model2.Area{
|
||
Province: params.Area.Province, City: params.Area.City, District: params.Area.District, Address: params.Area.Address,
|
||
}
|
||
mManageCompany.Product = other.Product
|
||
mManageCompany.Url = other.Url
|
||
mManageCompany.SetIndustryAttribute(params.Industrys)
|
||
mManageCompany.SetKeywordAttribute(params.Keywords)
|
||
mManageCompany.SetDirectionAttribute(other.Directions)
|
||
mManageCompany.Introduce = params.Introduce
|
||
|
||
if c.TenantID <= 0 {
|
||
mManageCompany.TenantID = params.TenantID
|
||
}
|
||
_industrys := make([]string, 0)
|
||
|
||
for _, v := range params.Industrys {
|
||
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", "-").Value)
|
||
}
|
||
manage := service.NewESManage(
|
||
service.WithManageIdentity(config.TenantUserIdentityForCompany),
|
||
service.WithManageIndustry(strings.Join(_industrys, ";")),
|
||
service.WithManageKeyword(strings.Join(params.Keywords, ";")),
|
||
)
|
||
if mManageCompany.ID > 0 {
|
||
if err := model2.Updates(mManageCompany.ManageCompany, mManageCompany.ManageCompany); err != nil {
|
||
return nil
|
||
}
|
||
if mManageCompany.ExamineStatus == model2.ExamineStatusForAgree {
|
||
_ = manage.Update()
|
||
}
|
||
return nil
|
||
}
|
||
// 查询手机号码是否在当前租户下是否已经注册了
|
||
mManageCompany.Name = params.Name
|
||
mManageCompany.Code = params.Code
|
||
mManageCompany.License = other.FilterLicense()
|
||
mManageCompany.ExamineStatus = model2.ExamineStatusForAgree
|
||
mManageCompany.ExamineRemark = "主动创建,无需审核"
|
||
|
||
if isExist, err := params.isExist(mManageCompany.ManageCompany,
|
||
model2.NewWhere("code", params.Code)); err != nil {
|
||
return err
|
||
} else if isExist {
|
||
return errors.New("操作错误,已存在同一公司组织机构代码")
|
||
}
|
||
if err := model2.Create(mManageCompany.ManageCompany); err != nil {
|
||
return err
|
||
}
|
||
service.WithManageID(mManageCompany.ID)(manage)
|
||
service.WithManageTitle(params.Name)(manage)
|
||
|
||
return manage.Create()
|
||
}
|
||
|
||
// Detail 详细信息
|
||
func (c *Company) Detail(id uint64) (*CompanyDetail, error) {
|
||
mManageCompany := model.NewManageCompany()
|
||
|
||
out, err := mManageCompany.Company(id)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
} else if out.ManageCompany == nil {
|
||
return nil, errors.New("操作错误,公司企业信息不存在或已被删除")
|
||
}
|
||
_industrys := make([]*config.Industry, 0)
|
||
|
||
for _, v := range out.GetIndustryAttribute() {
|
||
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", ">"))
|
||
}
|
||
out.Image.Image = out.Image.Analysis(config2.SystemConfig[config2.SysImageDomain])
|
||
out.License = (&model2.Image{Image: out.License}).Analysis(config2.SystemConfig[config2.SysImageDomain])
|
||
|
||
return &CompanyDetail{
|
||
ID: out.GetEncodeID(),
|
||
TenantID: out.GetEncodeTenantID(),
|
||
InvitedCode: (&model2.Model{ID: out.InviterID}).GetEncodeID(),
|
||
ManageCompany: out.ManageCompany,
|
||
Industrys: _industrys,
|
||
Keywords: out.GetKeywordAttribute(),
|
||
Directions: out.GetDirectionAttribute(),
|
||
//Area: out.FormatBasic(),
|
||
}, nil
|
||
}
|
||
|
||
// Import 导入数据
|
||
func (c *Company) Import(file string) error {
|
||
excel := lib.NewExcel()
|
||
|
||
err := excel.Import(strings.Replace(file, "/", "", 1), 1, 0)
|
||
|
||
if err != nil {
|
||
return err
|
||
}
|
||
data := excel.Analysis(&CompanyExcel{})
|
||
|
||
rows := make([]*model2.ManageCompany, 0, len(data))
|
||
|
||
now := time.Now()
|
||
|
||
for _, v := range data {
|
||
_data := v.(*CompanyExcel)
|
||
|
||
row := new(model2.ManageCompany)
|
||
|
||
var count int64
|
||
// 查询公司是否存在
|
||
if err = model2.Count(row, &count, model2.NewWhere("code", _data.Code)); err != nil {
|
||
return err
|
||
}
|
||
if count > 0 {
|
||
continue
|
||
}
|
||
industrys := make([]string, 0)
|
||
|
||
if industrys, err = _data.industry(); err != nil {
|
||
return err
|
||
}
|
||
// 所属领域
|
||
row.ModelTenant = model2.ModelTenant{TenantID: _data.tenant()}
|
||
row.Kind = _data.kind()
|
||
row.Name = _data.Name
|
||
row.Code = _data.Code
|
||
row.Area = _data.area()
|
||
row.Product = _data.Product
|
||
row.Url = _data.Url
|
||
row.License = ""
|
||
row.SetIndustryAttribute(industrys)
|
||
row.SetKeywordAttribute(_data.keyword())
|
||
row.Direction = ""
|
||
row.Introduce = _data.Introduce
|
||
row.Examine = model2.Examine{
|
||
ExamineStatus: model2.ExamineStatusForAgree,
|
||
ExamineRemark: "主动上传,无需审核",
|
||
ExamineAt: now,
|
||
}
|
||
row.CreatedAt = now
|
||
row.UpdatedAt = now
|
||
|
||
rows = append(rows, row)
|
||
}
|
||
if len(rows) > 0 {
|
||
mManageCompany := model2.NewManageCompany()
|
||
|
||
if err = model2.Creates(mManageCompany, rows); err != nil {
|
||
return err
|
||
}
|
||
manage := service.NewESManage()
|
||
|
||
for _, row := range rows {
|
||
_industrys := make([]string, 0)
|
||
|
||
for _, v := range row.GetIndustryAttribute() {
|
||
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", "-").Value)
|
||
}
|
||
service.WithManageID(row.ID)(manage)
|
||
service.WithManageTitle(row.Name)(manage)
|
||
service.WithManageIdentity(config.TenantUserIdentityForCompany)(manage)
|
||
service.WithManageIndustry(strings.Join(_industrys, ";"))(manage)
|
||
service.WithManageKeyword(strings.Join(row.GetKeywordAttribute(), ";"))(manage)
|
||
}
|
||
if err = manage.Create(); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (c *CompanyExcel) tenant() uint64 {
|
||
return 0
|
||
}
|
||
|
||
func (c *CompanyExcel) kind() model2.ManageCompanyKind {
|
||
switch c.Kind {
|
||
case "上市企业":
|
||
return model2.ManageCompanyKindForListedEnterprise
|
||
case "优质企业":
|
||
return model2.ManageCompanyKindForHighQuality
|
||
case "普通企业":
|
||
return model2.ManageCompanyKindForOrdinary
|
||
}
|
||
return 0
|
||
}
|
||
|
||
func (c *CompanyExcel) area() model2.Area {
|
||
area := model2.Area{
|
||
Address: c.Address,
|
||
}
|
||
|
||
filter := func(key, value string) string {
|
||
areaInfo, has := config.MemoryForAreaInfo[key]
|
||
|
||
if !has {
|
||
return ""
|
||
}
|
||
if value == "" {
|
||
return ""
|
||
}
|
||
for k, v := range areaInfo {
|
||
if v == value {
|
||
return k
|
||
}
|
||
}
|
||
return ""
|
||
}
|
||
// 省
|
||
area.Province = filter(config2.DefaultChinaAreaCode, c.Province)
|
||
|
||
if area.Province != "" {
|
||
area.City = filter(area.Province, c.City)
|
||
|
||
if area.City != "" {
|
||
area.District = filter(area.District, c.City)
|
||
}
|
||
}
|
||
return area
|
||
}
|
||
|
||
func (c *CompanyExcel) industry() ([]string, error) {
|
||
// 存在多个
|
||
industrys := strings.Split(c.Industry, "\n")
|
||
|
||
out := make([]string, 0)
|
||
|
||
for _, v := range industrys {
|
||
_industrys := strings.Split(v, "/")
|
||
|
||
_out := make([]string, 0, len(_industrys))
|
||
|
||
for key, industry := range _industrys {
|
||
mSysIndustry := model.NewSysIndustry()
|
||
|
||
isExist, err := model2.FirstField(mSysIndustry.SysIndustry, []string{"id"}, model2.NewWhere("name", industry))
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if !isExist {
|
||
if key > 0 {
|
||
mSysIndustry.ParentID = utils.StringToUnit64(_out[key-1])
|
||
}
|
||
mSysIndustry.Name = industry
|
||
|
||
if err = model2.Create(mSysIndustry.SysIndustry); err != nil {
|
||
return nil, err
|
||
}
|
||
} else {
|
||
_out = append(_out, fmt.Sprintf("%d", mSysIndustry.ID))
|
||
}
|
||
out = append(out, strings.Join(_out, "-"))
|
||
}
|
||
}
|
||
return out, nil
|
||
}
|
||
|
||
func (c *CompanyExcel) keyword() []string {
|
||
return strings.Split(c.Keyword, "\n")
|
||
}
|
||
|
||
func NewCompany() CompanyHandle {
|
||
return func(session *session.Admin) *Company {
|
||
return &Company{session}
|
||
}
|
||
}
|