micro-bundle/internal/dao/orderRecordsDao.go

229 lines
7.2 KiB
Go
Raw Permalink Normal View History

2025-02-20 12:40:39 +00:00
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"
)
2025-03-28 01:23:17 +00:00
func CreateOrderRecord(orderRecord *model.BundleOrderRecords) (res *bundle.CommonResponse, err error) {
2025-02-20 12:40:39 +00:00
res = new(bundle.CommonResponse)
orderRecord.UUID = app.ModuleClients.SfNode.Generate().Base64()
orderRecord.OrderNo = utils.GetOrderNo()
2025-03-25 08:30:09 +00:00
// 开启事务
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()
2025-02-20 12:40:39 +00:00
res.Msg = msg.ErrorCreateOrderInfo
return res, commonErr.ReturnError(err, msg.ErrorCreateOrderInfo, "创建订单信息失败: ")
}
2025-03-25 08:30:09 +00:00
// 提交事务
if err = tx.Commit().Error; err != nil {
res.Msg = msg.ErrorCommitTransaction
return res, commonErr.ReturnError(err, msg.ErrorCommitTransaction, "提交事务失败: ")
}
2025-02-20 12:40:39 +00:00
res.Uuid = orderRecord.UUID
2025-02-22 11:16:11 +00:00
res.OrderNo = orderRecord.OrderNo
2025-02-20 12:40:39 +00:00
res.Msg = msg.SuccessCreateOrderInfo
return
}
func UpdateOrderRecord(orderRecord *model.BundleOrderRecords) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
2025-02-22 11:40:30 +00:00
err = app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{}).Where("uuid = ?", orderRecord.UUID).Updates(orderRecord).Error
2025-02-20 12:40:39 +00:00
if err != nil {
res.Msg = msg.ErrorUpdateOrderInfo
return res, commonErr.ReturnError(err, msg.ErrorUpdateOrderInfo, "更新订单信息失败: ")
}
res.Uuid = orderRecord.UUID
res.Msg = msg.SuccessUpdateOrderInfo
return
}
2025-02-21 13:09:54 +00:00
func UpdateOrderRecordByOrderNO(orderRecord *model.BundleOrderRecords) (res *bundle.CommonResponse, err error) {
res = new(bundle.CommonResponse)
2025-02-22 11:40:30 +00:00
err = app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{}).Where("order_no = ?", orderRecord.OrderNo).Updates(orderRecord).Error
2025-02-21 13:09:54 +00:00
if err != nil {
res.Msg = msg.ErrorUpdateOrderInfo
return res, commonErr.ReturnError(err, msg.ErrorUpdateOrderInfo, "更新订单信息失败: ")
}
res.Uuid = orderRecord.UUID
res.Msg = msg.SuccessUpdateOrderInfo
return
}
2025-02-20 12:40:39 +00:00
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{})
2025-02-23 06:21:42 +00:00
if req.CustomerID != "" {
query = query.Where("customer_id = ?", req.CustomerID)
}
2025-02-20 12:40:39 +00:00
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)
}
2025-03-28 02:54:04 +00:00
if req.IsHaveValueAdd == 1 { // 有
query = query.Where("value_add_bundle_uuid != ?", "")
} else if req.IsHaveValueAdd == 2 { // 无
query = query.Where("IFNULL(value_add_bundle_uuid,'') = ''")
}
2025-02-22 12:12:34 +00:00
count := *query
2025-02-22 12:04:35 +00:00
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
2025-03-28 18:05:56 +00:00
2025-02-20 12:40:39 +00:00
if err != nil {
return res, commonErr.ReturnError(err, msg.ErrorGetOrderList, "获取订单信息失败: ")
}
2025-02-22 11:19:35 +00:00
//_ = copier.CopyWithOption(&res.OrderRecords, records, copier.Option{DeepCopy: true})
for _, record := range records {
res.OrderRecords = append(res.OrderRecords, &bundle.OrderRecord{
2025-03-28 18:05:56 +00:00
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,
ValueAddOriginalPrice: record.ValueAddOriginalPrice,
ValueAddDiscountPrice: record.ValueAddDiscountPrice,
ValueAddSavedAmount: record.ValueAddSavedAmount,
TotalAmount: record.TotalAmount,
Num: record.Num,
2025-03-28 22:00:18 +00:00
BundleCommonUid: record.BundleCommonUid,
AddBundleCommonUid: record.AddBundleCommonUid,
2025-02-22 11:19:35 +00:00
})
}
2025-02-20 12:40:39 +00:00
var total int64
count.Count(&total)
res.Total = int32(total)
return
}
2025-02-21 13:09:54 +00:00
func OrderRecordDetail(req *bundle.OrderRecordsDetailRequest) (res *bundle.OrderRecord, err error) {
2025-02-20 12:40:39 +00:00
res = new(bundle.OrderRecord)
orderRecord := new(model.BundleOrderRecords)
2025-02-21 13:09:54 +00:00
2025-02-22 11:40:30 +00:00
query := app.ModuleClients.BundleDB.Model(&model.BundleOrderRecords{})
2025-02-21 13:09:54 +00:00
if req.Uuid != "" {
query = query.Where("uuid = ?", req.Uuid)
}
if req.OrderNo != "" {
query = query.Where("order_no = ?", req.OrderNo)
}
2025-02-23 12:56:42 +00:00
if req.CustomerID != "" {
query = query.Where("customer_id = ?", req.CustomerID)
}
2025-02-21 13:09:54 +00:00
err = query.First(&orderRecord).Error
2025-02-20 12:40:39 +00:00
if err != nil {
return res, commonErr.ReturnError(err, msg.ErrorGetOrderInfo, "获取订单信息失败: ")
}
2025-02-23 03:51:24 +00:00
//_ = copier.CopyWithOption(&res, orderRecord, copier.Option{DeepCopy: true})
res = &bundle.OrderRecord{
2025-03-25 08:30:09 +00:00
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,
2025-03-28 01:23:17 +00:00
ValueAddBundleUuid: orderRecord.ValueAddBundleUUID,
2025-03-25 08:30:09 +00:00
ValueAddBundleAmount: orderRecord.ValueAddBundleAmount,
TotalAmount: orderRecord.TotalAmount,
2025-02-23 03:51:24 +00:00
}
2025-02-20 12:40:39 +00:00
return
}