mh_server/model/recycle_card.go
2022-08-01 15:14:53 +08:00

149 lines
4.2 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"` // 订单
DepressionRate uint32 `json:"depression_rate"` //
User *User `json:"user" gorm:"-"`
}
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,
}
evaluation, err := req.Evaluation()
if err != nil {
logger.Error("evaluation err:", err)
return order, err
}
evaluation = (evaluation * 1) / 100
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("06-01-02"))
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)
}
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)
}
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
}
return list, totalPage, nil
}