mh_server/model/recycle_card.go
2022-08-31 15:25:45 +08:00

254 lines
7.3 KiB
Go

package model
import (
"encoding/json"
"fmt"
"github.com/codinl/go-logger"
"math/rand"
"mh-server/lib/xianmai"
"time"
)
//go:generate goqueryset -in recycle_card.go
// gen:qs
type RecycleCardOrder struct {
Model
Uid uint32 `json:"uid" gorm:"index"`
GoodsId uint32 `json:"goods_id" gorm:"index"`
GoodsName string `json:"goods_name"`
GoodsImg string `json:"goods_img"`
Keyword string `json:"keyword"`
Price uint32 `json:"price"`
State uint32 `json:"state"` // 1-待回收 2-已完成 3-已被拒 4-已取消
StoreId uint32 `json:"store_id" gorm:"index"`
StoreName string `json:"store_name"`
Images string `json:"images" gorm:"type:text"` // 图片
SerialNumber string `json:"serial_number"`
EvaluationTime time.Time `json:"evaluation_time"`
Attribute string `json:"attribute" gorm:"type:text"` //
Number string `json:"number"` // 订单
CheckTime time.Time `json:"check_time"`
DepressionRate uint32 `json:"depression_rate"` //
User *User `json:"user" gorm:"-"`
Store *Store `json:"store" gorm:"-"`
Describe string `json:"describe"` // 描述
Remark string `json:"remark"` // 备注
AssistantName string `json:"assistant_name"` // 店员
RetrieveTime time.Time `json:"retrieve_time"` // 审核时间
RetrieveState uint32 `json:"retrieve_state"` // 回收卡状态 1-未确认 2-已确认
OriginalPrice uint32 `json:"original_price"` // 原价
CooperativeBusinessId uint32 `json:"cooperative_business_id" gorm:"index"` // 合作商id
CooperativeName string `json:"cooperative_name"` // 合作商名称
// recycle_card_order
}
type RecycleAttribute struct {
Id uint32 `json:"id"`
Name string `json:"name"`
ValueId uint32 `json:"value_id"`
ValueName string `json:"value_name"`
Type uint32 `json:"type"` // 1-sku 2-问题
}
//type GameEvaluationReq struct {
// GoodsId uint32 `json:"goods_id"`
// ProblemAttrList []struct {
// ProblemAttrId int `json:"problem_attr_id"`
// ProblemAttrValueId int `json:"problem_attr_value_id"`
// } `json:"problem_attr_list"`
// SkuList []struct {
// ProblemAttrId int `json:"problem_attr_id"`
// ProblemAttrValueId int `json:"problem_attr_value_id"`
// } `json:"sku_list"`
//}
func RecycleCardOrderCreate(uid uint32, req xianmai.GameEvaluationReq) (*RecycleCardOrder, error) {
order := &RecycleCardOrder{
Uid: uid,
GoodsId: uint32(req.GoodsId),
GoodsName: req.GoodsName,
GoodsImg: req.GoodsImg,
Keyword: req.Keyword,
State: 1,
StoreId: req.StoreId,
StoreName: "",
Images: "",
SerialNumber: "",
Attribute: "",
DepressionRate: 0,
RetrieveState: 0,
}
evaluation, err := req.Evaluation()
if err != nil {
logger.Error("evaluation err:", err)
return order, err
}
//evaluation = (evaluation * 1) / 100
order.OriginalPrice = uint32(evaluation * 100)
info, err := RecycleCardConfigInfo()
if err != nil {
logger.Error("config err:", err)
return order, err
}
if info.RebateRate > 0 && info.RebateRate < 10000 {
evaluation = (evaluation * int(10000-info.RebateRate)) / 100
} else {
evaluation = evaluation * 100
}
if evaluation > 70000 {
logger.Error("price err")
return order, err
}
evaluation = (evaluation / 100) * 100
//evaluation = 40 // TODO
order.Price = uint32(evaluation)
attributes := make([]RecycleAttribute, 0, len(req.ProblemAttrList)+len(req.SkuList))
rand.Seed(time.Now().UnixNano())
order.Number = fmt.Sprintf("%d%s", rand.Int31n(899999)+100000, time.Now().Format("060102"))
for i, _ := range req.SkuList {
attribute := RecycleAttribute{
Id: uint32(req.SkuList[i].ProblemAttrId),
Name: req.SkuList[i].ProblemAttrName,
ValueId: uint32(req.SkuList[i].ProblemAttrValueId),
ValueName: req.SkuList[i].ProblemAttrValueName,
Type: 1,
}
attributes = append(attributes, attribute)
}
for i, _ := range req.ProblemAttrList {
attribute := RecycleAttribute{
Id: uint32(req.ProblemAttrList[i].ProblemAttrId),
Name: req.ProblemAttrList[i].ProblemAttrName,
ValueId: uint32(req.ProblemAttrList[i].ProblemAttrValueId),
ValueName: req.ProblemAttrList[i].ProblemAttrValueName,
Type: 2,
}
attributes = append(attributes, attribute)
}
attributeByte, err := json.Marshal(&attributes)
if err != nil {
logger.Error("attributes marshal err:", err)
return order, err
}
store, err := GetStore(req.StoreId)
if err != nil {
logger.Error("store err:", err)
return order, err
}
order.CooperativeBusinessId = store.CooperativeBusinessId
order.CooperativeName = store.CooperativeName
order.Attribute = string(attributeByte)
err = DB.Create(order).Error
if err != nil {
logger.Error("create order err:", err)
return order, err
}
return order, err
}
type RecycleCardOrderListReq struct {
PageIdx int `json:"page_idx"`
PageSize int `json:"page_size"`
Uid uint32 `json:"uid"`
}
func (m *RecycleCardOrderListReq) List() ([]RecycleCardOrder, int, error) {
page := m.PageIdx - 1
if page < 0 {
page = 0
}
if m.PageSize == 0 {
m.PageSize = 10
}
var list []RecycleCardOrder
qs := NewRecycleCardOrderQuerySet(DB).UidEq(m.Uid)
count, err := qs.Count()
if err != nil {
logger.Error("err:", err)
return nil, 0, err
}
totalPage := count/m.PageSize + 1
err = qs.OrderDescByID().Offset(page * m.PageSize).Limit(m.PageSize).All(&list)
if err != nil {
logger.Error("err:", err)
return nil, 0, err
}
RecycleCardOrderListSetStore(list)
return list, totalPage, nil
}
func RecycleCardOrderListSetStore(orders []RecycleCardOrder) {
ids := make([]uint32, 0, len(orders))
for i, _ := range orders {
ids = append(ids, orders[i].StoreId)
}
storeMap, err := GetStoreMap(ids)
if err != nil {
logger.Error("store map err:", err)
return
}
for i, _ := range orders {
v, ok := storeMap[orders[i].StoreId]
if ok {
orders[i].Store = &v
}
}
}
type ConsoleRecycleCardOrderListReq struct {
PageIdx int `json:"page_idx"`
PageSize int `json:"page_size"`
Uid uint32 `json:"uid"`
StoreId uint32 `json:"store_id"`
Number string `json:"number"`
State uint32 `json:"state"`
}
func (m *ConsoleRecycleCardOrderListReq) List() ([]RecycleCardOrder, int, error) {
page := m.PageIdx - 1
if page < 0 {
page = 0
}
if m.PageSize == 0 {
m.PageSize = 10
}
var list []RecycleCardOrder
qs := NewRecycleCardOrderQuerySet(DB)
if m.Uid != 0 {
qs = qs.UidEq(m.Uid)
}
if m.StoreId != 0 {
qs = qs.StoreIdEq(m.StoreId)
}
if m.Number != "" {
qs = qs.NumberEq(m.Number)
}
if m.State != 0 {
qs = qs.StateEq(m.State)
}
count, err := qs.Count()
if err != nil {
logger.Error("err:", err)
return nil, 0, err
}
totalPage := count/m.PageSize + 1
err = qs.OrderDescByID().Offset(page * m.PageSize).Limit(m.PageSize).All(&list)
if err != nil {
logger.Error("err:", err)
return nil, 0, err
}
RecycleCardOrderListSetStore(list)
return list, totalPage, nil
}