221 lines
6.8 KiB
Go
221 lines
6.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"micro-bundle/internal/model"
|
|
"micro-bundle/pb/bundle"
|
|
"micro-bundle/pkg/app"
|
|
commonErr "micro-bundle/pkg/err"
|
|
"micro-bundle/pkg/msg"
|
|
"micro-bundle/pkg/utils"
|
|
)
|
|
|
|
func CreateOrderRecord(orderRecord *model.BundleOrderRecords) (res *bundle.CommonResponse, err error) {
|
|
res = new(bundle.CommonResponse)
|
|
orderRecord.UUID = app.ModuleClients.SfNode.Generate().Base64()
|
|
orderRecord.OrderNo = utils.GetOrderNo()
|
|
|
|
// 开启事务
|
|
tx := app.ModuleClients.BundleDB.Begin()
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
tx.Rollback()
|
|
}
|
|
}()
|
|
|
|
// 创建主订单
|
|
if err = tx.Model(&model.BundleOrderRecords{}).Create(&orderRecord).Error; err != nil {
|
|
tx.Rollback()
|
|
res.Msg = msg.ErrorCreateOrderInfo
|
|
return res, commonErr.ReturnError(err, msg.ErrorCreateOrderInfo, "创建订单信息失败: ")
|
|
}
|
|
|
|
// 提交事务
|
|
if err = tx.Commit().Error; err != nil {
|
|
res.Msg = msg.ErrorCommitTransaction
|
|
return res, commonErr.ReturnError(err, msg.ErrorCommitTransaction, "提交事务失败: ")
|
|
}
|
|
|
|
res.Uuid = orderRecord.UUID
|
|
res.OrderNo = orderRecord.OrderNo
|
|
res.Msg = msg.SuccessCreateOrderInfo
|
|
return
|
|
}
|
|
|
|
func UpdateOrderRecord(orderRecord *model.BundleOrderRecords) (res *bundle.CommonResponse, err error) {
|
|
res = new(bundle.CommonResponse)
|
|
err = app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{}).Where("uuid = ?", orderRecord.UUID).Updates(orderRecord).Error
|
|
if err != nil {
|
|
res.Msg = msg.ErrorUpdateOrderInfo
|
|
return res, commonErr.ReturnError(err, msg.ErrorUpdateOrderInfo, "更新订单信息失败: ")
|
|
}
|
|
res.Uuid = orderRecord.UUID
|
|
res.Msg = msg.SuccessUpdateOrderInfo
|
|
return
|
|
}
|
|
|
|
func UpdateOrderRecordByOrderNO(orderRecord *model.BundleOrderRecords) (res *bundle.CommonResponse, err error) {
|
|
res = new(bundle.CommonResponse)
|
|
err = app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{}).Where("order_no = ?", orderRecord.OrderNo).Updates(orderRecord).Error
|
|
if err != nil {
|
|
res.Msg = msg.ErrorUpdateOrderInfo
|
|
return res, commonErr.ReturnError(err, msg.ErrorUpdateOrderInfo, "更新订单信息失败: ")
|
|
}
|
|
res.Uuid = orderRecord.UUID
|
|
res.Msg = msg.SuccessUpdateOrderInfo
|
|
return
|
|
}
|
|
|
|
func OrderRecordsList(req *bundle.OrderRecordsRequest) (res *bundle.OrderRecordsResponse, err error) {
|
|
res = new(bundle.OrderRecordsResponse)
|
|
res.OrderRecords = make([]*bundle.OrderRecord, 0)
|
|
records := make([]*model.BundleOrderRecords, 0)
|
|
|
|
query := app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{})
|
|
|
|
if req.CustomerID != "" {
|
|
query = query.Where("customer_id = ?", req.CustomerID)
|
|
}
|
|
|
|
if req.CustomerNum != "" {
|
|
query = query.Where("customer_num like ?", "%"+req.CustomerNum+"%")
|
|
}
|
|
|
|
if req.CustomerName != "" {
|
|
query = query.Where("customer_name like ?", "%"+req.CustomerName+"%")
|
|
}
|
|
|
|
if req.BundleName != "" {
|
|
query = query.Where("bundle_name like ?", "%"+req.BundleName+"%")
|
|
}
|
|
|
|
if req.BundleUUID != "" {
|
|
query = query.Where("bundle_uuid = ?", req.BundleUUID)
|
|
}
|
|
|
|
if req.OrderNo != "" {
|
|
query = query.Where("order_no like ?", "%"+req.OrderNo+"%")
|
|
}
|
|
|
|
if req.Status != 0 {
|
|
query = query.Where("status = ?", req.Status)
|
|
}
|
|
|
|
if req.StartSignedTime != "" {
|
|
query = query.Where("signed_time >= ?", req.StartSignedTime)
|
|
}
|
|
|
|
if req.EndSignedTime != "" {
|
|
query = query.Where("signed_time <= ?", req.EndSignedTime)
|
|
}
|
|
|
|
if req.StartPayTime != "" {
|
|
query = query.Where("pay_time >= ?", req.StartPayTime)
|
|
}
|
|
|
|
if req.EndPayTime != "" {
|
|
query = query.Where("pay_time <= ?", req.EndPayTime)
|
|
}
|
|
|
|
if req.IsHaveValueAdd == "有" {
|
|
query = query.Where("is_have_value_add=1")
|
|
} else {
|
|
query = query.Where("is_have_value_add=0")
|
|
}
|
|
count := *query
|
|
|
|
if req.PageSize != 0 && req.Page != 0 {
|
|
query = query.Limit(int(req.PageSize)).Offset(int(req.Page-1) * int(req.PageSize))
|
|
}
|
|
|
|
err = query.Find(&records).Error
|
|
if err != nil {
|
|
return res, commonErr.ReturnError(err, msg.ErrorGetOrderList, "获取订单信息失败: ")
|
|
}
|
|
|
|
//_ = copier.CopyWithOption(&res.OrderRecords, records, copier.Option{DeepCopy: true})
|
|
|
|
for _, record := range records {
|
|
res.OrderRecords = append(res.OrderRecords, &bundle.OrderRecord{
|
|
Uuid: record.UUID,
|
|
OrderNo: record.OrderNo,
|
|
BundleUuid: record.BundleUUID,
|
|
BundleName: record.BundleName,
|
|
CustomerID: record.CustomerID,
|
|
CustomerNum: record.CustomerNum,
|
|
CustomerName: record.CustomerName,
|
|
Amount: record.Amount,
|
|
AmountType: record.AmountType,
|
|
SignContract: record.SignContract,
|
|
Signature: record.Signature,
|
|
SignedTime: record.SignedTime,
|
|
PayType: record.PayType,
|
|
PayTime: record.PayTime,
|
|
CheckoutSessionId: record.CheckoutSessionId,
|
|
CheckoutSessionUrl: record.CheckoutSessionUrl,
|
|
Status: record.Status,
|
|
ContractNo: record.ContractNo,
|
|
ValueAddBundleUuid: record.ValueAddBundleUUID,
|
|
ValueAddBundleAmount: record.ValueAddBundleAmount,
|
|
TotalAmount: record.TotalAmount,
|
|
})
|
|
}
|
|
|
|
var total int64
|
|
|
|
count.Count(&total)
|
|
|
|
res.Total = int32(total)
|
|
|
|
return
|
|
}
|
|
|
|
func OrderRecordDetail(req *bundle.OrderRecordsDetailRequest) (res *bundle.OrderRecord, err error) {
|
|
res = new(bundle.OrderRecord)
|
|
orderRecord := new(model.BundleOrderRecords)
|
|
|
|
query := app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{})
|
|
|
|
if req.Uuid != "" {
|
|
query = query.Where("uuid = ?", req.Uuid)
|
|
}
|
|
|
|
if req.OrderNo != "" {
|
|
query = query.Where("order_no = ?", req.OrderNo)
|
|
}
|
|
|
|
if req.CustomerID != "" {
|
|
query = query.Where("customer_id = ?", req.CustomerID)
|
|
}
|
|
|
|
err = query.First(&orderRecord).Error
|
|
if err != nil {
|
|
return res, commonErr.ReturnError(err, msg.ErrorGetOrderInfo, "获取订单信息失败: ")
|
|
}
|
|
//_ = copier.CopyWithOption(&res, orderRecord, copier.Option{DeepCopy: true})
|
|
|
|
res = &bundle.OrderRecord{
|
|
Uuid: orderRecord.UUID,
|
|
OrderNo: orderRecord.OrderNo,
|
|
BundleUuid: orderRecord.BundleUUID,
|
|
BundleName: orderRecord.BundleName,
|
|
CustomerID: orderRecord.CustomerID,
|
|
CustomerNum: orderRecord.CustomerNum,
|
|
CustomerName: orderRecord.CustomerName,
|
|
Amount: orderRecord.Amount,
|
|
AmountType: orderRecord.AmountType,
|
|
SignContract: orderRecord.SignContract,
|
|
Signature: orderRecord.Signature,
|
|
SignedTime: orderRecord.SignedTime,
|
|
PayType: orderRecord.PayType,
|
|
PayTime: orderRecord.PayTime,
|
|
CheckoutSessionId: orderRecord.CheckoutSessionId,
|
|
CheckoutSessionUrl: orderRecord.CheckoutSessionUrl,
|
|
Status: orderRecord.Status,
|
|
ContractNo: orderRecord.ContractNo,
|
|
ValueAddBundleUuid: orderRecord.ValueAddBundleUUID,
|
|
ValueAddBundleAmount: orderRecord.ValueAddBundleAmount,
|
|
TotalAmount: orderRecord.TotalAmount,
|
|
}
|
|
return
|
|
}
|