126 lines
3.1 KiB
Go
126 lines
3.1 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/utils"
|
|
chore "lone-services/rpc/chore/pb"
|
|
"lone-services/services/store/internal/dao"
|
|
"lone-services/services/store/internal/model"
|
|
"lone-services/services/store/validator"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"lone-services/rpc/store/pb"
|
|
"lone-services/services/store/internal/svc"
|
|
|
|
"github.com/samber/lo"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type WebLowerStoreItemsLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
BaseLogic
|
|
}
|
|
|
|
func NewWebLowerStoreItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WebLowerStoreItemsLogic {
|
|
return &WebLowerStoreItemsLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *WebLowerStoreItemsLogic) WebLowerStoreItems(in *store.WebLowerStoreItemsReq) (*store.Response, error) {
|
|
var v validator.ApiStoreItemsValidator
|
|
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)
|
|
}
|
|
if adminInfo.Type != dao.LoginTypeSale {
|
|
return l.fail(utils.ErrorNoLogin)
|
|
}
|
|
|
|
saleId := adminInfo.ID
|
|
if in.SaleId > utils.NumberZero {
|
|
saleId = in.SaleId
|
|
}
|
|
var info []dao.StoreWebInfo
|
|
w := modelbase.Params{
|
|
Page: int(in.Page),
|
|
Size: int(in.Size),
|
|
Order: "id desc",
|
|
Eq: map[string]string{"sale_id": strconv.FormatInt(saleId, utils.NumberTen)},
|
|
In: map[string][]string{"status": {utils.StringStatusOk, utils.StatusForbiddenString, strconv.Itoa(int(utils.StatusWaitPass))}},
|
|
}
|
|
w.Like = make(map[string]string)
|
|
if len(v.Name) > utils.NumberZero {
|
|
w.Like["name LIKE ? "] = "%%" + v.Name + "%%"
|
|
}
|
|
|
|
items, err := model.StoreModel{}.Init().Page(w, &info)
|
|
if err != nil {
|
|
l.Logger.Error(err)
|
|
return l.fail(utils.Fail)
|
|
}
|
|
|
|
l.setArea(&info)
|
|
|
|
items.Items = info
|
|
return l.ok(items)
|
|
}
|
|
|
|
func (l *WebLowerStoreItemsLogic) setArea(info *[]dao.StoreWebInfo) {
|
|
var ids []int64
|
|
for key, v := range *info {
|
|
mobile, aErr := utils.DecryptPhone(v.UserMobile)
|
|
if aErr == nil {
|
|
(*info)[key].UserMobile = mobile
|
|
}
|
|
strArr := strings.Split(v.District, utils.DecollatorComma)
|
|
for _, s := range strArr {
|
|
num, err := strconv.ParseInt(strings.TrimSpace(s), utils.NumberTen, utils.NumberSixtyFourth)
|
|
if err == nil {
|
|
ids = append(ids, num)
|
|
}
|
|
}
|
|
}
|
|
|
|
newIds := lo.Uniq(ids)
|
|
if len(newIds) > utils.NumberZero {
|
|
cli, err := svc.GetRpcClient(l.svcCtx.ChoreName)
|
|
if err != nil {
|
|
logx.Errorf("get chore err: %v", err)
|
|
return
|
|
}
|
|
|
|
client := chore.NewChoreClient(cli.Conn())
|
|
|
|
regions, err := client.RegionsByIds(context.Background(), &chore.RegionsByIdsReq{
|
|
Ids: newIds,
|
|
})
|
|
if err != nil {
|
|
logx.Errorf("get chore err: %v", err)
|
|
return
|
|
}
|
|
|
|
areaMap := make(map[string]string)
|
|
for _, item := range regions.Items {
|
|
areaMap[strconv.Itoa(int(item.Id))] = item.Name
|
|
}
|
|
|
|
for key, item := range *info {
|
|
for _, id := range strings.Split(item.District, utils.DecollatorComma) {
|
|
if m, ok := areaMap[id]; ok {
|
|
(*info)[key].DistrictNames = (*info)[key].DistrictNames + " " + m
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|