...
|
...
|
@@ -941,3 +941,261 @@ func (service OrderInfoService) ListOrderForExcel(listOrderQuery query.ListOrder |
|
|
}
|
|
|
return resultMaps, column, nil
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author SteveChan
|
|
|
* @Description // 批量导入创建订单
|
|
|
* @Date 11:00 2021/1/7
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func (service OrderInfoService) CreateNewOrderByImport(createOrderCommands []*command.CreateOrderCommand) ([]interface{}, error) {
|
|
|
// 事务初始化
|
|
|
var (
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
err error
|
|
|
failureDataList []interface{} // 错误数据返回
|
|
|
)
|
|
|
|
|
|
// 循环校验命令
|
|
|
for _, cmd := range createOrderCommands {
|
|
|
if err = cmd.Valid(); err != nil {
|
|
|
// 返回信息 0: 订单号, 1: 发货单号, 2: 客户名称, 3: 订单区域, 4: 编号, 5: 合伙人, 6: 类型, 7: 业务抽成比例, 8: 产品名称, 9: 数量, 10: 单价, 11: 合伙人分红比例
|
|
|
row := []interface{}{
|
|
|
lib.ThrowError(lib.BUSINESS_ERROR, err.Error()), // 错误信息
|
|
|
cmd.LineNumbers, // 错误影响的行
|
|
|
}
|
|
|
failureDataList = append(failureDataList, row)
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 开始事务
|
|
|
if err = transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
// 仓储、数据访问对象初始化
|
|
|
var (
|
|
|
orderBaseRepository domain.OrderBaseRepository
|
|
|
orderGoodRepository domain.OrderGoodRepository
|
|
|
PartnerInfoRepository domain.PartnerInfoRepository
|
|
|
categoryRepository domain.PartnerCategoryRepository
|
|
|
orderBaseDao *dao.OrderBaseDao
|
|
|
)
|
|
|
// 合伙人信息仓储初始化
|
|
|
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 订单仓储初始化
|
|
|
if orderBaseRepository, err = factory.CreateOrderBaseRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 订单产品仓储初始化
|
|
|
if orderGoodRepository, err = factory.CreateOrderGoodRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 合伙人类型仓储初始化
|
|
|
if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 订单数据访问对象初始化
|
|
|
if orderBaseDao, err = factory.CreateOrderBaseDao(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 批量创建订单
|
|
|
for _, cmd := range createOrderCommands {
|
|
|
// 批量校验合伙人信息
|
|
|
var partnerData *domain.PartnerInfo
|
|
|
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: cmd.PartnerId})
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("检索合伙人数据失败"))
|
|
|
}
|
|
|
|
|
|
// 批量校验订单
|
|
|
if ok, err := orderBaseDao.CheckOrderExist(cmd.CompanyId, cmd.OrderCode, cmd.DeliveryCode,
|
|
|
cmd.PartnerCategory, cmd.PartnerId, 0); err != nil {
|
|
|
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
|
|
|
} else if ok {
|
|
|
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单已存在")
|
|
|
}
|
|
|
|
|
|
// 批量校验产品
|
|
|
var goodMap = map[string]int{}
|
|
|
for i := range cmd.Goods {
|
|
|
goodName := cmd.Goods[i].GoodName
|
|
|
if _, ok := goodMap[goodName]; ok {
|
|
|
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "订单中货品重复已存在")
|
|
|
}
|
|
|
goodMap[goodName] = 1
|
|
|
}
|
|
|
|
|
|
newOrder := &domain.OrderBase{
|
|
|
OrderType: cmd.OrderType, OrderCode: cmd.OrderCode,
|
|
|
DeliveryCode: cmd.DeliveryCode,
|
|
|
Buyer: domain.Buyer{
|
|
|
BuyerName: cmd.BuyerName,
|
|
|
},
|
|
|
RegionInfo: domain.RegionInfo{
|
|
|
RegionName: cmd.OrderRegion,
|
|
|
},
|
|
|
PartnerId: cmd.PartnerId,
|
|
|
PartnerInfo: partnerData.Partner,
|
|
|
SalesmanBonusPercent: cmd.SalesmanBonusPercent,
|
|
|
CompanyId: cmd.CompanyId,
|
|
|
}
|
|
|
|
|
|
// 批量校验合伙人分类数据
|
|
|
var cmdPartnerCategoryOk bool
|
|
|
for _, v := range partnerData.PartnerCategoryInfos {
|
|
|
if v.Id == cmd.PartnerCategory {
|
|
|
_, categories, err := categoryRepository.Find(domain.PartnerCategoryFindQuery{
|
|
|
Ids: []int64{v.Id},
|
|
|
})
|
|
|
if err != nil {
|
|
|
e := fmt.Sprintf("获取合伙人分类数据失败:%s", err)
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
|
|
|
}
|
|
|
if len(categories) > 0 {
|
|
|
newOrder.PartnerCategory = categories[0]
|
|
|
cmdPartnerCategoryOk = true
|
|
|
}
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
if !cmdPartnerCategoryOk {
|
|
|
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "合伙人类型选择错误")
|
|
|
}
|
|
|
|
|
|
// 订单产品分红核算
|
|
|
var orderGoods []domain.OrderGood
|
|
|
for _, good := range cmd.Goods {
|
|
|
m := domain.NewOrderGood()
|
|
|
m.OrderId = 0
|
|
|
m.GoodName = good.GoodName
|
|
|
m.PlanGoodNumber = good.PlanGoodNumber
|
|
|
m.Price = good.Price
|
|
|
m.PartnerBonusPercent = good.PartnerBonusPercent
|
|
|
m.Remark = good.Remark
|
|
|
m.CompanyId = cmd.CompanyId
|
|
|
err = m.Compute()
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的数值失败:%s", err))
|
|
|
}
|
|
|
err = m.CurrentBonusStatus.WartPayPartnerBonus(&m)
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中商品的分红数值失败:%s", err))
|
|
|
}
|
|
|
orderGoods = append(orderGoods, m)
|
|
|
}
|
|
|
|
|
|
newOrder.Goods = orderGoods
|
|
|
|
|
|
err = newOrder.Compute()
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("核算订单中合计的数值失败:%s", err))
|
|
|
}
|
|
|
|
|
|
// 保存订单数据
|
|
|
err = orderBaseRepository.Save(newOrder)
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单数据失败:%s", err))
|
|
|
}
|
|
|
|
|
|
for i := range newOrder.Goods {
|
|
|
newOrder.Goods[i].OrderId = newOrder.Id
|
|
|
}
|
|
|
|
|
|
// 保存订单产品
|
|
|
err = orderGoodRepository.Save(orderGoods)
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("保存订单中的商品数据失败:%s", err))
|
|
|
}
|
|
|
|
|
|
newOrder.Goods = orderGoods
|
|
|
}
|
|
|
|
|
|
if len(failureDataList) == 0 {
|
|
|
// 完成事务
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
return failureDataList, nil
|
|
|
}
|
|
|
|
|
|
return failureDataList, nil
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @Author SteveChan
|
|
|
* @Description // 根据合伙人编号和合伙人类型获取合伙人id
|
|
|
* @Date 23:15 2021/1/6
|
|
|
* @Param
|
|
|
* @return
|
|
|
**/
|
|
|
func (service OrderInfoService) GetPartnerIdByCodeAndCategory(getPartnerIdQuery query.GetPartnerIdQuery) (*domain.PartnerInfo, error) {
|
|
|
// 事务初始化
|
|
|
var (
|
|
|
transactionContext, _ = factory.CreateTransactionContext(nil)
|
|
|
err error
|
|
|
partnerData *domain.PartnerInfo
|
|
|
)
|
|
|
|
|
|
// 开始事务
|
|
|
if err = transactionContext.StartTransaction(); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
// 收尾
|
|
|
defer func() {
|
|
|
transactionContext.RollbackTransaction()
|
|
|
}()
|
|
|
|
|
|
// 仓储、数据访问对象初始化
|
|
|
var (
|
|
|
PartnerInfoRepository domain.PartnerInfoRepository
|
|
|
)
|
|
|
// 合伙人信息仓储初始化
|
|
|
if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
|
|
|
"transactionContext": transactionContext,
|
|
|
}); err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
|
|
|
//var partnerData *domain.PartnerInfo
|
|
|
partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
|
|
|
CompanyId: getPartnerIdQuery.CompanyId,
|
|
|
Code: getPartnerIdQuery.Code,
|
|
|
PartnerCategory: getPartnerIdQuery.PartnerCategory,
|
|
|
})
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, fmt.Sprintf("检索合伙人数据失败"))
|
|
|
}
|
|
|
|
|
|
// 完成事务
|
|
|
err = transactionContext.CommitTransaction()
|
|
|
if err != nil {
|
|
|
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
|
|
|
}
|
|
|
return partnerData, nil
|
|
|
} |
...
|
...
|
|