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.
220 lines
7.7 KiB
220 lines
7.7 KiB
package logic
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/jinzhu/gorm"
|
|
"recook/internal/model/vend"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var BrandLogic = &brand{}
|
|
|
|
type brand struct {
|
|
}
|
|
|
|
type BrandItem struct {
|
|
Name string `json:"name" form:"name"`
|
|
Category string `json:"category" form:"category"`
|
|
Level uint `json:"level" form:"level"` // 商家类型
|
|
LevelInfo string `json:"levelInfo" form:"levelInfo"`
|
|
Registration []string `json:"registration" form:"registration"`
|
|
RegistrationStart string `json:"registrationStart" form:"registrationStart"`
|
|
RegistrationStartTime time.Time
|
|
RegistrationEnd string `json:"registrationEnd" form:"registrationEnd"`
|
|
RegistrationEndTime time.Time
|
|
Log string `json:"log" form:"log"` // logo
|
|
Authorization []Authorization `json:"authorization" form:"authorization"`
|
|
Other []string `json:"other" form:"other"`
|
|
Special []Special `json:"special" form:"special"`
|
|
}
|
|
type Authorization struct {
|
|
Authorization string `json:"authorization" form:"authorization"`
|
|
AuthorizationStart string `json:"authorizationStart" form:"authorizationStart"`
|
|
AuthorizationStartTime time.Time `json:"-"`
|
|
AuthorizationEnd string `json:"authorizationEnd" form:"authorizationEnd"`
|
|
AuthorizationEndTime time.Time `json:"-"`
|
|
}
|
|
type Special struct {
|
|
SpecialUrl string `json:"specialUrl" form:"specialUrl"`
|
|
SpecialStart string `json:"specialStart" form:"specialStart"`
|
|
SpecialStartTime time.Time
|
|
SpecialEnd string `json:"specialEnd" form:"specialEnd"`
|
|
SpecialEndTime time.Time
|
|
}
|
|
|
|
// @Style 品牌验证
|
|
func (b *brand) VerfiyBrand(data *[]BrandItem) error {
|
|
if len(*data) == 0 {
|
|
return errors.New("请完善品牌信息")
|
|
}
|
|
for key, brand := range *data {
|
|
if brand.Name == "" {
|
|
return errors.New("请填写品牌名称")
|
|
}
|
|
if brand.Category == "" {
|
|
return errors.New("请选择主营类目")
|
|
}
|
|
if brand.Level < 1 || brand.Level > 5 {
|
|
return errors.New("品牌-商家类型非法")
|
|
}
|
|
if brand.Level == 5 && brand.LevelInfo == "" {
|
|
return errors.New("请填写品牌-商家类型描述")
|
|
}
|
|
lenRegistration := len(brand.Registration)
|
|
if lenRegistration == 0 {
|
|
return errors.New("请上传商标注册证")
|
|
}
|
|
if lenRegistration > 6 {
|
|
return errors.New("商标注册证不能超过6个")
|
|
}
|
|
if brand.RegistrationStart == "" || brand.RegistrationEnd == "" {
|
|
return errors.New("商标有效日期格式错误")
|
|
}
|
|
tmpTime, err := time.ParseInLocation("2006-01-02", brand.RegistrationStart, time.Local)
|
|
if err != nil {
|
|
return errors.New("商标有效日期格式错误")
|
|
}
|
|
(*data)[key].RegistrationStartTime = tmpTime
|
|
tmpTime, err = time.ParseInLocation("2006-01-02", brand.RegistrationEnd, time.Local)
|
|
if err != nil {
|
|
return errors.New("商标有效日期格式错误")
|
|
}
|
|
(*data)[key].RegistrationEndTime = tmpTime
|
|
lenOther := len(brand.Other)
|
|
if lenOther > 10 {
|
|
return errors.New("其他资质证明不能超过10个")
|
|
}
|
|
lenAuthorization := len(brand.Authorization)
|
|
if lenAuthorization == 0 {
|
|
return errors.New("请上传品牌授权书")
|
|
}
|
|
if lenAuthorization > 10 {
|
|
return errors.New("品牌授权书不能超过10个")
|
|
}
|
|
for index, authorization := range brand.Authorization {
|
|
if authorization.AuthorizationStart == "" || authorization.AuthorizationEnd == "" {
|
|
return errors.New("授权有效日期格式错误")
|
|
}
|
|
tmpTime, err = time.ParseInLocation("2006-01-02", authorization.AuthorizationStart, time.Local)
|
|
if err != nil {
|
|
return errors.New("授权有效日期格式错误")
|
|
}
|
|
(*data)[key].Authorization[index].AuthorizationStartTime = tmpTime
|
|
tmpTime, err = time.ParseInLocation("2006-01-02", authorization.AuthorizationEnd, time.Local)
|
|
if err != nil {
|
|
return errors.New("授权有效日期格式错误")
|
|
}
|
|
(*data)[key].Authorization[index].AuthorizationEndTime = tmpTime
|
|
}
|
|
for index, item := range brand.Special {
|
|
if item.SpecialUrl != "" {
|
|
if item.SpecialStart == "" || item.SpecialEnd == "" {
|
|
return errors.New("特殊资质证明有效期格式错误")
|
|
}
|
|
tmpTime, err = time.ParseInLocation("2006-01-02", item.SpecialStart, time.Local)
|
|
if err != nil {
|
|
return errors.New("特殊资质证明有效期格式错误")
|
|
}
|
|
(*data)[key].Special[index].SpecialStartTime = tmpTime
|
|
tmpTime, err = time.ParseInLocation("2006-01-02", item.SpecialEnd, time.Local)
|
|
if err != nil {
|
|
return errors.New("特殊资质证明有效期格式错误")
|
|
}
|
|
(*data)[key].Special[index].SpecialEndTime = tmpTime
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// @Style 处理品牌信息
|
|
// @Param brandId uint true "品牌id 0=新增品牌"
|
|
// @Param myGysId uint true "供应商id"
|
|
// @Param data []BrandItem true "品牌数据"
|
|
// @Param enterpriseId uint true ""
|
|
// @Param state string true "状态"
|
|
// @Param db *gorm.DB true "db 需要上层处理事物"
|
|
func (b *brand) HandBrand(brandId, myGysId uint, data []BrandItem, enterpriseId uint, state string, db *gorm.DB) (err error) {
|
|
for _, brand := range data {
|
|
categorys := strings.Split(brand.Category, ",")
|
|
lenCategorys := len(categorys)
|
|
|
|
for index := 0; index < lenCategorys; index += 2 {
|
|
item := vend.GysEnterpriseBrand{
|
|
Name: brand.Name,
|
|
Category: categorys[index],
|
|
CategoryId: categorys[index+1],
|
|
Level: brand.Level,
|
|
LevelInfo: brand.LevelInfo,
|
|
RegistrationStart: strconv.FormatInt(brand.RegistrationStartTime.Unix(), 10),
|
|
RegistrationEnd: strconv.FormatInt(brand.RegistrationEndTime.Unix(), 10),
|
|
AuthorizationStart: strconv.FormatInt(brand.Authorization[0].AuthorizationStartTime.Unix(), 10),
|
|
AuthorizationEnd: strconv.FormatInt(brand.Authorization[0].AuthorizationEndTime.Unix(), 10),
|
|
EnterpriseID: enterpriseId,
|
|
State: state,
|
|
Log: brand.Log,
|
|
UserID: myGysId,
|
|
}
|
|
if brandId > 0 {
|
|
item.ID = brandId
|
|
if err = db.Table("gys_enterprise_brand").Where("id = ?", brandId).Updates(&item).Error; err != nil {
|
|
return errors.New("网络异常")
|
|
}
|
|
|
|
} else {
|
|
db.Create(&item)
|
|
}
|
|
if item.ID <= 0 {
|
|
return errors.New("网络异常")
|
|
}
|
|
valueStr := ""
|
|
values := []interface{}{}
|
|
for _, value := range brand.Registration {
|
|
valueStr += ",(?,?,?,?,?)"
|
|
values = append(values, 1, value, item.ID, brand.RegistrationStartTime.Unix(), brand.RegistrationEndTime.Unix())
|
|
}
|
|
for _, value := range brand.Authorization {
|
|
valueStr += ",(?,?,?,?,?)"
|
|
values = append(values, 2, value.Authorization, item.ID, value.AuthorizationStartTime.Unix(), value.AuthorizationEndTime.Unix())
|
|
}
|
|
for _, value := range brand.Other {
|
|
valueStr += ",(?,?,?,?,?)"
|
|
values = append(values, 3, value, item.ID, 0, 0)
|
|
}
|
|
if brandId > 0 {
|
|
db.Delete(&vend.GysEnterpriseImg{}, "brand_id = ?", brandId)
|
|
}
|
|
if len(values) > 0 {
|
|
err := db.Exec("insert into gys_enterprise_img(type,url,brand_id,start,end) values "+valueStr[1:], values...).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
valueStr = ""
|
|
values = []interface{}{}
|
|
for _, value := range brand.Special {
|
|
if value.SpecialUrl == "" {
|
|
continue
|
|
}
|
|
valueStr += ",(?,?,?,?)"
|
|
values = append(values, value.SpecialUrl, value.SpecialStartTime.Unix(), value.SpecialEndTime.Unix(), item.ID)
|
|
}
|
|
if brandId > 0 {
|
|
db.Delete(&vend.GysEnterpriseSpecial{}, "brand_id = ?", brandId)
|
|
}
|
|
if len(values) > 0 {
|
|
err = db.Exec("insert into gys_enterprise_special(url,start,end,brand_id) values "+valueStr[1:], values...).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if brandId > 0 {
|
|
return nil
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|