124 lines
3.3 KiB
Go
124 lines
3.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/log"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
product "lone-services/rpc/product/pb"
|
|
"lone-services/services/product/internal/apply"
|
|
"lone-services/services/product/internal/dao"
|
|
"lone-services/services/product/internal/model"
|
|
"lone-services/services/product/internal/svc"
|
|
"lone-services/services/product/internal/wecom"
|
|
"lone-services/services/product/validator"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type EditBaseLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewEditBaseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditBaseLogic {
|
|
return &EditBaseLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *EditBaseLogic) EditBase(in *product.EditBaseReq) (*product.Response, error) {
|
|
var req validator.ProductEditBaseValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, msg), nil
|
|
}
|
|
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return failResponse(utils.ErrorNoLoginInfo), nil
|
|
}
|
|
adminId := int(adminInfo.ID)
|
|
adminName := adminInfo.Name
|
|
|
|
if !apply.CanEdit(l.ctx, adminId, int(req.Id)) {
|
|
return failResponse(utils.ErrorAuthority), nil
|
|
}
|
|
|
|
w := modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(req.Id, 10)}}
|
|
var old dao.ProductEditBase
|
|
m := model.ProductModel{}.Init()
|
|
err := m.GetOne(w, &old)
|
|
if err != nil {
|
|
log.Errorf("edit base get: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if old.Id < 1 {
|
|
return failResponse(utils.ErrorNotFund), nil
|
|
}
|
|
|
|
var nameCheck dao.ProductCheckExist
|
|
err = m.GetOne(modelbase.Params{Eq: map[string]string{"name": req.Name}}, &nameCheck)
|
|
if err != nil {
|
|
log.Errorf("edit base name check: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if nameCheck.Id > 0 && nameCheck.Id != int(req.Id) {
|
|
return failResponse(utils.ErrorDataIsExist), nil
|
|
}
|
|
|
|
publishTime := time.Now()
|
|
if len(req.PublishTime) > 1 {
|
|
t, err := parsePublishTime(req.PublishTime)
|
|
if err != nil {
|
|
return outResponse(utils.ErrorParams, "时间错误"), nil
|
|
}
|
|
publishTime = t
|
|
}
|
|
|
|
data := dao.ProductEditBase{
|
|
Id: old.Id,
|
|
Name: req.Name,
|
|
Subhead: req.Subhead,
|
|
Content: req.Content,
|
|
Images: req.Images,
|
|
PeriodValidity: int16(req.PeriodValidity),
|
|
Waybill: req.Waybill,
|
|
Weight: float32(req.Weight),
|
|
Cubage: req.Cubage,
|
|
IsIndex: uint8(req.IsIndex),
|
|
IndexImage: req.IndexImage,
|
|
Label: req.Label,
|
|
PublishTime: publishTime.Format(time.DateTime),
|
|
AdminName: adminName,
|
|
AdminId: adminId,
|
|
IsBuy: uint8(req.IsBuy),
|
|
}
|
|
if data.IsIndex == 0 {
|
|
data.IsIndex = 2
|
|
}
|
|
if data.IsBuy == 0 {
|
|
data.IsBuy = 2
|
|
}
|
|
|
|
if _, err = m.Edit(w, &data); err != nil {
|
|
log.Errorf("edit base: %v", err)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
|
|
addEditLog(int(req.Id), dao.EditLogTypeProduct, adminId, adminName, data, old)
|
|
wecom.NotifyProduct(wecom.NotifyJob{
|
|
ObjId: int(req.Id),
|
|
ObjType: dao.EditLogTypeProduct,
|
|
Type: wecom.ProductOrdinaryReview,
|
|
})
|
|
_ = apply.Del(l.ctx, adminId, int(req.Id))
|
|
|
|
return okResponse(utils.StringEmpty), nil
|
|
}
|