336 lines
9.3 KiB
Go
336 lines
9.3 KiB
Go
package technology
|
||
|
||
import (
|
||
"SciencesServer/app/api/admin/model"
|
||
"SciencesServer/app/basic/config"
|
||
"SciencesServer/app/basic/controller"
|
||
model2 "SciencesServer/app/common/model"
|
||
"SciencesServer/app/session"
|
||
"SciencesServer/serve/orm"
|
||
"SciencesServer/utils"
|
||
"errors"
|
||
"fmt"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// Patent 专利信息
|
||
type Patent struct {
|
||
*session.Admin
|
||
}
|
||
|
||
type PatentHandle func(session *session.Admin) *Patent
|
||
|
||
type (
|
||
// PatentInfo 专利信息
|
||
PatentInfo struct {
|
||
ID string `json:"id"`
|
||
UID string `json:"uid"`
|
||
*model.SysPatentInfo
|
||
}
|
||
// PatentDetailInfo 专利详细信息
|
||
PatentDetailInfo struct {
|
||
ID string `json:"id"`
|
||
*model2.SysPatent
|
||
}
|
||
// 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.SysPatentClassify, parentID uint64) []*PatentIPCInfo {
|
||
out := make([]*PatentIPCInfo, 0)
|
||
|
||
for _, v := range src {
|
||
if v.ParentID == parentID {
|
||
industrys := v.GetIndustryAttribute()
|
||
industrysTitle := make([]string, 0)
|
||
|
||
for _, v := range industrys {
|
||
industrysTitle = append(industrysTitle, config.GetIndustryInfo(v, "-", "/").Value)
|
||
}
|
||
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.SysPatent, condition map[string]interface{}) error {
|
||
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.NewSysPatent()
|
||
|
||
where := make([]*model2.ModelWhere, 0)
|
||
|
||
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
|
||
}
|
||
list := make([]*PatentInfo, 0)
|
||
|
||
for _, v := range out {
|
||
list = append(list, &PatentInfo{
|
||
ID: v.GetEncodeID(),
|
||
UID: fmt.Sprintf("%d", v.UID),
|
||
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
|
||
}
|
||
|
||
// Form 数据操作
|
||
func (c *Patent) Form(params *PatentParams) error {
|
||
mSysPatent := model.NewSysPatent()
|
||
|
||
_condition := make(map[string]interface{}, 0)
|
||
|
||
if params.ID > 0 {
|
||
mSysPatent.ID = params.ID
|
||
|
||
isExist, err := model2.FirstField(mSysPatent.SysPatent, []string{"id", "tenant_id", "apply_code", "ipc_code", "created_at"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,专利信息不存在或已被删除")
|
||
}
|
||
if mSysPatent.ApplyCode != params.ApplyCode {
|
||
_condition["apply_code"] = params.ApplyCode
|
||
}
|
||
if mSysPatent.IPCCode != params.IPCCode {
|
||
_condition["ipc_code"] = params.IPCCode
|
||
}
|
||
if len(_condition) > 0 {
|
||
if isExist, err = mSysPatent.IsExistParams(_condition); err != nil {
|
||
return err
|
||
} else if isExist {
|
||
return errors.New("操作错误,申请号或公开(公告)号已存在")
|
||
}
|
||
}
|
||
}
|
||
mSysPatent.Kind = model2.SysParentKind(params.Kind)
|
||
mSysPatent.Title = params.Title
|
||
mSysPatent.FileUrl = params.FileUrl
|
||
mSysPatent.ApplyCode = params.ApplyCode
|
||
mSysPatent.ApplyAt = params.ApplyAt
|
||
mSysPatent.OpenCode = params.OpenCode
|
||
mSysPatent.OpenAt = params.OpenAt
|
||
mSysPatent.ApplyName = params.ApplyName
|
||
mSysPatent.ApplyAddress = params.ApplyAddress
|
||
mSysPatent.Inventor = params.Inventor
|
||
mSysPatent.Description = params.Description
|
||
mSysPatent.PrincipalClaim = params.PrincipalClaim
|
||
mSysPatent.IPCCode = params.IPCCode
|
||
mSysPatent.ShelfStatus = model2.ShelfStatusKind(params.ShelfStatus)
|
||
mSysPatent.Status = model2.SysParentStatus(params.Status)
|
||
|
||
if mSysPatent.ID > 0 {
|
||
return model2.Updates(mSysPatent.SysPatent, mSysPatent.SysPatent)
|
||
}
|
||
_condition["apply_code"] = params.ApplyCode
|
||
_condition["ipc_code"] = params.IPCCode
|
||
|
||
if isExist, err := mSysPatent.IsExistParams(_condition); err != nil {
|
||
return err
|
||
} else if isExist {
|
||
return errors.New("操作错误,申请号或公开(公告)号已存在")
|
||
}
|
||
return model2.Create(mSysPatent.SysPatent)
|
||
}
|
||
|
||
// Bind 绑定信息
|
||
func (c *Patent) Bind(id, uid uint64) error {
|
||
mSysPatent := model.NewSysPatent()
|
||
mSysPatent.ID = id
|
||
|
||
isExist, err := model2.FirstField(mSysPatent.SysPatent, []string{"id", "tenant_id"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,专利信息不存在或已被删除")
|
||
}
|
||
mUserPatent := model.NewUserPatent()
|
||
|
||
if isExist, err = model2.FirstField(mUserPatent.UserPatent, []string{"id", "uid"}, model2.NewWhere("patent_id", id)); err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
mUserPatent.UID = uid
|
||
mUserPatent.PatentID = id
|
||
return model2.Create(mUserPatent.UserPatent)
|
||
}
|
||
if mUserPatent.UID == uid {
|
||
return nil
|
||
}
|
||
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
||
if err = model2.DeleteWhere(mUserPatent.UserPatent, []*model2.ModelWhere{model2.NewWhere("patent_id", id)}); err != nil {
|
||
return err
|
||
}
|
||
mUserPatent.UID = uid
|
||
mUserPatent.PatentID = id
|
||
return model2.Create(mUserPatent.UserPatent, tx)
|
||
})
|
||
}
|
||
|
||
func (c *Patent) Shelf(id uint64) error {
|
||
return controller.NewShelf(controller.WithShelfSessionAdmin(c.Admin)).Handle(model2.NewSysPatent(), id)
|
||
}
|
||
|
||
// Delete 删除操作
|
||
func (c *Patent) Delete(id uint64) error {
|
||
mSysPatent := model.NewSysPatent()
|
||
mSysPatent.ID = id
|
||
|
||
isExist, err := model2.FirstField(mSysPatent.SysPatent, []string{"id", "tenant_id"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,专利信息不存在或已被删除")
|
||
} else if c.TenantID > 0 && mSysPatent.TenantID != c.TenantID {
|
||
return errors.New("操作错误,无权限操作")
|
||
}
|
||
return model2.Delete(mSysPatent.SysPatent)
|
||
}
|
||
|
||
// IPC IPC信息
|
||
func (c *Patent) IPC() ([]*PatentIPCInfo, error) {
|
||
mSysPatentClassify := model.NewSysPatentClassify()
|
||
out := make([]*model2.SysPatentClassify, 0)
|
||
|
||
if err := model2.Scan(mSysPatentClassify.SysPatentClassify, &out); err != nil {
|
||
return nil, err
|
||
}
|
||
return c.ipcTree(out, 0), nil
|
||
}
|
||
|
||
// IPCForm IPC数据操作
|
||
func (c *Patent) IPCForm(params *PatentIPCParams) error {
|
||
mSysPatentClassify := model.NewSysPatentClassify()
|
||
|
||
if params.ID > 0 {
|
||
mSysPatentClassify.ID = params.ID
|
||
|
||
isExist, err := model2.FirstField(mSysPatentClassify.SysPatentClassify, []string{"id", "created_at"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,IPC信息不存在或已被删除")
|
||
}
|
||
}
|
||
mSysPatentClassify.SetIndustryDetailAttribute(params.Industrys)
|
||
// 查询上级元素
|
||
if params.ParentID > 0 {
|
||
mPSysPatentClassify := model.NewSysPatentClassify()
|
||
mPSysPatentClassify.ID = params.ParentID
|
||
|
||
isExist, err := model2.FirstField(mPSysPatentClassify.SysPatentClassify, []string{"id", "industry"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,IPC父级信息不存在或已被删除")
|
||
}
|
||
_identitys := mPSysPatentClassify.GetIndustryDetailAttribute()
|
||
|
||
for _, v := range params.Industrys {
|
||
if utils.InArray(v, _identitys) {
|
||
continue
|
||
}
|
||
_identitys = append(_identitys, v)
|
||
}
|
||
mSysPatentClassify.SetIndustryDetailAttribute(_identitys)
|
||
}
|
||
mSysPatentClassify.ParentID = params.ParentID
|
||
mSysPatentClassify.IPC = params.IPC
|
||
mSysPatentClassify.SetIndustryAttribute(params.Industrys)
|
||
|
||
if mSysPatentClassify.ID > 0 {
|
||
return model2.Updates(mSysPatentClassify.SysPatentClassify, mSysPatentClassify.SysPatentClassify)
|
||
}
|
||
|
||
return model2.Create(mSysPatentClassify.SysPatentClassify)
|
||
}
|
||
|
||
// IPCDelete IPC删除操作
|
||
func (c *Patent) IPCDelete(id uint64) error {
|
||
mSysPatentClassify := model.NewSysPatentClassify()
|
||
mSysPatentClassify.ID = id
|
||
|
||
isExist, err := model2.FirstField(mSysPatentClassify.SysPatentClassify, []string{"id", "created_at"})
|
||
|
||
if err != nil {
|
||
return err
|
||
} else if !isExist {
|
||
return errors.New("操作错误,IPC信息不存在或已被删除")
|
||
}
|
||
return model2.Delete(mSysPatentClassify.SysPatentClassify)
|
||
}
|
||
|
||
func NewPatent() PatentHandle {
|
||
return func(session *session.Admin) *Patent {
|
||
return &Patent{
|
||
Admin: session,
|
||
}
|
||
}
|
||
}
|