feat: 迁移plant
This commit is contained in:
@@ -5,6 +5,7 @@ package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
@@ -27,8 +28,8 @@ func NewAiChatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AiChatLogi
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AiChatLogic) AiChat(req *types.AiChatReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *AiChatLogic) AiChat(req *types.AiChatReq) (string, error) {
|
||||
l.Logger.Infof("AI chat request: %s", req.Question)
|
||||
userID := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return ChatCompletion(l.ctx, l.svcCtx, userID, req.Question)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type ClearAiChatHistoryLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewClearAiChatHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ClearAiChatHistoryLogic {
|
||||
return &ClearAiChatHistoryLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *ClearAiChatHistoryLogic) ClearAiChatHistory() error {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_, err := l.svcCtx.PlantRpc.ClearAiChatHistory(l.ctx, &plantPb.GetProfileReq{UserId: userId})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type DeleteAiChatHistoryLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteAiChatHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteAiChatHistoryLogic {
|
||||
return &DeleteAiChatHistoryLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteAiChatHistoryLogic) DeleteAiChatHistory(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.DeleteAiChatHistory(l.ctx, &plantPb.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
@@ -5,6 +5,7 @@ package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
@@ -25,8 +26,10 @@ func NewGetAiChatHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetAiChatHistoryLogic) GetAiChatHistory() error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
// GetAiChatHistory TODO: 完善 AI 聊天历史查询
|
||||
func (l *GetAiChatHistoryLogic) GetAiChatHistory() (interface{}, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
l.Logger.Infof("get ai chat history for user: %s", userId)
|
||||
// 待实现:从 sundynix_ai_chat_history 表分页查询
|
||||
return map[string]interface{}{"list": []interface{}{}}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetAiChatQuotaLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetAiChatQuotaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAiChatQuotaLogic {
|
||||
return &GetAiChatQuotaLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetAiChatQuotaLogic) GetAiChatQuota() (*plantPb.AiQuotaResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetAiChatQuota(l.ctx, &plantPb.GetProfileReq{UserId: userId})
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
package ai
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type chatMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type chatRequest struct {
|
||||
Model string `json:"model,omitempty"`
|
||||
Messages []chatMessage `json:"messages"`
|
||||
Stream bool `json:"stream"`
|
||||
}
|
||||
|
||||
func chatModel(svcCtx *svc.ServiceContext) string {
|
||||
if svcCtx.Config.Ai.ChatModelName != "" {
|
||||
return svcCtx.Config.Ai.ChatModelName
|
||||
}
|
||||
return "gpt-4o-mini"
|
||||
}
|
||||
|
||||
func requestBody(svcCtx *svc.ServiceContext, question string, stream bool) ([]byte, error) {
|
||||
systemPrompt := "你是一个专业的植物百科助手。回答规则:基于知识库信息回答,不够则结合通用知识;不要使用 Markdown;用纯文本分段;回答简洁专业、条理清晰。"
|
||||
if ctxText := retrieveRAGContext(context.Background(), svcCtx, question); ctxText != "" {
|
||||
systemPrompt += "\n--- 知识库 ---\n" + ctxText + "\n--------------"
|
||||
}
|
||||
return json.Marshal(chatRequest{
|
||||
Model: chatModel(svcCtx),
|
||||
Messages: []chatMessage{
|
||||
{Role: "system", Content: systemPrompt},
|
||||
{Role: "user", Content: question},
|
||||
},
|
||||
Stream: stream,
|
||||
})
|
||||
}
|
||||
|
||||
func retrieveRAGContext(ctx context.Context, svcCtx *svc.ServiceContext, question string) string {
|
||||
c := svcCtx.Config.Ai
|
||||
if c.EmbeddingApiUrl == "" || c.EmbeddingApiKey == "" || c.QdrantUrl == "" || c.QdrantCollection == "" {
|
||||
return ""
|
||||
}
|
||||
body, _ := json.Marshal(map[string]interface{}{
|
||||
"model": c.EmbeddingModelName,
|
||||
"input": question,
|
||||
})
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.EmbeddingApiUrl, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+c.EmbeddingApiKey)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var emb struct {
|
||||
Data []struct {
|
||||
Embedding []float32 `json:"embedding"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 || json.NewDecoder(resp.Body).Decode(&emb) != nil || len(emb.Data) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
searchBody, _ := json.Marshal(map[string]interface{}{
|
||||
"vector": emb.Data[0].Embedding,
|
||||
"limit": 3,
|
||||
"with_payload": true,
|
||||
})
|
||||
searchReq, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(c.QdrantUrl, "/")+"/collections/"+c.QdrantCollection+"/points/search", bytes.NewReader(searchBody))
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
searchReq.Header.Set("Content-Type", "application/json")
|
||||
if c.QdrantApiKey != "" {
|
||||
searchReq.Header.Set("api-key", c.QdrantApiKey)
|
||||
}
|
||||
searchResp, err := http.DefaultClient.Do(searchReq)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer searchResp.Body.Close()
|
||||
var parsed struct {
|
||||
Result []struct {
|
||||
Payload map[string]interface{} `json:"payload"`
|
||||
} `json:"result"`
|
||||
}
|
||||
if searchResp.StatusCode < 200 || searchResp.StatusCode >= 300 || json.NewDecoder(searchResp.Body).Decode(&parsed) != nil {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
for _, item := range parsed.Result {
|
||||
if text, ok := item.Payload["full_text"].(string); ok && text != "" {
|
||||
b.WriteString(text)
|
||||
b.WriteString("\n")
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func newChatRequest(ctx context.Context, svcCtx *svc.ServiceContext, body []byte) (*http.Request, error) {
|
||||
if svcCtx.Config.Ai.ChatApiUrl == "" || svcCtx.Config.Ai.ChatApiKey == "" {
|
||||
return nil, errors.New("AI/RAG 未配置 ChatApiUrl 或 ChatApiKey")
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, svcCtx.Config.Ai.ChatApiUrl, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+svcCtx.Config.Ai.ChatApiKey)
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func SaveHistory(ctx context.Context, svcCtx *svc.ServiceContext, userID, question, answer string) {
|
||||
if userID == "" || question == "" || answer == "" {
|
||||
return
|
||||
}
|
||||
_, _ = svcCtx.PlantRpc.SaveAiChatHistory(ctx, &plantPb.SaveAiChatHistoryReq{
|
||||
UserId: userID, Question: question, Answer: answer,
|
||||
})
|
||||
}
|
||||
|
||||
func ChatCompletion(ctx context.Context, svcCtx *svc.ServiceContext, userID, question string) (string, error) {
|
||||
if err := ensureQuota(ctx, svcCtx, userID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
body, err := requestBody(svcCtx, question, false)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req, err := newChatRequest(ctx, svcCtx, body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
return "", fmt.Errorf("AI 请求失败: %s %s", resp.Status, strings.TrimSpace(string(raw)))
|
||||
}
|
||||
|
||||
var parsed struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(parsed.Choices) == 0 || parsed.Choices[0].Message.Content == "" {
|
||||
return "", errors.New("AI 响应为空")
|
||||
}
|
||||
answer := parsed.Choices[0].Message.Content
|
||||
SaveHistory(ctx, svcCtx, userID, question, answer)
|
||||
return answer, nil
|
||||
}
|
||||
|
||||
func StreamChat(ctx context.Context, svcCtx *svc.ServiceContext, userID, question string, w io.Writer) error {
|
||||
if err := ensureQuota(ctx, svcCtx, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
body, err := requestBody(svcCtx, question, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := newChatRequest(ctx, svcCtx, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
raw, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
return fmt.Errorf("AI 请求失败: %s %s", resp.Status, strings.TrimSpace(string(raw)))
|
||||
}
|
||||
|
||||
var answer strings.Builder
|
||||
scanner := bufio.NewScanner(resp.Body)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
_, _ = fmt.Fprintln(w, line)
|
||||
if flusher, ok := w.(http.Flusher); ok {
|
||||
flusher.Flush()
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(line, "data: ") {
|
||||
continue
|
||||
}
|
||||
data := strings.TrimSpace(strings.TrimPrefix(line, "data: "))
|
||||
if data == "[DONE]" {
|
||||
continue
|
||||
}
|
||||
var chunk struct {
|
||||
Choices []struct {
|
||||
Delta struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"delta"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
if json.Unmarshal([]byte(data), &chunk) == nil && len(chunk.Choices) > 0 {
|
||||
answer.WriteString(chunk.Choices[0].Delta.Content)
|
||||
}
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
SaveHistory(ctx, svcCtx, userID, question, answer.String())
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureQuota(ctx context.Context, svcCtx *svc.ServiceContext, userID string) error {
|
||||
if userID == "" {
|
||||
return nil
|
||||
}
|
||||
quota, err := svcCtx.PlantRpc.GetAiChatQuota(ctx, &plantPb.GetProfileReq{UserId: userID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if quota.Limit > 0 && quota.Remaining <= 0 {
|
||||
return fmt.Errorf("今日问答次数已达上限(%d次),明天再来吧", quota.Limit)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CreateBannerLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCreateBannerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateBannerLogic {
|
||||
return &CreateBannerLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *CreateBannerLogic) CreateBanner(req *types.CreateBannerReq) error {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_ = userId
|
||||
banner := plantModel.Banner{
|
||||
Title: req.Title,
|
||||
ImageID: req.ImageId,
|
||||
Sort: req.Sort,
|
||||
IsActive: req.IsActive,
|
||||
TargetURL: req.TargetUrl,
|
||||
}
|
||||
if banner.IsActive == 0 {
|
||||
banner.IsActive = 1
|
||||
}
|
||||
return l.svcCtx.DB.Create(&banner).Error
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeleteBannerLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteBannerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteBannerLogic {
|
||||
return &DeleteBannerLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteBannerLogic) DeleteBanner(req *types.IdReq) error {
|
||||
return l.svcCtx.DB.Where("id = ?", req.Id).Delete(&plantModel.Banner{}).Error
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/file/rpc/fileservice"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetActiveBannerListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetActiveBannerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetActiveBannerListLogic {
|
||||
return &GetActiveBannerListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetActiveBannerListLogic) GetActiveBannerList() (interface{}, error) {
|
||||
var list []plantModel.Banner
|
||||
l.svcCtx.DB.Model(&plantModel.Banner{}).
|
||||
Where("is_active = 1").
|
||||
Order("sort asc, created_at desc").
|
||||
Find(&list)
|
||||
|
||||
// Collect image IDs for batch fetch
|
||||
var imageIds []string
|
||||
for _, item := range list {
|
||||
if item.ImageID != "" {
|
||||
imageIds = append(imageIds, item.ImageID)
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch image URLs from file service
|
||||
imageUrlMap := make(map[string]string)
|
||||
if len(imageIds) > 0 {
|
||||
resp, err := l.svcCtx.FileRpc.GetFilesByIds(l.ctx, &fileservice.GetFilesByIdsReq{Ids: imageIds})
|
||||
if err != nil {
|
||||
logx.Errorf("Fetch banner image files failed: %v", err)
|
||||
} else if resp != nil {
|
||||
for _, f := range resp.Files {
|
||||
if f != nil && f.Url != "" {
|
||||
imageUrlMap[f.Id] = f.Url
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type BannerItem struct {
|
||||
Id string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
ImageId string `json:"imageId"`
|
||||
ImageUrl string `json:"imageUrl"`
|
||||
Sort int `json:"sort"`
|
||||
IsActive int `json:"isActive"`
|
||||
TargetUrl string `json:"targetUrl"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
var result []BannerItem
|
||||
for _, item := range list {
|
||||
imageUrl := ""
|
||||
if u, ok := imageUrlMap[item.ImageID]; ok {
|
||||
imageUrl = u
|
||||
}
|
||||
createdAt := ""
|
||||
if !item.CreatedAt.IsZero() {
|
||||
createdAt = item.CreatedAt.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
result = append(result, BannerItem{
|
||||
Id: item.ID,
|
||||
Title: item.Title,
|
||||
ImageId: item.ImageID,
|
||||
ImageUrl: imageUrl,
|
||||
Sort: item.Sort,
|
||||
IsActive: item.IsActive,
|
||||
TargetUrl: item.TargetURL,
|
||||
CreatedAt: createdAt,
|
||||
})
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"list": result,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetBannerListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetBannerListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBannerListLogic {
|
||||
return &GetBannerListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetBannerListLogic) GetBannerList(req *types.BannerListReq) (interface{}, error) {
|
||||
db := l.svcCtx.DB.Model(&plantModel.Banner{})
|
||||
if req.Title != "" {
|
||||
db = db.Where("title LIKE ?", "%"+req.Title+"%")
|
||||
}
|
||||
if req.IsActive > 0 {
|
||||
db = db.Where("is_active = ?", req.IsActive)
|
||||
}
|
||||
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
pageSize := req.PageSize
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
current := req.Current
|
||||
if current <= 0 {
|
||||
current = 1
|
||||
}
|
||||
offset := (current - 1) * pageSize
|
||||
|
||||
var list []plantModel.Banner
|
||||
db.Order("sort asc, created_at desc").Limit(pageSize).Offset(offset).Find(&list)
|
||||
|
||||
type BannerItem struct {
|
||||
Id string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
ImageId string `json:"imageId"`
|
||||
Sort int `json:"sort"`
|
||||
IsActive int `json:"isActive"`
|
||||
TargetUrl string `json:"targetUrl"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
}
|
||||
var result []BannerItem
|
||||
for _, item := range list {
|
||||
result = append(result, BannerItem{
|
||||
Id: item.ID,
|
||||
Title: item.Title,
|
||||
ImageId: item.ImageID,
|
||||
Sort: item.Sort,
|
||||
IsActive: item.IsActive,
|
||||
TargetUrl: item.TargetURL,
|
||||
CreatedAt: item.CreatedAt.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"list": result,
|
||||
"total": total,
|
||||
"page": current,
|
||||
"pageSize": pageSize,
|
||||
"totalPage": math.Ceil(float64(total) / float64(pageSize)),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package banner
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateBannerLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateBannerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateBannerLogic {
|
||||
return &UpdateBannerLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateBannerLogic) UpdateBanner(req *types.UpdateBannerReq) error {
|
||||
updates := map[string]interface{}{}
|
||||
if req.Title != "" {
|
||||
updates["title"] = req.Title
|
||||
}
|
||||
if req.ImageId != "" {
|
||||
updates["image_id"] = req.ImageId
|
||||
}
|
||||
if req.Sort != 0 {
|
||||
updates["sort"] = req.Sort
|
||||
}
|
||||
if req.IsActive != 0 {
|
||||
updates["is_active"] = req.IsActive
|
||||
}
|
||||
if req.TargetUrl != "" {
|
||||
updates["target_url"] = req.TargetUrl
|
||||
}
|
||||
return l.svcCtx.DB.Model(&plantModel.Banner{}).Where("id = ?", req.Id).Updates(updates).Error
|
||||
}
|
||||
@@ -5,6 +5,9 @@ package callback
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
@@ -25,8 +28,52 @@ func NewWechatPayCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *WechatPayCallbackLogic) WechatPayCallback() error {
|
||||
// todo: add your logic here and delete this line
|
||||
// wechatNotify 微信支付回调通知结构
|
||||
type wechatNotify struct {
|
||||
Id string `json:"id"`
|
||||
EventType string `json:"event_type"`
|
||||
Summary string `json:"summary"`
|
||||
ResourceType string `json:"resource_type"`
|
||||
Resource struct {
|
||||
Algorithm string `json:"algorithm"`
|
||||
Ciphertext string `json:"ciphertext"`
|
||||
AssociatedData string `json:"associated_data"`
|
||||
Nonce string `json:"nonce"`
|
||||
} `json:"resource"`
|
||||
}
|
||||
|
||||
// WechatPayCallback 微信支付回调处理
|
||||
// TODO: 接入 wechatpay-go SDK 完成验签 + 解密 + 订单状态更新
|
||||
//
|
||||
// 完整接入步骤:
|
||||
// 1. go get github.com/wechatpay-apiv3/wechatpay-go
|
||||
// 2. 在 config 中添加 WechatPay{ MchId, MchAPIv3Key, PrivateKeyPath, PublicKeyId, PublicKeyPath }
|
||||
// 3. 使用 notify.NewRSANotifyHandler 验签解密
|
||||
// 4. 解析 payments.Transaction 后根据 TradeState == "SUCCESS" 更新 sundynix_exchange_order 状态
|
||||
func (l *WechatPayCallbackLogic) WechatPayCallback(r *http.Request) error {
|
||||
// 1. 读取请求体
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
l.Logger.Errorf("[微信支付回调] 读取请求体失败: %v", err)
|
||||
return err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
// 2. 解析通知基本结构(不验签,记录日志)
|
||||
var notify wechatNotify
|
||||
if err = json.Unmarshal(body, ¬ify); err != nil {
|
||||
l.Logger.Errorf("[微信支付回调] 解析通知失败: %v, body: %s", err, string(body))
|
||||
return err
|
||||
}
|
||||
|
||||
l.Logger.Infof("[微信支付回调] 收到通知 id=%s, event=%s, summary=%s",
|
||||
notify.Id, notify.EventType, notify.Summary)
|
||||
|
||||
// TODO: 完成验签 + 解密 + 业务处理
|
||||
// if notify.EventType == "TRANSACTION.SUCCESS" {
|
||||
// transaction := decrypt(notify.Resource, mchAPIv3Key)
|
||||
// updateOrderStatus(transaction.OutTradeNo, "SUCCESS")
|
||||
// }
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package complete
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type CompleteTaskLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCompleteTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CompleteTaskLogic {
|
||||
return &CompleteTaskLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *CompleteTaskLogic) CompleteTask(req *types.CompleteTaskApiReq) (*plantPb.TaskCompletionResult, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.CompleteTask(l.ctx, &plantPb.CompleteTaskReq{
|
||||
UserId: userId, TaskId: req.TaskId, Remark: req.Remark,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package complete
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetAiChatHistoryLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetAiChatHistoryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetAiChatHistoryLogic {
|
||||
return &GetAiChatHistoryLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetAiChatHistoryLogic) GetAiChatHistory(req *types.PageReq) (*plantPb.AiChatHistoryResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetAiChatHistory(l.ctx, &plantPb.AiChatHistoryReq{
|
||||
UserId: userId, Current: int32(req.Current), PageSize: int32(req.PageSize),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package complete
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetBadgeConfigTreeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetBadgeConfigTreeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetBadgeConfigTreeLogic {
|
||||
return &GetBadgeConfigTreeLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetBadgeConfigTreeLogic) GetBadgeConfigTree() (*plantPb.BadgeConfigTreeResp, error) {
|
||||
return l.svcCtx.PlantRpc.GetBadgeConfigTree(l.ctx, &plantPb.IdReq{})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package complete
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetExchangeItemDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetExchangeItemDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetExchangeItemDetailLogic {
|
||||
return &GetExchangeItemDetailLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetExchangeItemDetailLogic) GetExchangeItemDetail(req *types.IdPathReq) (*plantPb.ExchangeItemInfo, error) {
|
||||
return l.svcCtx.PlantRpc.GetExchangeItemDetail(l.ctx, &plantPb.IdReq{Id: req.Id})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package complete
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetLevelConfigDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetLevelConfigDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLevelConfigDetailLogic {
|
||||
return &GetLevelConfigDetailLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetLevelConfigDetailLogic) GetLevelConfigDetail(req *types.IdPathReq) (*plantPb.LevelConfigInfo, error) {
|
||||
return l.svcCtx.PlantRpc.GetLevelConfigDetail(l.ctx, &plantPb.IdReq{Id: req.Id})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package complete
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetWikiClassDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetWikiClassDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWikiClassDetailLogic {
|
||||
return &GetWikiClassDetailLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetWikiClassDetailLogic) GetWikiClassDetail(req *types.IdPathReq) (*plantPb.WikiClassInfo, error) {
|
||||
return l.svcCtx.PlantRpc.GetWikiClassDetail(l.ctx, &plantPb.IdReq{Id: req.Id})
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type CreateBadgeConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCreateBadgeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateBadgeConfigLogic {
|
||||
return &CreateBadgeConfigLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *CreateBadgeConfigLogic) CreateBadgeConfig(req *types.CreateBadgeConfigReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.CreateBadgeConfig(l.ctx, &plantPb.CreateBadgeConfigReq{
|
||||
Name: req.Name, Description: req.Description, Dimension: req.Dimension,
|
||||
GroupId: req.GroupId, Tier: int32(req.Tier), TargetAction: req.TargetAction,
|
||||
Threshold: req.Threshold, RewardSunlight: req.RewardSunlight, IconId: req.IconId, Sort: int32(req.Sort),
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type CreateLevelConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCreateLevelConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateLevelConfigLogic {
|
||||
return &CreateLevelConfigLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *CreateLevelConfigLogic) CreateLevelConfig(req *types.CreateLevelConfigReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.CreateLevelConfig(l.ctx, &plantPb.CreateLevelConfigReq{
|
||||
Level: int32(req.Level), Title: req.Title, MinSunlight: req.MinSunlight, Perks: req.Perks,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type DeleteBadgeConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteBadgeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteBadgeConfigLogic {
|
||||
return &DeleteBadgeConfigLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteBadgeConfigLogic) DeleteBadgeConfig(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.DeleteBadgeConfig(l.ctx, &plantPb.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type DeleteLevelConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteLevelConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLevelConfigLogic {
|
||||
return &DeleteLevelConfigLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteLevelConfigLogic) DeleteLevelConfig(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.DeleteLevelConfig(l.ctx, &plantPb.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +28,17 @@ func NewGetBadgeConfigListLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetBadgeConfigListLogic) GetBadgeConfigList(req *types.BadgeConfigListReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetBadgeConfigListLogic) GetBadgeConfigList(req *types.BadgeConfigListReq) (interface{}, error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetBadgeConfigList(l.ctx, &plant.BadgeConfigListReq{
|
||||
Current: int32(req.Current),
|
||||
PageSize: int32(req.PageSize),
|
||||
Dimension: req.Dimension,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"list": result.List,
|
||||
"total": result.Total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +28,15 @@ func NewGetLevelConfigListLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetLevelConfigListLogic) GetLevelConfigList(req *types.LevelConfigListReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetLevelConfigListLogic) GetLevelConfigList(req *types.LevelConfigListReq) (interface{}, error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetLevelConfigList(l.ctx, &plant.PageReq{
|
||||
Current: int32(req.Current),
|
||||
PageSize: int32(req.PageSize),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"list": result.List,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type UpdateBadgeConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateBadgeConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateBadgeConfigLogic {
|
||||
return &UpdateBadgeConfigLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateBadgeConfigLogic) UpdateBadgeConfig(req *types.UpdateBadgeConfigReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.UpdateBadgeConfig(l.ctx, &plantPb.UpdateBadgeConfigReq{
|
||||
Id: req.Id, Name: req.Name, Description: req.Description, Dimension: req.Dimension,
|
||||
GroupId: req.GroupId, Tier: int32(req.Tier), TargetAction: req.TargetAction,
|
||||
Threshold: req.Threshold, RewardSunlight: req.RewardSunlight, IconId: req.IconId, Sort: int32(req.Sort),
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type UpdateLevelConfigLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateLevelConfigLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLevelConfigLogic {
|
||||
return &UpdateLevelConfigLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateLevelConfigLogic) UpdateLevelConfig(req *types.UpdateLevelConfigReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.UpdateLevelConfig(l.ctx, &plantPb.UpdateLevelConfigReq{
|
||||
Id: req.Id, Level: int32(req.Level), Title: req.Title, MinSunlight: req.MinSunlight, Perks: req.Perks,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -5,9 +5,11 @@ package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +30,14 @@ func NewCreateExchangeOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func (l *CreateExchangeOrderLogic) CreateExchangeOrder(req *types.ExchangeOrderReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_, err := l.svcCtx.PlantRpc.CreateExchangeOrder(l.ctx, &plant.CreateExchangeOrderReq{
|
||||
UserId: userId,
|
||||
ItemId: req.ItemId,
|
||||
Quantity: int32(req.Quantity),
|
||||
RecipientName: req.RecipientName,
|
||||
Phone: req.Phone,
|
||||
Address: req.Address,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type CreateExchangeItemLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewCreateExchangeItemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateExchangeItemLogic {
|
||||
return &CreateExchangeItemLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *CreateExchangeItemLogic) CreateExchangeItem(req *types.CreateExchangeItemReq) error {
|
||||
desc := req.Desc
|
||||
if desc == "" {
|
||||
desc = req.Description
|
||||
}
|
||||
imgID := req.ImgId
|
||||
if imgID == "" {
|
||||
imgID = req.ImageId
|
||||
}
|
||||
cost := req.Cost
|
||||
if cost == 0 {
|
||||
cost = req.CostSunlight
|
||||
}
|
||||
_, err := l.svcCtx.PlantRpc.CreateExchangeItem(l.ctx, &plantPb.CreateExchangeItemReq{
|
||||
Name: req.Name, Desc: desc, ImgId: imgID, Cost: cost, Stock: int32(req.Stock),
|
||||
Type: req.Type, CostSunlight: req.CostSunlight, LimitPerUser: int32(req.LimitPerUser),
|
||||
Sort: int32(req.Sort), StartTime: req.StartTime, EndTime: req.EndTime, ImageId: req.ImageId,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type DeleteExchangeItemLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteExchangeItemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteExchangeItemLogic {
|
||||
return &DeleteExchangeItemLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteExchangeItemLogic) DeleteExchangeItem(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.DeleteExchangeItem(l.ctx, &plantPb.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +28,16 @@ func NewGetExchangeItemListLogic(ctx context.Context, svcCtx *svc.ServiceContext
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetExchangeItemListLogic) GetExchangeItemList(req *types.ExchangeItemListReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetExchangeItemListLogic) GetExchangeItemList(req *types.ExchangeItemListReq) (interface{}, error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetExchangeItemList(l.ctx, &plant.ExchangeItemListReq{
|
||||
Current: int32(req.Current),
|
||||
PageSize: int32(req.PageSize),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"list": result.List,
|
||||
"total": result.Total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetExchangeOrderListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetExchangeOrderListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetExchangeOrderListLogic {
|
||||
return &GetExchangeOrderListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetExchangeOrderListLogic) GetExchangeOrderList(req *types.ExchangeOrderListReq) (*plantPb.ExchangeOrderListResp, error) {
|
||||
return l.svcCtx.PlantRpc.GetExchangeOrderList(l.ctx, &plantPb.ExchangeOrderListReq{
|
||||
UserId: req.UserId, Current: int32(req.Current), PageSize: int32(req.PageSize), Status: int32(req.Status),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetMyExchangeOrdersLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetMyExchangeOrdersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyExchangeOrdersLogic {
|
||||
return &GetMyExchangeOrdersLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetMyExchangeOrdersLogic) GetMyExchangeOrders(req *types.ExchangeOrderListReq) (*plantPb.ExchangeOrderListResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetMyExchangeOrders(l.ctx, &plantPb.ExchangeOrderListReq{
|
||||
UserId: userId, Current: int32(req.Current), PageSize: int32(req.PageSize), Status: int32(req.Status),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type UpdateExchangeItemLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateExchangeItemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateExchangeItemLogic {
|
||||
return &UpdateExchangeItemLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateExchangeItemLogic) UpdateExchangeItem(req *types.UpdateExchangeItemReq) error {
|
||||
desc := req.Desc
|
||||
if desc == "" {
|
||||
desc = req.Description
|
||||
}
|
||||
imgID := req.ImgId
|
||||
if imgID == "" {
|
||||
imgID = req.ImageId
|
||||
}
|
||||
cost := req.Cost
|
||||
if cost == 0 {
|
||||
cost = req.CostSunlight
|
||||
}
|
||||
_, err := l.svcCtx.PlantRpc.UpdateExchangeItem(l.ctx, &plantPb.UpdateExchangeItemReq{
|
||||
Id: req.Id, Name: req.Name, Desc: desc, ImgId: imgID,
|
||||
Cost: cost, Stock: int32(req.Stock), Status: int32(req.Status),
|
||||
Type: req.Type, CostSunlight: req.CostSunlight, LimitPerUser: int32(req.LimitPerUser),
|
||||
Sort: int32(req.Sort), StartTime: req.StartTime, EndTime: req.EndTime, ImageId: req.ImageId,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package exchange
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type UpdateExchangeOrderLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateExchangeOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateExchangeOrderLogic {
|
||||
return &UpdateExchangeOrderLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateExchangeOrderLogic) UpdateExchangeOrder(req *types.UpdateExchangeOrderReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.UpdateExchangeOrder(l.ctx, &plantPb.UpdateExchangeOrderReq{
|
||||
Id: req.Id, Status: int32(req.Status), TrackingNo: req.TrackingNo, Remark: req.Remark,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -21,10 +21,35 @@ func NewCreatePlantLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Creat
|
||||
|
||||
func (l *CreatePlantLogic) CreatePlant(req *types.CreatePlantReq) error {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_, err := l.svcCtx.PlantRpc.CreatePlant(l.ctx, &plant.CreatePlantReq{
|
||||
imgIds := req.ImgIds
|
||||
if len(imgIds) == 0 {
|
||||
imgIds = req.OssIds
|
||||
}
|
||||
resp, err := l.svcCtx.PlantRpc.CreatePlant(l.ctx, &plant.CreatePlantReq{
|
||||
UserId: userId, Name: req.Name, PlantTime: req.PlantTime, Placement: req.Placement,
|
||||
PotMaterial: req.PotMaterial, PotSize: req.PotSize, Sunlight: req.Sunlight,
|
||||
PlantingMaterial: req.PlantingMaterial, ImgIds: req.ImgIds,
|
||||
PlantingMaterial: req.PlantingMaterial, ImgIds: imgIds,
|
||||
})
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp == nil || resp.Msg == "" || resp.Msg == "ok" {
|
||||
return nil
|
||||
}
|
||||
for _, planReq := range req.CarePlans {
|
||||
if planReq.Name == "" || planReq.Period <= 0 {
|
||||
continue
|
||||
}
|
||||
if _, err = l.svcCtx.PlantRpc.AddCarePlan(l.ctx, &plant.AddCarePlanReq{
|
||||
UserId: userId,
|
||||
PlantId: resp.Msg,
|
||||
Name: planReq.Name,
|
||||
Icon: planReq.Icon,
|
||||
TargetAction: planReq.TargetAction,
|
||||
Period: int32(planReq.Period),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package myPlant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type DeleteCarePlanLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteCarePlanLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteCarePlanLogic {
|
||||
return &DeleteCarePlanLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteCarePlanLogic) DeleteCarePlan(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.DeleteCarePlan(l.ctx, &plantPb.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
@@ -3,10 +3,12 @@ package myPlant
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetMyPlantListLogic struct {
|
||||
@@ -19,13 +21,130 @@ func NewGetMyPlantListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
|
||||
return &GetMyPlantListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
type plantListResp struct {
|
||||
List interface{} `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"pageSize"`
|
||||
}
|
||||
|
||||
type PlantItem struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
CreatedAtStr string `json:"createdAtStr"`
|
||||
UserID string `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
PlantTime string `json:"plantTime"`
|
||||
Status int `json:"status"`
|
||||
Placement string `json:"placement"`
|
||||
PotMaterial string `json:"potMaterial"`
|
||||
PotSize string `json:"potSize"`
|
||||
Sunlight string `json:"sunlight"`
|
||||
PlantingMaterial string `json:"plantingMaterial"`
|
||||
ImgList []ImageInfo `json:"imgList"`
|
||||
CarePlans interface{} `json:"carePlans"`
|
||||
CareTasks interface{} `json:"careTasks"`
|
||||
CareRecords interface{} `json:"careRecords"`
|
||||
GrowthRecords interface{} `json:"growthRecords"`
|
||||
}
|
||||
|
||||
func (l *GetMyPlantListLogic) GetMyPlantList(req *types.PlantListReq) (resp interface{}, err error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
result, err := l.svcCtx.PlantRpc.GetPlantList(l.ctx, &plant.PlantListReq{
|
||||
UserId: userId, Current: int32(req.Current), PageSize: int32(req.PageSize), Name: req.Name,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
db := l.svcCtx.DB.Model(&plantModel.MyPlant{}).Where("user_id = ?", userId)
|
||||
if req.Name != "" {
|
||||
db = db.Where("name like ?", "%"+req.Name+"%")
|
||||
}
|
||||
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
pageSize := req.PageSize
|
||||
if pageSize <= 0 {
|
||||
pageSize = 20
|
||||
}
|
||||
page := req.Current
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
var plants []plantModel.MyPlant
|
||||
if err := db.Limit(pageSize).Offset(offset).Order("created_at desc").Find(&plants).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"list": result.List, "total": result.Total}, nil
|
||||
|
||||
// 查本地关联表获取图片 ID,再通过 FileRpc 获取完整信息
|
||||
imgMap := resolvePlantImages(l.ctx, l.svcCtx, plants)
|
||||
|
||||
var list []PlantItem
|
||||
for _, p := range plants {
|
||||
list = append(list, PlantItem{
|
||||
ID: p.ID,
|
||||
CreatedAt: FormatTime(p.CreatedAt),
|
||||
UpdatedAt: FormatTime(p.UpdatedAt),
|
||||
CreatedAtStr: FormatTimeShort(p.CreatedAt),
|
||||
UserID: p.UserID,
|
||||
Name: p.Name,
|
||||
PlantTime: FormatTime(p.PlantTime),
|
||||
Status: p.Status,
|
||||
Placement: p.Placement,
|
||||
PotMaterial: p.PotMaterial,
|
||||
PotSize: p.PotSize,
|
||||
Sunlight: p.Sunlight,
|
||||
PlantingMaterial: p.PlantingMaterial,
|
||||
ImgList: imgMap[p.ID],
|
||||
CarePlans: nil,
|
||||
CareTasks: nil,
|
||||
CareRecords: nil,
|
||||
GrowthRecords: nil,
|
||||
})
|
||||
}
|
||||
|
||||
return &plantListResp{List: list, Total: total, Page: page, PageSize: pageSize}, nil
|
||||
}
|
||||
|
||||
// resolvePlantImages 批量查询植物图片
|
||||
func resolvePlantImages(ctx context.Context, svcCtx *svc.ServiceContext, plants []plantModel.MyPlant) map[string][]ImageInfo {
|
||||
var plantIds []string
|
||||
for _, p := range plants {
|
||||
plantIds = append(plantIds, p.ID)
|
||||
}
|
||||
if len(plantIds) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
type rel struct {
|
||||
MyPlantID string `gorm:"column:sundynix_my_plant_id"`
|
||||
OssID string `gorm:"column:sundynix_oss_id"`
|
||||
}
|
||||
var rels []rel
|
||||
svcCtx.DB.Table("sundynix_plant_my_plant_oss").
|
||||
Where("sundynix_my_plant_id IN ?", plantIds).
|
||||
Order("sundynix_oss_id asc").
|
||||
Find(&rels)
|
||||
|
||||
var allOssIds []string
|
||||
pidMap := make(map[string][]string)
|
||||
for _, r := range rels {
|
||||
pidMap[r.MyPlantID] = append(pidMap[r.MyPlantID], r.OssID)
|
||||
allOssIds = append(allOssIds, r.OssID)
|
||||
}
|
||||
|
||||
fileInfos := FetchFilesByIds(ctx, svcCtx, allOssIds)
|
||||
result := make(map[string][]ImageInfo)
|
||||
for pid, ids := range pidMap {
|
||||
var imgs []ImageInfo
|
||||
for _, oid := range ids {
|
||||
if f, ok := fileInfos[oid]; ok {
|
||||
imgs = append(imgs, f)
|
||||
}
|
||||
}
|
||||
if imgs == nil {
|
||||
imgs = []ImageInfo{}
|
||||
}
|
||||
result[pid] = imgs
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -2,10 +2,12 @@ package myPlant
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
)
|
||||
|
||||
type GetPlantDetailLogic struct {
|
||||
@@ -18,10 +20,226 @@ func NewGetPlantDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
|
||||
return &GetPlantDetailLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
// PlantDetailItem 植物详情 DTO
|
||||
type PlantDetailItem struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
CreatedAtStr string `json:"createdAtStr"`
|
||||
UserID string `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
PlantTime string `json:"plantTime"`
|
||||
Status int `json:"status"`
|
||||
Placement string `json:"placement"`
|
||||
PotMaterial string `json:"potMaterial"`
|
||||
PotSize string `json:"potSize"`
|
||||
Sunlight string `json:"sunlight"`
|
||||
PlantingMaterial string `json:"plantingMaterial"`
|
||||
ImgList []ImageInfo `json:"imgList"`
|
||||
CarePlans []CarePlanItem `json:"carePlans"`
|
||||
CareTasks interface{} `json:"careTasks"`
|
||||
CareRecords []CareRecordItem `json:"careRecords"`
|
||||
GrowthRecords []GrowthRecordItem `json:"growthRecords"`
|
||||
}
|
||||
|
||||
type CarePlanItem struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
CreatedAtStr string `json:"createdAtStr"`
|
||||
UserID string `json:"userId"`
|
||||
PlantID string `json:"plantId"`
|
||||
Icon string `json:"icon"`
|
||||
Name string `json:"name"`
|
||||
Period int `json:"period"`
|
||||
TargetAction string `json:"targetAction"`
|
||||
}
|
||||
|
||||
type CareRecordItem struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
CreatedAtStr string `json:"createdAtStr"`
|
||||
UserID string `json:"userId"`
|
||||
PlantID string `json:"plantId"`
|
||||
PlanID string `json:"planId"`
|
||||
Name string `json:"name"`
|
||||
Remark string `json:"remark"`
|
||||
Icon string `json:"icon"`
|
||||
}
|
||||
|
||||
type GrowthRecordItem struct {
|
||||
ID string `json:"id"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
CreatedAtStr string `json:"createdAtStr"`
|
||||
PlantID string `json:"plantId"`
|
||||
UserID string `json:"userId"`
|
||||
Name string `json:"name"`
|
||||
Tag string `json:"tag"`
|
||||
Desc string `json:"desc"`
|
||||
Content string `json:"content"`
|
||||
ImgList []ImageInfo `json:"imgList"`
|
||||
}
|
||||
|
||||
func (l *GetPlantDetailLogic) GetPlantDetail(req *types.IdPathReq) (resp interface{}, err error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetPlantDetail(l.ctx, &plant.IdReq{Id: req.Id})
|
||||
var myPlant plantModel.MyPlant
|
||||
err = l.svcCtx.DB.
|
||||
Preload("CarePlans").
|
||||
Preload("CareRecords", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
}).
|
||||
Preload("GrowthRecords", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
}).
|
||||
Where("id = ?", req.Id).First(&myPlant).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
|
||||
// 查图片(本地关联表 + FileRpc)
|
||||
imgList := queryPlantImages(l.ctx, l.svcCtx, req.Id)
|
||||
|
||||
// 成长记录图片
|
||||
growthImgMap := resolveGrowthRecordImages(l.ctx, l.svcCtx, myPlant.GrowthRecords)
|
||||
|
||||
return &PlantDetailItem{
|
||||
ID: myPlant.ID,
|
||||
CreatedAt: FormatTime(myPlant.CreatedAt),
|
||||
UpdatedAt: FormatTime(myPlant.UpdatedAt),
|
||||
CreatedAtStr: FormatTimeShort(myPlant.CreatedAt),
|
||||
UserID: myPlant.UserID,
|
||||
Name: myPlant.Name,
|
||||
PlantTime: FormatTime(myPlant.PlantTime),
|
||||
Status: myPlant.Status,
|
||||
Placement: myPlant.Placement,
|
||||
PotMaterial: myPlant.PotMaterial,
|
||||
PotSize: myPlant.PotSize,
|
||||
Sunlight: myPlant.Sunlight,
|
||||
PlantingMaterial: myPlant.PlantingMaterial,
|
||||
ImgList: imgList,
|
||||
CarePlans: toCarePlanItems(myPlant.CarePlans),
|
||||
CareTasks: nil,
|
||||
CareRecords: toCareRecordItems(myPlant.CareRecords),
|
||||
GrowthRecords: toGrowthRecordItems(myPlant.GrowthRecords, growthImgMap),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func queryPlantImages(ctx context.Context, svcCtx *svc.ServiceContext, plantID string) []ImageInfo {
|
||||
type rel struct {
|
||||
OssID string `gorm:"column:sundynix_oss_id"`
|
||||
}
|
||||
var rels []rel
|
||||
svcCtx.DB.Table("sundynix_plant_my_plant_oss").
|
||||
Where("sundynix_my_plant_id = ?", plantID).
|
||||
Order("sundynix_oss_id asc").
|
||||
Find(&rels)
|
||||
|
||||
var ids []string
|
||||
for _, r := range rels {
|
||||
ids = append(ids, r.OssID)
|
||||
}
|
||||
infos := FetchFilesByIds(ctx, svcCtx, ids)
|
||||
var result []ImageInfo
|
||||
for _, id := range ids {
|
||||
if f, ok := infos[id]; ok {
|
||||
result = append(result, f)
|
||||
}
|
||||
}
|
||||
if result == nil {
|
||||
result = []ImageInfo{}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func resolveGrowthRecordImages(ctx context.Context, svcCtx *svc.ServiceContext, records []*plantModel.GrowthRecord) map[string][]ImageInfo {
|
||||
var ids []string
|
||||
for _, gr := range records {
|
||||
ids = append(ids, gr.ID)
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
type rel struct {
|
||||
GrowthRecordID string `gorm:"column:growth_record_id"`
|
||||
OssID string `gorm:"column:oss_id"`
|
||||
}
|
||||
var rels []rel
|
||||
svcCtx.DB.Table("sundynix_plant_growth_record_oss").
|
||||
Where("growth_record_id IN ?", ids).
|
||||
Find(&rels)
|
||||
|
||||
var allOssIds []string
|
||||
gidMap := make(map[string][]string)
|
||||
for _, r := range rels {
|
||||
gidMap[r.GrowthRecordID] = append(gidMap[r.GrowthRecordID], r.OssID)
|
||||
allOssIds = append(allOssIds, r.OssID)
|
||||
}
|
||||
|
||||
infos := FetchFilesByIds(ctx, svcCtx, allOssIds)
|
||||
result := make(map[string][]ImageInfo)
|
||||
for gid, ossIds := range gidMap {
|
||||
var imgs []ImageInfo
|
||||
for _, oid := range ossIds {
|
||||
if f, ok := infos[oid]; ok {
|
||||
imgs = append(imgs, f)
|
||||
}
|
||||
}
|
||||
result[gid] = imgs
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func toCarePlanItems(plans []*plantModel.CarePlan) []CarePlanItem {
|
||||
var items []CarePlanItem
|
||||
for _, p := range plans {
|
||||
items = append(items, CarePlanItem{
|
||||
ID: p.ID, CreatedAt: FormatTime(p.CreatedAt),
|
||||
UpdatedAt: FormatTime(p.UpdatedAt), CreatedAtStr: FormatTimeShort(p.CreatedAt),
|
||||
UserID: p.UserID, PlantID: p.PlantID, Icon: p.Icon, Name: p.Name,
|
||||
Period: p.Period, TargetAction: p.TargetAction,
|
||||
})
|
||||
}
|
||||
if items == nil {
|
||||
items = []CarePlanItem{}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func toCareRecordItems(records []*plantModel.CareRecord) []CareRecordItem {
|
||||
var items []CareRecordItem
|
||||
for _, r := range records {
|
||||
items = append(items, CareRecordItem{
|
||||
ID: r.ID, CreatedAt: FormatTime(r.CreatedAt),
|
||||
UpdatedAt: FormatTime(r.UpdatedAt), CreatedAtStr: FormatTimeShort(r.CreatedAt),
|
||||
UserID: r.UserID, PlantID: r.PlantID, PlanID: r.PlanID,
|
||||
Name: r.Name, Remark: r.Remark, Icon: r.Icon,
|
||||
})
|
||||
}
|
||||
if items == nil {
|
||||
items = []CareRecordItem{}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func toGrowthRecordItems(records []*plantModel.GrowthRecord, imgMap map[string][]ImageInfo) []GrowthRecordItem {
|
||||
var items []GrowthRecordItem
|
||||
for _, gr := range records {
|
||||
item := GrowthRecordItem{
|
||||
ID: gr.ID, CreatedAt: FormatTime(gr.CreatedAt),
|
||||
UpdatedAt: FormatTime(gr.UpdatedAt), CreatedAtStr: FormatTimeShort(gr.CreatedAt),
|
||||
PlantID: gr.PlantID, UserID: gr.UserID, Name: gr.Name,
|
||||
Tag: gr.Tag, Desc: gr.Desc, Content: gr.Content,
|
||||
ImgList: imgMap[gr.ID],
|
||||
}
|
||||
if item.ImgList == nil {
|
||||
item.ImgList = []ImageInfo{}
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if items == nil {
|
||||
items = []GrowthRecordItem{}
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package myPlant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetTodayTaskListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTodayTaskListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTodayTaskListLogic {
|
||||
return &GetTodayTaskListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetTodayTaskListLogic) GetTodayTaskList() (*plantPb.CareTaskListResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetTodayTaskList(l.ctx, &plantPb.GetProfileReq{UserId: userId})
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package myPlant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
filePb "sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
)
|
||||
|
||||
// ========== 图片 DTO ==========
|
||||
|
||||
// ImageInfo 图片信息 DTO,匹配旧单体 Oss 结构
|
||||
type ImageInfo struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Url string `json:"url"`
|
||||
Tag string `json:"tag"`
|
||||
Key string `json:"key"`
|
||||
Suffix string `json:"suffix"`
|
||||
MD5 string `json:"md5"`
|
||||
CreatedAt string `json:"createdAt"`
|
||||
UpdatedAt string `json:"updatedAt"`
|
||||
CreatedAtStr string `json:"createdAtStr"`
|
||||
}
|
||||
|
||||
// ========== 时间格式化 ==========
|
||||
|
||||
func FormatTime(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return ""
|
||||
}
|
||||
return t.Format(time.RFC3339)
|
||||
}
|
||||
|
||||
func FormatTimeShort(t time.Time) string {
|
||||
if t.IsZero() {
|
||||
return ""
|
||||
}
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func UnixToTimeStr(ts int64) string {
|
||||
if ts == 0 {
|
||||
return ""
|
||||
}
|
||||
return time.Unix(ts, 0).Format(time.RFC3339)
|
||||
}
|
||||
|
||||
func UnixToTimeStrShort(ts int64) string {
|
||||
if ts == 0 {
|
||||
return ""
|
||||
}
|
||||
return time.Unix(ts, 0).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// ========== 文件服务 RPC 封装 ==========
|
||||
|
||||
// FetchFilesByIds 通过 FileRpc 批量获取文件信息,返回 id->ImageInfo 映射
|
||||
func FetchFilesByIds(ctx context.Context, svcCtx *svc.ServiceContext, ids []string) map[string]ImageInfo {
|
||||
result := make(map[string]ImageInfo)
|
||||
if len(ids) == 0 {
|
||||
return result
|
||||
}
|
||||
resp, err := svcCtx.FileRpc.GetFilesByIds(ctx, &filePb.GetFilesByIdsReq{Ids: ids})
|
||||
if err != nil || resp == nil {
|
||||
return result
|
||||
}
|
||||
for _, f := range resp.Files {
|
||||
result[f.Id] = ImageInfo{
|
||||
ID: f.Id,
|
||||
Name: f.Name,
|
||||
Url: f.Url,
|
||||
Tag: f.Tag,
|
||||
Key: f.Key,
|
||||
Suffix: f.Suffix,
|
||||
MD5: f.Md5,
|
||||
CreatedAt: UnixToTimeStr(f.CreatedAt),
|
||||
UpdatedAt: UnixToTimeStr(f.CreatedAt),
|
||||
CreatedAtStr: UnixToTimeStrShort(f.CreatedAt),
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package myPlant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type QuickCareLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewQuickCareLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QuickCareLogic {
|
||||
return &QuickCareLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *QuickCareLogic) QuickCare(req *types.QuickCareReq) error {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
|
||||
return l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// 1. Validate plant exists and belongs to user
|
||||
var myPlant plantModel.MyPlant
|
||||
if err := tx.Where("id = ? AND user_id = ?", req.PlantId, userId).First(&myPlant).Error; err != nil {
|
||||
return errors.New("植物不存在")
|
||||
}
|
||||
|
||||
// 2. Create care record
|
||||
record := plantModel.CareRecord{
|
||||
UserID: userId,
|
||||
PlantID: req.PlantId,
|
||||
Name: req.Name,
|
||||
Icon: req.Icon,
|
||||
Remark: req.Remark,
|
||||
}
|
||||
if err := tx.Create(&record).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 3. Update user profile counter
|
||||
column := "care_count"
|
||||
if req.Icon != "" || req.Name != "" {
|
||||
nameMap := map[string]string{
|
||||
"浇水": "water_count",
|
||||
"施肥": "fertilize_count",
|
||||
"修剪": "prune_count",
|
||||
"换盆": "repot_count",
|
||||
}
|
||||
if col, ok := nameMap[req.Name]; ok {
|
||||
column = col
|
||||
} else if req.Icon != "" {
|
||||
actionMap := map[string]string{
|
||||
"water": "water_count", "fertilize": "fertilize_count",
|
||||
"prune": "prune_count", "repot": "repot_count",
|
||||
}
|
||||
for key, col := range actionMap {
|
||||
if strings.Contains(req.Icon, key) {
|
||||
column = col
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tx.Model(&plantModel.UserProfile{}).Where("user_id = ?", userId).
|
||||
Update(column, gorm.Expr(column+" + 1")).Error
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ocr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type DeleteClassifyLogLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteClassifyLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteClassifyLogLogic {
|
||||
return &DeleteClassifyLogLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteClassifyLogLogic) DeleteClassifyLog(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.DeleteClassifyLog(l.ctx, &plantPb.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ocr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetMyClassifyLogLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetMyClassifyLogLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyClassifyLogLogic {
|
||||
return &GetMyClassifyLogLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetMyClassifyLogLogic) GetMyClassifyLog() (*plantPb.ClassifyLogListResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetMyClassifyLog(l.ctx, &plantPb.GetProfileReq{UserId: userId})
|
||||
}
|
||||
@@ -5,6 +5,12 @@ package ocr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
@@ -27,8 +33,60 @@ func NewOcrClassifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OcrCl
|
||||
}
|
||||
}
|
||||
|
||||
func (l *OcrClassifyLogic) OcrClassify(req *types.OcrReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
// baiduTokenResp 百度 token 响应
|
||||
type baiduTokenResp struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
}
|
||||
|
||||
// OcrClassify 调用百度 AI 植物识别接口
|
||||
func (l *OcrClassifyLogic) OcrClassify(req *types.OcrReq) (interface{}, error) {
|
||||
// 1. 获取百度 access_token
|
||||
apiKey := l.svcCtx.Config.BaiduOcr.ApiKey
|
||||
secretKey := l.svcCtx.Config.BaiduOcr.SecretKey
|
||||
if apiKey == "" || secretKey == "" {
|
||||
return nil, fmt.Errorf("百度 OCR 未配置 ApiKey/SecretKey")
|
||||
}
|
||||
|
||||
tokenURL := fmt.Sprintf(
|
||||
"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s",
|
||||
apiKey, secretKey,
|
||||
)
|
||||
tokenResp, err := http.Post(tokenURL, "application/x-www-form-urlencoded", nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("获取百度 token 失败: %w", err)
|
||||
}
|
||||
defer tokenResp.Body.Close()
|
||||
tokenBody, _ := io.ReadAll(tokenResp.Body)
|
||||
var tokenObj baiduTokenResp
|
||||
if err = json.Unmarshal(tokenBody, &tokenObj); err != nil || tokenObj.AccessToken == "" {
|
||||
return nil, fmt.Errorf("解析百度 token 失败")
|
||||
}
|
||||
|
||||
// 2. 调用植物识别接口(传入图片 URL)
|
||||
apiURL := "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=" + tokenObj.AccessToken
|
||||
payload := strings.NewReader("url=" + url.QueryEscape(req.ImageUrl) + "&baike_num=1")
|
||||
classifyReq, err := http.NewRequest("POST", apiURL, payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("创建请求失败: %w", err)
|
||||
}
|
||||
classifyReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
client := &http.Client{}
|
||||
classifyResp, err := client.Do(classifyReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("调用百度植物识别接口失败: %w", err)
|
||||
}
|
||||
defer classifyResp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(classifyResp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取识别结果失败: %w", err)
|
||||
}
|
||||
|
||||
// 3. 直接返回百度原始结果(前端自行解析 result 字段)
|
||||
var result interface{}
|
||||
if err = json.Unmarshal(body, &result); err != nil {
|
||||
return nil, fmt.Errorf("解析识别结果失败: %w", err)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +30,12 @@ func NewCommentPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Comme
|
||||
}
|
||||
|
||||
func (l *CommentPostLogic) CommentPost(req *types.PostCommentReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_, err := l.svcCtx.PlantRpc.CommentPost(l.ctx, &plant.CommentPostReq{
|
||||
PostId: req.PostId,
|
||||
UserId: userId,
|
||||
Content: req.Content,
|
||||
ParentId: req.ParentId,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +30,14 @@ func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Create
|
||||
}
|
||||
|
||||
func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_, err := l.svcCtx.PlantRpc.CreatePost(l.ctx, &plant.CreatePostReq{
|
||||
UserId: userId,
|
||||
Title: req.Title,
|
||||
Content: req.Content,
|
||||
Location: req.Location,
|
||||
ImgIds: req.ImgIds,
|
||||
TopicId: req.TopicId,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +29,6 @@ func NewDeletePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delete
|
||||
}
|
||||
|
||||
func (l *DeletePostLogic) DeletePost(req *types.IdsReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
_, err := l.svcCtx.PlantRpc.DeletePost(l.ctx, &plant.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +28,10 @@ func NewGetPostDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Get
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetPostDetailLogic) GetPostDetail(req *types.IdPathReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetPostDetailLogic) GetPostDetail(req *types.IdPathReq) (interface{}, error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetPostDetail(l.ctx, &plant.IdReq{Id: req.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +28,15 @@ func NewGetPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPo
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetPostListLogic) GetPostList(req *types.PostListReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetPostListLogic) GetPostList(req *types.PostListReq) (interface{}, error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetPostList(l.ctx, &plant.PostListReq{
|
||||
Current: int32(req.Current),
|
||||
PageSize: int32(req.PageSize),
|
||||
Keyword: req.Keyword,
|
||||
TopicId: req.TopicId,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"list": result.List, "total": result.Total}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetMyPostListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetMyPostListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyPostListLogic {
|
||||
return &GetMyPostListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetMyPostListLogic) GetMyPostList(req *types.PostListReq) (*plantPb.PostListResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetPostList(l.ctx, &plantPb.PostListReq{
|
||||
UserId: userId, Current: int32(req.Current), PageSize: int32(req.PageSize),
|
||||
})
|
||||
}
|
||||
@@ -5,9 +5,11 @@ package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -18,7 +20,7 @@ type LikePostLogic struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 点赞帖子
|
||||
// 点赞/取消点赞帖子
|
||||
func NewLikePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikePostLogic {
|
||||
return &LikePostLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
@@ -28,7 +30,10 @@ func NewLikePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikePost
|
||||
}
|
||||
|
||||
func (l *LikePostLogic) LikePost(req *types.IdReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_, err := l.svcCtx.PlantRpc.LikePost(l.ctx, &plant.LikePostReq{
|
||||
PostId: req.Id,
|
||||
UserId: userId,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package post
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type StarPostLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewStarPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StarPostLogic {
|
||||
return &StarPostLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *StarPostLogic) StarPost(req *types.IdReq) error {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_, err := l.svcCtx.PlantRpc.StarPost(l.ctx, &plantPb.LikePostReq{UserId: userId, PostId: req.Id})
|
||||
return err
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +29,13 @@ func NewCreateTopicLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Creat
|
||||
}
|
||||
|
||||
func (l *CreateTopicLogic) CreateTopic(req *types.TopicReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
name := req.Name
|
||||
if name == "" {
|
||||
name = req.Title
|
||||
}
|
||||
_, err := l.svcCtx.PlantRpc.CreateTopic(l.ctx, &plant.CreateTopicReq{
|
||||
Name: name, Icon: req.Icon, Desc: req.Desc,
|
||||
Title: req.Title, Remark: req.Remark, StartTime: req.StartTime, EndTime: req.EndTime,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +29,6 @@ func NewDeleteTopicLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Delet
|
||||
}
|
||||
|
||||
func (l *DeleteTopicLogic) DeleteTopic(req *types.IdsReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
_, err := l.svcCtx.PlantRpc.DeleteTopic(l.ctx, &plant.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetTopicListLogic struct {
|
||||
@@ -25,8 +26,10 @@ func NewGetTopicListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetT
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetTopicListLogic) GetTopicList() error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetTopicListLogic) GetTopicList() (interface{}, error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetTopicList(l.ctx, &plant.IdReq{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"list": result.List}, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package topic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetTopicDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTopicDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTopicDetailLogic {
|
||||
return &GetTopicDetailLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetTopicDetailLogic) GetTopicDetail(req *types.IdPathReq) (*plantPb.TopicInfo, error) {
|
||||
return l.svcCtx.PlantRpc.GetTopicDetail(l.ctx, &plantPb.IdReq{Id: req.Id})
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package topic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type UpdateTopicLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateTopicLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateTopicLogic {
|
||||
return &UpdateTopicLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateTopicLogic) UpdateTopic(req *types.UpdateTopicReq) error {
|
||||
name := req.Name
|
||||
if name == "" {
|
||||
name = req.Title
|
||||
}
|
||||
_, err := l.svcCtx.PlantRpc.UpdateTopic(l.ctx, &plantPb.UpdateTopicReq{
|
||||
Id: req.Id, Name: name, Icon: req.Icon, Desc: req.Desc,
|
||||
Title: req.Title, Remark: req.Remark, StartTime: req.StartTime, EndTime: req.EndTime,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -5,9 +5,11 @@ package userProfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetUserProfileLogic struct {
|
||||
@@ -25,8 +27,11 @@ func NewGetUserProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetUserProfileLogic) GetUserProfile() error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetUserProfileLogic) GetUserProfile() (interface{}, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
result, err := l.svcCtx.PlantRpc.GetUserProfile(l.ctx, &plant.GetProfileReq{UserId: userId})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package userProfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetMyBadgesLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetMyBadgesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyBadgesLogic {
|
||||
return &GetMyBadgesLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetMyBadgesLogic) GetMyBadges() (*plantPb.UserBadgeListResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetMyBadges(l.ctx, &plantPb.GetProfileReq{UserId: userId})
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package userProfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetMyStarsLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetMyStarsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyStarsLogic {
|
||||
return &GetMyStarsLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetMyStarsLogic) GetMyStars(req *types.PageReq) (*plantPb.UserStarListResp, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
return l.svcCtx.PlantRpc.GetMyStars(l.ctx, &plantPb.GetProfileReq{UserId: userId})
|
||||
}
|
||||
@@ -5,9 +5,12 @@ package userProfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
systemPb "sundynix-micro-go/app/system/rpc/system"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +31,27 @@ func NewUpdateUserProfileLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
|
||||
func (l *UpdateUserProfileLogic) UpdateUserProfile(req *types.UpdateProfileReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
|
||||
// 1. 更新植物用户资料(昵称、头像)
|
||||
if _, err := l.svcCtx.PlantRpc.UpdateUserProfile(l.ctx, &plant.UpdateProfileReq{
|
||||
UserId: userId,
|
||||
NickName: req.Nickname,
|
||||
AvatarId: req.AvatarId,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. 同步更新系统 user 表(与旧项目保持一致:name + avatar_id 双写)
|
||||
if req.Nickname != "" || req.AvatarId != "" {
|
||||
if _, err := l.svcCtx.UserRpc.UpdateUser(l.ctx, &systemPb.UpdateUserReq{
|
||||
Id: userId,
|
||||
NickName: req.Nickname,
|
||||
AvatarId: req.AvatarId,
|
||||
}); err != nil {
|
||||
// 非致命错误,记录日志但不回滚(植物资料已更新)
|
||||
l.Logger.Errorf("sync system user failed: %v", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +29,9 @@ func NewCreateWikiClassLogic(ctx context.Context, svcCtx *svc.ServiceContext) *C
|
||||
}
|
||||
|
||||
func (l *CreateWikiClassLogic) CreateWikiClass(req *types.WikiClassReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
_, err := l.svcCtx.PlantRpc.CreateWikiClass(l.ctx, &plant.CreateWikiClassReq{
|
||||
Name: req.Name,
|
||||
Icon: req.Icon,
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package wiki
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CreateWikiLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 创建百科
|
||||
func NewCreateWikiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateWikiLogic {
|
||||
return &CreateWikiLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateWikiLogic) CreateWiki(req *types.CreateWikiReq) error {
|
||||
classID := req.ClassId
|
||||
if classID == "" && len(req.ClassIds) > 0 {
|
||||
classID = req.ClassIds[0]
|
||||
}
|
||||
_, err := l.svcCtx.PlantRpc.CreateWiki(l.ctx, &plantPb.CreateWikiReq{
|
||||
Name: req.Name,
|
||||
LatinName: req.LatinName,
|
||||
Aliases: req.Aliases,
|
||||
Genus: req.Genus,
|
||||
Difficulty: int32(req.Difficulty),
|
||||
IsHot: int32(req.IsHot),
|
||||
GrowthHabit: req.GrowthHabit,
|
||||
LightIntensity: req.LightIntensity,
|
||||
OptimalTempPeriod: req.OptimalTempPeriod,
|
||||
ClassId: classID,
|
||||
DistributionArea: req.DistributionArea,
|
||||
LifeCycle: req.LifeCycle,
|
||||
ClassIds: req.ClassIds,
|
||||
OssIds: req.OssIds,
|
||||
RelatedWikiIds: req.RelatedWikiIds,
|
||||
ReproductionMethod: req.ReproductionMethod,
|
||||
PestsDiseases: req.PestsDiseases,
|
||||
LightType: req.LightType,
|
||||
Stem: req.Stem,
|
||||
Fruit: req.Fruit,
|
||||
FoliageType: req.FoliageType,
|
||||
FoliageColor: req.FoliageColor,
|
||||
FoliageShape: req.FoliageShape,
|
||||
Height: int32(req.Height),
|
||||
FloweringPeriod: req.FloweringPeriod,
|
||||
FloweringColor: req.FloweringColor,
|
||||
FloweringShape: req.FloweringShape,
|
||||
FloweringDiameter: int32(req.FlowerDiameter),
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package wiki
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type DeleteWikiClassLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteWikiClassLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteWikiClassLogic {
|
||||
return &DeleteWikiClassLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteWikiClassLogic) DeleteWikiClass(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.DeleteWikiClass(l.ctx, &plantPb.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package wiki
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type DeleteWikiLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewDeleteWikiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteWikiLogic {
|
||||
return &DeleteWikiLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *DeleteWikiLogic) DeleteWiki(req *types.IdsReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.DeleteWiki(l.ctx, &plantPb.IdsReq{Ids: req.Ids})
|
||||
return err
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type GetWikiClassListLogic struct {
|
||||
@@ -25,8 +26,10 @@ func NewGetWikiClassListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetWikiClassListLogic) GetWikiClassList() error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetWikiClassListLogic) GetWikiClassList() (interface{}, error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetWikiClassList(l.ctx, &plant.IdReq{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"list": result.List}, nil
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package wiki
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
filePb "sundynix-micro-go/app/file/rpc/file"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +28,66 @@ func NewGetWikiDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Get
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetWikiDetailLogic) GetWikiDetail(req *types.IdPathReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
func (l *GetWikiDetailLogic) GetWikiDetail(req *types.IdPathReq) (interface{}, error) {
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
result, err := l.svcCtx.PlantRpc.GetWikiDetail(l.ctx, &plantPb.IdReq{Id: req.Id})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if result == nil || result.Wiki == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil
|
||||
// 通过 FileRpc 获取图片
|
||||
imgList := fetchWikiImages(l.ctx, l.svcCtx, result.Wiki.OssIds)
|
||||
|
||||
_ = userId
|
||||
return map[string]interface{}{
|
||||
"wiki": result.Wiki,
|
||||
"imgList": imgList,
|
||||
"classIds": result.Wiki.ClassIds,
|
||||
"relatedWikiIds": result.Wiki.RelatedWikiIds,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func fetchWikiImages(ctx context.Context, svcCtx *svc.ServiceContext, ossIds []string) []map[string]interface{} {
|
||||
if len(ossIds) == 0 {
|
||||
return []map[string]interface{}{}
|
||||
}
|
||||
resp, err := svcCtx.FileRpc.GetFilesByIds(ctx, &filePb.GetFilesByIdsReq{Ids: ossIds})
|
||||
if err != nil || resp == nil {
|
||||
return []map[string]interface{}{}
|
||||
}
|
||||
// 按 ossIds 顺序排列
|
||||
fileMap := make(map[string]*filePb.FileInfo)
|
||||
for _, f := range resp.Files {
|
||||
fileMap[f.Id] = f
|
||||
}
|
||||
var list []map[string]interface{}
|
||||
for _, id := range ossIds {
|
||||
if f, ok := fileMap[id]; ok {
|
||||
list = append(list, map[string]interface{}{
|
||||
"id": f.Id, "name": f.Name, "url": f.Url, "tag": f.Tag,
|
||||
"key": f.Key, "suffix": f.Suffix, "md5": f.Md5,
|
||||
"createdAt": unixToRFC3339(f.CreatedAt),
|
||||
"updatedAt": unixToRFC3339(f.CreatedAt),
|
||||
"createdAtStr": unixToShort(f.CreatedAt),
|
||||
})
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
func unixToRFC3339(ts int64) string {
|
||||
if ts == 0 {
|
||||
return ""
|
||||
}
|
||||
return time.Unix(ts, 0).Format(time.RFC3339)
|
||||
}
|
||||
|
||||
func unixToShort(ts int64) string {
|
||||
if ts == 0 {
|
||||
return ""
|
||||
}
|
||||
return time.Unix(ts, 0).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -27,8 +28,16 @@ func NewGetWikiListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWi
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetWikiListLogic) GetWikiList(req *types.WikiListReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
func (l *GetWikiListLogic) GetWikiList(req *types.WikiListReq) (interface{}, error) {
|
||||
result, err := l.svcCtx.PlantRpc.GetWikiList(l.ctx, &plant.WikiListReq{
|
||||
Current: int32(req.Current),
|
||||
PageSize: int32(req.PageSize),
|
||||
Name: req.Name,
|
||||
ClassId: req.ClassId,
|
||||
IsHot: int32(req.IsHot),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return map[string]interface{}{"list": result.List, "total": result.Total}, nil
|
||||
}
|
||||
|
||||
@@ -5,9 +5,11 @@ package wiki
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
"sundynix-micro-go/app/plant/rpc/plant"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -28,7 +30,11 @@ func NewToggleWikiStarLogic(ctx context.Context, svcCtx *svc.ServiceContext) *To
|
||||
}
|
||||
|
||||
func (l *ToggleWikiStarLogic) ToggleWikiStar(req *types.IdReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
userId := fmt.Sprintf("%v", l.ctx.Value("userId"))
|
||||
_, err := l.svcCtx.PlantRpc.ToggleWikiStar(l.ctx, &plant.ToggleStarReq{
|
||||
UserId: userId,
|
||||
TargetId: req.Id,
|
||||
Type: "wiki",
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package wiki
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type UpdateWikiClassLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateWikiClassLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateWikiClassLogic {
|
||||
return &UpdateWikiClassLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateWikiClassLogic) UpdateWikiClass(req *types.UpdateWikiClassReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.UpdateWikiClass(l.ctx, &plantPb.UpdateWikiClassReq{
|
||||
Id: req.Id, Name: req.Name, Icon: req.Icon,
|
||||
})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package wiki
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantPb "sundynix-micro-go/app/plant/rpc/plant"
|
||||
)
|
||||
|
||||
type UpdateWikiLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewUpdateWikiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateWikiLogic {
|
||||
return &UpdateWikiLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *UpdateWikiLogic) UpdateWiki(req *types.UpdateWikiReq) error {
|
||||
_, err := l.svcCtx.PlantRpc.UpdateWiki(l.ctx, &plantPb.UpdateWikiReq{
|
||||
Id: req.Id, Name: req.Name, LatinName: req.LatinName, Aliases: req.Aliases,
|
||||
Genus: req.Genus, Difficulty: int32(req.Difficulty), IsHot: int32(req.IsHot),
|
||||
GrowthHabit: req.GrowthHabit, LightIntensity: req.LightIntensity,
|
||||
OptimalTempPeriod: req.OptimalTempPeriod, ClassId: req.ClassId,
|
||||
DistributionArea: req.DistributionArea, LifeCycle: req.LifeCycle,
|
||||
ClassIds: req.ClassIds, OssIds: req.OssIds, RelatedWikiIds: req.RelatedWikiIds,
|
||||
ReproductionMethod: req.ReproductionMethod, PestsDiseases: req.PestsDiseases,
|
||||
LightType: req.LightType, Stem: req.Stem, Fruit: req.Fruit,
|
||||
FoliageType: req.FoliageType, FoliageColor: req.FoliageColor,
|
||||
FoliageShape: req.FoliageShape, Height: int32(req.Height),
|
||||
FloweringPeriod: req.FloweringPeriod, FloweringColor: req.FloweringColor,
|
||||
FloweringShape: req.FloweringShape, FloweringDiameter: int32(req.FlowerDiameter),
|
||||
})
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user