作者 陈志颖

refactor:优化订单批量删除

... ... @@ -1275,7 +1275,7 @@ func (cooperationContractService *CooperationContractService) UpdateCooperationC
for _, rule := range cooperationContractFound.DividendsIncentivesRules {
jsons, errs := json.Marshal(rule)
if errs != nil {
fmt.Println(errs.Error())
(errs.Error())
}
cooperationContractFoundBytes = append(cooperationContractFoundBytes, jsons...)
}
... ... @@ -1288,7 +1288,7 @@ func (cooperationContractService *CooperationContractService) UpdateCooperationC
for _, rule := range cooperationContract.DividendsIncentivesRules {
jsons, errs := json.Marshal(rule)
if errs != nil {
fmt.Println(errs.Error())
(errs.Error())
}
cooperationContractBytes = append(cooperationContractBytes, jsons...)
}
... ... @@ -1331,7 +1331,7 @@ func (cooperationContractService *CooperationContractService) UpdateCooperationC
for _, rule := range cooperationContractFound.MoneyIncentivesRules {
jsons, errs := json.Marshal(rule)
if errs != nil {
fmt.Println(errs.Error())
(errs.Error())
}
cooperationContractFoundBytes = append(cooperationContractFoundBytes, jsons...)
}
... ... @@ -1344,7 +1344,7 @@ func (cooperationContractService *CooperationContractService) UpdateCooperationC
for _, rule := range cooperationContract.MoneyIncentivesRules {
jsons, errs := json.Marshal(rule)
if errs != nil {
fmt.Println(errs.Error())
(errs.Error())
}
cooperationContractBytes = append(cooperationContractBytes, jsons...)
}
... ... @@ -1388,7 +1388,7 @@ func (cooperationContractService *CooperationContractService) UpdateCooperationC
for _, undertaker := range cooperationContractFound.Undertakers {
jsons, errs := json.Marshal(undertaker)
if errs != nil {
fmt.Println(errs.Error())
(errs.Error())
}
cooperationContractFoundBytes = append(cooperationContractFoundBytes, jsons...)
}
... ... @@ -1400,7 +1400,7 @@ func (cooperationContractService *CooperationContractService) UpdateCooperationC
for _, undertaker := range cooperationContract.Undertakers {
jsons, errs := json.Marshal(undertaker) //转换成JSON返回的是byte[]
if errs != nil {
fmt.Println(errs.Error())
(errs.Error())
}
cooperationContractBytes = append(cooperationContractBytes, jsons...)
}
... ...
... ... @@ -26,9 +26,9 @@ func (batchRemoveCooperationModeCommand *BatchRemoveCooperationModeCommand) Vali
for _, cooperationModeId := range batchRemoveCooperationModeCommand.CooperationModeIds {
switch v := reflect.ValueOf(cooperationModeId); v.Kind() {
case reflect.String:
fmt.Println(v.String())
(v.String())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fmt.Println(v.Int())
(v.Int())
_ = validation.SetError("CustomValid", "共创模式ID类型错误")
default:
fmt.Printf("unhandled kind %s", v.Kind())
... ...
... ... @@ -405,8 +405,8 @@ func (dividendsOrderService *DividendsOrderService) ImportDividendsOrder(importD
t := reflect.TypeOf(dividendsOrder)
v := reflect.ValueOf(dividendsOrder)
for k := 0; k < t.NumField(); k++ {
//fmt.Println("name:", fmt.Sprintf("%+v", t.Field(k).Name), ", value:", fmt.Sprintf("%v", v.Field(k).Interface()), ", yaml:", t.Field(k).Tag.Get("yaml"))
//fmt.Println("name:", fmt.Sprintf("%+v", t.Field(k).Name), ", value:", fmt.Sprintf("%v", v.Field(k).Interface()))
//("name:", fmt.Sprintf("%+v", t.Field(k).Name), ", value:", fmt.Sprintf("%v", v.Field(k).Interface()), ", yaml:", t.Field(k).Tag.Get("yaml"))
//("name:", fmt.Sprintf("%+v", t.Field(k).Name), ", value:", fmt.Sprintf("%v", v.Field(k).Interface()))
if t.Field(k).Name != "Expense" {
if v.Field(k).Interface() == "" {
col := strconv.Itoa(k + 1)
... ... @@ -1151,14 +1151,14 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba
}
// 分红退货单仓储初始化
var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository
if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
dividendsReturnedOrderRepository = value
}
//var dividendsReturnedOrderRepository domain.DividendsReturnedOrderRepository
//if value, err := factory.CreateDividendsReturnedOrderRepository(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//} else {
// dividendsReturnedOrderRepository = value
//}
// 分红预算单仓储初始化
//var dividendsEstimateRepository domain.DividendsEstimateRepository
... ... @@ -1181,14 +1181,14 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba
//}
// 分红退货单DAO初始化
//var dividendsReturnedOrderDao *dao.DividendsReturnedOrderDao
//if value, err := factory.CreateDividendsReturnedOrderDao(map[string]interface{}{
// "transactionContext": transactionContext,
//}); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//} else {
// dividendsReturnedOrderDao = value
//}
var dividendsReturnedOrderDao *dao.DividendsReturnedOrderDao
if value, err := factory.CreateDividendsReturnedOrderDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
dividendsReturnedOrderDao = value
}
dividendsOrderIds, err := utils.SliceAtoi(batchRemoveDividendsOrderCommand.DividendsOrderIds)
if err != nil {
... ... @@ -1205,34 +1205,34 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba
if count > 0 {
for _, dividendsOrder := range dividendsOrders {
// 校验分红订单是否有关联的退货单
//startCount := time.Now()
//if countReturnedOrder, _, err := dividendsReturnedOrderRepository.Find(map[string]interface{}{
// "dividendsOrderNumber": dividendsOrder.DividendsOrderNumber,
// "companyId": dividendsOrder.Company.CompanyId,
// "limit": 1,
// "offset": 0,
//}); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//} else {
// if countReturnedOrder > 0 {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("分红订单%s有关联的退货单,不可删除", dividendsOrder.DividendsOrderNumber))
// }
//}
//("退货单计数:", time.Since(startCount))
startCount := time.Now()
if countReturnedOrder, _, err := dividendsReturnedOrderRepository.Find(map[string]interface{}{
if returnedOrderExist, err := dividendsReturnedOrderDao.CheckDividendsReturnedOrderExist(map[string]interface{}{
"dividendsOrderNumber": dividendsOrder.DividendsOrderNumber,
"companyId": dividendsOrder.Company.CompanyId,
"limit": 1,
"offset": 0,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
if countReturnedOrder > 0 {
if returnedOrderExist {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("分红订单%s有关联的退货单,不可删除", dividendsOrder.DividendsOrderNumber))
}
}
fmt.Println("退货单计数:", time.Since(startCount))
//startCount := time.Now()
//if returnedOrderExist, err := dividendsReturnedOrderDao.CheckDividendsReturnedOrderExist(map[string]interface{}{
// "dividendsOrderNumber": dividendsOrder.DividendsOrderNumber,
// "companyId": dividendsOrder.Company.CompanyId,
//}); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//} else {
// if returnedOrderExist {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("分红订单%s有关联的退货单,不可删除", dividendsOrder.DividendsOrderNumber))
// }
//}
//fmt.Println("退货单计数:", time.Since(startCount))
// 校验分红订单是否有分红预算
//startEstimateCount := time.Now()
//if countRelative, _, err2 := dividendsEstimateRepository.Find(map[string]interface{}{
... ... @@ -1249,7 +1249,7 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, fmt.Sprintf("分红订单%s有关联的预算单,不可删除", dividendsOrder.DividendsOrderNumber))
// }
//}
//fmt.Println("预算单计数:", time.Since(startEstimateCount))
//("预算单计数:", time.Since(startEstimateCount))
//startEstimateCount := time.Now()
//if estimateOrderExist, err3 := dividendsEstimateDao.CheckDividendsEstimateOrderExist(map[string]interface{}{
... ... @@ -1276,6 +1276,7 @@ func (dividendsOrderService *DividendsOrderService) BatchRemoveDividendsOrder(ba
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
fmt.Println("截止目前耗时3:", time.Since(start))
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
... ...
... ... @@ -445,7 +445,7 @@ func (dividendsReturnedOrderService *DividendsReturnedOrderService) ImportDivide
t := reflect.TypeOf(dividendsReturnedOrder)
v := reflect.ValueOf(dividendsReturnedOrder)
for k := 0; k < t.NumField(); k++ {
//fmt.Println("name:", fmt.Sprintf("%+v", t.Field(k).Name), ", value:", fmt.Sprintf("%v", v.Field(k).Interface()))
//("name:", fmt.Sprintf("%+v", t.Field(k).Name), ", value:", fmt.Sprintf("%v", v.Field(k).Interface()))
//if t.Field(k).Name != "RegionName" {
if v.Field(k).Interface() == "" {
col := strconv.Itoa(k + 1)
... ...
... ... @@ -6,7 +6,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/infrastructure/pg/models"
)
func TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModel *models.DividendsOrder, goods []*models.OrderGood) (*domain.DividendsOrder, error) {
func TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModels []*models.DividendsOrder, goods []*models.OrderGood) ([]*domain.DividendsOrder, error) {
var orderGoods []*domain.OrderGood
for _, good := range goods {
orderGoodAmount, _ := decimal.NewFromFloatWithExponent(good.OrderGoodAmount, -2).Float64()
... ... @@ -26,25 +26,59 @@ func TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModel *model
UpdatedAt: good.UpdatedAt,
})
}
dividendsOrderAmount, _ := decimal.NewFromFloatWithExponent(dividendsOrderModel.DividendsOrderAmount, -2).Float64()
return &domain.DividendsOrder{
DividendsOrderId: dividendsOrderModel.DividendsOrderId,
DividendsOrderNumber: dividendsOrderModel.DividendsOrderNumber,
DividendsOriginalOrderNum: dividendsOrderModel.DividendsOriginalOrderNum,
DividendsOrderAmount: dividendsOrderAmount,
OrderTime: dividendsOrderModel.OrderTime,
DividendTime: dividendsOrderModel.DividendTime,
DividendStatus: dividendsOrderModel.DividendStatus,
Region: dividendsOrderModel.Region,
CustomerName: dividendsOrderModel.CustomerName,
Org: dividendsOrderModel.Org,
Goods: orderGoods,
Company: dividendsOrderModel.Company,
CreatedAt: dividendsOrderModel.CreatedAt,
DeletedAt: dividendsOrderModel.DeletedAt,
UpdatedAt: dividendsOrderModel.UpdatedAt,
OperateTime: dividendsOrderModel.OperateTime,
Operator: dividendsOrderModel.Operator,
Remarks: dividendsOrderModel.Remarks,
}, nil
var dividendsOrders []*domain.DividendsOrder
for _, dividendsOrderModel := range dividendsOrderModels {
dividendsOrderAmount, _ := decimal.NewFromFloatWithExponent(dividendsOrderModel.DividendsOrderAmount, -2).Float64()
dividendsOrder := &domain.DividendsOrder{
DividendsOrderId: dividendsOrderModel.DividendsOrderId,
DividendsOrderNumber: dividendsOrderModel.DividendsOrderNumber,
DividendsOriginalOrderNum: dividendsOrderModel.DividendsOriginalOrderNum,
DividendsOrderAmount: dividendsOrderAmount,
OrderTime: dividendsOrderModel.OrderTime,
DividendTime: dividendsOrderModel.DividendTime,
DividendStatus: dividendsOrderModel.DividendStatus,
Region: dividendsOrderModel.Region,
CustomerName: dividendsOrderModel.CustomerName,
Org: dividendsOrderModel.Org,
Goods: make([]*domain.OrderGood, 0),
Company: dividendsOrderModel.Company,
CreatedAt: dividendsOrderModel.CreatedAt,
DeletedAt: dividendsOrderModel.DeletedAt,
UpdatedAt: dividendsOrderModel.UpdatedAt,
OperateTime: dividendsOrderModel.OperateTime,
Operator: dividendsOrderModel.Operator,
Remarks: dividendsOrderModel.Remarks,
}
for _, orderGood := range orderGoods {
if orderGood.CompanyId == dividendsOrder.Company.CompanyId && orderGood.DividendsOrderNumber == dividendsOrder.DividendsOrderNumber {
dividendsOrder.Goods = append(dividendsOrder.Goods, orderGood)
}
}
dividendsOrders = append(dividendsOrders, dividendsOrder)
}
return dividendsOrders, nil
//return &domain.DividendsOrder{
// DividendsOrderId: dividendsOrderModel.DividendsOrderId,
// DividendsOrderNumber: dividendsOrderModel.DividendsOrderNumber,
// DividendsOriginalOrderNum: dividendsOrderModel.DividendsOriginalOrderNum,
// DividendsOrderAmount: dividendsOrderAmount,
// OrderTime: dividendsOrderModel.OrderTime,
// DividendTime: dividendsOrderModel.DividendTime,
// DividendStatus: dividendsOrderModel.DividendStatus,
// Region: dividendsOrderModel.Region,
// CustomerName: dividendsOrderModel.CustomerName,
// Org: dividendsOrderModel.Org,
// Goods: orderGoods,
// Company: dividendsOrderModel.Company,
// CreatedAt: dividendsOrderModel.CreatedAt,
// DeletedAt: dividendsOrderModel.DeletedAt,
// UpdatedAt: dividendsOrderModel.UpdatedAt,
// OperateTime: dividendsOrderModel.OperateTime,
// Operator: dividendsOrderModel.Operator,
// Remarks: dividendsOrderModel.Remarks,
//}, nil
}
... ...
... ... @@ -402,10 +402,10 @@ func (repository *DividendsOrderRepository) SaveMany(dividendsOrders []*domain.D
return nil, fmt.Errorf("分红订单关联的产品不存在")
}
// 聚合分红订单
if dividendsOrder, err := transform.TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModel, orderGoodModels); err != nil {
if dividendsOrder, err := transform.TransformToDividendsOrderDomainModelFromPgModels([]*models.DividendsOrder{dividendsOrderModel}, orderGoodModels); err != nil {
return dividendsOrders, err
} else {
dividendsOrders = append(dividendsOrders, dividendsOrder)
dividendsOrders = append(dividendsOrders, dividendsOrder...)
}
}
return dividendsOrdersSaved, nil
... ... @@ -488,21 +488,23 @@ func (repository *DividendsOrderRepository) BatchRemove(dividendsOrders []*domai
if _, err := tx.Model(&dividendsOrderModels).WherePK().Delete(); err != nil {
return dividendsOrders, err
} else {
var dividendsOrderNumbers []string
for _, dividendsOrder := range dividendsOrders {
// 删除订单产品
var orderGoodModels []*models.OrderGood
orderGoodQuery := tx.Model(&orderGoodModels)
if err := orderGoodQuery.
Where("company_id = ?", dividendsOrder.Company.CompanyId).
Where("org_id = ?", dividendsOrder.Org.OrgId).
Where("dividends_order_number = ?", dividendsOrder.DividendsOrderNumber).
Select(); err != nil {
return nil, err
} else {
if len(orderGoodModels) > 0 {
if _, err := tx.Model(&orderGoodModels).WherePK().Delete(); err != nil {
return nil, err
}
dividendsOrderNumbers = append(dividendsOrderNumbers, dividendsOrder.DividendsOrderNumber)
}
// 删除订单产品
var orderGoodModels []*models.OrderGood
orderGoodQuery := tx.Model(&orderGoodModels)
if err := orderGoodQuery.
Where("company_id = ?", dividendsOrders[0].Company.CompanyId).
Where("org_id = ?", dividendsOrders[0].Org.OrgId).
Where("dividends_order_number in (?)", dividendsOrderNumbers).
Select(); err != nil {
return nil, err
} else {
if len(orderGoodModels) > 0 {
if _, err := tx.Model(&orderGoodModels).WherePK().Delete(); err != nil {
return nil, err
}
}
}
... ... @@ -548,7 +550,11 @@ func (repository *DividendsOrderRepository) FindOne(queryOptions map[string]inte
return nil, fmt.Errorf("分红订单关联的产品不存在")
}
// 聚合分红订单
return transform.TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModel, orderGoodModels)
result, err := transform.TransformToDividendsOrderDomainModelFromPgModels([]*models.DividendsOrder{dividendsOrderModel}, orderGoodModels)
if err != nil {
return nil, fmt.Errorf("分红订单不存在")
}
return result[0], nil
}
}
... ... @@ -591,26 +597,28 @@ func (repository *DividendsOrderRepository) Find(queryOptions map[string]interfa
return 0, dividendsOrders, err
} else {
if count > 0 {
var dividendsOrderNumbers []string
for _, dividendsOrderModel := range dividendsOrderModels {
//获取订单产品
var orderGoodModels []*models.OrderGood
orderGoodModelQuery := tx.Model(&orderGoodModels)
if err := orderGoodModelQuery.
Where("company_id = ?", dividendsOrderModel.Company.CompanyId).
Where("org_id = ?", dividendsOrderModel.Org.OrgId).
Where("dividends_order_number = ?", dividendsOrderModel.DividendsOrderNumber).
Select(); err != nil {
log.Logger.Error("分红订单关联的产品不存在", map[string]interface{}{
"dividendsOrderModel": dividendsOrderModel,
})
return 0, nil, fmt.Errorf("分红订单关联的产品不存在")
}
// 聚合分红订单
if dividendsOrder, err := transform.TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModel, orderGoodModels); err != nil {
return 0, dividendsOrders, err
} else {
dividendsOrders = append(dividendsOrders, dividendsOrder)
}
dividendsOrderNumbers = append(dividendsOrderNumbers, dividendsOrderModel.DividendsOrderNumber)
}
//获取订单产品
var orderGoodModels []*models.OrderGood
orderGoodModelQuery := tx.Model(&orderGoodModels)
if err := orderGoodModelQuery.
Where("company_id = ?", dividendsOrderModels[0].Company.CompanyId).
//Where("org_id = ?", dividendsOrderModel.Org.OrgId).
Where("dividends_order_number in (?)", dividendsOrderNumbers).
Select(); err != nil {
//log.Logger.Error("分红订单关联的产品不存在", map[string]interface{}{
// "dividendsOrderModel": dividendsOrderModel,
//})
return 0, nil, fmt.Errorf("分红订单关联的产品不存在")
}
// 聚合分红订单
if dividendsOrder, err := transform.TransformToDividendsOrderDomainModelFromPgModels(dividendsOrderModels, orderGoodModels); err != nil {
return 0, dividendsOrders, err
} else {
dividendsOrders = append(dividendsOrders, dividendsOrder...)
}
}
return int64(count), dividendsOrders, nil
... ...
package utils
import (
"fmt"
"strconv"
"time"
)
... ... @@ -48,6 +47,6 @@ func TransformTimestampToTime(timeStamp int64) time.Time {
ipart, _ := strconv.ParseInt(tIpartStr, 10, 64)
//decpart, _ := strconv.ParseInt(tDecpartStr, 10, 64)
myTime := time.Unix(ipart, 0)
fmt.Println(myTime)
//(myTime)
return time.Date(myTime.Year(), myTime.Month(), myTime.Day(), myTime.Hour(), myTime.Minute(), myTime.Second(), 0, time.Local)
}
... ...
... ... @@ -99,19 +99,19 @@ func NewBeegoLog(conf LoggerConfig) *BeegoLog {
confByte, _ := json.Marshal(conf)
err := logger.SetLogger(logs.AdapterFile, string(confByte))
if err != nil {
fmt.Println(err.Error())
(err.Error())
return nil
}
} else if constant.LOG_TYPE == "es" { // 日志存储到ES
err := logger.SetLogger(logs.AdapterEs, `{"dsn":"http://192.168.100.222:9200/","level":7}`)
if err != nil {
fmt.Println(err.Error())
(err.Error())
return nil
}
} else { // 日志输出到控制台
err := logger.SetLogger(logs.AdapterConsole, `{"level":7,"color":true}`)
if err != nil {
fmt.Println(err.Error())
(err.Error())
return nil
}
}
... ...