You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

475 lines
15 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package order
import (
"errors"
"fmt"
"recook/internal/dbc"
"recook/internal/libs/bean"
"recook/internal/model/order"
goods2 "recook/internal/v2/logic/manage/goods"
"recook/internal/v2/model/recook/after"
"recook/internal/v2/model/recook/goods"
manage "recook/internal/v2/model/recook/order"
"git.oa00.com/go/mysql"
"github.com/golangkit/formatime"
"github.com/shopspring/decimal"
)
var OrderLogic = &orderLogic{}
type orderLogic struct {
}
type OrderItem struct {
OrderId uint `json:"orderId"`
ShippingMethod uint `json:"shippingMethod"`
CreatedAt int64 `json:"createdAt"`
PayTime int64 `json:"payTime"`
ExpireTime int64 `json:"expireTime"`
Status uint `json:"status"`
GoodsTotalAmount decimal.Decimal `json:"goodsTotalAmount"`
ActualTotalAmount decimal.Decimal `json:"actualTotalAmount"`
Goods []OrderGooadsItem `json:"goods"`
TotalGoodsCount uint `json:"totalGoodsCount"`
}
type OrderGooadsItem struct {
OrderGoodsId uint `json:"orderGoodsId"`
MainPhotoURL string `json:"mainPhotoUrl"`
GoodsName string `json:"goodsName"`
SkuName string `json:"skuName"`
IsImport int `json:"isImport"`
Quantity uint `json:"quantity"`
UnitPrice decimal.Decimal `json:"unitPrice"`
ExpressStatus uint `json:"expressStatus"`
AssType uint `json:"assType"`
RefundStatus uint `json:"refundStatus"`
GoodsID uint `json:"goods_id"`
}
// @Style 订单列表
func (o *orderLogic) Lists(userId, status uint, page bean.Page) (lists []OrderItem, total int) {
lists = []OrderItem{}
recookOrderInfoModel := &manage.RecookOrderInfoModel{}
query := "user_id = ?"
queryArgs := []interface{}{userId}
if status > 0 {
switch status {
case 1: // 未付款
query += " and status = ?"
queryArgs = append(queryArgs, manage.RecookOrderInfoStatusNone)
case 2: // 待发货
query += " and status = ? and express_status = ?"
queryArgs = append(queryArgs, manage.RecookOrderInfoStatusSuc, manage.RecookOrderInfoExpressStatusNone)
case 3: // 待收货
query += " and status = ? and express_status = ?"
queryArgs = append(queryArgs, manage.RecookOrderInfoStatusSuc, manage.RecookOrderInfoExpressStatusIng)
}
}
total = recookOrderInfoModel.ListCount(query, queryArgs...)
start := page.GetStart()
if total > start {
orderLists := recookOrderInfoModel.List(start, page.GetLimit(), "id desc", query, queryArgs...)
var orderIds []uint
for _, item := range orderLists {
orderIds = append(orderIds, item.Id)
}
recookOrderGoodsDetailModel := &manage.RecookOrderGoodsDetailModel{}
orderGoodsDetails := recookOrderGoodsDetailModel.FindByOrderIds(orderIds)
orderGoodsDetailMap := map[uint][]OrderGooadsItem{}
for _, item := range orderGoodsDetails {
orderGoodsDetailMap[item.OrderId] = append(orderGoodsDetailMap[item.OrderId], OrderGooadsItem{
OrderGoodsId: item.Id,
MainPhotoURL: item.MainPhotoURL,
GoodsName: item.GoodsName,
SkuName: item.SkuName,
IsImport: item.IsImport,
Quantity: item.Quantity,
UnitPrice: item.UnitPrice,
ExpressStatus: item.ExpressStatus,
AssType: item.AssType,
RefundStatus: item.RefundStatus,
})
}
for _, item := range orderLists {
lists = append(lists, OrderItem{
OrderId: item.Id,
ShippingMethod: item.ShippingMethod,
CreatedAt: item.CreatedAt.Time.Unix(),
PayTime: item.PayTime.Time.Unix(),
ExpireTime: item.ExpireTime.Time.Unix(),
Status: item.Status,
GoodsTotalAmount: item.GoodsTotalAmount,
ActualTotalAmount: item.ActualTotalAmount,
Goods: orderGoodsDetailMap[item.Id],
})
}
}
return
}
// @Style 导购订单列表
func (o *orderLogic) GuideLists(userId, status uint, page bean.Page, isSale bool) (lists []OrderItem, total int) {
ids := make([]uint, 0)
mysql.Db.Table((&order.Profit{}).TableName()).Where("user_id = ?", userId).Group("order_id").Pluck("order_id", &ids)
lists = []OrderItem{}
recookOrderInfoModel := &manage.RecookOrderInfoModel{}
query := "id in (?) and user_id != ?"
//userCoinHistory := user.RecookUserCoinHistory{}
queryArgs := []interface{}{ids, userId}
if isSale {
query += " and order_type = 2"
} else {
query += " and order_type = 1"
}
if status > 0 {
switch status {
case 1: // 待发货
query += " and status = ? and express_status = ?"
queryArgs = append(queryArgs, manage.RecookOrderInfoStatusSuc, manage.RecookOrderInfoExpressStatusNone)
case 2: // 待收货
query += " and status = ? and express_status = ?"
queryArgs = append(queryArgs, manage.RecookOrderInfoStatusSuc, manage.RecookOrderInfoExpressStatusIng)
case 3: // 已收货
query += " and status = ? and express_status = ?"
queryArgs = append(queryArgs, manage.RecookOrderInfoStatusSuc, manage.RecookOrderInfoExpressStatusComp)
case 4:
if isSale {
query += " and status = 0 and can_pay = 0"
queryArgs = append(queryArgs, manage.RecookOrderInfoStatusSuc, manage.RecookOrderInfoExpressStatusComp)
}
}
}
total = recookOrderInfoModel.ListCount(query, queryArgs...)
start := page.GetStart()
if total > start {
orderLists := recookOrderInfoModel.List(start, page.GetLimit(), "id desc", query, queryArgs...)
var orderIds []uint
for _, item := range orderLists {
orderIds = append(orderIds, item.Id)
}
recookOrderGoodsDetailModel := &manage.RecookOrderGoodsDetailModel{}
orderGoodsDetails := recookOrderGoodsDetailModel.FindByOrderIds(orderIds)
orderGoodsDetailMap := map[uint][]OrderGooadsItem{}
for _, item := range orderGoodsDetails {
orderGoodsDetailMap[item.OrderId] = append(orderGoodsDetailMap[item.OrderId], OrderGooadsItem{
OrderGoodsId: item.Id,
MainPhotoURL: item.MainPhotoURL,
GoodsName: item.GoodsName,
SkuName: item.SkuName,
IsImport: item.IsImport,
Quantity: item.Quantity,
UnitPrice: item.UnitPrice,
ExpressStatus: item.ExpressStatus,
AssType: item.AssType,
RefundStatus: item.RefundStatus,
GoodsID: item.GoodsId,
})
}
for _, item := range orderLists {
var totalCount uint = 0
for _, v := range orderGoodsDetailMap[item.Id] {
totalCount += v.Quantity
}
lists = append(lists, OrderItem{
OrderId: item.Id,
ShippingMethod: item.ShippingMethod,
CreatedAt: item.CreatedAt.Time.Unix(),
PayTime: item.PayTime.Time.Unix(),
ExpireTime: item.ExpireTime.Time.Unix(),
Status: item.Status,
GoodsTotalAmount: item.GoodsTotalAmount,
ActualTotalAmount: item.ActualTotalAmount,
Goods: orderGoodsDetailMap[item.Id],
TotalGoodsCount: totalCount,
})
}
}
return
}
//可开发票列表回显
type CanBillVo struct {
EndTime formatime.Second `json:"end_time"`
GoodsName string `json:"goods_name"`
GoodsTotalAmount decimal.Decimal `json:"goods_total_amount"`
OrderId uint `json:"order_id"`
}
//可开发票列表
func (o *orderLogic) CanBill(id uint, size uint, num uint) (rest []CanBillVo) {
var order manage.RecookOrderInfoModel
//var detail manage.RecookOrderGoodsDetailModel
//获取order列表
orderList := order.CanBill(id, size, num)
//主动关闭的售后订单
afterMap := make(map[uint]after.RecookAfterSalesGoodsModel)
for _, v := range orderList {
for _, k := range v.After {
if k.Id > 0 && k.IsClosed == 1 {
afterMap[k.OrderGoodsId] = k
}
}
}
//遍历orderList 查看是否有进口商品,进口商品不能开票
for _, v := range orderList {
var sum decimal.Decimal
for _, k := range v.OrderSku {
//开票金额为CoinAmount+GoodsAmount
if k.ActualAmount.IsZero() {
continue
}
if k.AssType > 0 {
if _, ok := afterMap[k.Id]; ok {
//有售后关闭的,可以继续开票
} else {
//其他的跳过
continue
}
}
if k.Storehouse == 0 || k.Storehouse == 1 {
//sum = sum.Add(k.CoinAmount)
//sum = sum.Add(k.GoodsAmount)
sum = sum.Add(k.ActualAmount)
} else {
//进口商品不能开票,需跳过此循环
continue
}
}
if sum.GreaterThan(decimal.NewFromFloat(0)) {
canBillOne := CanBillVo{
EndTime: v.PayTime,
GoodsName: v.Title,
OrderId: v.Id,
GoodsTotalAmount: sum,
}
rest = append(rest, canBillOne)
}
}
return rest
}
type InvoiceApplyRequest struct {
UserId int `gorm:"column:user_id" json:"user_id"` //购买者id
OrderId []uint `gorm:"column:order_id" json:"order_id"` //订单id
BuyerName string `gorm:"column:buyer_name" json:"buyer_name"` //抬头名称
TaxNum string `gorm:"column:tax_num" json:"tax_num"` //购方税号
Address string `gorm:"column:address" json:"address"` //公司地址
Telephone string `gorm:"column:telephone" json:"telephone"` //公司电话
Phone string `gorm:"column:phone" json:"phone"` //购方手机
Email string `gorm:"column:email" json:"email"` //推送邮箱
Account string `gorm:"column:account" json:"account"` //银行账号
Message string `gorm:"column:message" json:"message"` //备注
TotalAmount decimal.Decimal `gorm:"column:total_amount" json:"total_amount"` //订单总金额
InvoiceStatus int `gorm:"column:invoice_status" json:"invoice_status"`
}
//新增发票申请
func (o *orderLogic) InvoiceCreate(p InvoiceApplyRequest) error {
s := goods.RecookInvoiceApplyModel{}
if len(p.OrderId) == 0 {
return errors.New("请输入正确订单id")
}
if p.BuyerName == "" {
return errors.New("抬头名称为必填项")
}
if p.Phone == "" || p.Email == "" {
return errors.New("手机号和邮箱为必填项")
}
for _, v := range p.OrderId {
re := s.FindByOrderId(v)
if re.Id != 0 {
return errors.New("该订单已开发票,请勿重复上传")
}
//startTimestamp := time.Now().Unix() //获得时间戳
////把时间戳转换成时间,并格式化为年月日
//StartTimeStr := time.Unix(startTimestamp, 0).Format("2006-01-02 15:04")
t := formatime.NewSecondNow()
fmt.Println(t)
rest := goods.RecookInvoiceApplyModel{
UserId: p.UserId,
OrderId: v,
BuyerName: p.BuyerName,
TaxNum: p.TaxNum,
Address: p.Address,
Telephone: p.Telephone,
Phone: p.Phone,
Email: p.Email,
Account: p.Account,
Message: p.Message,
TotalAmount: p.TotalAmount,
InvoiceStatus: p.InvoiceStatus,
Ctime: t,
FailReasons: "",
Fpqqlsh: "",
}
tx := dbc.DB.Begin()
err := tx.Create(&rest).Error
if err != nil {
tx.Rollback()
return err
}
//if res := s.FindByOrderId(v); res.Id == 0 { //验证是否成功
// tx.Rollback()
// return errors.New("添加失败")
//}
//修改订单表Recook_order_info表中invoice_status状态为1已申请
err1 := tx.Table("recook_order_info").Where("id=? ", p.OrderId).Update("invoice_status", 1).Error
if err1 != nil {
tx.Rollback()
return err
}
tx.Commit()
}
return nil
}
func (o *orderLogic) InvoiceTitleCreate(p goods.InvoiceTitleModel) error {
title := goods.InvoiceTitleModel{}
//使用map先确保关键字段不为零值
if p.Name == "" {
return errors.New("请填写抬头名称")
}
if p.Type == 1 && p.Taxnum == "" {
return errors.New("请填写公司税号")
}
resq := title.FindById(p.ID, p.UID) //查询该抬头是否存在,存在就修改
if resq.ID != 0 { //存在则修改
//update抬头参数
resq.UID = p.UID
resq.Type = p.Type
resq.Name = p.Name
resq.Taxnum = p.Taxnum
resq.Address = p.Address
resq.Phone = p.Phone
resq.Bank = p.Bank
resq.Default = p.Default
dbc.DB.Table("recook_user_letterhead").Where("id=?", p.ID).Updates(map[string]interface{}{
"uid": resq.UID,
"type": resq.Type,
"name": resq.Name,
"taxnum": resq.Taxnum,
"address": resq.Address,
"phone": resq.Phone,
"bank": resq.Bank,
"default": resq.Default,
})
} else { //不存在则新增
if p.Type == 1 { //1为公司公司名称和公司税号为必填项
if p.Taxnum == "" || p.Name == "" {
return errors.New("公司名称和公司税号为必填项")
}
//判断该公司名称是否重复
a := title.FindByName(p.Name, p.UID)
if a.ID != 0 {
return errors.New("该抬头名称已存在")
}
//b:=title.FindByTaxNum(p.Taxnum,p.UID)
//if b.ID!=0 {
// return errors.New("该税号名称已存在")
//}
}
title.Add(&p) //新增
rest := title.FindByName(p.Name, p.UID)
if rest.ID == 0 { //验证是否添加成功
return errors.New("添加失败,请重新添加")
}
}
return nil
}
type TitleUid struct {
Uid uint `json:"uid"`
}
//回显抬头列表
func (o *orderLogic) FindAllTitle(t *TitleUid) (rest []goods.InvoiceTitleModel) {
title := goods.InvoiceTitleModel{}
fmt.Println(t.Uid)
rest = title.FindAll(t.Uid)
return rest
}
//回显发票历史
func (o *orderLogic) InvoiceHistory(u uint) (rest []goods.RecookInvoiceApplyModel) {
invoices := goods.RecookInvoiceApplyModel{}
rest = invoices.FindByUid(u)
return rest
}
func (o *orderLogic) InvoiceOrderDetail(oid uint) (rest []goods2.InvoiceOrderDetailVo, err error) {
//先判断传入订单是否为空
if oid == 0 {
return nil, errors.New("请传入订单编号")
}
//订单详情表
orderDetail := manage.RecookOrderGoodsDetailModel{}
//供应商用户表
//user := user2.GysUsersModel{}
//1查询订单表
order := orderDetail.FindByOrderId(oid)
//判断该订单是否存在
if len(order) == 0 {
return nil, errors.New("请传入正确的订单编号")
}
//遍历子订单
for _, v := range order {
//仓库为2或3为进口商品不显示
if v.Storehouse == 2 || v.Storehouse == 3 {
continue
}
invoiceDetail := goods2.InvoiceOrderDetailVo{
Id: v.Id,
GoodsName: v.GoodsName,
GoodsId: v.GoodsId,
OrderId: v.OrderId,
UnitPrice: v.UnitPrice,
GysName: v.VendorName,
Num: v.Quantity,
}
n := decimal.NewFromInt(int64(invoiceDetail.Num))
invoiceDetail.SubtotalOfPrice = invoiceDetail.UnitPrice.Mul(n)
//通过skuId来查看税率表内容
var tax goods.RecookSkuInvoiceModel
dbc.DB.Table(tax.TableName()).First(&tax, "sku_id=?", v.SkuId)
invoiceDetail.TaxName = tax.TaxName
invoiceDetail.TaxRate = tax.PlatformRate
invoiceDetail.Unit = tax.Unit
//通过供应商id来查询供应商名称
//vent := user.FindById(v.VendorId)
//invoiceDetail.GysName = vent.Username
rest = append(rest, invoiceDetail)
}
return rest, err
}
//显示单个发票内容
func (o *orderLogic) ViewOne(oid uint) (rest goods.RecookInvoiceApplyModel) {
dbc.DB.Table("recook_invoice_apply").Where("order_id=?", oid).First(&rest)
return rest
}