package technology import ( "SciencesServer/app/api/admin/model" "SciencesServer/app/basic/config" "SciencesServer/app/basic/controller" model2 "SciencesServer/app/common/model" "SciencesServer/app/service" "SciencesServer/app/session" "SciencesServer/utils" "errors" "strings" "time" ) // Patent 专利信息 type Patent struct { *session.Admin } type PatentHandle func(session *session.Admin) *Patent type ( // PatentInfo 专利信息 PatentInfo struct { ID string `json:"id"` *model.TechnologyPatentInfo } // PatentDetailInfo 专利详细信息 PatentDetailInfo struct { ID string `json:"id"` *model2.TechnologyPatent } // 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 { 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.TechnologyPatent, 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.NewTechnologyPatent() 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)) } 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(), TechnologyPatentInfo: v, }) } 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 } // Form 数据操作 func (c *Patent) Form(params *PatentParams) error { mTechnologyPatent := model.NewTechnologyPatent() _condition := make(map[string]interface{}, 0) if params.ID > 0 { mTechnologyPatent.ID = params.ID isExist, err := model2.FirstField(mTechnologyPatent.TechnologyPatent, []string{"id", "tenant_id", "uid", "apply_code", "ipc_code", "created_at"}) 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("操作错误,无权限操作") } if mTechnologyPatent.ApplyCode != params.ApplyCode { _condition["apply_code"] = params.ApplyCode } if mTechnologyPatent.IPCCode != params.IPCCode { _condition["ipc_code"] = params.IPCCode } if len(_condition) > 0 { if isExist, err = mTechnologyPatent.IsExistParams(_condition); err != nil { return err } else if isExist { return errors.New("操作错误,申请号或公开(公告)号已存在") } } } 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) mTechnologyPatent.Status = model2.TechnologyPatentStatus(params.Status) // 查询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 { if err = model2.Updates(mTechnologyPatent.TechnologyPatent, mTechnologyPatent.TechnologyPatent); err != nil { return err } _ = manage.Update() return nil } _condition["apply_code"] = params.ApplyCode _condition["ipc_code"] = params.IPCCode if isExist, err := mTechnologyPatent.IsExistParams(_condition); err != nil { return err } else if isExist { return errors.New("操作错误,申请号或公开(公告)号已存在") } mTechnologyPatent.TenantID = c.TenantID if err := model2.Create(mTechnologyPatent.TechnologyPatent); err != nil { return err } service.WithPatentID(mTechnologyPatent.ID)(manage) return manage.Create() } // Bind 绑定信息 func (c *Patent) Bind(id, uid uint64) error { mTechnologyPatent := model.NewTechnologyPatent() mTechnologyPatent.ID = id isExist, err := model2.FirstField(mTechnologyPatent.TechnologyPatent, []string{"id", "uid", "tenant_id"}) 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("操作错误,无权限操作") } if err = model2.Updates(mTechnologyPatent.TechnologyPatent, map[string]interface{}{ "uid": uid, "updated_at": time.Now(), }); err != nil { return err } return nil } // Shelf 上下架操作 func (c *Patent) Shelf(id uint64) error { 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() }) } // Delete 删除操作 func (c *Patent) Delete(id uint64) error { mTechnologyPatent := model.NewTechnologyPatent() mTechnologyPatent.ID = id isExist, err := model2.FirstField(mTechnologyPatent.TechnologyPatent, []string{"id", "uid", "tenant_id"}) 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("操作错误,无权限操作") } err = model2.Delete(mTechnologyPatent.TechnologyPatent) if err != nil { return err } _ = service.NewESPatent(service.WithPatentID(mTechnologyPatent.ID)).Delete() return nil } // IPC IPC信息 func (c *Patent) IPC() ([]*PatentIPCInfo, error) { mTechnologyPatentClassify := model.NewTechnologyPatentClassify() out := make([]*model2.TechnologyPatentClassify, 0) if err := model2.Scan(mTechnologyPatentClassify.TechnologyPatentClassify, &out); err != nil { return nil, err } return c.ipcTree(out, 0), nil } // IPCForm IPC数据操作 func (c *Patent) IPCForm(params *PatentIPCParams) error { mTechnologyPatentClassify := model.NewTechnologyPatentClassify() if params.ID > 0 { mTechnologyPatentClassify.ID = params.ID isExist, err := model2.FirstField(mTechnologyPatentClassify.TechnologyPatentClassify, []string{"id", "created_at"}) if err != nil { return err } else if !isExist { return errors.New("操作错误,IPC信息不存在或已被删除") } } mTechnologyPatentClassify.SetIndustryDetailAttribute(params.Industrys) // 查询上级元素 if params.ParentID > 0 { mPTechnologyPatentClassify := model.NewTechnologyPatentClassify() mPTechnologyPatentClassify.ID = params.ParentID isExist, err := model2.FirstField(mPTechnologyPatentClassify.TechnologyPatentClassify, []string{"id", "industry"}) if err != nil { return err } else if !isExist { return errors.New("操作错误,IPC父级信息不存在或已被删除") } _identitys := mPTechnologyPatentClassify.GetIndustryDetailAttribute() for _, v := range params.Industrys { if utils.InArray(v, _identitys) { continue } _identitys = append(_identitys, v) } mTechnologyPatentClassify.SetIndustryDetailAttribute(_identitys) } mTechnologyPatentClassify.ParentID = params.ParentID mTechnologyPatentClassify.IPC = params.IPC mTechnologyPatentClassify.SetIndustryAttribute(params.Industrys) if mTechnologyPatentClassify.ID > 0 { return model2.Updates(mTechnologyPatentClassify.TechnologyPatentClassify, mTechnologyPatentClassify.TechnologyPatentClassify) } return model2.Create(mTechnologyPatentClassify.TechnologyPatentClassify) } // IPCDelete IPC删除操作 func (c *Patent) IPCDelete(id uint64) error { mTechnologyPatentClassify := model.NewTechnologyPatentClassify() mTechnologyPatentClassify.ID = id isExist, err := model2.FirstField(mTechnologyPatentClassify.TechnologyPatentClassify, []string{"id", "created_at"}) if err != nil { return err } else if !isExist { return errors.New("操作错误,IPC信息不存在或已被删除") } return model2.Delete(mTechnologyPatentClassify.TechnologyPatentClassify) } func NewPatent() PatentHandle { return func(session *session.Admin) *Patent { return &Patent{ Admin: session, } } }