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.

265 lines
6.7 KiB

package gys
import (
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
"recook/internal/api/manage/gys/logic"
vend2 "recook/internal/api/manage/vend"
"recook/internal/back"
"recook/internal/dbc"
"recook/internal/model/vend"
"recook/tools"
"strconv"
"time"
)
type Brand struct {
base
}
// @Style 供应商品牌列表
func (b *Brand) List(c *gin.Context) {
myGysId, err := b.GetGysId(c)
if err != nil {
back.Fail(c, err.Error())
return
}
args := tools.Page{}
err = tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
// 状态判断
gysEnterpriseState := vend.GysEnterpriseState{}
dbc.DB.First(&gysEnterpriseState, "user_id = ? and state = ?", myGysId, 2)
if gysEnterpriseState.ID <= 0 {
back.Err(c, "数据非法")
return
}
list := []vend.GysEnterpriseBrand{}
tableName := (&vend.GysEnterpriseBrand{}).TableName()
count := 0
dbc.DB.Table(tableName).Where("enterprise_id in (?) and user_id = ?", []uint{1, gysEnterpriseState.ID}, myGysId).Count(&count)
dbc.DB.Table(tableName).Where("enterprise_id in (?) and user_id = ?", []uint{1, gysEnterpriseState.ID}, myGysId).
Offset(args.GetStart()).Limit(args.GetLimit()).Order("id desc").Find(&list)
back.Suc(c, "操作成功", vend2.BaseResponse{Total: count, List: list})
}
type argsInfo struct {
BrandId uint `json:"brandId" form:"brandId"`
}
type replyInfo struct {
vend.GysEnterpriseBrand
Registration []string `json:"registration"` // 商标注册图片地址
Authorization []logic.Authorization `json:"authorization"` // 品牌授权书
Other []string `json:"other"` // 其他资质证明
Special []vend.GysEnterpriseSpecial `json:"special"` // 类目特殊资质
}
// @Style 供应商品牌详情
func (b *Brand) Info(c *gin.Context) {
args := argsInfo{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if args.BrandId <= 0 {
back.Fail(c, "参数错误")
}
info := replyInfo{
GysEnterpriseBrand: vend.GysEnterpriseBrand{ID: args.BrandId},
Registration: []string{},
Authorization: []logic.Authorization{},
Other: []string{},
Special: []vend.GysEnterpriseSpecial{},
}
if dbc.DB.First(&info.GysEnterpriseBrand).Error == gorm.ErrRecordNotFound {
back.Fail(c, "未查询到品牌信息")
}
// 图片获取
imgLists := []vend.GysEnterpriseImg{}
dbc.DB.Model(vend.GysEnterpriseImg{}).Where("brand_id = ? and type in (1,2,3)", args.BrandId).Find(&imgLists)
for _, list := range imgLists {
switch list.Type {
case 1:
// 商标注册图片地址
info.Registration = append(info.Registration, list.Url)
case 2:
// 品牌授权书
info.Authorization = append(info.Authorization, logic.Authorization{
Authorization: list.Url,
AuthorizationStart: b.timeFormat(strconv.FormatUint(uint64(list.Start), 10)),
AuthorizationEnd: b.timeFormat(strconv.FormatUint(uint64(list.End), 10)),
})
case 3:
// 其他资质证明
info.Other = append(info.Other, list.Url)
}
}
// 特殊资质证明
dbc.DB.Model(vend.GysEnterpriseSpecial{}).Where("brand_id = ?", args.BrandId).Find(&info.Special)
info.RegistrationStart = b.timeFormat(info.RegistrationStart)
info.RegistrationEnd = b.timeFormat(info.RegistrationEnd)
info.AuthorizationStart = b.timeFormat(info.AuthorizationStart)
info.AuthorizationEnd = b.timeFormat(info.AuthorizationEnd)
for i, special := range info.Special {
info.Special[i].Start = b.timeFormat(special.Start)
info.Special[i].End = b.timeFormat(special.End)
}
back.Suc(c, "操作成功", info)
}
func (b *Brand) timeFormat(timestamp string) string {
parseInt, err := strconv.ParseInt(timestamp, 10, 64)
if err != nil {
return ""
}
return time.Unix(parseInt, 0).Format("2006-01-02")
}
type argsCreate struct {
Data []logic.BrandItem `json:"data" form:"data"`
}
// @Style 新增品牌
func (b *Brand) Create(c *gin.Context) {
myGysId, err := b.GetGysId(c)
if err != nil {
back.Fail(c, err.Error())
return
}
// 参数获取
args := argsCreate{}
err = tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
if err = logic.BrandLogic.VerfiyBrand(&args.Data); err != nil {
back.Fail(c, err.Error())
return
}
db := dbc.DB.Begin()
defer func() {
if r := recover(); r != nil {
db.Rollback()
} else if err != nil {
db.Rollback()
} else {
db.Commit()
}
}()
err = logic.BrandLogic.HandBrand(0, myGysId, args.Data, 1, "1", db)
if err != nil {
back.Fail(c, err.Error())
return
}
back.Suc(c, "操作成功", nil)
}
type argsUpdate struct {
BrandId uint `json:"brandId" form:"brandId"`
logic.BrandItem
}
// @Style 供应商修改品牌
func (b *Brand) Update(c *gin.Context) {
myGysId, err := b.GetGysId(c)
if err != nil {
back.Fail(c, err.Error())
return
}
// 参数获取
args := argsUpdate{}
err = tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
data := []logic.BrandItem{args.BrandItem}
if err = logic.BrandLogic.VerfiyBrand(&data); err != nil {
back.Fail(c, err.Error())
return
}
info := &vend.GysEnterpriseBrand{ID: args.BrandId}
if err := dbc.DB.First(&info).Error; err != nil {
back.Fail(c, "数据错误")
return
}
if info.UserID != myGysId {
back.Fail(c, "数据错误")
return
}
db := dbc.DB.Begin()
defer func() {
if r := recover(); r != nil {
db.Rollback()
} else if err != nil {
db.Rollback()
} else {
db.Commit()
}
}()
err = logic.BrandLogic.HandBrand(args.BrandId, myGysId, data, 1, "1", db)
if err != nil {
back.Fail(c, err.Error())
return
}
back.Suc(c, "操作成功", nil)
}
type argsSelect struct {
IsVerify int `json:"isVerify" form:"isVerify"`
}
type replySelect struct {
Id uint `json:"id"`
Name string `json:"name"`
}
// @Style 品牌单选筛选
func (b *Brand) Select(c *gin.Context) {
// 参数获取
args := argsSelect{}
err := tools.Params(&args, c)
if err != nil {
back.Fail(c, err.Error())
return
}
tableName := (&vend.GysEnterpriseBrand{}).TableName()
result := []replySelect{}
// 只查询当前供应商的数据
if args.IsVerify == 1 {
// 获取供应商id
gysId, err := b.GetGysId(c)
if err != nil {
back.Fail(c, err.Error())
return
}
// 状态判断
gysEnterpriseState := vend.GysEnterpriseState{}
dbc.DB.First(&gysEnterpriseState, "user_id = ? and state = ?", gysId, 2)
if gysEnterpriseState.ID <= 0 {
back.Err(c, "数据非法")
return
}
dbc.DB.Table(tableName).Where("enterprise_id in (?) and user_id = ? and state = ?", []uint{1, gysEnterpriseState.ID}, gysId, 2).
Order("id desc").Find(&result)
} else {
dbc.DB.Table(tableName).Where("state = ?", 2).
Order("id desc").Find(&result)
}
back.Suc(c, "操作成功", result)
}