package middleware

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/services/company"
	"hongze/hongze_yb/services/user"
)

// CheckChartAuth 图库权限验证
func CheckChartAuth() gin.HandlerFunc {

	return func(c *gin.Context) {

		userInfo := user.GetInfoByClaims(c)

		// 是否为内部员工
		ok, _, err := user.GetAdminByUserInfo(userInfo)
		if err != nil {
			response.FailMsg("图库权限验证失败", "获取系统用户信息失败" + err.Error(), c)
			c.Abort()
			return
		}

		if !ok {
			checkOk, permissionCheckInfo, tempErr := company.CheckUserChartPermission(userInfo.CompanyID, int(userInfo.UserID))
			if tempErr != nil {
				response.FailMsg("图库权限验证失败", "图库权限验证失败" + tempErr.Error(), c)
				c.Abort()
				return
			}
			if !checkOk {
				response.AuthError(permissionCheckInfo, "暂无权限", c)
				c.Abort()
				return
			}
		}

		c.Next()
	}
}