作者 陈志颖

feat:增加合伙人编号去重

appname = partnermg
runmode = "${RUN_MODE||dev}"
httpport = "${HTTP_PORT||8083}"
httpport = "${HTTP_PORT||8082}"
#开启监控
EnableAdmin = false
... ...
... ... @@ -23,7 +23,7 @@ func NewPartnerInfoService(options map[string]interface{}) *PartnerInfoService {
/**
* @Author SteveChan
* @Description //TODO 创建合伙人,判断编号是否重复
* @Description // 创建合伙人
* @Date 15:42 2020/12/29
* @Param
* @return
... ... @@ -51,6 +51,7 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.Cre
}); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
ok, err := partnerInfoDao.PartnerAccountExist(cmd.Account, cmd.CompanyId)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
... ... @@ -59,6 +60,15 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.Cre
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "账号已存在")
}
// 编号去重
for _, partnerCategory := range cmd.PartnerCategory {
if ok, err := partnerInfoDao.PartnerCodeExist(partnerCategory.Id, partnerCategory.Code, cmd.CompanyId, 0); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else if ok {
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "合伙类型"+""+"编号"+partnerCategory.Code+"已存在")
}
}
var (
partnerInfoRepository domain.PartnerInfoRepository
categoryRepository domain.PartnerCategoryRepository
... ... @@ -74,6 +84,7 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.Cre
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
var ids []int64
for _, partnerCategory := range cmd.PartnerCategory {
ids = append(ids, partnerCategory.Id)
... ... @@ -85,6 +96,13 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.Cre
e := fmt.Sprintf("获取合伙人分类数据失败:%s", err)
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
for i, category := range categories {
for _, partnerCategory := range cmd.PartnerCategory {
if category.Id == partnerCategory.Id {
categories[i].Code = partnerCategory.Code
}
}
}
newPartnerInfo := domain.PartnerInfo{
Partner: domain.Partner{
... ... @@ -99,6 +117,7 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.Cre
CooperateTime: cmd.CooperateTime,
CompanyId: cmd.CompanyId,
PartnerCategoryInfos: categories,
Remark: cmd.Remark,
}
if err = partnerInfoRepository.Save(&newPartnerInfo); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
... ... @@ -120,7 +139,7 @@ func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.Cre
/**
* @Author SteveChan
* @Description //TODO 返回合伙人,增加合伙人编号字段
* @Description // 返回合伙人,增加合伙人编号字段
* @Date 15:43 2020/12/29
* @Param
* @return
... ... @@ -129,6 +148,7 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(q query.GetPartnerI
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
if err = q.ValidateQuery(); err != nil {
return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
}
... ... @@ -138,17 +158,21 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(q query.GetPartnerI
defer func() {
transactionContext.RollbackTransaction()
}()
var (
PartnerInfoRepository domain.PartnerInfoRepository
categoryRepository domain.PartnerCategoryRepository
categories []domain.PartnerCategory
partnerData *domain.PartnerInfo
)
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
// 获取合伙人数据
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
UserId: q.Id, CompanyId: q.CompanyId,
})
... ... @@ -158,6 +182,8 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(q query.GetPartnerI
if !partnerData.IsCompany(q.CompanyId) {
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "企业信息异常操作")
}
// 获取合伙人类别
if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
... ... @@ -174,17 +200,31 @@ func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(q query.GetPartnerI
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
for i, category := range categories {
for _, partnerCategory := range partnerData.PartnerCategoryInfos {
if category.Id == partnerCategory.Id {
categories[i].Code = partnerCategory.Code
}
}
}
}
partnerData.PartnerCategoryInfos = categories
err = transactionContext.CommitTransaction()
return partnerData, nil
}
//UpdatePartnerInfo 更新合伙人
/**
* @Author SteveChan
* @Description // 更新合伙人
* @Date 00:07 2020/12/30
* @Param
* @return
**/
func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.UpdatePartnerInfoCommand) (err error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
)
if err = cmd.ValidateCommand(); err != nil {
return application.ThrowError(application.ARG_ERROR, err.Error())
}
... ... @@ -194,32 +234,62 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.Upd
defer func() {
transactionContext.RollbackTransaction()
}()
var (
partnerInfoRepository domain.PartnerInfoRepository
categoryRepository domain.PartnerCategoryRepository
categorys []domain.PartnerCategory
categories []domain.PartnerCategory
partnerInfoDao *dao.PartnerInfoDao
)
if partnerInfoDao, err = factory.CreatePartnerInfoDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
// 编号去重
for _, partnerCategory := range cmd.PartnerCategory {
if ok, err := partnerInfoDao.PartnerCodeExist(partnerCategory.Id, partnerCategory.Code, cmd.CompanyId, cmd.Id); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "编号"+partnerCategory.Code+"已存在")
}
}
var ids []int64
for _, partnerCategory := range cmd.PartnerCategory {
ids = append(ids, partnerCategory.Id)
}
_, categorys, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{
_, categories, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{
Ids: ids,
})
if err != nil {
e := fmt.Sprintf("获取合伙人分类数据失败:%s", err)
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
}
for i, category := range categories {
for _, partnerCategory := range cmd.PartnerCategory {
if category.Id == partnerCategory.Id {
categories[i].Code = partnerCategory.Code
}
}
}
partnerInfo, err := partnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
UserId: cmd.Id,
CompanyId: cmd.CompanyId,
... ... @@ -230,11 +300,14 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.Upd
if !partnerInfo.IsCompany(cmd.CompanyId) {
return lib.ThrowError(lib.BUSINESS_ERROR, "异常操作")
}
partnerInfo.Salesman = cmd.Salesman
partnerInfo.Status = cmd.Status
partnerInfo.RegionInfo = *cmd.RegionInfo
partnerInfo.CooperateTime = cmd.CooperateTime
partnerInfo.PartnerCategoryInfos = categorys
partnerInfo.PartnerCategoryInfos = categories
partnerInfo.Remark = cmd.Remark
if err = partnerInfoRepository.Save(partnerInfo); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
... ... @@ -254,7 +327,13 @@ func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.Upd
return
}
// ListPartnerInfo 合伙人列表
/**
* @Author SteveChan
* @Description //合伙人列表,返回合伙人编号
* @Date 00:07 2020/12/30
* @Param
* @return
**/
func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(listPartnerInfoQuery *query.ListPartnerInfoQuery) (int, []domain.PartnerInfo, error) {
var (
transactionContext, _ = factory.CreateTransactionContext(nil)
... ... @@ -318,6 +397,7 @@ func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(listPartnerInfoQue
var categoryInPartner []domain.PartnerCategory
for _, vv := range partnerInfos[i].PartnerCategoryInfos {
if categoryData, ok := categoriesMap[vv.Id]; ok {
categoryData.Code = vv.Code
categoryInPartner = append(categoryInPartner, categoryData)
}
}
... ... @@ -350,6 +430,23 @@ func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(cmd command.Remo
transactionContext.RollbackTransaction()
}()
var (
partnerInfoRepository domain.PartnerInfoRepository
)
if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
// 判断合伙人是否有业务数据
if err = partnerInfoRepository.Remove(cmd.Id); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return nil
}
... ...
... ... @@ -4,9 +4,9 @@ import "os"
var POSTGRESQL_DB_NAME = "partner_test"
var POSTGRESQL_USER = "postgres"
var POSTGRESQL_PASSWORD = "eagle1010"
var POSTGRESQL_HOST = "114.55.200.59"
var POSTGRESQL_PORT = "31543"
var POSTGRESQL_PASSWORD = "1993618jack" // eagle1010
var POSTGRESQL_HOST = "127.0.0.1" // 114.55.200.59
var POSTGRESQL_PORT = "5432" // 31543
var DISABLE_CREATE_TABLE = true
var DISABLE_SQL_GENERATE_PRINT = false
... ...
... ... @@ -74,6 +74,7 @@ type PartnerFindQuery struct {
PartnerName string //合伙人姓名
CompanyId int64
Ids []int64
PartnerType []*PartnerCategory
}
type PartnerInfoRepository interface {
... ...
... ... @@ -2,7 +2,7 @@ package dao
import (
"fmt"
"github.com/go-pg/pg/v10"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
... ... @@ -28,7 +28,6 @@ func (dao PartnerInfoDao) PartnerAccountExist(account string, companyId int64) (
Where("account=?", account).
Where("company_id=?", companyId).
Exists()
return ok, err
}
... ... @@ -45,11 +44,18 @@ func (dao PartnerInfoDao) UpdatePartnerStatus(ids []int64, companyId int64, stat
/**
* @Author SteveChan
* @Description //TODO 编号查重
* @Description // 编号查重
* @Date 17:55 2020/12/28
* @Param
* @return
**/
func (dao PartnerInfoDao) CheckDuplication() {
func (dao PartnerInfoDao) PartnerCodeExist(categoryId int64, code string, companyId int64, id int64) (bool, error) {
tx := dao.transactionContext.PgDd
m := &models.PartnerInfo{}
ok, err := tx.Model(m).
Where(`partner_category_infos@> '[{"id":?,"code":?}]'`, categoryId, pg.Ident(code)).
Where("company_id=?", companyId).
Where("id <> ?", id).
Exists()
return ok, err
}
... ...
... ... @@ -55,7 +55,7 @@ func (repository *PartnerInfoRepository) Save(dm *domain.PartnerInfo) error {
} else {
_, err = tx.Model(m).WherePK().
Column("partner_name", "account", "password", "status", "partner_category", "salesman",
"region_info", "cooperate_time", "update_at", "partner_category_infos, remark").
"region_info", "cooperate_time", "update_at", "partner_category_infos", "remark").
Update()
if err != nil {
return err
... ...
... ... @@ -43,6 +43,7 @@ func (c *PartnerInfoController) CreatePartnerInfo() {
CooperationTime string `json:"cooperationTime"`
SalesmanName string `json:"salesmanName"`
Phone string `json:"phone"`
Remark string `json:"remark"`
}
var (
param Parameter
... ... @@ -69,6 +70,7 @@ func (c *PartnerInfoController) CreatePartnerInfo() {
PartnerCategory: param.PartnerType,
CooperateTime: cooperateTime,
CompanyId: companyId,
Remark: param.Remark,
}
if len(param.SalesmanName) > 0 || len(param.Phone) > 0 {
cmd.Salesman = []domain.Salesman{
... ... @@ -110,6 +112,7 @@ func (c *PartnerInfoController) UpdatePartnerInfo() {
CooperationTime string `json:"cooperationTime"`
SalesmanName string `json:"salesmanName"`
Phone string `json:"phone"`
Remark string `json:"remark"`
}
var (
param Parameter
... ... @@ -142,6 +145,7 @@ func (c *PartnerInfoController) UpdatePartnerInfo() {
},
},
CompanyId: companyId,
Remark: param.Remark,
}
serve := partnerInfoService.NewPartnerInfoService(nil)
err = serve.UpdatePartnerInfo(&cmd)
... ... @@ -190,6 +194,7 @@ func (c *PartnerInfoController) GetPartnerInfo() {
"cooperationTime": partnerInfo.CooperateTime.Local().Format("2006-01-02"),
"state": partnerInfo.Status,
"id": partnerInfo.Partner.Id,
"remark": partnerInfo.Remark,
}
if len(partnerInfo.Salesman) > 0 {
rspResult["salesmanName"] = partnerInfo.Salesman[0].Name
... ... @@ -217,7 +222,30 @@ func (c *PartnerInfoController) GetPartnerInfo() {
* @return
**/
func (c *PartnerInfoController) RemovePartnerInfo() {
//用与适配前端定义的数据结构
type Parameter struct {
ID int64 `json:"id"`
}
var (
param Parameter
err error
)
if err = c.BindJsonData(&param); err != nil {
logs.Error(err)
c.ResponseError(errors.New("json数据解析失败"))
return
}
cmd := partnerInfoCmd.RemovePartnerInfoCommand{
Id: param.ID,
}
serve := partnerInfoService.NewPartnerInfoService(nil)
err = serve.RemovePartnerInfo(cmd)
if err != nil {
c.ResponseError(err)
return
}
c.ResponseData(nil)
return
}
//PartnerInfoSetState 合伙人批量禁用.启用
... ... @@ -321,6 +349,7 @@ func (c *PartnerInfoController) ListPartnerInfo() {
"partnerType": partners[i].PartnerCategoryInfos,
"salesmanName": "",
"phone": "",
"remark": partners[i].Remark,
}
if len(partners[i].Salesman) > 0 {
m["salesmanName"] = partners[i].Salesman[0].Name
... ...
... ... @@ -23,7 +23,7 @@ func init() {
beego.NSRouter("/edit", &controllers.PartnerInfoController{}, "POST:UpdatePartnerInfo"),
beego.NSRouter("/detail", &controllers.PartnerInfoController{}, "POST:GetPartnerInfo"),
beego.NSRouter("/batchDisabled", &controllers.PartnerInfoController{}, "POST:PartnerInfoSetState"),
beego.NSRouter("/:id", &controllers.PartnerInfoController{}, "DELETE:RemovePartnerInfo"),
beego.NSRouter("/remove", &controllers.PartnerInfoController{}, "POST:RemovePartnerInfo"),
),
beego.NSNamespace("/dividends",
beego.NSRouter("/list", &controllers.OrderDividendController{}, "POST:PageListOrderDividend"),
... ...