116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/services/order/internal/dao"
|
|
"lone-services/services/order/internal/model"
|
|
"lone-services/services/order/validator"
|
|
"strconv"
|
|
|
|
"lone-services/rpc/order/pb"
|
|
"lone-services/services/order/internal/svc"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type WebAddressEditLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewWebAddressEditLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebAddressEditLogic {
|
|
return &WebAddressEditLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *WebAddressEditLogic) WebAddressEdit(in *order.EditAddressRequest) (*order.Response, error) {
|
|
var v validator.AddressEditValidator
|
|
if fail := l.checkParams(in, &v); fail != nil {
|
|
return fail, nil
|
|
}
|
|
adminInfo := utils.GetUserFromCtx(l.ctx)
|
|
if adminInfo.ID < utils.NumberOne {
|
|
return l.fail(utils.ErrorNoLoginInfo)
|
|
}
|
|
encryPhone, cErr := utils.EncryptPhone(in.Mobile)
|
|
if cErr != nil {
|
|
l.Logger.Error(cErr)
|
|
return l.fail(utils.ErrorEncryptAesError)
|
|
}
|
|
|
|
if in.Id < utils.NumberOne {
|
|
return l.out(utils.ErrorParams, "地址ID不能为空")
|
|
}
|
|
|
|
var info dao.Address
|
|
w := modelbase.Params{Eq: map[string]string{
|
|
"id": strconv.FormatInt(in.Id, utils.NumberTen),
|
|
"target_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
|
|
"type": strconv.Itoa(int(in.Type)),
|
|
}}
|
|
err := model.AddressModel{}.Init().GetOne(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.ErrorNotFund)
|
|
}
|
|
if info.Id < utils.NumberOne {
|
|
return l.fail(utils.ErrorNotFund)
|
|
}
|
|
|
|
if info.Type == dao.AddressTypeUser {
|
|
info.District = in.District
|
|
info.DistrictIds = in.DistrictIds
|
|
info.Address = in.Address
|
|
info.Name = in.Name
|
|
info.Mobile = encryPhone
|
|
info.Status = utils.StatusOk
|
|
} else {
|
|
jsonStr, err := jsoniter.MarshalToString(v)
|
|
if err != nil {
|
|
l.Logger.Error("转换失败:", err)
|
|
return l.fail(utils.ErrorJsonDataError)
|
|
}
|
|
info.NewContent = jsonStr
|
|
info.Status = utils.StatusApply
|
|
}
|
|
|
|
info.Default = dao.AddressDefaultNo
|
|
modelObj := model.AddressModel{}.Init()
|
|
|
|
modelObj.Begin()
|
|
if in.Default {
|
|
info.Default = dao.AddressDefaultYes
|
|
ww := modelbase.Params{Eq: map[string]string{
|
|
"target_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
|
|
"type": strconv.Itoa(int(in.Type)),
|
|
}}
|
|
|
|
_, err = modelObj.Edit(ww, &dao.AllDefault{
|
|
Default: dao.AddressDefaultNo,
|
|
})
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
modelObj.Rollback()
|
|
return l.fail(utils.Fail)
|
|
}
|
|
}
|
|
_, err = modelObj.Edit(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
modelObj.Rollback()
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
modelObj.Commit()
|
|
return l.ok(info.Id)
|
|
|
|
}
|