feat:完善网站信息

This commit is contained in:
henry
2021-12-22 14:11:14 +08:00
parent b0a9ef3569
commit 332f67d1c1
14 changed files with 637 additions and 28 deletions

40
app/api/website/api/es.go Normal file
View File

@ -0,0 +1,40 @@
package api
import (
"SciencesServer/app/basic/api"
"SciencesServer/app/service"
"github.com/gin-gonic/gin"
)
type ES struct{}
func (*ES) Create(c *gin.Context) {
form := &struct {
ID uint64 `json:"id" form:"id"`
Title string `json:"title" form:"title"`
Keyword string `json:"keyword" form:"keyword"`
}{}
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
manage := service.NewManage(
service.WithManageID(form.ID),
service.WithManageTitle(form.Title),
service.WithManageKeyword(form.Keyword),
)
api.APIResponse(manage.Create())(c)
}
func (*ES) Search(c *gin.Context) {
form := &struct {
Params map[string]interface{} `json:"params" form:"params"`
}{}
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
manage := service.NewManage()
data, err := manage.Search(form.Params)
api.APIResponse(err, data)(c)
}

View File

@ -0,0 +1,62 @@
package api
import (
"SciencesServer/app/api/website/controller/technology"
"SciencesServer/app/basic/api"
"github.com/gin-gonic/gin"
)
type Technology struct{}
func (*Technology) Patent(c *gin.Context) {
form := &struct {
Title string `json:"title" form:"title"`
Industry string `json:"industry" form:"industry"`
api.PageForm
}{}
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
data, err := technology.NewPatent()().Instance(form.Title, form.Industry, form.Page, form.PageSize)
api.APIResponse(err, data)(c)
}
func (*Technology) PatentDetail(c *gin.Context) {
form := &struct {
api.IDStringForm
}{}
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
data, err := technology.NewPatent()().Detail(form.Convert())
api.APIResponse(err, data)(c)
}
func (*Technology) Demand(c *gin.Context) {
form := &struct {
Title string `json:"title" form:"title"`
Industry string `json:"industry" form:"industry"`
Kind string `json:"kind" form:"kind"`
api.PageForm
}{}
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
data, err := technology.NewDemand()(nil).Instance(form.Title, form.Industry, form.Kind, form.Page, form.PageSize)
api.APIResponse(err, data)(c)
}
func (*Technology) DemandDetail(c *gin.Context) {
form := &struct {
api.IDStringForm
}{}
if err := api.Bind(form)(c); err != nil {
api.APIFailure(err.(error))(c)
return
}
data, err := technology.NewDemand()(nil).Detail(form.Convert())
api.APIResponse(err, data)(c)
}

View File

@ -0,0 +1,121 @@
package manage
import (
"SciencesServer/app/api/website/model"
"SciencesServer/app/basic/config"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/service"
"errors"
)
type Search struct{}
type SearchHandle func() *Search
type (
// SearchParams 搜索参数
SearchParams struct {
}
)
// searchIdentityHandle 搜索信息处理
var searchIdentityHandle = map[int]func(ids []uint64) (interface{}, error){
config.TenantUserIdentityForCompany: company,
config.TenantUserIdentityForExpert: company,
config.TenantUserIdentityForResearch: company,
config.TenantUserIdentityForLaboratory: company,
config.TenantUserIdentityForAgent: company,
}
func company(ids []uint64) (interface{}, error) {
mManageCompany := model.NewManageCompany()
out := make([]*model2.ManageCompany, 0)
if err := model2.ScanFields(mManageCompany.ManageCompany, &out, []string{}, &model2.ModelWhereOrder{
Where: model2.NewWhereIn("id", ids),
Order: model2.NewOrder("id", model2.OrderModeToDesc),
}, &model2.ModelWhereOrder{
Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree),
}); err != nil {
return nil, err
}
return out, nil
}
func expert(ids []uint64) (interface{}, error) {
mManageCompany := model.NewManageCompany()
out := make([]*model2.ManageCompany, 0)
if err := model2.ScanFields(mManageCompany.ManageCompany, &out, []string{}, &model2.ModelWhereOrder{
Where: model2.NewWhereIn("id", ids),
Order: model2.NewOrder("id", model2.OrderModeToDesc),
}, &model2.ModelWhereOrder{
Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree),
}); err != nil {
return nil, err
}
return out, nil
}
func research(ids []uint64) (interface{}, error) {
mManageCompany := model.NewManageCompany()
out := make([]*model2.ManageCompany, 0)
if err := model2.ScanFields(mManageCompany.ManageCompany, &out, []string{}, &model2.ModelWhereOrder{
Where: model2.NewWhereIn("id", ids),
Order: model2.NewOrder("id", model2.OrderModeToDesc),
}, &model2.ModelWhereOrder{
Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree),
}); err != nil {
return nil, err
}
return out, nil
}
func laboratory(ids []uint64) (interface{}, error) {
mManageCompany := model.NewManageCompany()
out := make([]*model2.ManageCompany, 0)
if err := model2.ScanFields(mManageCompany.ManageCompany, &out, []string{}, &model2.ModelWhereOrder{
Where: model2.NewWhereIn("id", ids),
Order: model2.NewOrder("id", model2.OrderModeToDesc),
}, &model2.ModelWhereOrder{
Where: model2.NewWhere("examine_status", model2.ExamineStatusForAgree),
}); err != nil {
return nil, err
}
return out, nil
}
func (c *Search) Launch(identity int, params string) (interface{}, error) {
manage := service.NewManage()
_params := map[string]interface{}{
"title": params, "keyword": params, "research": params,
}
data, err := manage.Search(_params)
if err != nil {
return nil, err
}
ids := make([]uint64, 0)
for _, v := range data.([]*service.Manage) {
if v.Identity != identity {
continue
}
ids = append(ids, v.ID)
}
handle, has := searchIdentityHandle[identity]
if !has {
return nil, errors.New("操作错误,未知的身份信息")
}
return handle(ids)
}
func NewSearch() SearchHandle {
return func() *Search {
return &Search{}
}
}

