Files

354 lines
11 KiB
Go
Raw Normal View History

2021-10-09 11:55:54 +08:00
package technology
2021-10-15 11:05:38 +08:00
import (
2021-10-15 15:06:02 +08:00
"SciencesServer/app/api/enterprise/model"
2022-02-08 16:11:59 +08:00
"SciencesServer/app/basic/config"
2022-01-05 11:29:27 +08:00
"SciencesServer/app/basic/controller"
2021-10-15 11:05:38 +08:00
model2 "SciencesServer/app/common/model"
2022-01-04 11:59:58 +08:00
"SciencesServer/app/service"
2021-11-24 10:50:09 +08:00
"SciencesServer/app/session"
2021-11-29 13:31:33 +08:00
"SciencesServer/serve/orm"
2021-10-15 11:05:38 +08:00
"errors"
2021-11-29 13:31:33 +08:00
"gorm.io/gorm"
2022-01-04 11:59:58 +08:00
"strings"
2021-10-15 11:05:38 +08:00
)
2021-10-13 11:23:55 +08:00
2021-10-09 11:55:54 +08:00
// Patent 专利管理
2021-10-13 11:23:55 +08:00
type Patent struct {
2021-11-24 10:50:09 +08:00
*session.Enterprise
2022-01-15 16:48:49 +08:00
tenantID uint64
2021-10-13 11:23:55 +08:00
}
2022-01-15 16:48:49 +08:00
type PatentHandle func(session *session.Enterprise, tenantID uint64) *Patent
2021-10-15 11:05:38 +08:00
type (
// PatentInfo 专利信息
PatentInfo struct {
2022-02-09 11:06:59 +08:00
ID string `json:"id"`
2022-02-09 18:38:32 +08:00
*model.TechnologyPatentExpertInfo
2021-10-15 11:05:38 +08:00
}
2022-01-04 11:59:58 +08:00
// PatentMatchInfo 专利匹配信息
PatentMatchInfo struct {
2022-02-08 08:56:28 +08:00
ID string `json:"id"`
Kind model2.TechnologyPatentKind `json:"kind"`
Title string `json:"title"`
Description string `json:"description"`
ApplyAt string `json:"apply_at"`
2022-01-04 11:59:58 +08:00
}
2021-10-15 11:05:38 +08:00
// PatentDetailInfo 专利详细信息
PatentDetailInfo struct {
ID string `json:"id"`
*model2.TechnologyPatent
2021-10-15 11:05:38 +08:00
}
// PatentParams 专利参数信息
PatentParams struct {
2021-11-29 13:31:33 +08:00
ID uint64
Kind int
Title, FileUrl string
ApplyCode, ApplyName, ApplyAddress, ApplyAt,
OpenCode, OpenAt string
Inventor, IPCCode, Description, PrincipalClaim string
Status int
2021-10-15 11:05:38 +08:00
}
)
2021-11-29 13:31:33 +08:00
// add 新增专利信息
2022-01-15 16:48:49 +08:00
func (c *PatentParams) add(tenantID, uid uint64) error {
mTechnologyPatent := model.NewTechnologyPatent()
isExist, err := mTechnologyPatent.IsExistParams(map[string]interface{}{
2021-11-29 13:31:33 +08:00
"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 {
2022-02-08 08:56:28 +08:00
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
2022-02-08 08:56:28 +08:00
mTechnologyPatent.Status = model2.TechnologyPatentStatus(c.Status)
if err = model2.Create(mTechnologyPatent.TechnologyPatent, tx); err != nil {
2021-11-29 13:31:33 +08:00
return err
}
2022-02-09 18:38:32 +08:00
// 查询当前的专家信息
mUserExpert := model.NewUserExpert()
2022-02-09 11:06:59 +08:00
2022-02-10 11:36:47 +08:00
if isExist, err = model2.LastWhere(mUserExpert.UserExpert, []string{"id", "expert_id"}, model2.NewWhere("uid", uid),
2022-02-09 18:38:32 +08:00
model2.NewWhere("invalid_status", model2.InvalidStatusForNot)); err != nil {
2022-02-09 11:06:59 +08:00
return err
2022-02-09 18:38:32 +08:00
} else if !isExist {
return errors.New("操作错误,无专家信息")
2022-02-09 11:06:59 +08:00
}
2022-02-09 18:38:32 +08:00
mTechnologyPatentExpert := model.NewTechnologyPatentExpert()
mTechnologyPatentExpert.ExpertID = mUserExpert.ExpertID
mTechnologyPatentExpert.PatentID = mTechnologyPatent.ID
if err = model2.Create(mTechnologyPatentExpert.TechnologyPatentExpert, tx); err != nil {
return err
}
2022-02-08 16:11:59 +08:00
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()
2021-11-29 13:31:33 +08:00
})
}
// edit 删除专利信息
func (c *PatentParams) edit(uid uint64) error {
2022-02-09 18:38:32 +08:00
mTechnologyPatentExpert := model.NewTechnologyPatentExpert()
mTechnologyPatentExpert.ID = c.ID
2021-11-29 13:31:33 +08:00
2022-02-09 18:38:32 +08:00
isExist, err := model2.FirstField(mTechnologyPatentExpert.TechnologyPatentExpert, []string{"id", "expert_id", "patent_id"})
2021-11-29 13:31:33 +08:00
if err != nil {
return err
} else if !isExist {
2022-02-09 11:06:59 +08:00
return errors.New("操作错误,用户专利信息不存在或已被删除")
2022-02-09 18:38:32 +08:00
}
// 查询当前用户是否存在对应的专家信息
mUserExpert := model.NewUserExpert()
var count int64
if err = model2.Count(mUserExpert.UserExpert, &count, model2.NewWhere("uid", uid),
model2.NewWhere("expert_id", mTechnologyPatentExpert.ExpertID),
model2.NewWhere("invalid_status", model2.InvalidStatusForNot),
); err != nil {
return err
} else if count > 0 {
return errors.New("操作错误,无权限操作")
2021-11-29 13:31:33 +08:00
}
2022-02-09 11:06:59 +08:00
mTechnologyPatent := model.NewTechnologyPatent()
2022-02-09 18:38:32 +08:00
mTechnologyPatent.ID = mTechnologyPatentExpert.PatentID
2022-02-09 11:06:59 +08:00
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("操作错误,专利信息不存在或已被删除")
}
2021-11-29 13:31:33 +08:00
_condition := make(map[string]interface{}, 0)
if mTechnologyPatent.ApplyCode != c.ApplyCode {
2021-11-29 13:31:33 +08:00
_condition["apply_code"] = c.ApplyCode
2021-10-15 11:05:38 +08:00
}
if mTechnologyPatent.OpenCode != c.OpenCode {
2021-11-29 13:31:33 +08:00
_condition["open_code"] = c.OpenCode
}
if len(_condition) > 0 {
if isExist, err = mTechnologyPatent.IsExistParams(_condition); err != nil {
2021-11-29 13:31:33 +08:00
return err
} else if !isExist {
return errors.New("操作错误,申请号或公开(公告)号已存在")
}
}
2022-02-08 08:56:28 +08:00
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
2022-02-08 08:56:28 +08:00
mTechnologyPatent.Status = model2.TechnologyPatentStatus(c.Status)
2022-02-08 16:11:59 +08:00
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()
})
2021-11-29 13:31:33 +08:00
}
// List 列表信息
func (c *Patent) List(kind int, title, applyCode, openCode, ipcCode string, page, pageSize int) (*controller.ReturnPages, error) {
2022-02-09 18:38:32 +08:00
// 查询当前用户对应的专家信息
mUserExpert := model.NewUserExpert()
2022-02-09 11:06:59 +08:00
2022-02-09 18:38:32 +08:00
isExist, err := model2.LastWhere(mUserExpert.UserExpert, []string{"id", "expert_id"},
model2.NewWhere("uid", c.UID),
model2.NewWhere("invalid_status", model2.InvalidStatusForNot),
)
if err != nil {
return nil, err
} else if !isExist {
return nil, nil
}
mTechnologyPatentExpert := model.NewTechnologyPatentExpert()
where := []*model2.ModelWhere{model2.NewWhere("u.expert_id", mUserExpert.ExpertID)}
2021-10-15 11:05:38 +08:00
2021-11-29 13:31:33 +08:00
if kind <= 0 {
2022-02-09 11:06:59 +08:00
where = append(where, model2.NewWhere("p.kind", kind))
2021-11-29 13:31:33 +08:00
}
if title != "" {
2022-02-09 11:06:59 +08:00
where = append(where, model2.NewWhereLike("p.title", title))
2021-11-29 13:31:33 +08:00
}
if applyCode != "" {
2022-02-09 11:06:59 +08:00
where = append(where, model2.NewWhereLike("p.apply_code", applyCode))
2021-11-29 13:31:33 +08:00
}
if openCode != "" {
2022-02-09 11:06:59 +08:00
where = append(where, model2.NewWhereLike("p.open_code", openCode))
2021-11-29 13:31:33 +08:00
}
if ipcCode != "" {
2022-02-09 11:06:59 +08:00
where = append(where, model2.NewWhereLike("p.ipc_code", ipcCode))
2021-11-29 13:31:33 +08:00
}
2021-10-15 11:05:38 +08:00
var count int64
2022-02-09 18:38:32 +08:00
out := make([]*model.TechnologyPatentExpertInfo, 0)
2021-11-29 13:31:33 +08:00
2022-02-09 18:38:32 +08:00
if out, err = mTechnologyPatentExpert.Patent(page, pageSize, &count, where...); err != nil {
2021-10-15 11:05:38 +08:00
return nil, err
}
list := make([]*PatentInfo, 0)
for _, v := range out {
list = append(list, &PatentInfo{
2022-02-09 18:38:32 +08:00
ID: v.GetEncodeID(), TechnologyPatentExpertInfo: v,
2021-10-15 11:05:38 +08:00
})
}
return &controller.ReturnPages{Data: list, Count: count}, nil
}
2022-01-04 15:04:37 +08:00
// Match 搜索信息
2022-01-20 11:51:02 +08:00
func (c *Patent) Match(title string, industrys, keywords []string) (*controller.ReturnPages, error) {
2022-01-04 11:59:58 +08:00
params := strings.Join([]string{
title, strings.Join(industrys, ","), strings.Join(keywords, ","),
}, ",")
patent := service.NewESPatent(
service.WithPatentTitle(params),
service.WithPatentIndustry(params),
)
2022-01-20 11:51:02 +08:00
out, count, err := patent.Search(0, 0)
2022-01-04 11:59:58 +08:00
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()
2022-01-04 11:59:58 +08:00
2022-02-08 08:56:28 +08:00
patents := make([]*model2.TechnologyPatent, 0)
2022-01-04 11:59:58 +08:00
if err = model2.ScanFields(mTechnologyPatent.TechnologyPatent, &patents, []string{"id", "kind", "title", "description", "apply_at"}, &model2.ModelWhereOrder{
2022-01-04 11:59:58 +08:00
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,
})
}
2022-01-20 11:51:02 +08:00
return &controller.ReturnPages{Data: list, Count: count}, nil
2022-01-04 11:59:58 +08:00
}
2021-10-15 11:05:38 +08:00
// Detail 详细信息
func (c *Patent) Detail(id uint64) (*PatentDetailInfo, error) {
mTechnologyPatent := model.NewTechnologyPatent()
mTechnologyPatent.ID = id
2021-10-15 11:05:38 +08:00
isExist, err := model2.First(mTechnologyPatent.TechnologyPatent)
2021-10-15 11:05:38 +08:00
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("操作错误,专利信息不存在或已被删除")
2021-11-29 13:31:33 +08:00
}
return &PatentDetailInfo{ID: mTechnologyPatent.GetEncodeID(), TechnologyPatent: mTechnologyPatent.TechnologyPatent}, nil
2021-10-15 11:05:38 +08:00
}
func (c *Patent) Form(params *PatentParams) error {
if params.ID > 0 {
2021-11-29 13:31:33 +08:00
return params.edit(c.UID)
}
2022-01-15 16:48:49 +08:00
return params.add(c.tenantID, c.UID)
2021-10-15 11:05:38 +08:00
2021-11-29 13:31:33 +08:00
}
2021-10-15 11:05:38 +08:00
// Delete 删除操作
func (c *Patent) Delete(id uint64) error {
2022-02-09 18:38:32 +08:00
mTechnologyPatentExpert := model.NewTechnologyPatentExpert()
mTechnologyPatentExpert.ID = id
2021-10-15 11:05:38 +08:00
2022-02-09 18:38:32 +08:00
isExist, err := model2.FirstField(mTechnologyPatentExpert.TechnologyPatentExpert, []string{"id", "patent_id"})
2021-10-15 11:05:38 +08:00
if err != nil {
return err
} else if !isExist {
2022-02-09 11:06:59 +08:00
return errors.New("操作错误,用户专利信息不存在或已被删除")
2021-10-15 11:05:38 +08:00
}
2022-02-09 11:06:59 +08:00
// 只删除关联关系
2022-02-09 18:38:32 +08:00
if err = model2.Delete(mTechnologyPatentExpert.TechnologyPatentExpert); err != nil {
2022-02-08 16:11:59 +08:00
return err
}
2022-02-09 11:06:59 +08:00
//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
2021-10-15 11:05:38 +08:00
}
2021-10-13 11:23:55 +08:00
func NewPatent() PatentHandle {
2022-01-15 16:48:49 +08:00
return func(session *session.Enterprise, tenantID uint64) *Patent {
return &Patent{Enterprise: session, tenantID: tenantID}
2021-10-13 11:23:55 +08:00
}
}