2022-08-01 07:14:53 +00:00
|
|
|
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"` // 订单
|
2022-08-12 02:41:33 +00:00
|
|
|
CheckTime time.Time `json:"check_time"`
|
|
|
|
DepressionRate uint32 `json:"depression_rate"` //
|
2022-08-01 07:14:53 +00:00
|
|
|
User *User `json:"user" gorm:"-"`
|
2022-08-12 02:41:33 +00:00
|
|
|
Store *Store `json:"store" gorm:"-"`
|
|
|
|
Describe string `json:"describe"` // 描述
|
|
|
|
Remark string `json:"remark"` // 备注
|
|
|
|
AssistantName string `json:"assistant_name"` // 店员
|
2022-08-15 09:51:41 +00:00
|
|
|
RetrieveState uint32 `json:"retrieve_state"` // 回收卡状态 1-未确认 2-已确认
|
2022-08-12 02:41:33 +00:00
|
|
|
// recycle_card_order
|
2022-08-01 07:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2022-08-15 09:51:41 +00:00
|
|
|
RetrieveState: 0,
|
2022-08-01 07:14:53 +00:00
|
|
|
}
|
|
|
|
evaluation, err := req.Evaluation()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("evaluation err:", err)
|
|
|
|
return order, err
|
|
|
|
}
|
2022-08-12 02:41:33 +00:00
|
|
|
//evaluation = (evaluation * 1) / 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
|
2022-08-01 07:14:53 +00:00
|
|
|
order.Price = uint32(evaluation)
|
|
|
|
attributes := make([]RecycleAttribute, 0, len(req.ProblemAttrList)+len(req.SkuList))
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
2022-08-12 02:41:33 +00:00
|
|
|
order.Number = fmt.Sprintf("%d%s", rand.Int31n(899999)+100000, time.Now().Format("060102"))
|
2022-08-01 07:14:53 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-08-12 02:41:33 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-08-01 07:14:53 +00:00
|
|
|
attributeByte, err := json.Marshal(&attributes)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error("attributes marshal err:", err)
|
|
|
|
return order, err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2022-08-12 02:41:33 +00:00
|
|
|
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
|
2022-08-01 07:14:53 +00:00
|
|
|
|
2022-08-12 02:41:33 +00:00
|
|
|
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)
|
2022-08-01 07:14:53 +00:00
|
|
|
return list, totalPage, nil
|
|
|
|
}
|