package sandbox

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	sandboxLogic "hongze/hongze_yb/logic/sandbox"
	sandboxReq "hongze/hongze_yb/models/request/sandbox"
	responseModel "hongze/hongze_yb/models/response"
	sandboxModel "hongze/hongze_yb/models/tables/sandbox"
	"hongze/hongze_yb/services"
	"hongze/hongze_yb/services/company"
	"hongze/hongze_yb/services/user"
	"strconv"
	"strings"
)

// List 获取沙盘逻辑图列表
// @Tags 沙盘模块
// @Summary  获取沙盘逻辑图列表
// @Description 获取沙盘逻辑图列表
// @Security ApiKeyAuth
// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Accept  json
// @Product json
// @Param chart_permission query int false "品种权限id"
// @Param keyword query string false "关键字"
// @Param is_high_light query bool false "是否高亮显示"
// @Param curr_page query int false "当前页码"
// @Param page_size query int false "每页返回数据"
// @Success 200 {object} sandbox.SandboxListResp
// @failure 400 {string} string "获取失败"
// @Router /sandbox/list [get]
func List(c *gin.Context) {
	var req sandboxReq.ListReq
	if err := c.Bind(&req); err != nil {
		response.Fail("参数有误", c)
		return
	}
	// 品种id
	chartPermissionId := req.ChartPermissionId

	// 关键字
	keyword := req.Keyword
	// 是否高亮
	isHighLight := req.IsHighLight

	page := services.GetCurrPageByClaims(c)
	pageSize := services.GetPageSizeByClaims(c)

	userInfo := user.GetInfoByClaims(c)

	//权限验证
	checkOk, companyPermissionIdList, permissionCheckInfo, tempErr := company.CheckUserSandboxPermission(userInfo.CompanyID, int(userInfo.UserID), chartPermissionId)
	if tempErr != nil {
		response.FailMsg("沙盘权限验证失败", "沙盘权限验证失败："+tempErr.Error(), c)
		c.Abort()
		return
	}
	if !checkOk {
		response.AuthError(permissionCheckInfo, "暂无权限", c)
		c.Abort()
		return
	}

	//获取指标信息
	total, list, err, errMsg := sandboxLogic.GetList(chartPermissionId, companyPermissionIdList, keyword, page, pageSize)
	if err != nil {
		response.FailMsg(errMsg, err.Error(), c)
		return
	}

	// 如果要高亮，且有输入关键字搜索，那么就返回高亮显示字符串
	if isHighLight && keyword != `` {
		for index, v := range list {
			tmpKeyword := `<span style="color:#E3B377">` + keyword + `</span>`
			list[index].Name = strings.Replace(v.Name, keyword, tmpKeyword, -1)
		}
	}

	resp := responseModel.SandboxListResp{
		List:   list,
		Paging: responseModel.GetPaging(page, pageSize, int(total)),
	}
	response.OkData("获取成功", resp, c)
}

// Detail 获取沙盘逻辑图详情
// @Tags 沙盘模块
// @Summary  获取沙盘逻辑图详情
// @Description 获取沙盘逻辑图详情
// @Security ApiKeyAuth
// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Accept  json
// @Product json
// @Param sandbox_id query int false "沙盘逻辑id"
// @Success 200 {object} sandbox.SandboxItem
// @failure 400 {string} string "获取失败"
// @Router /sandbox/detail [get]
func Detail(c *gin.Context) {
	// 沙盘id
	sandboxIdStr := c.DefaultQuery("sandbox_id", "0")
	sandboxId, _ := strconv.Atoi(sandboxIdStr)

	userInfo := user.GetInfoByClaims(c)

	sandboxInfo, err := sandboxModel.GetBySandboxId(uint32(sandboxId))
	if err != nil {
		response.FailMsg("获取沙盘失败", "获取沙盘失败："+err.Error(), c)
		c.Abort()
		return
	}
	//权限验证
	checkOk, _, permissionCheckInfo, tempErr := company.CheckUserSandboxPermission(userInfo.CompanyID, int(userInfo.UserID), int(sandboxInfo.ChartPermissionID))
	if tempErr != nil {
		response.FailMsg("沙盘权限验证失败", "沙盘权限验证失败："+tempErr.Error(), c)
		c.Abort()
		return
	}
	if !checkOk {
		response.AuthError(permissionCheckInfo, "暂无权限", c)
		c.Abort()
		return
	}

	response.OkData("获取成功", sandboxLogic.SandboxItem{
		SandboxID:           sandboxInfo.SandboxID,
		Name:                sandboxInfo.Name,
		ChartPermissionID:   sandboxInfo.ChartPermissionID,
		ChartPermissionName: sandboxInfo.ChartPermissionName,
		CurrVersion:         sandboxInfo.CurrVersion,
		Code:                sandboxInfo.Code,
		PicURL:              sandboxInfo.PicURL,
		OpUserName:          sandboxInfo.OpUserName,
		ModifyTime:          sandboxInfo.ModifyTime,
	}, c)
}

// PermissionCountList 获取各个品种下的沙盘图数量列表
// @Tags 沙盘模块
// @Summary  获取各个品种下的沙盘图数量列表
// @Description 获取各个品种下的沙盘图数量列表
// @Security ApiKeyAuth
// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Accept  json
// @Product json
// @Param sandbox_id query int false "沙盘逻辑id"
// @Success 200 {object} []sandboxModel.PermissionSandboxCount
// @failure 400 {string} string "获取失败"
// @Router /sandbox/permission_count [get]
func PermissionCountList(c *gin.Context) {
	userInfo := user.GetInfoByClaims(c)

	//权限验证
	checkOk, companyPermissionIdList, permissionCheckInfo, tempErr := company.CheckUserSandboxPermission(userInfo.CompanyID, int(userInfo.UserID), 0)
	if tempErr != nil {
		response.FailMsg("沙盘权限验证失败", "沙盘权限验证失败："+tempErr.Error(), c)
		c.Abort()
		return
	}
	if !checkOk {
		response.AuthError(permissionCheckInfo, "暂无权限", c)
		c.Abort()
		return
	}

	//获取指标信息
	list, err, errMsg := sandboxLogic.GetPermissionCountList(companyPermissionIdList)
	if err != nil {
		response.FailMsg(errMsg, err.Error(), c)
		return
	}

	response.OkData("获取成功", list, c)
}
