Files

353 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"
"SciencesServer/app/session"
"SciencesServer/utils"
"errors"
2022-01-15 09:00:47 +08:00
"fmt"
"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 {
ID string `json:"id"`
UID string `json:"uid"`
2022-01-15 09:00:47 +08:00
*model.SysPatentInfo
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{
ID: v.GetEncodeID(),
UID: fmt.Sprintf("%d", v.UID),
SysPatentInfo: v,
})
}
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)
if mTechnologyPatent.ID > 0 {
return model2.Updates(mTechnologyPatent.TechnologyPatent, mTechnologyPatent.TechnologyPatent)
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
return model2.Create(mTechnologyPatent.TechnologyPatent)
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
//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)
//})
2022-01-14 17:09:06 +08:00
}
2022-01-24 11:28:21 +08:00
func (c *Patent) Shelf(id uint64) error {
return controller.NewShelf(controller.WithShelfSessionAdmin(c.Admin)).Handle(model2.NewTechnologyPatent(), id)
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
}
return model2.Delete(mTechnologyPatent.TechnologyPatent)
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,
}
}
}