#!/bin/bash
# 批量生成 system-api logic，全部改为调用 SystemRpc
BASE="/Users/zhangjianmin/sourceCode/GolandProjects/src/sundynix-micro-go/app/system/api/internal/logic"

# Helper function
gen() {
  local dir="$1" file="$2" content="$3"
  mkdir -p "$BASE/$dir"
  echo "$content" > "$BASE/$dir/$file"
}

# ===== Client =====
gen client createClientLogic.go 'package client

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateClientLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateClientLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateClientLogic {
	return &CreateClientLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *CreateClientLogic) CreateClient(req *types.ClientReq) error {
	_, err := l.svcCtx.SystemRpc.CreateClient(l.ctx, &system.ClientReq{
		ClientId: req.ClientId, Name: req.Name, GrantType: req.GrantType,
		AdditionalInfo: req.AdditionalInfo, ActiveTimeout: req.ActiveTimeout,
	})
	return err
}'

gen client updateClientLogic.go 'package client

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateClientLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateClientLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateClientLogic {
	return &UpdateClientLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *UpdateClientLogic) UpdateClient(req *types.ClientUpdateReq) error {
	_, err := l.svcCtx.SystemRpc.UpdateClient(l.ctx, &system.ClientUpdateReq{
		Id: req.Id, ClientId: req.ClientId, Name: req.Name, GrantType: req.GrantType,
		AdditionalInfo: req.AdditionalInfo, ActiveTimeout: req.ActiveTimeout,
	})
	return err
}'

gen client deleteClientLogic.go 'package client

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteClientLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteClientLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteClientLogic {
	return &DeleteClientLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *DeleteClientLogic) DeleteClient(req *types.IdsReq) error {
	_, err := l.svcCtx.SystemRpc.DeleteClient(l.ctx, &system.IdsReq{Ids: req.Ids})
	return err
}'

gen client getClientListLogic.go 'package client

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetClientListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetClientListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetClientListLogic {
	return &GetClientListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *GetClientListLogic) GetClientList(req *types.ClientListReq) (interface{}, error) {
	resp, err := l.svcCtx.SystemRpc.GetClientList(l.ctx, &system.ClientListReq{
		Current: int32(req.Current), PageSize: int32(req.PageSize), Name: req.Name,
	})
	if err != nil { return nil, err }
	return map[string]interface{}{"list": resp.List, "total": resp.Total, "current": req.Current, "size": req.PageSize}, nil
}'

echo "===Client done==="

# ===== Role =====
gen role createRoleLogic.go 'package role

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateRoleLogic {
	return &CreateRoleLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *CreateRoleLogic) CreateRole(req *types.RoleReq) error {
	_, err := l.svcCtx.SystemRpc.CreateRole(l.ctx, &system.RoleReq{
		Name: req.Name, Code: req.Code, Sort: int32(req.Sort), MenuIds: req.MenuIds,
	})
	return err
}'

gen role updateRoleLogic.go 'package role

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRoleLogic {
	return &UpdateRoleLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *UpdateRoleLogic) UpdateRole(req *types.RoleUpdateReq) error {
	_, err := l.svcCtx.SystemRpc.UpdateRole(l.ctx, &system.RoleUpdateReq{
		Id: req.Id, Name: req.Name, Code: req.Code, Sort: int32(req.Sort), MenuIds: req.MenuIds,
	})
	return err
}'

gen role deleteRoleLogic.go 'package role

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteRoleLogic {
	return &DeleteRoleLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *DeleteRoleLogic) DeleteRole(req *types.IdsReq) error {
	_, err := l.svcCtx.SystemRpc.DeleteRole(l.ctx, &system.IdsReq{Ids: req.Ids})
	return err
}'

gen role getRoleListLogic.go 'package role

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetRoleListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetRoleListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRoleListLogic {
	return &GetRoleListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *GetRoleListLogic) GetRoleList(req *types.RoleListReq) (interface{}, error) {
	resp, err := l.svcCtx.SystemRpc.GetRoleList(l.ctx, &system.RoleListReq{
		Current: int32(req.Current), PageSize: int32(req.PageSize), Name: req.Name,
	})
	if err != nil { return nil, err }
	return map[string]interface{}{"list": resp.List, "total": resp.Total}, nil
}'

echo "===Role done==="

# ===== Menu =====
gen menu createMenuLogic.go 'package menu

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateMenuLogic {
	return &CreateMenuLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *CreateMenuLogic) CreateMenu(req *types.MenuReq) error {
	_, err := l.svcCtx.SystemRpc.CreateMenu(l.ctx, &system.MenuReq{
		ParentId: req.ParentId, Category: int32(req.Category), Name: req.Name, Title: req.Title,
		Code: req.Code, Path: req.Path, Permission: req.Permission, Locale: req.Locale,
		Icon: req.Icon, Sort: int32(req.Sort),
	})
	return err
}'

