323 lines
9.7 KiB
Go
323 lines
9.7 KiB
Go
package technology
|
||
|
||
import (
|
||
"SciencesServer/app/api/enterprise/model"
|
||
"SciencesServer/app/basic/config"
|
||
"SciencesServer/app/basic/controller"
|
||
model2 "SciencesServer/app/common/model"
|
||
"SciencesServer/app/service"
|
||
"SciencesServer/app/session"
|
||
"SciencesServer/serve/orm"
|
||
"errors"
|
||
"gorm.io/gorm"
|
||
"strings"
|
||
)
|
||
|
||
// Patent 专利管理
|
||
type Patent struct {
|
||
*session.Enterprise
|
||
tenantID uint64
|
||
}
|
||
|
||
type PatentHandle func(session *session.Enterprise, tenantID uint64) *Patent
|
||
|
||
type (
|
||
// PatentInfo 专利信息
|
||
PatentInfo struct {
|
||
ID string `json:"id"`
|
||
*model.UserPatentInfo
|
||
}
|
||
// PatentMatchInfo 专利匹配信息
|
||
PatentMatchInfo struct {
|
||
ID string `json:"id"`
|
||
Kind model2.TechnologyPatentKind `json:"kind"`
|
||
Title string `json:"title"`
|
||
Description string `json:"description"`
|
||
ApplyAt string `json:"apply_at"`
|
||
}
|
||
// PatentDetailInfo 专利详细信息
|
||
PatentDetailInfo struct {
|
||
ID string `json:"id"`
|
||
*model2.TechnologyPatent
|
||
}
|
||
// PatentParams 专利参数信息
|
||
PatentParams struct {
|
||
ID uint64
|
||
Kind int
|
||
Title, FileUrl string
|
||
ApplyCode, ApplyName, ApplyAddress, ApplyAt,
|
||
OpenCode, OpenAt string
|
||
Inventor, IPCCode, Description, PrincipalClaim string
|
||
Status int
|
||
}
|
||
)
|
||
|
||
// add 新增专利信息
|
||
func (c *PatentParams) add(tenantID, uid uint64) error {
|
||
mTechnologyPatent := model.NewTechnologyPatent()
|
||
|
||
isExist, err := mTechnologyPatent.IsExistParams(map[string]interface{}{
|
||
"apply_code": c.ApplyCode, "open_code": c.OpenCode,
|
||
})
|
||
if err != nil {
|
||
return err
|
||
} else if isExist {
|
||
return errors.New("操作错误,申请号或公开(公告)号已存在")
|
||
}
|
||
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
||
mTechnologyPatent.Kind = model2.TechnologyPatentKind(c.Kind)
|
||
mTechnologyPatent.Title = c.Title
|
||
mTechnologyPatent.FileUrl = c.FileUrl
|
||
mTechnologyPatent.ApplyCode = c.ApplyCode
|
||
mTechnologyPatent.ApplyAt = c.ApplyAt
|
||
mTechnologyPatent.OpenCode = c.OpenCode
|
||
mTechnologyPatent.OpenAt = c.OpenAt
|
||
mTechnologyPatent.ApplyName = c.ApplyName
|
||
mTechnologyPatent.ApplyAddress = c.ApplyAddress
|
||
mTechnologyPatent.Inventor = c.Inventor
|
||
mTechnologyPatent.Description = c.Description
|
||
mTechnologyPatent.PrincipalClaim = c.PrincipalClaim
|
||
mTechnologyPatent.IPCCode = c.IPCCode
|
||
mTechnologyPatent.Status = model2.TechnologyPatentStatus(c.Status)
|
||
|
||
if err = model2.Create(mTechnologyPatent.TechnologyPatent, tx); err != nil {
|
||
return err
|
||
}
|
||
mUserPatent := model.NewUserPatent()
|
||
mUserPatent.UID = uid
|
||
mUserPatent.PatentID = mTechnologyPatent.ID
|
||
|
||
if err = model2.Create(mUserPatent.UserPatent, tx); err != nil {
|
||
return err
|
||
}
|
||
manage := service.NewESPatent(
|
||
service.WithPatentID(mTechnologyPatent.ID),
|
||
service.WithPatentTitle(mTechnologyPatent.Title),
|
||
)
|
||
// 查询分类信息
|
||
mTechnologyPatentClassify := model.NewTechnologyPatentClassify()
|
||
|
||
isExist, err = model2.FirstField(mTechnologyPatentClassify.TechnologyPatentClassify, []string{"id", "ipc", "industry_detail"},
|
||
model2.NewWhere("ipc", c.IPCCode))
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if isExist {
|
||
_industrys := make([]string, 0)
|
||
|
||
for _, v := range mTechnologyPatentClassify.GetIndustryDetailAttribute() {
|
||
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", "-").Value)
|
||
}
|
||
service.WithPatentIndustry(strings.Join(_industrys, ";"))(manage)
|
||
}
|
||
return manage.Create()
|
||
})
|
||
}
|
||
|
||
// edit 删除专利信息
|
||
func (c *PatentParams) edit(uid uint64) error {
|
||
mUserPatent := model.NewUserPatent()
|
||
mUserPatent.ID = c.ID
|
||
|
||
isExist, err := model2.FirstField(mUserPatent.UserPatent, []string{"id", "uid", "patent_id"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,用户专利信息不存在或已被删除")
|
||
} else if mUserPatent.UID != uid {
|
||
return errors.New("操作错误,无权限操作")
|
||
}
|
||
mTechnologyPatent := model.NewTechnologyPatent()
|
||
mTechnologyPatent.ID = mUserPatent.PatentID
|
||
|
||
if isExist, err = model2.FirstField(mTechnologyPatent.TechnologyPatent, []string{"id", "apply_code", "open_code", "created_at"}); err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,专利信息不存在或已被删除")
|
||
}
|
||
_condition := make(map[string]interface{}, 0)
|
||
|
||
if mTechnologyPatent.ApplyCode != c.ApplyCode {
|
||
_condition["apply_code"] = c.ApplyCode
|
||
}
|
||
if mTechnologyPatent.OpenCode != c.OpenCode {
|
||
_condition["open_code"] = c.OpenCode
|
||
}
|
||
if len(_condition) > 0 {
|
||
if isExist, err = mTechnologyPatent.IsExistParams(_condition); err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,申请号或公开(公告)号已存在")
|
||
}
|
||
}
|
||
mTechnologyPatent.Kind = model2.TechnologyPatentKind(c.Kind)
|
||
mTechnologyPatent.Title = c.Title
|
||
mTechnologyPatent.FileUrl = c.FileUrl
|
||
mTechnologyPatent.ApplyCode = c.ApplyCode
|
||
mTechnologyPatent.ApplyAt = c.ApplyAt
|
||
mTechnologyPatent.OpenCode = c.OpenCode
|
||
mTechnologyPatent.OpenAt = c.OpenAt
|
||
mTechnologyPatent.ApplyName = c.ApplyName
|
||
mTechnologyPatent.ApplyAddress = c.ApplyAddress
|
||
mTechnologyPatent.Inventor = c.Inventor
|
||
mTechnologyPatent.Description = c.Description
|
||
mTechnologyPatent.PrincipalClaim = c.PrincipalClaim
|
||
mTechnologyPatent.IPCCode = c.IPCCode
|
||
mTechnologyPatent.Status = model2.TechnologyPatentStatus(c.Status)
|
||
|
||
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
||
if err = model2.Updates(mTechnologyPatent.TechnologyPatent, mTechnologyPatent.TechnologyPatent, tx); err != nil {
|
||
return err
|
||
}
|
||
manage := service.NewESPatent(
|
||
service.WithPatentID(mTechnologyPatent.ID),
|
||
service.WithPatentTitle(mTechnologyPatent.Title),
|
||
)
|
||
return manage.Update()
|
||
})
|
||
}
|
||
|
||
// List 列表信息
|
||
func (c *Patent) List(kind int, title, applyCode, openCode, ipcCode string, page, pageSize int) (*controller.ReturnPages, error) {
|
||
mUserPatent := model.NewUserPatent()
|
||
|
||
where := []*model2.ModelWhere{model2.NewWhere("u.uid", c.UID)}
|
||
|
||
if kind <= 0 {
|
||
where = append(where, model2.NewWhere("p.kind", kind))
|
||
}
|
||
if title != "" {
|
||
where = append(where, model2.NewWhereLike("p.title", title))
|
||
}
|
||
if applyCode != "" {
|
||
where = append(where, model2.NewWhereLike("p.apply_code", applyCode))
|
||
}
|
||
if openCode != "" {
|
||
where = append(where, model2.NewWhereLike("p.open_code", openCode))
|
||
}
|
||
if ipcCode != "" {
|
||
where = append(where, model2.NewWhereLike("p.ipc_code", ipcCode))
|
||
}
|
||
var count int64
|
||
|
||
out, err := mUserPatent.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(), UserPatentInfo: v,
|
||
})
|
||
}
|
||
return &controller.ReturnPages{Data: list, Count: count}, nil
|
||
}
|
||
|
||
// Match 搜索信息
|
||
func (c *Patent) Match(title string, industrys, keywords []string) (*controller.ReturnPages, error) {
|
||
params := strings.Join([]string{
|
||
title, strings.Join(industrys, ","), strings.Join(keywords, ","),
|
||
}, ",")
|
||
patent := service.NewESPatent(
|
||
service.WithPatentTitle(params),
|
||
service.WithPatentIndustry(params),
|
||
)
|
||
out, count, err := patent.Search(0, 0)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
ids := make([]uint64, 0)
|
||
|
||
for _, v := range out.([]interface{}) {
|
||
val := v.(*service.ESAchievement)
|
||
ids = append(ids, val.ID)
|
||
}
|
||
mTechnologyPatent := model.NewTechnologyPatent()
|
||
|
||
patents := make([]*model2.TechnologyPatent, 0)
|
||
|
||
if err = model2.ScanFields(mTechnologyPatent.TechnologyPatent, &patents, []string{"id", "kind", "title", "description", "apply_at"}, &model2.ModelWhereOrder{
|
||
Where: model2.NewWhereIn("id", ids),
|
||
Order: model2.NewOrder("id", model2.OrderModeToDesc),
|
||
}); err != nil {
|
||
return nil, err
|
||
}
|
||
list := make([]*PatentMatchInfo, 0)
|
||
|
||
for _, v := range patents {
|
||
list = append(list, &PatentMatchInfo{
|
||
ID: v.GetEncodeID(), Title: v.Title, Description: v.Description, ApplyAt: v.ApplyAt,
|
||
})
|
||
}
|
||
return &controller.ReturnPages{Data: list, Count: count}, nil
|
||
}
|
||
|
||
// Detail 详细信息
|
||
func (c *Patent) Detail(id uint64) (*PatentDetailInfo, error) {
|
||
mTechnologyPatent := model.NewTechnologyPatent()
|
||
mTechnologyPatent.ID = id
|
||
|
||
isExist, err := model2.First(mTechnologyPatent.TechnologyPatent)
|
||
|
||
if err != nil {
|
||
return nil, err
|
||
} else if !isExist {
|
||
return nil, errors.New("操作错误,专利信息不存在或已被删除")
|
||
}
|
||
return &PatentDetailInfo{ID: mTechnologyPatent.GetEncodeID(), TechnologyPatent: mTechnologyPatent.TechnologyPatent}, nil
|
||
}
|
||
|
||
func (c *Patent) Form(params *PatentParams) error {
|
||
if params.ID > 0 {
|
||
return params.edit(c.UID)
|
||
}
|
||
return params.add(c.tenantID, c.UID)
|
||
|
||
}
|
||
|
||
// Delete 删除操作
|
||
func (c *Patent) Delete(id uint64) error {
|
||
mUserPatent := model.NewUserPatent()
|
||
mUserPatent.ID = id
|
||
|
||
isExist, err := model2.FirstField(mUserPatent.UserPatent, []string{"id", "uid", "patent_id"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,用户专利信息不存在或已被删除")
|
||
} else if mUserPatent.UID != c.UID {
|
||
return errors.New("操作错误,无权限操作")
|
||
}
|
||
// 只删除关联关系
|
||
if err = model2.Delete(mUserPatent.UserPatent); err != nil {
|
||
return err
|
||
}
|
||
//mTechnologyPatent := model.NewTechnologyPatent()
|
||
//mTechnologyPatent.ID = mUserPatent.PatentID
|
||
//
|
||
//if isExist, err := model2.FirstField(mTechnologyPatent.TechnologyPatent, []string{"id"}); err != nil {
|
||
// return err
|
||
//} else if !isExist {
|
||
// return errors.New("操作错误,专利信息不存在或已被删除")
|
||
//}
|
||
//if err = model2.Delete(mTechnologyPatent.TechnologyPatent); err != nil {
|
||
// return err
|
||
//}
|
||
//manage := service.NewESPatent(
|
||
// service.WithPatentID(mTechnologyPatent.ID),
|
||
//)
|
||
//return manage.Delete()
|
||
return nil
|
||
}
|
||
|
||
func NewPatent() PatentHandle {
|
||
return func(session *session.Enterprise, tenantID uint64) *Patent {
|
||
return &Patent{Enterprise: session, tenantID: tenantID}
|
||
}
|
||
}
|