Files

375 lines
11 KiB
Go
Raw Normal View History

2022-01-14 17:09:06 +08:00
package technology
import (
"SciencesServer/app/api/admin/model"
"SciencesServer/app/basic/config"
"SciencesServer/app/basic/controller"
model2 "SciencesServer/app/common/model"
2022-02-08 18:26:40 +08:00
"SciencesServer/app/service"
2022-01-14 17:09:06 +08:00
"SciencesServer/app/session"
"SciencesServer/utils"
"errors"
2022-02-08 18:26:40 +08:00
"strings"
"time"
2022-01-14 17:09:06 +08:00
)
// Patent 专利信息
type Patent struct {
*session.Admin
}
type PatentHandle func(session *session.Admin) *Patent
type (
// PatentInfo 专利信息
PatentInfo struct {
2022-02-08 18:26:40 +08:00
ID string `json:"id"`
*model.TechnologyPatentInfo
2022-01-14 17:09:06 +08:00
}
// PatentDetailInfo 专利详细信息
PatentDetailInfo struct {
ID string `json:"id"`
*model2.TechnologyPatent
2022-01-14 17:09:06 +08:00
}
// PatentParams 专利参数信息
PatentParams struct {
ID, TenantID uint64
Kind int
Title, FileUrl string
ApplyCode, ApplyName, ApplyAddress, ApplyAt,
OpenCode, OpenAt string
Inventor, IPCCode, Description, PrincipalClaim string
ShelfStatus, Status int
}
// PatentIPCInfo 专利IPC信息
PatentIPCInfo struct {
ID string `json:"id"`
ParentID string `json:"parent_id"`
Industrys []string `json:"industrys"`
IndustrysTitle []string `json:"industrys_title"`
}
// PatentIPCParams 专利IPC参数信息
PatentIPCParams struct {
ID, ParentID uint64
IPC string
Industrys []string `json:"industrys"`
}
)
func (c *Patent) ipcTree(src []*model2.TechnologyPatentClassify, parentID uint64) []*PatentIPCInfo {
2022-01-14 17:09:06 +08:00
out := make([]*PatentIPCInfo, 0)
for _, v := range src {
if v.ParentID == parentID {
industrys := v.GetIndustryAttribute()
industrysTitle := make([]string, 0)
for _, v := range industrys {
2022-01-20 09:43:26 +08:00
industrysTitle = append(industrysTitle, config.GetIndustryInfo(v, "-", "/").Value)
2022-01-14 17:09:06 +08:00
}
out = append(out, &PatentIPCInfo{
ID: v.GetEncodeID(),
ParentID: (&model2.Model{ID: v.ParentID}).GetEncodeID(),
Industrys: industrys,
IndustrysTitle: industrysTitle,
})
}
}
return out
}
func (c *PatentParams) checkParams(iModel *model.TechnologyPatent, condition map[string]interface{}) error {
2022-01-14 17:09:06 +08:00
isExist, err := iModel.IsExistParams(condition)
if err != nil {
return err
} else if isExist {
return errors.New("操作错误,申请号或公开(公告)号已存在")
}
return nil
}
// Instance 首页信息
func (c *Patent) Instance(tenantID uint64, title, ipc string, page, pageSize int) (*controller.ReturnPages, error) {
mSysPatent := model.NewTechnologyPatent()
2022-01-14 17:09:06 +08:00
where := make([]*model2.ModelWhere, 0)
if c.TenantID > 0 {
where = append(where, model2.NewWhere("p.tenant_id", c.TenantID))
}
if tenantID > 0 {
where = append(where, model2.NewWhere("p.tenant_id", tenantID))
}
2022-01-14 17:09:06 +08:00
if title != "" {
where = append(where, model2.NewWhereLike("p.title", title))
}
if ipc != "" {
where = append(where, model2.NewWhereLike("p.ipc", ipc))
}
var count int64
out, err := mSysPatent.Patent(page, pageSize, &count, where...)
if err != nil {
return nil, err
}
2022-01-15 09:00:47 +08:00
list := make([]*PatentInfo, 0)
for _, v := range out {
list = append(list, &PatentInfo{
2022-02-08 18:26:40 +08:00
ID: v.GetEncodeID(),
TechnologyPatentInfo: v,
2022-01-15 09:00:47 +08:00
})
}
return &controller.ReturnPages{Data: list, Count: count}, nil
2022-01-14 17:09:06 +08:00
}
// Detail 详细信息
func (c *Patent) Detail(id uint64) (*PatentDetailInfo, error) {
mTechnologyPatent := model.NewTechnologyPatent()
mTechnologyPatent.ID = id
2022-01-14 17:09:06 +08:00
isExist, err := model2.First(mTechnologyPatent.TechnologyPatent)
2022-01-14 17:09:06 +08:00
if err != nil {
return nil, err
} else if !isExist {
return nil, errors.New("操作错误,专利信息不存在或已被删除")
}
return &PatentDetailInfo{ID: mTechnologyPatent.GetEncodeID(),
TechnologyPatent: mTechnologyPatent.TechnologyPatent}, nil
2022-01-14 17:09:06 +08:00
}
// Form 数据操作
func (c *Patent) Form(params *PatentParams) error {
mTechnologyPatent := model.NewTechnologyPatent()
2022-01-14 17:09:06 +08:00
_condition := make(map[string]interface{}, 0)
if params.ID > 0 {
mTechnologyPatent.ID = params.ID
2022-01-14 17:09:06 +08:00
isExist, err := model2.FirstField(mTechnologyPatent.TechnologyPatent, []string{"id", "tenant_id", "uid", "apply_code", "ipc_code", "created_at"})
2022-01-14 17:09:06 +08:00
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,专利信息不存在或已被删除")
} else if c.TenantID > 0 && mTechnologyPatent.TenantID > 0 && c.TenantID != mTechnologyPatent.TenantID {
return errors.New("操作错误,无权限操作")
2022-01-14 17:09:06 +08:00
}
if mTechnologyPatent.ApplyCode != params.ApplyCode {
2022-01-14 17:09:06 +08:00
_condition["apply_code"] = params.ApplyCode
}
if mTechnologyPatent.IPCCode != params.IPCCode {
2022-01-14 17:09:06 +08:00
_condition["ipc_code"] = params.IPCCode
}
if len(_condition) > 0 {
if isExist, err = mTechnologyPatent.IsExistParams(_condition); err != nil {
2022-01-14 17:09:06 +08:00
return err
} else if isExist {
return errors.New("操作错误,申请号或公开(公告)号已存在")
}
}
}
2022-02-08 08:56:28 +08:00
mTechnologyPatent.Kind = model2.TechnologyPatentKind(params.Kind)
mTechnologyPatent.Title = params.Title
mTechnologyPatent.FileUrl = params.FileUrl
mTechnologyPatent.ApplyCode = params.ApplyCode
mTechnologyPatent.ApplyAt = params.ApplyAt
mTechnologyPatent.OpenCode = params.OpenCode
mTechnologyPatent.OpenAt = params.OpenAt
mTechnologyPatent.ApplyName = params.ApplyName
mTechnologyPatent.ApplyAddress = params.ApplyAddress
mTechnologyPatent.Inventor = params.Inventor
mTechnologyPatent.Description = params.Description
mTechnologyPatent.PrincipalClaim = params.PrincipalClaim
mTechnologyPatent.IPCCode = params.IPCCode
mTechnologyPatent.ShelfStatus = model2.ShelfStatusKind(params.ShelfStatus)
2022-02-08 08:56:28 +08:00
mTechnologyPatent.Status = model2.TechnologyPatentStatus(params.Status)
2022-02-08 18:26:40 +08:00
// 查询IPCCode信息
mTechnologyPatentClassify := model.NewTechnologyPatentClassify()
isExist, err := model2.FirstField(mTechnologyPatentClassify.TechnologyPatentClassify, []string{"id", "industry_detail"},
model2.NewWhere("ipc", params.IPCCode))
_industrys := make([]string, 0)
if err != nil {
return err
} else if isExist {
for _, v := range mTechnologyPatentClassify.GetIndustryDetailAttribute() {
_industrys = append(_industrys, config.GetIndustryInfo(v, "-", "-").Value)
}
}
manage := service.NewESPatent(
service.WithPatentTitle(mTechnologyPatent.Title),
service.WithPatentIndustry(strings.Join(_industrys, "")),
service.WithPatentShow(params.Status),
)
if mTechnologyPatent.ID > 0 {
2022-02-08 18:26:40 +08:00
if err = model2.Updates(mTechnologyPatent.TechnologyPatent, mTechnologyPatent.TechnologyPatent); err != nil {
return err
}
_ = manage.Update()
return nil
2022-01-14 17:09:06 +08:00
}
_condition["apply_code"] = params.ApplyCode
_condition["ipc_code"] = params.IPCCode
if isExist, err := mTechnologyPatent.IsExistParams(_condition); err != nil {
2022-01-14 17:09:06 +08:00
return err
} else if isExist {
return errors.New("操作错误,申请号或公开(公告)号已存在")
}
mTechnologyPatent.TenantID = c.TenantID
2022-02-08 18:26:40 +08:00
if err := model2.Create(mTechnologyPatent.TechnologyPatent); err != nil {
return err
}
service.WithPatentID(mTechnologyPatent.ID)(manage)
return manage.Create()
2022-01-14 17:09:06 +08:00
}
// Bind 绑定信息
func (c *Patent) Bind(id, uid uint64) error {
mTechnologyPatent := model.NewTechnologyPatent()
mTechnologyPatent.ID = id
2022-01-14 17:09:06 +08:00
isExist, err := model2.FirstField(mTechnologyPatent.TechnologyPatent, []string{"id", "uid", "tenant_id"})
2022-01-14 17:09:06 +08:00
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,专利信息不存在或已被删除")
} else if c.TenantID > 0 && mTechnologyPatent.TenantID > 0 && c.TenantID != mTechnologyPatent.TenantID {
return errors.New("操作错误,无权限操作")
2022-01-14 17:09:06 +08:00
}
if err = model2.Updates(mTechnologyPatent.TechnologyPatent, map[string]interface{}{
"uid": uid, "updated_at": time.Now(),
}); err != nil {
2022-01-14 17:09:06 +08:00
return err
}
return nil
2022-01-14 17:09:06 +08:00
}
2022-02-08 18:26:40 +08:00
// Shelf 上下架操作
2022-01-24 11:28:21 +08:00
func (c *Patent) Shelf(id uint64) error {
2022-02-08 18:26:40 +08:00
return controller.NewShelf(controller.WithShelfSessionAdmin(c.Admin)).Handle(model2.NewTechnologyPatent(), id, func(kind model2.ShelfStatusKind) error {
manage := service.NewESPatent(
service.WithPatentID(id),
service.WithPatentShow(int(kind)),
)
return manage.Update()
})
2022-01-21 11:42:58 +08:00
}
2022-01-14 17:09:06 +08:00
// Delete 删除操作
func (c *Patent) Delete(id uint64) error {
mTechnologyPatent := model.NewTechnologyPatent()
mTechnologyPatent.ID = id
2022-01-14 17:09:06 +08:00
isExist, err := model2.FirstField(mTechnologyPatent.TechnologyPatent, []string{"id", "uid", "tenant_id"})
2022-01-14 17:09:06 +08:00
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误,专利信息不存在或已被删除")
} else if c.TenantID > 0 && mTechnologyPatent.TenantID > 0 && c.TenantID != mTechnologyPatent.TenantID {
2022-01-17 13:23:49 +08:00
return errors.New("操作错误,无权限操作")
2022-01-14 17:09:06 +08:00
}
2022-02-08 18:26:40 +08:00
err = model2.Delete(mTechnologyPatent.TechnologyPatent)
if err != nil {
return err
}
_ = service.NewESPatent(service.WithPatentID(mTechnologyPatent.ID)).Delete()
return nil
2022-01-14 17:09:06 +08:00
}
// IPC IPC信息
func (c *Patent) IPC() ([]*PatentIPCInfo, error) {
mTechnologyPatentClassify := model.NewTechnologyPatentClassify()
out := make([]*model2.TechnologyPatentClassify, 0)
2022-01-14 17:09:06 +08:00
if err := model2.Scan(mTechnologyPatentClassify.TechnologyPatentClassify, &out); err != nil {
2022-01-14 17:09:06 +08:00
return nil, err
}
return c.ipcTree(out, 0), nil
}
// IPCForm IPC数据操作
func (c *Patent) IPCForm(params *PatentIPCParams) error {
mTechnologyPatentClassify := model.NewTechnologyPatentClassify()
2022-01-14 17:09:06 +08:00
if params.ID > 0 {
mTechnologyPatentClassify.ID = params.ID
2022-01-14 17:09:06 +08:00
isExist, err := model2.FirstField(mTechnologyPatentClassify.TechnologyPatentClassify, []string{"id", "created_at"})
2022-01-14 17:09:06 +08:00
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误IPC信息不存在或已被删除")
}
}
mTechnologyPatentClassify.SetIndustryDetailAttribute(params.Industrys)
2022-01-14 17:09:06 +08:00
// 查询上级元素
if params.ParentID > 0 {
mPTechnologyPatentClassify := model.NewTechnologyPatentClassify()
mPTechnologyPatentClassify.ID = params.ParentID
2022-01-14 17:09:06 +08:00
isExist, err := model2.FirstField(mPTechnologyPatentClassify.TechnologyPatentClassify, []string{"id", "industry"})
2022-01-14 17:09:06 +08:00
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误IPC父级信息不存在或已被删除")
}
_identitys := mPTechnologyPatentClassify.GetIndustryDetailAttribute()
2022-01-14 17:09:06 +08:00
for _, v := range params.Industrys {
if utils.InArray(v, _identitys) {
continue
}
_identitys = append(_identitys, v)
}
mTechnologyPatentClassify.SetIndustryDetailAttribute(_identitys)
2022-01-14 17:09:06 +08:00
}
mTechnologyPatentClassify.ParentID = params.ParentID
mTechnologyPatentClassify.IPC = params.IPC
mTechnologyPatentClassify.SetIndustryAttribute(params.Industrys)
2022-01-14 17:09:06 +08:00
if mTechnologyPatentClassify.ID > 0 {
return model2.Updates(mTechnologyPatentClassify.TechnologyPatentClassify, mTechnologyPatentClassify.TechnologyPatentClassify)
2022-01-14 17:09:06 +08:00
}
return model2.Create(mTechnologyPatentClassify.TechnologyPatentClassify)
2022-01-14 17:09:06 +08:00
}
// IPCDelete IPC删除操作
func (c *Patent) IPCDelete(id uint64) error {
mTechnologyPatentClassify := model.NewTechnologyPatentClassify()
mTechnologyPatentClassify.ID = id
2022-01-14 17:09:06 +08:00
isExist, err := model2.FirstField(mTechnologyPatentClassify.TechnologyPatentClassify, []string{"id", "created_at"})
2022-01-14 17:09:06 +08:00
if err != nil {
return err
} else if !isExist {
return errors.New("操作错误IPC信息不存在或已被删除")
}
return model2.Delete(mTechnologyPatentClassify.TechnologyPatentClassify)
2022-01-14 17:09:06 +08:00
}
func NewPatent() PatentHandle {
return func(session *session.Admin) *Patent {
return &Patent{
Admin: session,
}
}
}