105 lines
2.5 KiB
Go
105 lines
2.5 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/services/equipment/internal/dao"
|
|
"lone-services/services/equipment/internal/model"
|
|
"lone-services/services/equipment/validator"
|
|
"strconv"
|
|
"time"
|
|
|
|
"lone-services/rpc/equipment/pb"
|
|
"lone-services/services/equipment/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type ApiBindExpiryLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewApiBindExpiryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApiBindExpiryLogic {
|
|
return &ApiBindExpiryLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *ApiBindExpiryLogic) ApiBindExpiry(in *equipment.ApiBindExpiryRequest) (*equipment.Response, error) {
|
|
var v validator.BindExpiryValidator
|
|
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)
|
|
}
|
|
|
|
var info dao.BindExpiry
|
|
w := modelbase.Params{
|
|
Eq: map[string]string{
|
|
"host_code": in.HostCode,
|
|
"user_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
|
|
"status": utils.StringStatusOk,
|
|
"type": strconv.Itoa(int(in.EquipmentType))},
|
|
}
|
|
|
|
modelObj := model.BindModel{}.Init()
|
|
err := modelObj.GetOne(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
if info.Id < utils.NumberOne {
|
|
return l.fail(utils.SetError(utils.ErrorNotFund, "没有找到数据或没有绑定用户"))
|
|
}
|
|
|
|
if len(info.ExpiryTime) > utils.NumberOne {
|
|
return l.ok(info.ExpiryTime)
|
|
}
|
|
|
|
ww := modelbase.Params{
|
|
Order: "id desc",
|
|
Eq: map[string]string{
|
|
"host_code": v.HostCode,
|
|
"user_id": strconv.FormatInt(adminInfo.ID, utils.NumberTen),
|
|
"status": utils.StatusFailString},
|
|
}
|
|
|
|
var info2 dao.BindExpiry
|
|
err = modelObj.GetOne(ww, &info2)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
if info2.Id < utils.NumberOne {
|
|
if in.Type == utils.NumberOne {
|
|
info.ExpiryTime = "9999-12-31"
|
|
} else {
|
|
now := time.Now()
|
|
oneYearLater := now.AddDate(1, 0, 0)
|
|
info.ExpiryTime = oneYearLater.Format(time.DateOnly)
|
|
}
|
|
} else {
|
|
info.ExpiryTime = info2.ExpiryTime
|
|
}
|
|
|
|
w = modelbase.Params{
|
|
Eq: map[string]string{"id": strconv.FormatInt(info.Id, utils.NumberTen)},
|
|
}
|
|
_, err = modelObj.Edit(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
return l.ok(info.ExpiryTime)
|
|
}
|