package mkt_network_detail

import (
	"context"
	"git.i2edu.net/i2/i2-bill-erp/transform"

	"git.i2edu.net/i2/i2-bill-api/internal/svc"
	"git.i2edu.net/i2/i2-bill-api/internal/types"

	"git.i2edu.net/i2/go-zero/core/logx"
)

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

func NewGetErpMktNetworkDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) GetErpMktNetworkDetailLogic {
	return GetErpMktNetworkDetailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetErpMktNetworkDetailLogic) GetErpMktNetworkDetail() (*types.Response, error) {
	// todo: add your logic here and delete this line
	var code = 200
	var msg string
	var data interface{}
	res, err := l.svcCtx.GetErpNetworkDetailTree(l.ctx)
	if err != nil {
		logx.Error(err.Error())
		code = 500
		msg = err.Error()
		data = nil
		return &types.Response{code, msg, data}, nil
	}
	for _, node := range res {
		if node.Id == 1001 {
			res = []*transform.TreeNode{node}
		}
	}
	data = transform.TreeNodes{Nodes: res}
	if err != nil {
		logx.Error(err.Error())
		code = 500
		msg = err.Error()
		data = nil
	}
	return &types.Response{code, msg, data}, nil
}
