feat:完善公司导入模块

This commit is contained in:
henry
2022-05-10 23:01:03 +08:00
parent 3f2ba359b3
commit c8e5a7601d
2 changed files with 160 additions and 5 deletions

View File

@ -9,9 +9,11 @@ import (
"SciencesServer/app/session"
config2 "SciencesServer/config"
"SciencesServer/lib"
"SciencesServer/utils"
"errors"
"fmt"
"strings"
"time"
)
type Company struct {
@ -43,7 +45,6 @@ type (
// CompanyParams 公司企业参数信息
CompanyParams struct {
}
// CompanyExcel 公司企业表格信息
CompanyExcel struct {
Name string `json:"name" xlsx:"企业名称"`
@ -230,6 +231,7 @@ func (c *Company) Detail(id uint64) (*CompanyDetail, error) {
// Import 导入数据
func (c *Company) Import(file string) error {
excel := lib.NewExcel()
err := excel.Import(strings.Replace(file, "/", "", 1), 1, 0)
if err != nil {
@ -237,17 +239,170 @@ func (c *Company) Import(file string) error {
}
data := excel.Analysis(&CompanyExcel{})
//now := time.Now()
rows := make([]*model2.ManageCompany, 0, len(data))
now := time.Now()
//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
}
// 所属领域
//industrys := strings.Split(_data.Industry, "\n")
fmt.Println(_data)
row.ModelTenant = model2.ModelTenant{}
row.Kind = _data.kind()
row.Name = _data.Name
row.Code = _data.Code
row.Area = _data.area()
row.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}

View File

@ -1,7 +1,7 @@
package model
import (
"SciencesServer/app/api/enterprise/model"
"SciencesServer/app/common/model"
)
type SysIndustry struct {