gen menu updateMenuLogic.go 'package menu

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMenuLogic {
	return &UpdateMenuLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *UpdateMenuLogic) UpdateMenu(req *types.MenuUpdateReq) error {
	_, err := l.svcCtx.SystemRpc.UpdateMenu(l.ctx, &system.MenuUpdateReq{
		Id: req.Id, ParentId: req.ParentId, Category: int32(req.Category), Name: req.Name, Title: req.Title,
		Code: req.Code, Path: req.Path, Permission: req.Permission, Locale: req.Locale,
		Icon: req.Icon, Sort: int32(req.Sort),
	})
	return err
}'

gen menu deleteMenuLogic.go 'package menu

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteMenuLogic {
	return &DeleteMenuLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *DeleteMenuLogic) DeleteMenu(req *types.IdsReq) error {
	_, err := l.svcCtx.SystemRpc.DeleteMenu(l.ctx, &system.IdsReq{Ids: req.Ids})
	return err
}'

gen menu getMenuListLogic.go 'package menu

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetMenuListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetMenuListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuListLogic {
	return &GetMenuListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *GetMenuListLogic) GetMenuList() (interface{}, error) {
	resp, err := l.svcCtx.SystemRpc.GetMenuList(l.ctx, &system.IdReq{})
	if err != nil { return nil, err }
	return resp.Menus, nil
}'

gen menu getMenuByRoleLogic.go 'package menu

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetMenuByRoleLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetMenuByRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMenuByRoleLogic {
	return &GetMenuByRoleLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *GetMenuByRoleLogic) GetMenuByRole(req *types.MenuByRoleReq) (interface{}, error) {
	resp, err := l.svcCtx.SystemRpc.GetMenusByRoleId(l.ctx, &system.GetMenusByRoleIdReq{RoleId: req.RoleId})
	if err != nil { return nil, err }
	return resp.Menus, nil
}'

echo "===Menu done==="

# ===== Dict =====
gen dict createDictLogic.go 'package dict

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateDictLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateDictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDictLogic {
	return &CreateDictLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *CreateDictLogic) CreateDict(req *types.DictReq) error {
	_, err := l.svcCtx.SystemRpc.CreateDict(l.ctx, &system.DictReq{
		Type: req.Type, Label: req.Label, Value: req.Value, Sort: int32(req.Sort), Desc: req.Desc,
	})
	return err
}'

gen dict updateDictLogic.go 'package dict

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateDictLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateDictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateDictLogic {
	return &UpdateDictLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *UpdateDictLogic) UpdateDict(req *types.DictUpdateReq) error {
	_, err := l.svcCtx.SystemRpc.UpdateDict(l.ctx, &system.DictUpdateReq{
		Id: req.Id, Type: req.Type, Label: req.Label, Value: req.Value, Sort: int32(req.Sort), Desc: req.Desc,
	})
	return err
}'

gen dict deleteDictLogic.go 'package dict

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteDictLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteDictLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteDictLogic {
	return &DeleteDictLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *DeleteDictLogic) DeleteDict(req *types.IdsReq) error {
	_, err := l.svcCtx.SystemRpc.DeleteDict(l.ctx, &system.IdsReq{Ids: req.Ids})
	return err
}'

gen dict getDictListLogic.go 'package dict

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetDictListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetDictListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDictListLogic {
	return &GetDictListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *GetDictListLogic) GetDictList(req *types.DictListReq) (interface{}, error) {
	resp, err := l.svcCtx.SystemRpc.GetDictList(l.ctx, &system.DictListReq{
		Current: int32(req.Current), PageSize: int32(req.PageSize), Type: req.Type,
	})
	if err != nil { return nil, err }
	return map[string]interface{}{"list": resp.List, "total": resp.Total}, nil
}'

echo "===Dict done==="

# ===== OperationRecord =====
gen operationRecord deleteOperationRecordLogic.go 'package operationRecord

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteOperationRecordLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteOperationRecordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteOperationRecordLogic {
	return &DeleteOperationRecordLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *DeleteOperationRecordLogic) DeleteOperationRecord(req *types.IdsReq) error {
	_, err := l.svcCtx.SystemRpc.DeleteOperationRecord(l.ctx, &system.IdsReq{Ids: req.Ids})
	return err
}'

gen operationRecord getOperationRecordListLogic.go 'package operationRecord

import (
	"context"
	"sundynix-micro-go/app/system/api/internal/svc"
	"sundynix-micro-go/app/system/api/internal/types"
	"sundynix-micro-go/app/system/rpc/pb/system"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetOperationRecordListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetOperationRecordListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOperationRecordListLogic {
	return &GetOperationRecordListLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}

func (l *GetOperationRecordListLogic) GetOperationRecordList(req *types.OperationRecordListReq) (interface{}, error) {
	resp, err := l.svcCtx.SystemRpc.GetOperationRecordList(l.ctx, &system.OperationRecordListReq{
		Current: int32(req.Current), PageSize: int32(req.PageSize),
		Method: req.Method, Path: req.Path, Status: int32(req.Status),
	})
	if err != nil { return nil, err }
	return map[string]interface{}{"list": resp.List, "total": resp.Total}, nil
}'

echo "===ALL SYSTEM-API LOGIC DONE==="
