package order
import (
"errors"
"fmt"
"path/filepath"
"recook/internal/libs/bean"
"recook/internal/static_path"
"recook/internal/v2/lib/common"
"recook/internal/v2/lib/excel"
"recook/internal/v2/model/gys/enterprise"
goods2 "recook/internal/v2/model/gys/goods"
"recook/internal/v2/model/recook/goods"
manage2 "recook/internal/v2/model/recook/manage"
manage "recook/internal/v2/model/recook/order"
"recook/tools"
"time"
"github.com/golangkit/formatime"
"github.com/jinzhu/gorm"
"github.com/shopspring/decimal"
)
var OrderLogic = & orderLogic { }
type orderLogic struct {
}
type OrderItem struct {
OrderId uint ` json:"orderId" `
OrderGoodsId uint ` json:"orderGoodsId" `
OrderTime int64 ` json:"orderTime" `
PayTime int64 ` json:"payTime" `
GoodsName string ` json:"goodsName" `
SkuName string ` json:"skuName" `
Quantity uint ` json:"quantity" `
BuyerMessage string ` json:"buyerMessage" `
StoreMessage string ` json:"storeMessage" `
UnitPrice decimal . Decimal ` json:"unitPrice" `
AssType uint ` json:"assType" `
AssName string ` json:"assName" `
RefundStatus uint ` json:"refundStatus" `
RefundName string ` json:"refundName" `
ExpressStatus uint ` json:"expressStatus" `
ExpressName string ` json:"expressName" `
ActualAmount decimal . Decimal ` json:"actualAmount" `
ExpressFee decimal . Decimal ` json:"expressFee" `
CoinAmount decimal . Decimal ` json:"coinAmount" `
UniverseCouponAmount decimal . Decimal ` json:"universeCouponAmount" `
OrderAddr OrderAddr ` json:"orderAddr" `
PayMethod uint ` json:"payMethod" `
PayMethodName string ` json:"payMethodName" `
}
type OrderAddr struct {
ReceiverName string ` json:"receiverName" `
Mobile string ` json:"mobile" `
Province string ` json:"province" `
City string ` json:"city" `
District string ` json:"district" `
Address string ` json:"address" `
}
type Where struct {
OrderGoodsId uint ` json:"orderGoodsId" form:"orderGoodsId" `
OrderId uint ` json:"orderId" form:"orderId" `
FirstCategoryId uint ` json:"firstCategoryId" form:"firstCategoryId" `
SecondCategoryId uint ` json:"secondCategoryId" form:"secondCategoryId" `
OrderStatus uint ` json:"orderStatus" form:"orderStatus" `
GoodsId uint ` json:"goodsId" form:"goodsId" `
ExpressStatus uint ` json:"expressStatus" form:"expressStatus" `
OrderTimeStart string ` json:"orderTimeStart" form:"orderTimeStart" `
OrderTimeEnd string ` json:"orderTimeEnd" form:"orderTimeEnd" `
PayMethod uint ` json:"payMethod" form:"payMethod" `
RefundType uint ` json:"refundType" form:"refundType" `
CompletedTimeStart string ` json:"completedTimeStart" form:"completedTimeStart" `
CompletedTimeEnd string ` json:"completedTimeEnd" form:"completedTimeEnd" `
ReceiverName string ` json:"receiverName" form:"receiverName" `
Mobile string ` json:"mobile" form:"mobile" `
ExpressNo string ` json:"expressNo" form:"expressNo" `
LableStatus uint ` json:"lableStatus" form:"lableStatus" `
GoodsName string ` json:"goodsName" form:"goodsName" `
VendorId uint ` json:"vendorId" form:"vendorId" `
GoodsSn string ` json:"goodsSn" form:"goodsSn" `
SkuCode string ` json:"skuCode" form:"skuCode" `
BrandID uint ` json:"brandID" `
TimeOut uint ` json:"time_out" `
OrderFiling uint ` json:"order_filing" `
IsSale bool ` json:"is_sale" `
}
func ( o * orderLogic ) Lists ( where Where , page bean . Page ) ( list [ ] ListItem , total int ) {
list , total = o . Data ( where , & page )
return
}
// @Style 所有数据
func ( o * orderLogic ) All ( where Where ) ( list [ ] ListItem ) {
list , _ = o . Data ( where , nil )
return
}
type ListStruct struct {
manage . RecookOrderGoodsDetailModel
Province string ` gorm:"column:province" json:"province" `
City string ` gorm:"column:city" json:"city" `
District string ` gorm:"column:district" json:"district" `
Address string ` gorm:"column:address" json:"address" `
ReceiverName string ` gorm:"column:receiver_name" json:"receiverName" `
Mobile string ` gorm:"column:mobile" json:"mobile" `
PayTime formatime . Second ` gorm:"column:pay_time" json:"payTime" `
PayMethod uint ` gorm:"column:pay_method" json:"payMethod" `
GoodsSn string ` json:"goodsSn" `
EnterPriseName string ` json:"enter_prise_name" `
CanPay bool ` json:"can_pay" gorm:"column:can_pay" `
}
type ListItem struct {
Id uint ` json:"id" `
OrderId uint ` json:"orderId" `
GoodsName string ` json:"goodsName" `
MainPhotoURL string ` json:"mainPhotoUrl" `
OrderTime int64 ` json:"orderTime" `
SkuName string ` json:"skuName" `
Quantity uint ` json:"quantity" `
Mobile string ` json:"mobile" `
Province string ` json:"province" `
City string ` json:"city" `
District string ` json:"district" `
Address string ` json:"address" `
ReceiverName string ` json:"receiverName" `
PayMethod uint ` json:"payMethod" `
PurchasePrice decimal . Decimal ` json:"purchasePrice" `
ExpressFee decimal . Decimal ` json:"expressFee" `
PayTime int64 ` json:"payTime" `
CouponAmount decimal . Decimal ` json:"couponAmount" `
ExpressStatus uint ` json:"expressStatus" `
AssType uint ` json:"assType" `
RefundStatus uint ` json:"refundStatus" `
IsClosed int ` json:"isClosed" `
BuyerMessage string ` json:"buyerMessage" `
StoreMessage string ` json:"storeMessage" `
PayMethodName string ` json:"payMethodName" `
SkuCode string ` json:"skuCode" `
BrandName string ` json:"brandName" `
VendorName string ` json:"vendorName" `
UnitPrice decimal . Decimal ` json:"unitPrice" `
AssName string ` json:"assName" `
RefundName string ` json:"refundName" `
ExpressName string ` json:"expressName" `
ActualAmount decimal . Decimal ` json:"actualAmount" `
CoinAmount decimal . Decimal ` json:"coinAmount" `
GoodsSn string ` json:"goodsSn" `
StoreHouse int ` json:"-" `
TimeOut int ` json:"time_out" `
OrderFiling uint ` json:"order_filing" `
MakeUpStatus uint ` json:"make_up_status" `
CanPay bool ` json:"can_pay" `
}
type QueryDownload struct {
manage . RecookOrderGoodsDetailModel
Province string
City string
District string
Address string
ReceiverName string
Mobile string
Status string
PayTime formatime . Second
AssTypeStr string
AsID uint
CompletedAt formatime . Second
Remark string
PayMethod uint
RealName string
Card string
Et formatime . Second
DeliveryTimeOut uint
}
func ( o * orderLogic ) Query2excel ( result [ ] QueryDownload , isVendor bool , mu * manage2 . RecookManageUserInfoModel ) ( string , error ) {
var list [ ] interface { }
if len ( result ) == 0 {
return "" , errors . New ( "查询结果为空" )
}
for _ , v := range result {
et := v . Et . Time . Format ( "2006-01-02 15:04:05" )
if v . Et . IsZero ( ) {
et = ""
}
ct := v . CompletedAt . Time . Format ( "2006-01-02 15:04:05" )
if v . CompletedAt . IsZero ( ) {
ct = ""
}
temp := manage . RecookOrderInfoModel { }
temp . PayMethod = v . PayMethod
settleAmount := v . PurchasePrice . Mul ( decimal . NewFromInt ( int64 ( v . Quantity ) ) ) . Truncate ( 2 )
totalSettleAmount := settleAmount . Add ( v . ExpressFee )
timeout := "否"
if v . DeliveryTimeOut == 1 {
timeout = "是"
}
var col interface { } = & excel . VendorOrderQueryRow {
OrderID : v . OrderId ,
OrderGoodsID : v . Id ,
Status : v . Status ,
CreatedAt : v . CreatedAt . Time . Format ( "2006-01-02 15:04:05" ) ,
PayTime : v . PayTime . Time . Format ( "2006-01-02 15:04:05" ) ,
ExpressTime : et ,
TimeOut : timeout ,
ReceiveTime : ct ,
AssType : v . AssTypeStr ,
AsID : v . AsID ,
Category : v . CategoryName ,
Brand : v . BrandName ,
VendorName : v . VendorName ,
GoodsName : v . GoodsName ,
SkuCode : v . SkuCode ,
SkuName : v . SkuName ,
Quantity : v . Quantity ,
Receiver : v . ReceiverName ,
Phone : v . Mobile ,
ReceiveAddr : v . Province + v . City + v . District + v . Address ,
BuyerName : v . RealName ,
Card : v . Card ,
Remark : v . Remark ,
PurchasePrice : v . PurchasePrice ,
SettlementAmount : settleAmount ,
ExpressFee : v . ExpressFee ,
TotalSettledAmount : totalSettleAmount ,
}
if ! isVendor {
col = & excel . OrderQueryRow {
VendorOrderQueryRow : * ( col . ( * excel . VendorOrderQueryRow ) ) ,
UnitPrice : v . UnitPrice ,
GoodsAmount : v . GoodsAmount ,
CoinAmount : v . CoinAmount ,
ActualAmount : v . ActualAmount ,
PayMethod : temp . GetPayMethodTypeName ( ) ,
TotalCommission : v . TotalCommission ,
}
}
list = append ( list , col )
}
name := time . Now ( ) . Format ( "20060102-150405" ) + tools . GenerateGoodsHashSign ( ) + ".xlsx"
path := filepath . Join ( static_path . Dir . Root , static_path . Dir . Temp , name )
sheet := excel . CreateSheet ( "recook" , list )
if mu != nil && mu . NewroleId != 1 {
temp := ( & manage2 . RecookExportDisplayRoleMenuModel { } ) . FindByRoleID ( mu . NewroleId )
exclude := ( & manage2 . RecookExportDisplayMenuModel { } ) . FindExportByIDs ( temp )
sheet . RegisterAuth ( "check" , func ( sheet * excel . Sheet , s string ) bool {
return ! common . StringSliceContains ( exclude , s )
} )
}
excelFile := excel . NewFile ( )
excelFile . AddSheet ( sheet )
if err := excelFile . ToExcel ( path ) ; err != nil {
return "" , err
}
return name , nil
}
// @Style 获取数据
func ( o * orderLogic ) Query ( where Where ) ( list [ ] QueryDownload , e error ) {
rog := manage . RecookOrderGoodsDetailModel { }
query := rog . GetDb ( ) . Table ( rog . TableName ( ) + " AS rog" ) .
Select ( ` rog . * , addr . province , addr . city , addr . district , addr . address , addr . receiver_name , addr . mobile ,
( CASE WHEN rog . express_status = 0 AND rog . is_closed = 0 THEN ' 未 发 货 '
WHEN rog . express_status = 1 AND rog . is_closed = 0 THEN ' 已 发 货 '
WHEN rog . express_status = 2 AND rog . is_closed = 0 THEN ' 已 确 认 收 货 '
WHEN rog . ass_type in ( 0 , 1 , 2 ) AND rog . is_closed = 1 THEN ' 已 关 闭 '
ELSE "未知" END ) AS status ,
ro . pay_time ,
( CASE WHEN rog . ass_type = 0 AND rog . is_closed = 0 THEN ' 无 售 后 '
WHEN rog . ass_type = 1 AND rog . is_closed = 0 THEN ' 仅 退 款 '
WHEN rog . ass_type = 2 AND rog . is_closed = 0 THEN ' 退 货 退 款 '
WHEN rog . ass_type = 3 AND rog . is_closed = 1 THEN ' 补 偿 '
ELSE "未知" END ) AS ass_type_str ,
IF ( rog . ass_type < > 0 , ( SELECT id FROM recook_after_sales_goods AS aft WHERE rog . id = aft . order_goods_id ORDER BY id DESC LIMIT 1 ) , 0 ) AS as_id ,
ro . completed_at ,
ro . buyer_message ,
ro . pay_method ,
ru . real_name ,
ru . id_card AS card ,
express . express_time AS et ,
rog . delivery_time_out ,
rog . order_filing
` ) .
Joins ( "JOIN recook_order_info AS ro ON ro.id = rog.order_id" ) .
Joins ( "JOIN recook_goods_info AS rgi ON rgi.id = rog.goods_id" ) .
Joins ( "JOIN recook_order_addr AS addr ON addr.order_id = rog.order_id" ) .
Joins ( "JOIN recook_user_info AS ru ON ru.id = rog.user_id" ) .
Joins ( "LEFT JOIN gys_goods_info AS ggi ON rgi.id = ggi.main_goods_id" ) .
Joins ( "LEFT JOIN (SELECT order_goods_id, MIN(express_time) as express_time FROM recook_order_goods_express GROUP BY order_goods_id) AS express ON express.order_goods_id = rog.id" ) .
Where ( "rog.pay_status = ?" , manage . RecookOrderGoodsDetailPayStatusSuc )
query = query . Where ( "rog.shipping_method = ? AND is_split = 0" , manage . RecookOrderGoodsDetailShippingMethodExpress )
{
// 供应商筛选
if where . IsSale {
query = query . Where ( "ro.order_type = 2" )
} else {
query = query . Where ( "ro.order_type = 1" )
}
if where . VendorId > 0 {
query = query . Where ( "rog.vendor_id = ?" , where . VendorId )
}
// 订单商品id筛选
if where . OrderGoodsId > 0 {
query = query . Where ( "rog.id = ?" , where . OrderGoodsId )
}
// 订单编号筛选
if where . OrderId > 0 {
query = query . Where ( "rog.order_id = ?" , where . OrderId )
}
if where . BrandID > 0 {
query = query . Where ( "rog.brand_id = ?" , where . BrandID )
}
// 订单状态
if where . OrderStatus != 0 {
switch where . OrderStatus {
case 1 :
query = query . Where ( "rog.express_status = ?" , manage . RecookOrderGoodsDetailExpressStatusNone )
query = query . Where ( "rog.is_closed = ?" , manage . RecookOrderGoodsDetailIsClosedFalse )
case 2 :
query = query . Where ( "rog.express_status = ?" , manage . RecookOrderGoodsDetailExpressStatusTrue )
query = query . Where ( "rog.is_closed = ?" , manage . RecookOrderGoodsDetailIsClosedFalse )
case 3 :
query = query . Where ( "rog.express_status = ?" , manage . RecookOrderGoodsDetailExpressStatusConfirm )
query = query . Where ( "rog.is_closed = ?" , manage . RecookOrderGoodsDetailIsClosedFalse )
case 4 :
query = query . Where ( "rog.is_closed = ?" , manage . RecookOrderGoodsDetailIsClosedTrue )
assTypes := [ ] int { manage . RecookOrderGoodsDetailAssTypeRefund , manage . RecookOrderGoodsDetailAssTypeRejected }
query = query . Where ( "rog.ass_type in (?)" , assTypes )
}
}
// 商品id
if where . GoodsId != 0 {
query = query . Where ( "rog.goods_id = ?" , where . GoodsId )
}
// 商品名称
if where . GoodsName != "" {
query = query . Where ( "rog.goods_name like ?" , fmt . Sprintf ( "%s%%" , where . GoodsName ) )
}
// 下单时间
if where . OrderTimeStart != "" && where . OrderTimeEnd != "" {
query = query . Where ( "rog.order_time between ? and ?" ,
where . OrderTimeStart + " 00:00:00" , where . OrderTimeEnd + " 23:59:59" )
}
// 售后类型
if where . RefundType != manage . RecookOrderGoodsDetailAssTypeNone {
if where . RefundType == 4 {
query = query . Where ( "rog.ass_type = ?" , manage . RecookOrderGoodsDetailAssTypeNone )
} else {
query = query . Where ( "rog.ass_type = ?" , where . RefundType )
}
}
// 快递单号
if where . ExpressNo != "" {
express := & manage . RecookOrderGoodsExpressModel { }
subQuery := express . GetDb ( ) . Select ( "order_goods_id" ) .
Where ( "express_no = ? " , where . ExpressNo ) .
Table ( express . TableName ( ) ) . SubQuery ( )
query = query . Where ( "rog.id in (?)" , subQuery )
}
// 商品条码
if where . SkuCode != "" {
query = query . Where ( "rog.sku_code like ?" , fmt . Sprintf ( "%s%%" , where . SkuCode ) )
}
switch where . TimeOut {
case 1 :
query = query . Where ( "rog.delivery_time_out = 1" )
case 2 :
query = query . Where ( "rog.delivery_time_out <> 1" )
}
switch where . OrderFiling {
case 1 :
query = query . Where ( "rog.order_filing=1" )
case 2 :
query = query . Where ( "rog.order_filing=0" )
}
// 标签
if where . LableStatus != 0 {
if where . LableStatus != 7 {
query = query . Where ( "rog.is_closed = ?" , manage . RecookOrderGoodsDetailIsClosedFalse )
}
switch where . LableStatus {
case 1 : // 代发货
query = query . Where ( "rog.express_status = ?" , manage . RecookOrderGoodsDetailExpressStatusNone )
query = query . Where ( "rog.ass_type = ?" , manage . RecookOrderGoodsDetailAssTypeNone )
case 2 : // 未发货退款
query = query . Where ( "rog.express_status = ?" , manage . RecookOrderGoodsDetailExpressStatusNone )
query = query . Where ( "rog.ass_type = ?" , manage . RecookOrderGoodsDetailAssTypeRefund )
//case 3: // 超时未发货
// query = query.Where("rog.express_status = ?", manage.RecookOrderGoodsDetailExpressStatusNone)
// query = query.Where("rog.ass_type in (?)",
// []int{manage.RecookOrderGoodsDetailAssTypeNone})
// query = query.Where("ro.pay_time < ?",
// time.Now().Add(-1*time.Second*manage.ExpressTime).Format("2006-01-02 15:04:05"))
case 4 : // 已发货
query = query . Where ( "rog.express_status = ?" , manage . RecookOrderGoodsDetailExpressStatusTrue )
query = query . Where ( "rog.ass_type = ?" , manage . RecookOrderGoodsDetailAssTypeNone )
case 5 : // 已发货退款
query = query . Where ( "rog.express_status = ?" , manage . RecookOrderGoodsDetailExpressStatusTrue )
query = query . Where ( "rog.ass_type in (?)" ,
[ ] int { manage . RecookOrderGoodsDetailAssTypeRejected , manage . RecookOrderGoodsDetailAssTypeCompensate } )
case 6 : // 已完成
query = query . Where ( "rog.express_status = ?" , manage . RecookOrderGoodsDetailExpressStatusConfirm )
query = query . Where ( "rog.ass_type in (?)" ,
[ ] int { manage . RecookOrderGoodsDetailAssTypeNone , manage . RecookOrderGoodsDetailAssTypeCompensate } )
case 7 : // 已关闭
query = query . Where ( "rog.is_closed = ?" , manage . RecookOrderGoodsDetailIsClosedTrue )
query = query . Where ( "rog.ass_type in (?)" ,
[ ] int { manage . RecookOrderGoodsDetailAssTypeRefund , manage . RecookOrderGoodsDetailAssTypeRejected } )
}
}
// 一级类目
if where . FirstCategoryId != 0 {
query = query . Where ( "rgi.first_category_id = ?" , where . FirstCategoryId )
}
// 二级类目
if where . SecondCategoryId != 0 {
query = query . Where ( "second_category_id = ?" , where . SecondCategoryId )
}
// 支付方式
if where . PayMethod != 0 {
recookOrderInfoModel := & manage . RecookOrderInfoModel { }
payMethods := recookOrderInfoModel . GetPayMethods ( where . PayMethod )
query = query . Where ( "ro.pay_method in (?)" , payMethods )
}
// 完成时间
if where . CompletedTimeStart != "" && where . CompletedTimeEnd != "" {
query = query . Where ( "ro.completed_at between ? and ?" ,
where . CompletedTimeStart + " 00:00:00" , where . CompletedTimeEnd + " 23:59:59" )
}
// 收货人姓名
if where . ReceiverName != "" {
query = query . Where ( "addr.receiver_name like ?" , fmt . Sprintf ( "%%%s%%" , where . ReceiverName ) )
}
// 收货人手机号
if where . Mobile != "" {
query = query . Where ( "addr.mobile like ?" , fmt . Sprintf ( "%%%s%%" , where . Mobile ) )
}
//供应商新品编号
if where . GoodsSn != "" {
query = query . Where ( "ggi.goods_sn like ?" , fmt . Sprintf ( "%%%s%%" , where . GoodsSn ) )
}
}
if err := query . Find ( & list ) . Error ; err != nil {
e = err
return
}
return
}
// @Style 获取数据
func ( o * orderLogic ) Data ( where Where , page * bean . Page ) ( list [ ] ListItem , total int ) {
recookOrderGoodsDetailModel := & manage . RecookOrderGoodsDetailModel { }
query := "is_split=0"
queryArgs := [ ] interface { } { }
if ! where . IsSale {
query = "pay_status = ? and orderGoodsDetail.shipping_method = ? and is_split = 0"
queryArgs = [ ] interface { } { manage . RecookOrderGoodsDetailPayStatusSuc , manage . RecookOrderGoodsDetailShippingMethodExpress }
}
// 供应商筛选
if where . IsSale {
query += " and order_type = 2"
} else {
query += " and order_type = 1"
}
if where . VendorId > 0 {
query += " and orderGoodsDetail.vendor_id = ?"
queryArgs = append ( queryArgs , where . VendorId )
}
// 订单商品id筛选
if where . OrderGoodsId > 0 {
query += " and orderGoodsDetail.id = ?"
queryArgs = append ( queryArgs , where . OrderGoodsId )
}
if where . BrandID > 0 {
query += " and orderGoodsDetail.brand_id = ?"
queryArgs = append ( queryArgs , where . BrandID )
}
// 订单编号筛选
if where . OrderId > 0 {
query += " and orderGoodsDetail.order_id = ?"
queryArgs = append ( queryArgs , where . OrderId )
}
// 订单状态
if where . OrderStatus != 0 {
switch where . OrderStatus {
case 1 :
query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ?"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailExpressStatusNone , manage . RecookOrderGoodsDetailIsClosedFalse )
case 2 :
query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ?"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailExpressStatusTrue , manage . RecookOrderGoodsDetailIsClosedFalse )
case 3 :
query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ?"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailExpressStatusConfirm , manage . RecookOrderGoodsDetailIsClosedFalse )
case 4 :
query += " and orderGoodsDetail.is_closed = ? and orderGoodsDetail.ass_type in (?)"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailIsClosedTrue , [ ] int { manage . RecookOrderGoodsDetailAssTypeRefund , manage . RecookOrderGoodsDetailAssTypeRejected } )
case 10 :
// 待处理
if where . IsSale {
query += " and orderInfo.status=0 and orderInfo.can_pay=0"
}
}
}
// 商品id
if where . GoodsId != 0 {
query += " and orderGoodsDetail.goods_id = ?"
queryArgs = append ( queryArgs , where . GoodsId )
}
// 商品名称
if where . GoodsName != "" {
query += " and orderGoodsDetail.goods_name like ?"
queryArgs = append ( queryArgs , fmt . Sprintf ( "%%%s%%" , where . GoodsName ) )
}
// 一级类目
if where . FirstCategoryId != 0 {
query += " and goodsInfo.first_category_id = ?"
queryArgs = append ( queryArgs , where . FirstCategoryId )
}
// 二级类目
if where . SecondCategoryId != 0 {
query += " and goodsInfo.second_category_id = ?"
queryArgs = append ( queryArgs , where . SecondCategoryId )
}
// 下单时间
if where . OrderTimeStart != "" && where . OrderTimeEnd != "" {
query += " and orderGoodsDetail.order_time between ? and ?"
queryArgs = append ( queryArgs , where . OrderTimeStart + " 00:00:00" , where . OrderTimeEnd + " 23:59:59" )
}
// 支付方式
recookOrderInfoModel := & manage . RecookOrderInfoModel { }
if where . PayMethod != 0 {
payMethods := recookOrderInfoModel . GetPayMethods ( where . PayMethod )
query += " and orderInfo.pay_method in (?)"
queryArgs = append ( queryArgs , payMethods )
}
// 售后类型
if where . RefundType != manage . RecookOrderGoodsDetailAssTypeNone {
if where . RefundType == 4 {
query += " and orderGoodsDetail.ass_type = ?"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailAssTypeNone )
} else {
query += " and orderGoodsDetail.ass_type = ?"
queryArgs = append ( queryArgs , where . RefundType )
}
}
// 完成时间
if where . CompletedTimeStart != "" && where . CompletedTimeEnd != "" {
query += " and orderInfo.completed_at between ? and ?"
queryArgs = append ( queryArgs , where . CompletedTimeStart + " 00:00:00" , where . CompletedTimeEnd + " 23:59:59" )
}
// 收货人姓名
if where . ReceiverName != "" {
query += " and orderAddr.receiver_name like ?"
queryArgs = append ( queryArgs , fmt . Sprintf ( "%%%s%%" , where . ReceiverName ) )
}
// 收货人手机号
if where . Mobile != "" {
query += " and orderAddr.mobile like ?"
queryArgs = append ( queryArgs , fmt . Sprintf ( "%%%s%%" , where . Mobile ) )
}
// 快递单号
if where . ExpressNo != "" {
recookOrderGoodsExpressModel := & manage . RecookOrderGoodsExpressModel { }
subQuery := recookOrderGoodsExpressModel . GetDb ( ) . Select ( "order_goods_id" ) . Where ( "express_no = ? " , where . ExpressNo ) . Table ( recookOrderGoodsExpressModel . TableName ( ) ) . SubQuery ( )
query += " and orderGoodsDetail.id in ?"
queryArgs = append ( queryArgs , subQuery )
}
// 供应商新品编号
if where . GoodsSn != "" {
query += " and gysGoodsInfo.goods_sn like ?"
queryArgs = append ( queryArgs , fmt . Sprintf ( "%%%s%%" , where . GoodsSn ) )
}
// 商品条码
if where . SkuCode != "" {
query += " and orderGoodsDetail.sku_code like ?"
queryArgs = append ( queryArgs , fmt . Sprintf ( "%%%s%%" , where . SkuCode ) )
}
switch where . TimeOut {
case 1 :
query += " and orderGoodsDetail.delivery_time_out = 1"
case 2 :
query += " and orderGoodsDetail.delivery_time_out <> 1"
}
switch where . OrderFiling {
case 1 :
query += " and orderGoodsDetail.order_filing = 1"
case 2 :
query += " and orderGoodsDetail.order_filing = 0"
}
if where . LableStatus != 0 {
switch where . LableStatus {
case 1 : // 代发货
query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ? and orderGoodsDetail.ass_type = ?"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailExpressStatusNone , manage . RecookOrderGoodsDetailIsClosedFalse , manage . RecookOrderGoodsDetailAssTypeNone )
case 2 : // 未发货退款
query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ? and orderGoodsDetail.ass_type = ?"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailExpressStatusNone , manage . RecookOrderGoodsDetailIsClosedFalse , manage . RecookOrderGoodsDetailAssTypeRefund )
//case 3: // 超时未发货
// query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ? and orderGoodsDetail.ass_type in (?) and orderInfo.pay_time < ?"
// queryArgs = append(queryArgs, manage.RecookOrderGoodsDetailExpressStatusNone, manage.RecookOrderGoodsDetailIsClosedFalse, []int{manage.RecookOrderGoodsDetailAssTypeNone}, time.Now().Add(-1*time.Second*manage.ExpressTime).Format("2006-01-02 15:04:05"))
case 4 : // 已发货
query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ? and orderGoodsDetail.ass_type = ?"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailExpressStatusTrue , manage . RecookOrderGoodsDetailIsClosedFalse , manage . RecookOrderGoodsDetailAssTypeNone )
case 5 : // 已发货退款
query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ? and orderGoodsDetail.ass_type in (?)"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailExpressStatusTrue , manage . RecookOrderGoodsDetailIsClosedFalse , [ ] int { manage . RecookOrderGoodsDetailAssTypeRejected , manage . RecookOrderGoodsDetailAssTypeCompensate } )
case 6 : // 已完成
query += " and orderGoodsDetail.express_status = ? and orderGoodsDetail.is_closed = ? and orderGoodsDetail.ass_type in (?)"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailExpressStatusConfirm , manage . RecookOrderGoodsDetailIsClosedFalse , [ ] int { manage . RecookOrderGoodsDetailAssTypeNone , manage . RecookOrderGoodsDetailAssTypeCompensate } )
case 7 : // 已关闭
query += " and orderGoodsDetail.is_closed = ? and orderGoodsDetail.ass_type in (?)"
queryArgs = append ( queryArgs , manage . RecookOrderGoodsDetailIsClosedTrue , [ ] int { manage . RecookOrderGoodsDetailAssTypeRefund , manage . RecookOrderGoodsDetailAssTypeRejected } )
case 10 :
// 待处理
if where . IsSale {
query += " and orderInfo.status=0 and orderInfo.can_pay=0"
}
}
}
recookOrderAddrModel := & manage . RecookOrderAddrModel { }
recookGoodsInfoModel := & goods . RecookGoodsInfoModel { }
gysGoodsInfoModel := & goods2 . GysGoodsInfoModel { }
start := 0
if page != nil {
recookOrderGoodsDetailModel . GetDb ( ) . Table ( recookOrderGoodsDetailModel . TableName ( ) + " as orderGoodsDetail" ) .
Joins ( fmt . Sprintf ( "left join %s as orderInfo on orderGoodsDetail.order_id = orderInfo.id" , recookOrderInfoModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as orderAddr on orderGoodsDetail.order_id = orderAddr.order_id" , recookOrderAddrModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as goodsInfo on orderGoodsDetail.goods_id = goodsInfo.id" , recookGoodsInfoModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as gysGoodsInfo on goodsInfo.id = gysGoodsInfo.main_goods_id" , gysGoodsInfoModel . TableName ( ) ) ) .
Where ( query , queryArgs ... ) .
Count ( & total )
start = page . GetStart ( )
}
if total > start || page == nil {
var orderGoodsDetails [ ] ListStruct
if page == nil {
recookOrderGoodsDetailModel . GetDb ( ) . Table ( recookOrderGoodsDetailModel . TableName ( ) + " as orderGoodsDetail" ) .
Select ( [ ] string {
"orderGoodsDetail.*" ,
"orderAddr.province,orderAddr.city,orderAddr.district,orderAddr.address,orderAddr.receiver_name,orderAddr.mobile" ,
"orderInfo.pay_method,orderInfo.pay_time,orderInfo.can_pay" ,
"gysGoodsInfo.goods_sn" ,
"enter.enterprise_name as enter_prise_name" ,
} ) .
Joins ( fmt . Sprintf ( "left join %s as orderInfo on orderGoodsDetail.order_id = orderInfo.id" , recookOrderInfoModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as orderAddr on orderGoodsDetail.order_id = orderAddr.order_id" , recookOrderAddrModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as goodsInfo on orderGoodsDetail.goods_id = goodsInfo.id" , recookGoodsInfoModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as gysGoodsInfo on goodsInfo.id = gysGoodsInfo.main_goods_id" , gysGoodsInfoModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as enter on orderGoodsDetail.vendor_id = enter.user_id" , ( & enterprise . GysEnterpriseStateModel { } ) . TableName ( ) ) ) .
Where ( query , queryArgs ... ) .
Order ( "orderGoodsDetail.created_at DESC" ) .
Find ( & orderGoodsDetails )
} else {
recookOrderGoodsDetailModel . GetDb ( ) . Table ( recookOrderGoodsDetailModel . TableName ( ) + " as orderGoodsDetail" ) .
Select ( [ ] string {
"orderGoodsDetail.*" ,
"orderAddr.province,orderAddr.city,orderAddr.district,orderAddr.address,orderAddr.receiver_name,orderAddr.mobile" ,
"orderInfo.pay_method,orderInfo.pay_time,orderInfo.can_pay" ,
"gysGoodsInfo.goods_sn" ,
"enter.enterprise_name as enter_prise_name" ,
} ) .
Joins ( fmt . Sprintf ( "left join %s as orderInfo on orderGoodsDetail.order_id = orderInfo.id" , recookOrderInfoModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as orderAddr on orderGoodsDetail.order_id = orderAddr.order_id" , recookOrderAddrModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as goodsInfo on orderGoodsDetail.goods_id = goodsInfo.id" , recookGoodsInfoModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as gysGoodsInfo on goodsInfo.id = gysGoodsInfo.main_goods_id" , gysGoodsInfoModel . TableName ( ) ) ) .
Joins ( fmt . Sprintf ( "left join %s as enter on orderGoodsDetail.vendor_id = enter.user_id" , ( & enterprise . GysEnterpriseStateModel { } ) . TableName ( ) ) ) .
Offset ( start ) .
Limit ( page . GetLimit ( ) ) .
Where ( query , queryArgs ... ) .
Order ( "orderGoodsDetail.created_at DESC" ) .
Find ( & orderGoodsDetails )
}
for _ , detail := range orderGoodsDetails {
recookOrderInfoModel . PayMethod = detail . PayMethod
list = append ( list , ListItem {
Id : detail . Id ,
OrderId : detail . OrderId ,
GoodsName : detail . GoodsName ,
MainPhotoURL : detail . MainPhotoURL ,
OrderTime : detail . OrderTime . Time . Unix ( ) ,
SkuName : detail . SkuName ,
Quantity : detail . Quantity ,
Province : detail . Province ,
City : detail . City ,
District : detail . District ,
Address : detail . Address ,
ReceiverName : detail . ReceiverName ,
Mobile : detail . Mobile ,
PayMethod : detail . PayMethod ,
PayMethodName : recookOrderInfoModel . GetPayMethodTypeName ( ) ,
PurchasePrice : detail . PurchasePrice ,
ExpressFee : detail . ExpressFee ,
PayTime : detail . PayTime . Time . Unix ( ) ,
CouponAmount : detail . BrandCouponAmount . Add ( detail . UniverseCouponAmount ) , // 优惠卷
ExpressStatus : detail . ExpressStatus ,
AssType : detail . AssType ,
RefundStatus : detail . RefundStatus ,
IsClosed : detail . IsClosed ,
BuyerMessage : detail . BuyerMessage ,
StoreMessage : detail . StoreMessage ,
SkuCode : detail . SkuCode ,
BrandName : detail . BrandName ,
VendorName : detail . EnterPriseName ,
UnitPrice : detail . UnitPrice ,
AssName : detail . GetAssName ( ) ,
RefundName : detail . GetRefundName ( ) ,
ExpressName : recookOrderInfoModel . GetExpressName ( detail . ExpressStatus ) ,
ActualAmount : detail . ActualAmount ,
CoinAmount : detail . CoinAmount ,
GoodsSn : detail . GoodsSn ,
StoreHouse : detail . Storehouse ,
TimeOut : detail . DeliveryTimeOut ,
OrderFiling : detail . OrderFiling ,
MakeUpStatus : detail . MakeUpStatus ,
CanPay : detail . CanPay ,
} )
}
}
return
}
type GoodsDetail struct {
Id uint ` json:"id" `
OrderId uint ` json:"orderId" `
PayMethod uint ` json:"payMethod" `
PayTime int64 ` json:"payTime" `
ExpressTime int64 ` json:"expressTime" `
OrderAddr OrderAddr ` json:"orderAddr" `
GoodsInfo GoodsInfo ` json:"goodsInfo" `
CompletedAt int64 ` json:"completedAt" `
BuyerMessage string ` json:"buyerMessage" `
ExpressStatus uint ` json:"expressStatus" `
AssType uint ` json:"assType" `
RefundStatus uint ` json:"refundStatus" `
IsClosed int ` json:"isClosed" `
OrderTime int64 ` json:"orderTime" `
Exclude [ ] string ` json:"exclude" `
MakeUpStatus uint ` json:"make_up_status" `
}
type GoodsInfo struct {
GoodsId uint ` json:"goodsId" `
GoodsName string ` json:"goodsName" `
SkuName string ` json:"skuName" `
PurchasePrice decimal . Decimal ` json:"purchasePrice" `
Quantity uint ` json:"quantity" `
CouponAmount decimal . Decimal ` json:"couponAmount" `
ExpressFee decimal . Decimal ` json:"expressFee" `
MainPhotoURL string ` json:"mainPhotoUrl" `
UnitPrice decimal . Decimal ` json:"unitPrice" `
ActualAmount decimal . Decimal ` json:"actualAmount" `
CoinAmount decimal . Decimal ` json:"coinAmount" `
TotalCommission decimal . Decimal ` json:"totalCommission" `
SkuCode string ` json:"skuCode" `
SettlementAmount decimal . Decimal ` json:"settlementAmount" `
GoodsAmount decimal . Decimal ` json:"goodsAmount" `
TotalAmount decimal . Decimal ` json:"totalAmount" `
}
// @Style 详情
func ( o * orderLogic ) Info ( orderGoodsId uint , user * manage2 . RecookManageUserInfoModel , vendorId ... uint ) ( result GoodsDetail ) {
recookOrderGoodsDetailModel := & manage . RecookOrderGoodsDetailModel { }
goodsDetail := recookOrderGoodsDetailModel . FindById ( orderGoodsId )
// 供应商id筛选
if len ( vendorId ) > 0 {
if goodsDetail . VendorId != vendorId [ 0 ] {
return
}
}
recookOrderInfoModel := & manage . RecookOrderInfoModel { }
orderInfo := recookOrderInfoModel . FindById ( goodsDetail . OrderId )
recookOrderAddrModel := & manage . RecookOrderAddrModel { }
orderAddr := recookOrderAddrModel . FindByOrderId ( goodsDetail . OrderId )
var exclude [ ] string
if user != nil {
temp := ( & manage2 . RecookExportDisplayRoleMenuModel { } ) . FindByRoleID ( user . NewroleId )
exclude = ( & manage2 . RecookExportDisplayMenuModel { } ) . FindExportByIDs ( temp )
}
settledAmount := goodsDetail . PurchasePrice . Mul ( decimal . NewFromInt ( int64 ( goodsDetail . Quantity ) ) ) . Truncate ( 2 )
totalAmount := settledAmount . Add ( goodsDetail . ExpressFee )
result = GoodsDetail {
Id : goodsDetail . Id ,
OrderId : goodsDetail . OrderId ,
PayMethod : orderInfo . PayMethod ,
PayTime : orderInfo . PayTime . Time . Unix ( ) ,
ExpressTime : goodsDetail . ExpressTime . Time . Unix ( ) ,
CompletedAt : orderInfo . CompletedAt . Time . Unix ( ) ,
OrderTime : goodsDetail . OrderTime . Time . Unix ( ) ,
BuyerMessage : orderInfo . BuyerMessage ,
ExpressStatus : goodsDetail . ExpressStatus ,
AssType : goodsDetail . AssType ,
RefundStatus : goodsDetail . RefundStatus ,
IsClosed : goodsDetail . IsClosed ,
OrderAddr : OrderAddr {
Province : orderAddr . Province ,
City : orderAddr . City ,
District : orderAddr . District ,
Address : orderAddr . Address ,
ReceiverName : orderAddr . ReceiverName ,
Mobile : orderAddr . Mobile ,
} ,
GoodsInfo : GoodsInfo {
GoodsId : goodsDetail . GoodsId ,
GoodsName : goodsDetail . GoodsName ,
SkuName : goodsDetail . SkuName ,
PurchasePrice : goodsDetail . PurchasePrice ,
Quantity : goodsDetail . Quantity ,
CouponAmount : goodsDetail . BrandCouponAmount . Add ( goodsDetail . UniverseCouponAmount ) , // 优惠卷
ExpressFee : goodsDetail . ExpressFee ,
MainPhotoURL : goodsDetail . MainPhotoURL ,
UnitPrice : goodsDetail . UnitPrice ,
ActualAmount : goodsDetail . ActualAmount ,
CoinAmount : goodsDetail . CoinAmount ,
TotalCommission : goodsDetail . TotalCommission ,
SkuCode : goodsDetail . SkuCode ,
SettlementAmount : settledAmount ,
GoodsAmount : goodsDetail . GoodsAmount ,
TotalAmount : totalAmount ,
} ,
Exclude : exclude ,
MakeUpStatus : goodsDetail . MakeUpStatus ,
}
return
}
type OrderGoodsDetailItem struct {
Id uint ` json:"id" `
OrderId uint ` json:"orderId" `
Type uint ` json:"type" `
TypeName string ` json:"typeName" `
Content string ` json:"content" `
CreatedAt int64 ` json:"createdAt" `
User string ` json:"user" `
}
// @Style 订单商品日志列表
func ( o * orderLogic ) LogsList ( vendorId , orderGoodsId uint , page bean . Page ) ( list [ ] OrderGoodsDetailItem , total int ) {
recookOrderGoodsDetailModel := & manage . RecookOrderGoodsDetailModel { }
goodsDetail := recookOrderGoodsDetailModel . FindById ( orderGoodsId )
if vendorId > 0 {
if goodsDetail . VendorId != vendorId {
return
}
}
list = [ ] OrderGoodsDetailItem { }
recookOrderGoodsDetailLogsModel := & manage . RecookOrderGoodsDetailLogsModel { }
total = recookOrderGoodsDetailLogsModel . ListCount ( "order_id = ?" , orderGoodsId )
start := page . GetStart ( )
if total > start {
lists := recookOrderGoodsDetailLogsModel . List ( start , page . GetLimit ( ) , "id desc" , "order_id = ?" , orderGoodsId )
for _ , item := range lists {
list = append ( list , OrderGoodsDetailItem {
Id : item . Id ,
OrderId : item . OrderId ,
Type : item . Type ,
TypeName : item . GetTypeName ( ) ,
Content : item . Content ,
CreatedAt : item . CreatedAt . Time . Unix ( ) ,
User : item . User ,
} )
}
}
return
}
// @Style 商家日志
func ( o * orderLogic ) Logs ( orderId , actionType uint , user , content string , tx ... * gorm . DB ) error {
recookOrderGoodsDetailLogsModel := & manage . RecookOrderGoodsDetailLogsModel {
OrderId : orderId ,
Type : actionType ,
Content : content ,
User : user ,
}
if len ( tx ) > 0 {
recookOrderGoodsDetailLogsModel . SetDb ( tx [ 0 ] )
}
recookOrderGoodsDetailLogsModel . Create ( recookOrderGoodsDetailLogsModel )
if recookOrderGoodsDetailLogsModel . Id == 0 {
return errors . New ( "网络异常" )
}
return nil
}