package controller

import (
	"strings"

	"be.ems/src/framework/i18n"
	"be.ems/src/framework/utils/ctx"
	"be.ems/src/framework/utils/parse"
	"be.ems/src/framework/vo/result"
	traceService "be.ems/src/modules/trace/service"
	"github.com/gin-gonic/gin"
)

// 实例化控制层 TraceDataController 结构体
var NewTraceData = &TraceDataController{
	traceDataService: traceService.NewTraceData,
}

// 跟踪任务数据
//
// PATH /data
type TraceDataController struct {
	// 跟踪_数据信息服务
	traceDataService *traceService.TraceData
}

// 跟踪任务数据列表
//
// GET /list
func (s *TraceDataController) List(c *gin.Context) {
	query := ctx.QueryMap(c)

	// 查询数据
	data := s.traceDataService.SelectPage(query)
	c.JSON(200, result.Ok(data))
}

// 跟踪任务数据删除
//
// DELETE /:ids
func (s *TraceDataController) Remove(c *gin.Context) {
	language := ctx.AcceptLanguage(c)
	rowIds := c.Param("ids")
	if rowIds == "" {
		c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
		return
	}
	// 处理字符转id数组后去重
	ids := strings.Split(rowIds, ",")
	uniqueIDs := parse.RemoveDuplicates(ids)
	if len(uniqueIDs) <= 0 {
		c.JSON(200, result.Err(nil))
		return
	}
	rows, err := s.traceDataService.DeleteByIds(uniqueIDs)
	if err != nil {
		c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
		return
	}
	msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
	c.JSON(200, result.OkMsg(msg))
}
