作者 陈志颖

fix:共创申请校验

... ... @@ -14,11 +14,11 @@ type ApplyForCooperationCommand struct {
// 共创申请描述
CooperationApplicationDescription string `cname:"共创申请描述" json:"cooperationApplicationDescription,omitempty"`
// 申请人ID
UserId int64 `cname:"申请人ID" json:"userId" valid:"Required"`
UserId int64 `cname:"申请人ID" json:"userId"`
// 用户基本id
UserBaseId int64 `cname:"用户基本数据ID" json:"userBaseId" valid:"Required"`
UserBaseId int64 `cname:"用户基本数据ID" json:"userBaseId"`
// 组织机构ID
OrgId int64 `cname:"组织机构ID" json:"orgId" valid:"Required"`
OrgId int64 `cname:"组织机构ID" json:"orgId"`
// 公司ID,通过集成REST上下文获取
CompanyId int64 `cname:"公司ID" json:"companyId"`
// 共创项目ID
... ...
... ... @@ -10,6 +10,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/utils"
"strconv"
"time"
... ... @@ -53,14 +54,6 @@ func (cooperationApplicationService *CooperationApplicationService) ApplyForCoop
userService = value
}
// 获取申请人
var applicant *domain.User
if data, err := userService.UserFrom(applyForCooperationCommand.CompanyId, applyForCooperationCommand.OrgId, applyForCooperationCommand.UserId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取申请人失败")
} else {
applicant = data
}
// 公司REST服务初始化
var companyService service.CompanyService
if value, err := factory.CreateCompanyService(map[string]interface{}{}); err != nil {
... ... @@ -69,14 +62,6 @@ func (cooperationApplicationService *CooperationApplicationService) ApplyForCoop
companyService = value
}
// 获取公司信息
var company *domain.Company
if data, err := companyService.CompanyFrom(applyForCooperationCommand.CompanyId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司信息失败")
} else {
company = data
}
// 组织机构REST服务初始化
var organizationService service.OrgService
if value, err := factory.CreateOrganizationService(map[string]interface{}{}); err != nil {
... ... @@ -85,12 +70,24 @@ func (cooperationApplicationService *CooperationApplicationService) ApplyForCoop
organizationService = value
}
// 获取组织机构信息
var organization *domain.Org
if data, err := organizationService.OrgFrom(applyForCooperationCommand.CompanyId, applyForCooperationCommand.OrgId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取组织机构数据失败")
// 共创申请仓储初始化
var cooperationApplicationRepository domain.CooperationApplicationRepository
if value, err := factory.CreateCooperationApplicationRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
organization = data
cooperationApplicationRepository = value
}
// 共创申请DAO初始化
var cooperationApplicationDao *dao.CooperationApplicationDao
if value, err := factory.CreateCooperationApplicationDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
cooperationApplicationDao = value
}
// 获取共创项目
... ... @@ -102,36 +99,95 @@ func (cooperationApplicationService *CooperationApplicationService) ApplyForCoop
return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(applyForCooperationCommand.CooperationProjectId, 10)))
}
// TODO 校验:同一个用户,不能多次申请同一个项目
var newCooperationApplication *domain.CooperationApplication
if applyForCooperationCommand.CompanyId == 0 && applyForCooperationCommand.OrgId == 0 && applyForCooperationCommand.UserId == 0 { // 游客操作
// TODO 获取申请人信息
// TODO 校验:同一个用户,不能多次申请同一个项目
// TODO 校验:判断用户类型是否属于承接对象
//if !utils.IsContain(cooperationProject.CooperationProjectUndertakerTypes, applicant.UserType) {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "抱歉,您不属于当前项目的承接对象")
//}
newCooperationApplication = &domain.CooperationApplication{
CooperationApplicationApplicant: nil, // TODO 获取游客(申请人)信息
CooperationApplicationAttachment: applyForCooperationCommand.CooperationApplicationAttachment,
CooperationApplicationDescription: applyForCooperationCommand.CooperationApplicationDescription,
CooperationApplicationStatus: 1,
CooperationApplicationVerifier: nil,
CooperationApplicationVerifyDescription: "",
CooperationApplicationVerifyTime: time.Time{},
CooperationApplyTime: time.Now(),
CooperationProject: cooperationProject,
Org: cooperationProject.Org,
IsCanceled: 1,
Company: cooperationProject.Company,
CreatedAt: time.Now(),
DeletedAt: time.Time{},
UpdatedAt: time.Time{},
}
} else {
// 获取申请人
var applicant *domain.User
if data, err := userService.UserFrom(applyForCooperationCommand.CompanyId, applyForCooperationCommand.OrgId, applyForCooperationCommand.UserId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取申请人失败")
} else {
applicant = data
}
// TODO 校验:判断用户类型是否属于承接对象
// 获取公司信息
var company *domain.Company
if data, err := companyService.CompanyFrom(applyForCooperationCommand.CompanyId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取公司信息失败")
} else {
company = data
}
newCooperationApplication := &domain.CooperationApplication{
CooperationApplicationApplicant: applicant,
CooperationApplicationAttachment: applyForCooperationCommand.CooperationApplicationAttachment,
CooperationApplicationDescription: applyForCooperationCommand.CooperationApplicationDescription,
CooperationApplicationStatus: 1,
CooperationApplicationVerifier: nil,
CooperationApplicationVerifyDescription: "",
CooperationApplicationVerifyTime: time.Time{},
CooperationApplyTime: time.Now(),
CooperationProject: cooperationProject,
Org: organization,
IsCanceled: 1,
Company: company,
CreatedAt: time.Now(),
DeletedAt: time.Time{},
UpdatedAt: time.Time{},
}
// 获取组织机构信息
var organization *domain.Org
if data, err := organizationService.OrgFrom(applyForCooperationCommand.CompanyId, applyForCooperationCommand.OrgId); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "获取组织机构数据失败")
} else {
organization = data
}
var cooperationApplicationRepository domain.CooperationApplicationRepository
if value, err := factory.CreateCooperationApplicationRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
cooperationApplicationRepository = value
// 校验:同一个用户,不能多次申请同一个项目
applicationExist, _ := cooperationApplicationDao.CheckApplicationExist(map[string]interface{}{
"companyId": applyForCooperationCommand.CompanyId,
"orgId": applyForCooperationCommand.OrgId,
"cooperationApplicationId": cooperationProject.CooperationProjectId,
})
if applicationExist {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "抱歉,您已经申请过该项目")
}
// 校验:判断用户类型是否属于承接对象
if !utils.IsContain(cooperationProject.CooperationProjectUndertakerTypes, applicant.UserType) {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "抱歉,您不属于当前项目的承接对象")
}
newCooperationApplication = &domain.CooperationApplication{
CooperationApplicationApplicant: applicant,
CooperationApplicationAttachment: applyForCooperationCommand.CooperationApplicationAttachment,
CooperationApplicationDescription: applyForCooperationCommand.CooperationApplicationDescription,
CooperationApplicationStatus: 1,
CooperationApplicationVerifier: nil,
CooperationApplicationVerifyDescription: "",
CooperationApplicationVerifyTime: time.Time{},
CooperationApplyTime: time.Now(),
CooperationProject: cooperationProject,
Org: organization,
IsCanceled: 1,
Company: company,
CreatedAt: time.Now(),
DeletedAt: time.Time{},
UpdatedAt: time.Time{},
}
}
// 保存共创申请
if cooperationApplication, err := cooperationApplicationRepository.Save(newCooperationApplication); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
... ...
... ... @@ -60,3 +60,11 @@ func CreateDividendsEstimateDao(options map[string]interface{}) (*dao.DividendsE
}
return dao.NewDividendsEstimateDao(transactionContext)
}
func CreateCooperationApplicationDao(options map[string]interface{}) (*dao.CooperationApplicationDao, error) {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return dao.NewCooperationApplicationDao(transactionContext)
}
... ...
package dao
import (
"fmt"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
)
type CooperationApplicationDao struct {
transactionContext *pgTransaction.TransactionContext
}
// CheckApplicationExist 判断该申请人是否提交过申请
func (dao *CooperationApplicationDao) CheckApplicationExist(queryOptions map[string]interface{}) (bool, error) {
tx := dao.transactionContext.PgTx
var cooperationApplicationModels []*models.CooperationApplication
query := tx.Model(&cooperationApplicationModels)
if cooperationApplicationId, ok := queryOptions["cooperationApplicationId"]; ok && cooperationApplicationId != "" {
query = query.Where("cooperation_application_id = ?", cooperationApplicationId)
}
if companyId, ok := queryOptions["companyId"]; ok && companyId.(int64) != 0 {
query = query.Where(`cooperation_application.company @> '{"companyId":"?"}'`, companyId)
}
if orgId, ok := queryOptions["orgId"]; ok && orgId.(int64) != 0 {
query = query.Where(`cooperation_application.org @> '{"orgId":"?"}'`, orgId)
}
if applicantId, ok := queryOptions["applicantId"]; ok && applicantId.(int64) != 0 {
query = query.Where(`cooperation_application.cooperation_application_applicant @>'{"userId":"?"}'`, applicantId)
}
if applicantBaseId, ok := queryOptions["applicantBaseId"]; ok && applicantBaseId.(int64) != 0 {
query = query.Where(`cooperation_application.cooperation_application_applicant @>'{"userBaseId":"?"}'`, applicantBaseId)
}
ok, err := query.Exists()
return ok, err
}
func NewCooperationApplicationDao(transactionContext *pgTransaction.TransactionContext) (*CooperationApplicationDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &CooperationApplicationDao{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -125,3 +125,13 @@ func RemoveDuplication(arr []string) []string {
}
return arr[:j]
}
// IsContain 判断int32数组是否包含
func IsContain(items []int32, item int32) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
... ...