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.

319 lines
7.3 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 evaluation
import (
"github.com/gin-gonic/gin"
"github.com/golangkit/formatime"
"recook/configs"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/goods"
"recook/internal/model/order"
"recook/internal/model/user"
"recook/internal/v2/model/baiduapi"
"recook/tools"
)
type goodsEvaluation struct {
GoodsID uint `json:"goodsId" validate:"required"`
Content string `json:"content" validate:"required"`
Images []image `json:"images"`
}
type image struct {
Path string `json:"path"`
Width uint `json:"width"`
Height uint `json:"height"`
}
type createParam struct {
UserID uint `json:"userId" validate:"required"`
OrderID uint `json:"orderId" validate:"required"`
Evaluations []goodsEvaluation `json:"evaluations" validate:"required"`
}
//老的根据订单创建评价,先废弃
func CreateOrderEvaluation(c *gin.Context) {
var p createParam
err := tools.ParseParams(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
var orderInfo order.Information
dbc.DB.Select("id, user_id, evaluated_at").First(&orderInfo, p.OrderID)
var userInfo user.Information
dbc.DB.Select("id, nickname, head_img_url").First(&userInfo, p.UserID)
if orderInfo.UserID != p.UserID {
back.Fail(c, "非法调用,参数错误")
return
}
if orderInfo.EvaluatedAt.Valid == true {
back.Fail(c, "请勿重复评价")
return
}
tx := dbc.DB.Begin()
{
err = tx.Model(&orderInfo).Updates(order.Information{EvaluatedAt: formatime.NewSecondNow()}).Error
if err != nil {
back.Fail(c, err.Error())
tx.Rollback()
return
}
for _, v1 := range p.Evaluations {
if len(v1.Content) == 0 && len(v1.Images) == 0 {
continue
}
if len(v1.Images) > 9 {
back.Fail(c, "图片最多9张")
tx.Rollback()
return
}
e := goods.Evaluation{
GoodsID: v1.GoodsID,
OrderID: p.OrderID,
UserID: userInfo.ID,
Nickname: userInfo.Nickname,
HeadImgUrl: userInfo.HeadImgUrl,
Content: v1.Content,
}
err = tx.Create(&e).Error
if err != nil {
back.Fail(c, err.Error())
tx.Rollback()
return
}
for i2, v2 := range v1.Images {
if len(v2.Path) == 0 {
back.Fail(c, "图片路径为空")
tx.Rollback()
return
}
photo := goods.EvaluationPhoto{
EvaluationID: e.ID,
URL: v2.Path,
Width: v2.Width,
Height: v2.Height,
OrderNo: uint(i2),
}
err = tx.Create(&photo).Error
if err != nil {
back.Fail(c, err.Error())
tx.Rollback()
return
}
}
}
}
tx.Commit()
back.Suc(c, "", nil)
}
type orderEvaluationListPem struct {
UserID uint `json:"userId" validate:"required"` //用户id
Status int `json:"status"` //1已评价0未评价
}
type myOrderGoodsDeaAll struct {
MyOrderGoodsDea order.GoodsDetail `json:"my_order_goods_dea"`
PassEvaluation int `json:"pass_evaluation"`
Compliance int `json:"compliance"`
}
//评价商品列表
func OrderEvaluationList(c *gin.Context) {
var p orderEvaluationListPem
err := tools.ParseParams(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
var myOrderGoodsDea []order.GoodsDetail
//条件userid status 是交易完成的
where := ""
if p.Status == 1 {
//已评价
where = "and evaluated_id!=0"
} else {
//未评价
where = "and evaluated_id=0"
}
dbc.DB.Find(&myOrderGoodsDea, "user_id=? and status=1 "+where, p.UserID)
//新增内容,返回审核中,安全或风险等状态
var list []myOrderGoodsDeaAll
for _, v := range myOrderGoodsDea {
var eva goods.Evaluation
dbc.DB.Table(eva.TableName()).Where("id=?", v.EvaluatedId).First(&eva)
list = append(list, myOrderGoodsDeaAll{
MyOrderGoodsDea: v,
PassEvaluation: eva.Pass,
Compliance: eva.Compliance,
})
}
back.Suc(c, "", list)
}
//对单个产品进行评价
type createOneParam struct {
UserID uint `json:"userId" validate:"required"` //用户id
GoodsDetailId uint `json:"goodsDetailId" validate:"required"` //订单详情id
Content string `json:"content" validate:"required"` //评价内容
Images []image `json:"images"` //评价图片 数组
}
//对单个产品进行评价
func CreateOrderEvaluationOne(c *gin.Context) {
var p createOneParam
err := tools.ParseParams(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
//获取订单商品详情
var orderGoodsDea order.GoodsDetail
dbc.DB.First(&orderGoodsDea, "id=?", p.GoodsDetailId)
var userInfo user.Information
dbc.DB.Select("id, nickname, head_img_url").First(&userInfo, p.UserID)
if orderGoodsDea.UserID != p.UserID {
back.Fail(c, "非法调用,参数错误")
return
}
if orderGoodsDea.EvaluatedId != 0 {
back.Fail(c, "请勿重复评价")
return
}
/*新增内容,审核图文是否合规*/
imgs := []string{}
var aduid uint
var text string
tx := dbc.DB.Begin()
{
if len(p.Content) == 0 && len(p.Images) == 0 {
back.Fail(c, "评价内容不能为空")
tx.Rollback()
return
}
if len(p.Images) > 9 {
back.Fail(c, "图片最多9张")
tx.Rollback()
return
}
e := goods.Evaluation{
GoodsID: orderGoodsDea.GoodsID,
OrderID: orderGoodsDea.OrderID,
UserID: userInfo.ID,
Nickname: userInfo.Nickname,
HeadImgUrl: userInfo.HeadImgUrl,
Content: p.Content,
}
err = tx.Create(&e).Error
if err != nil {
back.Fail(c, err.Error())
tx.Rollback()
return
}
//给评价详情里面插入id
tx.Model(&orderGoodsDea).Updates(order.GoodsDetail{
EvaluatedId: int(e.ID),
})
for i2, v2 := range p.Images {
imgs = append(imgs, configs.ConfigImageUrl+v2.Path)
if len(v2.Path) == 0 {
back.Fail(c, "图片路径为空")
tx.Rollback()
return
}
photo := goods.EvaluationPhoto{
EvaluationID: e.ID,
URL: v2.Path,
Width: v2.Width,
Height: v2.Height,
OrderNo: uint(i2),
}
err = tx.Create(&photo).Error
if err != nil {
back.Fail(c, err.Error())
tx.Rollback()
return
}
}
aduid = e.ID
text = e.Content
}
tx.Commit()
go auditImagesAndText(text, imgs, aduid)
back.Suc(c, "", nil)
}
func auditImagesAndText(text string, images []string, id uint) {
b := baiduapi.BdAudit{}
t := b.TextAudit(text)
var aud goods.Evaluation
if t.Conclusion != "合规" { //不合格
aud = goods.Evaluation{
Compliance: 1,
Emg: t.Data[0].Msg,
}
dbc.DB.Table(aud.TableName()).Where("id=?", id).Updates(&aud)
return
}
for _, v := range images {
im := b.ImageAudit(v)
if im.Conclusion != "合规" { //不合格
aud = goods.Evaluation{
Compliance: 1,
Emg: im.Data[0].Msg,
}
dbc.DB.Table(aud.TableName()).Where("id=?", id).Updates(&aud)
return
}
}
}
type EvaluationDeaPem struct {
ID uint `json:"id"` //评价的id
}
//查看评价详情
func EvaluationDea(c *gin.Context) {
var p EvaluationDeaPem
err := tools.ParseParams(&p, c)
if err != nil {
back.Fail(c, err.Error())
return
}
var goodsEva goods.Evaluation
dbc.DB.First(&goodsEva, "id=?", p.ID)
if goodsEva.ID <= 0 {
back.Fail(c, "数据不存在")
return
}
var goodsEvaGoods []goods.EvaluationPhoto
dbc.DB.Find(&goodsEvaGoods, "evaluation_id=?", goodsEva.ID)
back.Suc(c, "", gin.H{
"goodsEva": goodsEva,
"goodsEvaGoods": goodsEvaGoods,
})
return
}