View File

@ -0,0 +1,100 @@
package technology
import (
"SciencesServer/app/api/website/model"
"SciencesServer/app/basic/controller"
model2 "SciencesServer/app/common/model"
"SciencesServer/app/session"
"errors"
"fmt"
"time"
)
type Demand struct {
*session.Enterprise
}
type DemandHandle func(session *session.Enterprise) *Demand
type (
// DemandInfo 需求基本信息
DemandInfo struct {
ID string `json:"id"`
Kinds []string `json:"kinds"`
Industrys []string `json:"industrys"`
Deadline time.Time `json:"deadline"`
}
// DemandDetailInfo 需求详细信息
DemandDetailInfo struct {
ID string `json:"id"`
*model2.TechnologyDemand
Kinds []string `json:"kinds"`
Industrys []string `json:"industrys"`
}
)
// Instance 需求信息
func (c *Demand) Instance(title, industry, kind string, page, pageSize int) (*controller.ReturnPages, error) {
mTechnologyDemand := model.NewTechnologyDemand()
out := make([]*model2.TechnologyDemand, 0)
var count int64
where := []*model2.ModelWhereOrder{
&model2.ModelWhereOrder{
Where: model2.NewWhere("status", model2.TechnologyDemandStatusForAgree),
Order: model2.NewOrder("id", model2.OrderModeToDesc),
},
}
if title != "" {
where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhere("title", title)})
}
if industry != "" {
where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhereCondition("industry", "LIKE",
"%"+fmt.Sprintf(`"%v`, industry)+"%")})
}
if kind != "" {
where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhereLike("kind", kind)})
}
err := model2.PagesFields(mTechnologyDemand.TechnologyDemand, &out, []string{}, page, pageSize, &count, where...)
if err != nil {
return nil, err
}
list := make([]*DemandInfo, 0)
for _, v := range out {
list = append(list, &DemandInfo{
ID: v.GetEncodeID(), Kinds: v.GetKindAttribute(),
Industrys: v.GetIndustryAttribute(), Deadline: v.Deadline,
})
}
return &controller.ReturnPages{Data: list, Count: count}, nil
}
// Detail 详细信息
func (c *Demand) Detail(id uint64) (*DemandDetailInfo, error) {
mTechnologyDemand := model.NewTechnologyDemand()
mTechnologyDemand.ID = id
isExist, err := model2.First(mTechnologyDemand.TechnologyDemand)
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("操作错误,需求信息不存在或已被删除")
}
return &DemandDetailInfo{
ID: mTechnologyDemand.GetEncodeID(),
TechnologyDemand: mTechnologyDemand.TechnologyDemand,
Kinds: mTechnologyDemand.GetKindAttribute(),
Industrys: mTechnologyDemand.GetIndustryAttribute(),
}, nil
}
func NewDemand() DemandHandle {
return func(session *session.Enterprise) *Demand {
return &Demand{Enterprise: session}
}
}

View File

@ -0,0 +1,78 @@
package technology
import (
"SciencesServer/app/api/website/model"
"SciencesServer/app/basic/controller"
model2 "SciencesServer/app/common/model"
"errors"
"fmt"
)
type Patent struct{}
type PatentHandle func() *Patent
type (
// PatentInfo 专利信息
PatentInfo struct {
ID string `json:"id"`
*model.SysPatentInfo
}
// PatentDetailInfo 专利详细信息
PatentDetailInfo struct {
ID string `json:"id"`
*model2.SysPatent
}
)
// Instance 查询信息
func (c *Patent) Instance(title, industry string, page, pageSize int) (*controller.ReturnPages, error) {
mSysPatent := model.NewSysPatent()
where := make([]*model2.ModelWhere, 0)
if title != "" {
where = append(where, model2.NewWhereLike("p.title", title))
}
if industry != "" {
where = append(where, model2.NewWhereCondition("c.industry_detail", "LIKE",
"%"+fmt.Sprintf(`"%v`, industry)+"%"))
}
var count int64
out, err := mSysPatent.Patent(page, pageSize, &count, where...)
if err != nil {
return nil, err
}
list := make([]*PatentInfo, 0)
for _, v := range out {
list = append(list, &PatentInfo{
ID: v.GetEncodeID(),
SysPatentInfo: v,
})
}
return &controller.ReturnPages{Data: list, Count: count}, nil
}
// Detail 详细信息
func (c *Patent) Detail(id uint64) (*PatentDetailInfo, error) {
mSysPatent := model.NewSysPatent()
mSysPatent.ID = id
isExist, err := model2.First(mSysPatent.SysPatent)
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("操作错误,专利信息不存在或已被删除")
}
return &PatentDetailInfo{ID: mSysPatent.GetEncodeID(), SysPatent: mSysPatent.SysPatent}, nil
}
func NewPatent() PatentHandle {
return func() *Patent {
return &Patent{}
}
}

View File

@ -10,6 +10,40 @@ type SysPatent struct {
*model.SysPatent
}
type SysPatentInfo struct {
model.Model
Kind model.SysParentKind `json:"kind"`
Title string `json:"title"`
Description string `json:"description"`
ApplyAt string `json:"apply_at"`
}
// Patent 专利信息
func (m *SysPatent) Patent(page, pageSize int, count *int64, where ...*model.ModelWhere) ([]*SysPatentInfo, error) {
db := orm.GetDB().Table(m.TableName()+" AS p").
Select("p.id", "p.title", "LEFT(p.description, 80) AS description", "p.apply_at").
Joins(fmt.Sprintf("LEFT JOIN %s AS c ON p.ipc_code = c.ipc AND c.is_deleted = %d",
model.NewSysPatentClassify().TableName(), model.DeleteStatusForNot)).
Where("p.shelf_status = ?", model.ShelfStatusForUp).
Where("p.is_deleted = ?", model.DeleteStatusForNot)
if len(where) > 0 {
for _, v := range where {
db = db.Where(v.Condition, v.Value)
}
}
out := make([]*SysPatentInfo, 0)
if err := db.Count(count).Error; err != nil {
return nil, err
}
if err := db.Order("p.id " + model.OrderModeToDesc).Offset((page - 1) * pageSize).Limit(pageSize).Scan(&out).Error; err != nil {
return nil, err
}
return out, nil
}
// Distribution 分布信息
func (m *SysPatent) Distribution() ([]*DataAreaDistributionInfo, error) {
out := make([]*DataAreaDistributionInfo, 0)

View File

@ -83,7 +83,7 @@ type ShelfStatusKind int
const (
// ShelfStatusForUp 上架
ShelfStatusForUp ShelfStatusKind = iota
ShelfStatusForUp ShelfStatusKind = iota + 1
// ShelfStatusForDown 下架
ShelfStatusForDown
)

View File

@ -3,7 +3,7 @@ package model
// SysPatent 专利信息数据模型
type SysPatent struct {
Model
Kind SysParentKind `gorm:"column:kind;type:tinyint(1);default:0;comment:专利类型" json:"kind"`
Kind SysParentKind `gorm:"column:kind;index:idx_sys_patent_kind;type:tinyint(1);default:0;comment:专利类型" json:"kind"`
Title string `gorm:"column:title;type:varchar(100);default:'';comment:名称标题" json:"title"`
FileUrl string `gorm:"column:file_url;type:varchar(255);default:'';comment:文件地址" json:"file_url"`
ApplyCode string `gorm:"column:apply_code;type:varchar(30);default:'';comment:申请号" json:"apply_code"`
@ -15,7 +15,7 @@ type SysPatent struct {
Inventor string `gorm:"column:inventor;type:varchar(255);default:'';comment:发明人" json:"inventor"`
Description string `gorm:"column:description;type:text;comment:摘要" json:"description"`
PrincipalClaim string `gorm:"column:principal_claim;type:text;comment:主权项" json:"principal_claim"`
IPCCode string `gorm:"column:ipc_code;type:varchar(30);default:'';comment:IPC主分类号" json:"ipc_code"`
IPCCode string `gorm:"column:ipc_code;index:idx_sys_patent_ipc_code;type:varchar(30);default:'';comment:IPC主分类号" json:"ipc_code"`
Shelf
Status SysParentStatus `gorm:"column:status;type:tinyint(1);default:1;comment:专利状态(1授权2实审3公开)" json:"-"`
ModelDeleted

7
app/logic/ies.go Normal file
View File

@ -0,0 +1,7 @@
package logic
type IES interface {
Index() string
Create() error
Search(condition map[string]interface{}) (interface{}, error)
}

88
app/service/es.go Normal file
View File

@ -0,0 +1,88 @@
package service
import (
"SciencesServer/serve/es"
"context"
"encoding/json"
"fmt"
"github.com/olivere/elastic/v7"
)
type Manage struct {
ID uint64 `json:"id"` // ID
Identity int `json:"identity"` // 身份信息
Title string `json:"title"` // 名称
Keyword string `json:"keyword"` // 关键词
Research string `json:"research"` // 研究方向
}
type ManageOption func(manage *Manage)
func (c *Manage) Index() string {
return "es_manage_index"
}
func (this *Manage) Create() error {
_bytes, _ := json.Marshal(this)
_, err := es.GetInstance().Index().Index(this.Index()).BodyJson(string(_bytes)).Do(context.Background())
return err
}
func (this *Manage) Search(condition map[string]interface{}) (interface{}, error) {
search := elastic.NewSearchSource()
for k, v := range condition {
search.Query(elastic.NewMatchQuery(k, fmt.Sprintf("%v", v)))
}
service := es.GetInstance().Search().Index(this.Index()).SearchSource(search)
result, err := service.Do(context.Background())
if err != nil {
return nil, err
}
out := make([]*Manage, 0)
for _, hit := range result.Hits.Hits {
data := new(Manage)
if err = json.Unmarshal(hit.Source, data); err != nil {
return nil, err
}
out = append(out, data)
}
return out, nil
}
func WithManageID(id uint64) ManageOption {
return func(manage *Manage) {
manage.ID = id
}
}
func WithManageTitle(title string) ManageOption {
return func(manage *Manage) {
manage.Title = title
}
}
func WithManageKeyword(keyword string) ManageOption {
return func(manage *Manage) {
manage.Keyword = keyword
}
}
func WithManageResearch(research string) ManageOption {
return func(manage *Manage) {
manage.Research = research
}
}
func NewManage(options ...ManageOption) *Manage {
out := new(Manage)
for _, option := range options {
option(out)
}
return out
}

View File

@ -15,6 +15,13 @@ func registerAPI(app *gin.Engine) {
g := app.Group(apiPrefix)
v1 := g.Group("/v1")
esV1 := v1.Group("/es")
{
_api := new(api2.ES)
esV1.POST("/create", _api.Create)
esV1.POST("/search", _api.Search)
}
// Index 首页信息管理
indexV1 := v1.Group("/index")
{
@ -59,6 +66,15 @@ func registerAPI(app *gin.Engine) {
serviceV1.GET("/innovate/kind", _api.InnovateKind)
serviceV1.POST("/innovate/detail", _api.InnovateDetail)
}
//Technology 技术信息管理
technologyV1 := v1.Group("/technology")
{
_api := new(api2.Technology)
technologyV1.POST("/patent", _api.Patent)
technologyV1.POST("/patent/detail", _api.PatentDetail)
technologyV1.POST("/demand", _api.Demand)
technologyV1.POST("/demand/detail", _api.DemandDetail)
}
// About 关于我们管理
aboutV1 := v1.Group("/about")
{

View File

@ -1,20 +1,22 @@
package es
import (
"github.com/elastic/go-elasticsearch/v7"
"context"
"github.com/olivere/elastic/v7"
)
type Instance struct {
client *elasticsearch.Client
client *elastic.Client
address []string
username, password string
logger elastic.Logger
}
type Option func(*Instance)
var (
client *elasticsearch.Client
client *elastic.Client
)
func WithEsAddress(address []string) Option {
@ -35,24 +37,29 @@ func WithEsPassword(password string) Option {
}
}
func WithEsLog(logger elastic.Logger) Option {
return func(instance *Instance) {
instance.logger = logger
}
}
func (this *Instance) Init() *Instance {
obj := elasticsearch.Config{
Addresses: this.address,
Username: this.username,
Password: this.password,
}
client, err := elasticsearch.NewClient(obj)
client, err := elastic.NewClient(
elastic.SetSniff(false),
elastic.SetURL(this.address...),
//elastic.SetInfoLog(this.logger),
)
if err != nil {
panic("Elasticsearch New Error " + err.Error())
panic(err)
}
ctx := context.Background()
if _, err = client.Ping(); err != nil {
panic("Elasticsearch Ping Error " + err.Error())
for _, address := range this.address {
if _, _, err = client.Ping(address).Do(ctx); err != nil {
panic(err)
}
}
this.client = client
return this
}
@ -69,3 +76,7 @@ func NewInstance(option ...Option) *Instance {
}
return instance
}
func GetInstance() *elastic.Client {
return client
}

View File

@ -2,7 +2,8 @@ package es
import (
"context"
"github.com/elastic/go-elasticsearch/v7/esapi"
"encoding/json"
"fmt"
"github.com/olivere/elastic/v7"
"strings"
"testing"
@ -14,19 +15,67 @@ var (
host = "http://192.168.0.188:9200"
)
type Student struct {
Name string `json:"name"`
Age int64 `json:"age"`
AverageScore float64 `json:"average_score"`
}
func TestNewInstance(t *testing.T) {
client, err := elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(host))
if err != nil {
panic(err)
}
if _, _, err = client.Ping(host).Do(context.Background()); err != nil {
t.Log(err)
return
}
//client.CreateIndex(dataIndex).BodyJson().Do(context.Background())
esversion, _ := client.ElasticsearchVersion(host)
t.Log(esversion)
//newStudent := Student{
// Name: "熊,桥,顺",
// Age: 10,
// AverageScore: 99.9,
//}
//dataJSON, _ := json.Marshal(newStudent)
//
//_, err = client.Index().
// Index("students").
// BodyJson(string(dataJSON)).
// Do(context.Background())
//
//if err != nil {
// panic(err)
//}
var students []Student
searchSource := elastic.NewSearchSource()
searchSource.Query(elastic.NewMatchQuery("name", "顺"))
queryStr, _ := searchSource.Source()
queryJs, _ := json.Marshal(queryStr)
fmt.Println("[esclient]Final ESQuery=\n", string(queryJs))
searchService := client.Search().Index("students").SearchSource(searchSource)
searchResult, err1 := searchService.Do(context.Background())
if err1 != nil {
fmt.Println("[ProductsES][GetPIds]Error=", err1)
return
}
for _, hit := range searchResult.Hits.Hits {
var student Student
err := json.Unmarshal(hit.Source, &student)
if err != nil {
fmt.Println("[Getting Students][Unmarshal] Err=", err)
}
students = append(students, student)
}
t.Log(students)
}
func TestNewInstance1(t *testing.T) {
@ -37,9 +86,9 @@ func TestNewInstance1(t *testing.T) {
b.WriteString(`{"title" : "`)
b.WriteString("我是好人")
b.WriteString(`"}`)
resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String()))
t.Log(err)
t.Log(resp)
//resp, err := client.Create(dataIndex, "2", strings.NewReader(b.String()))
//t.Log(err)
//t.Log(resp)
}
func TestNewInstance2(t *testing.T) {
@ -47,7 +96,7 @@ func TestNewInstance2(t *testing.T) {
WithEsAddress([]string{"http://192.168.0.188:9200"}),
).Init().Local()
client.Search(func(request *esapi.SearchRequest) {
})
//client.Search(func(request *esapi.SearchRequest) {
//
//})
}

View File

@ -2,6 +2,9 @@ package es
func Create() error {
//_, err := client.Index().
// Index("students").
// BodyJson(string(dataJSON)).
// Do(context.Background())
return nil
}