package controller

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	"be.ems/src/framework/config"
	"be.ems/src/framework/constants"
	"be.ems/src/framework/i18n"
	"be.ems/src/framework/reqctx"
	"be.ems/src/framework/resp"
	"be.ems/src/framework/utils/date"
	"be.ems/src/framework/utils/file"
	"be.ems/src/framework/utils/parse"
	"be.ems/src/framework/utils/regular"
	"be.ems/src/modules/system/model"
	"be.ems/src/modules/system/service"

	"github.com/gin-gonic/gin"
)

// 实例化控制层 SysUserController 结构体
var NewSysUser = &SysUserController{
	sysUserService: service.NewSysUser,
	sysRoleService: service.NewSysRole,
	sysPostService: service.NewSysPost,
}

// 用户信息
//
// PATH /system/user
type SysUserController struct {
	sysUserService *service.SysUser // 用户服务
	sysRoleService *service.SysRole // 角色服务
	sysPostService *service.SysPost // 岗位服务
}

// 用户信息列表
//
// GET /list
//
//	@Tags			system/user
//	@Accept			json
//	@Produce		json
//	@Param			userName	query		string	false	"userName"
//	@Param			pageNum		query		number	true	"pageNum"	default(1)
//	@Param			pageSize	query		number	true	"pageSize"	default(10)
//	@Success		200			{object}	object	"Response Results"
//	@Security		TokenAuth
//	@Summary		User Information List
//	@Description	User Information List
//	@Router			/system/user/list [get]
func (s *SysUserController) List(c *gin.Context) {
	queryMap := reqctx.QueryMap(c)
	dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_user", "sys_user")
	rows, total := s.sysUserService.FindByPage(queryMap, dataScopeSQL)

	// 闭包函数处理多语言
	language := reqctx.AcceptLanguage(c)
	converI18n := func(language string, arr *[]model.SysUser) {
		for i := range *arr {
			(*arr)[i].NickName = i18n.TKey(language, (*arr)[i].NickName)
			(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
			(*arr)[i].Dept.DeptName = i18n.TKey(language, (*arr)[i].Dept.DeptName)
			for ri := range (*arr)[i].Roles {
				(*arr)[i].Roles[ri].RoleName = i18n.TKey(language, (*arr)[i].Roles[ri].RoleName)
			}
		}
	}
	converI18n(language, &rows)

	c.JSON(200, resp.OkData(map[string]any{"rows": rows, "total": total}))
}

// 用户信息详情
//
// GET /:userId
func (s *SysUserController) Info(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	userId := parse.Number(c.Param("userId"))
	if userId < 0 {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: userId is empty"))
		return
	}

	// 查询系统角色列表
	roles := s.sysRoleService.Find(model.SysRole{})
	// 查询系统岗位列表
	posts := s.sysPostService.Find(model.SysPost{})

	// 闭包函数处理多语言
	converI18nRoles := func(language string, arr *[]model.SysRole) {
		for i := range *arr {
			(*arr)[i].RoleName = i18n.TKey(language, (*arr)[i].RoleName)
			(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
		}
	}
	converI18nRoles(language, &roles)

	// 闭包函数处理多语言
	converI18nPosts := func(language string, arr *[]model.SysPost) {
		for i := range *arr {
			(*arr)[i].PostName = i18n.TKey(language, (*arr)[i].PostName)
			(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
		}
	}
	converI18nPosts(language, &posts)

	// 新增用户时，用户ID为0
	if userId == 0 {
		c.JSON(200, resp.OkData(map[string]any{
			"user":    map[string]any{},
			"roleIds": []string{},
			"postIds": []string{},
			"roles":   roles,
			"posts":   posts,
		}))
		return
	}

	// 检查用户是否存在
	userInfo := s.sysUserService.FindById(userId)
	if userInfo.UserId != userId {
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.noData")))
		return
	}

	// 处理多语言
	userInfo.NickName = i18n.TKey(language, userInfo.NickName)
	userInfo.Remark = i18n.TKey(language, userInfo.Remark)
	userInfo.Dept.DeptName = i18n.TKey(language, userInfo.Dept.DeptName)
	for ri := range userInfo.Roles {
		userInfo.Roles[ri].RoleName = i18n.TKey(language, userInfo.Roles[ri].RoleName)
	}

	// 角色ID组
	roleIds := make([]int64, 0)
	for _, r := range userInfo.Roles {
		roleIds = append(roleIds, r.RoleId)
	}

	// 岗位ID组
	postIds := make([]int64, 0)
	userPosts := s.sysPostService.FindByUserId(userId)
	for _, p := range userPosts {
		postIds = append(postIds, p.PostId)
	}

	c.JSON(200, resp.OkData(map[string]any{
		"user":    userInfo,
		"roleIds": roleIds,
		"postIds": postIds,
		"roles":   roles,
		"posts":   posts,
	}))
}

// 用户信息新增
//
// POST /
func (s *SysUserController) Add(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.SysUser
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 密码单独取，避免序列化输出
	var bodyPassword struct {
		Password string `json:"password" binding:"required"`
	}
	if err := c.ShouldBindBodyWithJSON(&bodyPassword); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}
	body.Password = bodyPassword.Password

	if body.UserId > 0 {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: userId not is empty"))
		return
	}
	if !regular.ValidUsername(body.UserName) {
		// msg := fmt.Sprintf("新增用户【%s】失败，登录账号用户账号只能包含大写小写字母，数字，且不少于4位", body.UserName)
		msg := fmt.Sprintf("Add a new user [%s] failed to log in the account user account not less than 4 digits", body.UserName)
		c.JSON(400, resp.ErrMsg(msg))
		return
	}
	// if !regular.ValidPassword(body.Password) {
	// 	// msg := fmt.Sprintf("新增用户【%s】失败，登录密码至少包含大小写字母、数字、特殊符号，且不少于6位", body.UserName)
	// 	msg := fmt.Sprintf("New user [%s] failed, the login password contains at least upper and lower case letters, numbers, special symbols, and not less than 6 bits", body.UserName)
	// 	c.JSON(400, resp.ErrMsg(msg))
	// 	return
	// }

	// 检查用户密码策略强度
	ok, errMsg := s.sysUserService.ValidatePasswordPolicy(body.Password, language)
	if !ok {
		c.JSON(200, resp.ErrMsg(errMsg))
		return
	}
	// 检查用户登录账号是否唯一
	uniqueUserName := s.sysUserService.CheckUniqueByUserName(body.UserName, 0)
	if !uniqueUserName {
		// msg := fmt.Sprintf("新增用户【%s】失败，登录账号已存在", body.UserName)
		msg := i18n.TTemplate(language, "user.errNameExists", map[string]any{"name": body.UserName})
		c.JSON(200, resp.ErrMsg(msg))
		return
	}

	// 检查手机号码格式并判断是否唯一
	if body.Phone != "" {
		if regular.ValidMobile(body.Phone) {
			uniquePhone := s.sysUserService.CheckUniqueByPhone(body.Phone, 0)
			if !uniquePhone {
				// msg := fmt.Sprintf("新增用户【%s】失败，手机号码已存在", body.UserName)
				msg := i18n.TTemplate(language, "user.errPhoneExists", map[string]any{"name": body.UserName})
				c.JSON(200, resp.ErrMsg(msg))
				return
			}
		} else {
			// msg := fmt.Sprintf("新增用户【%s】失败，手机号码格式错误", body.UserName)
			msg := i18n.TTemplate(language, "user.errPhoneFormat", map[string]any{"name": body.UserName})
			c.JSON(200, resp.ErrMsg(msg))
			return
		}
	}

	// 检查邮箱格式并判断是否唯一
	if body.Email != "" {
		if regular.ValidEmail(body.Email) {
			uniqueEmail := s.sysUserService.CheckUniqueByEmail(body.Email, 0)
			if !uniqueEmail {
				// msg := fmt.Sprintf("新增用户【%s】失败，邮箱已存在", body.UserName)
				msg := i18n.TTemplate(language, "user.errEmailExists", map[string]any{"name": body.UserName})
				c.JSON(200, resp.ErrMsg(msg))
				return
			}
		} else {
			// msg := fmt.Sprintf("新增用户【%s】失败，邮箱格式错误", body.UserName)
			msg := i18n.TTemplate(language, "user.errEmailFormat", map[string]any{"name": body.UserName})
			c.JSON(200, resp.ErrMsg(msg))
			return
		}
	}

	userInfo := model.SysUser{
		UserName:   body.UserName,
		Password:   body.Password,
		NickName:   body.NickName,
		Email:      body.Email,
		Phone:      body.Phone,
		Sex:        body.Sex,
		StatusFlag: body.StatusFlag,
		Remark:     body.Remark,
		DeptId:     body.DeptId,  // 部门ID
		RoleIds:    body.RoleIds, // 角色ID组
		PostIds:    body.PostIds, // 岗位ID组
		Avatar:     body.Avatar,
		CreateBy:   reqctx.LoginUserToUserName(c),
	}
	insertId := s.sysUserService.Insert(userInfo)
	if insertId > 0 {
		c.JSON(200, resp.OkData(insertId))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 用户信息修改
//
// POST /
func (s *SysUserController) Edit(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body model.SysUser
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}
	if body.UserId <= 0 {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: userId is empty"))
		return
	}

	// 检查是否系统管理员用户
	if config.IsSystemUser(body.UserId) {
		// c.JSON(200, resp.ErrMsg("不允许操作系统管理员用户"))
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errOperateAdmin")))
		return
	}

	// 检查用户登录账号是否唯一
	uniqueUserName := s.sysUserService.CheckUniqueByUserName(
		body.UserName,
		body.UserId,
	)
	if !uniqueUserName {
		// msg := fmt.Sprintf("修改用户【%s】失败，登录账号已存在", body.UserName)
		msg := i18n.TTemplate(language, "user.errNameExists", map[string]any{"name": body.UserName})
		c.JSON(200, resp.ErrMsg(msg))
		return
	}

	// 检查是否存在
	userInfo := s.sysUserService.FindById(body.UserId)
	if userInfo.UserId != body.UserId {
		// c.JSON(200, resp.ErrMsg("没有权限访问用户数据！"))
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.noData")))
		return
	}

	// 检查手机号码格式并判断是否唯一
	if body.Phone != "" {
		if regular.ValidMobile(body.Phone) {
			uniquePhone := s.sysUserService.CheckUniqueByPhone(body.Phone, body.UserId)
			if !uniquePhone {
				// msg := fmt.Sprintf("修改用户【%s】失败，手机号码已存在", body.UserName)
				msg := i18n.TTemplate(language, "user.errPhoneExists", map[string]any{"name": body.UserName})
				c.JSON(200, resp.ErrMsg(msg))
				return
			}
		} else {
			// msg := fmt.Sprintf("修改用户【%s】失败，手机号码格式错误", body.UserName)
			msg := i18n.TTemplate(language, "user.errPhoneFormat", map[string]any{"name": body.UserName})
			c.JSON(200, resp.ErrMsg(msg))
			return
		}
	}

	// 检查邮箱格式并判断是否唯一
	if body.Email != "" {
		if regular.ValidEmail(body.Email) {
			uniqueEmail := s.sysUserService.CheckUniqueByEmail(body.Email, body.UserId)
			if !uniqueEmail {
				// msg := fmt.Sprintf("修改用户【%s】失败，邮箱已存在", body.UserName)
				msg := i18n.TTemplate(language, "user.errEmailExists", map[string]any{"name": body.UserName})
				c.JSON(200, resp.ErrMsg(msg))
				return
			}
		} else {
			// msg := fmt.Sprintf("修改用户【%s】失败，邮箱格式错误", body.UserName)
			msg := i18n.TTemplate(language, "user.errEmailFormat", map[string]any{"name": body.UserName})
			c.JSON(200, resp.ErrMsg(msg))
			return
		}
	}

	if body.Avatar != "" {
		userInfo.Avatar = body.Avatar
	}

	userInfo.Phone = body.Phone
	userInfo.Email = body.Email
	userInfo.Sex = body.Sex
	userInfo.StatusFlag = body.StatusFlag
	userInfo.Remark = body.Remark
	userInfo.DeptId = body.DeptId
	userInfo.RoleIds = body.RoleIds
	userInfo.PostIds = body.PostIds
	userInfo.Password = "" // 忽略修改密码
	userInfo.UpdateBy = reqctx.LoginUserToUserName(c)
	rows := s.sysUserService.UpdateUserAndRolePost(userInfo)
	if rows > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// 用户信息删除
//
// DELETE /:userId
func (s *SysUserController) Remove(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	userId := c.Param("userId")
	if userId == "" {
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: userId is empty"))
		return
	}

	// 处理字符转id数组后去重
	uniqueIDs := parse.RemoveDuplicatesToArray(userId, ",")
	// 转换成int64数组类型
	ids := make([]int64, 0)
	for _, v := range uniqueIDs {
		ids = append(ids, parse.Number(v))
	}

	loginUserID := reqctx.LoginUserToUserID(c)
	for _, id := range ids {
		// 不能删除自己
		if id == loginUserID {
			c.JSON(200, resp.ErrMsg("Current user cannot be deleted"))
			return
		}
		// 检查是否管理员用户
		if config.IsSystemUser(id) {
			// c.JSON(200, resp.ErrMsg("不允许操作系统管理员用户"))
			c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errOperateAdmin")))
			return
		}
	}

	rows, err := s.sysUserService.DeleteByIds(ids)
	if err != nil {
		c.JSON(200, resp.ErrMsg(err.Error()))
		return
	}
	// msg := fmt.Sprintf("删除成功：%d", rows)
	msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
	c.JSON(200, resp.OkMsg(msg))
}

// 用户重置密码
//
// PUT /password
func (s *SysUserController) Password(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body struct {
		UserId   int64  `json:"userId" binding:"required"`
		Password string `json:"password" binding:"required"`
	}
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 检查是否系统管理员用户
	if config.IsSystemUser(body.UserId) {
		// c.JSON(200, resp.ErrMsg("不允许操作系统管理员用户"))
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errOperateAdmin")))
		return
	}

	// if !regular.ValidPassword(body.Password) {
	// 	c.JSON(200, resp.ErrMsg("Login password contains at least upper and lower case letters, numbers, special symbols, and not less than 6 digits"))
	// 	return
	// }

	// 检查用户密码策略强度
	ok, errMsg := s.sysUserService.ValidatePasswordPolicy(body.Password, language)
	if !ok {
		c.JSON(200, resp.ErrMsg(errMsg))
		return
	}
	// 检查是否存在
	userInfo := s.sysUserService.FindById(body.UserId)
	if userInfo.UserId != body.UserId {
		// c.JSON(200, resp.ErrMsg("没有权限访问用户数据！"))
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.noData")))
		return
	}

	userInfo.Password = body.Password
	userInfo.UpdateBy = reqctx.LoginUserToUserName(c)
	rows := s.sysUserService.Update(userInfo)
	if rows > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// Status 用户状态修改
//
// PUT /status
func (s *SysUserController) Status(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body struct {
		UserId     int64  `json:"userId" binding:"required"`
		StatusFlag string `json:"statusFlag" binding:"required,oneof=0 1"`
	}
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 检查是否系统管理员用户
	if config.IsSystemUser(body.UserId) {
		// c.JSON(200, resp.ErrMsg("不允许操作系统管理员用户"))
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.errOperateAdmin")))
		return
	}

	// 检查是否存在
	userInfo := s.sysUserService.FindById(body.UserId)
	if userInfo.UserId != body.UserId {
		// c.JSON(200, resp.ErrMsg("没有权限访问用户数据！"))
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.noData")))
		return
	}

	// 与旧值相等不变更
	if userInfo.StatusFlag == body.StatusFlag {
		// c.JSON(200, resp.ErrMsg("变更状态与旧值相等！"))
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "user.statusEq")))
		return
	}

	userInfo.StatusFlag = body.StatusFlag
	userInfo.Password = "" // 密码不更新
	userInfo.UpdateBy = reqctx.LoginUserToUserName(c)
	rows := s.sysUserService.Update(userInfo)
	if rows > 0 {
		c.JSON(200, resp.Ok(nil))
		return
	}
	c.JSON(200, resp.Err(nil))
}

// Export 用户信息列表导出
//
// GET /export
func (s *SysUserController) Export(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	// 查询结果，根据查询条件结果，单页最大值限制
	queryMap := reqctx.QueryMap(c)
	dataScopeSQL := reqctx.LoginUserToDataScopeSQL(c, "sys_user", "sys_user")
	rows, total := s.sysUserService.FindByPage(queryMap, dataScopeSQL)
	if total == 0 {
		// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
		c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
		return
	}

	// 闭包函数处理多语言
	converI18n := func(language string, arr *[]model.SysUser) {
		for i := range *arr {
			(*arr)[i].NickName = i18n.TKey(language, (*arr)[i].NickName)
			(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
			(*arr)[i].Dept.DeptName = i18n.TKey(language, (*arr)[i].Dept.DeptName)
			for ri := range (*arr)[i].Roles {
				(*arr)[i].Roles[ri].RoleName = i18n.TKey(language, (*arr)[i].Roles[ri].RoleName)
			}
		}
	}
	converI18n(language, &rows)

	// 第一行表头标题
	headerCells := map[string]string{
		"A1": i18n.TKey(language, "user.export.id"),
		"B1": i18n.TKey(language, "user.export.name"),
		"C1": i18n.TKey(language, "user.export.nick"),
		"D1": i18n.TKey(language, "user.export.role"),
		"E1": i18n.TKey(language, "user.export.deptName"),
		"F1": i18n.TKey(language, "user.export.userType"),
		"G1": i18n.TKey(language, "user.export.loginIP"),
		"H1": i18n.TKey(language, "user.export.loginDate"),
		"I1": i18n.TKey(language, "user.export.status"),
		// "F1": i18n.TKey(language, "user.export.sex"),
		// "E1": i18n.TKey(language, "user.export.phone"),
		// "D1": i18n.TKey(language, "user.export.email"),
		// "I1": i18n.TKey(language, "user.export.deptID"),
		// "K1": i18n.TKey(language, "user.export.deptLeader"),
	}
	// 读取用户性别字典数据
	dictSysUserType := service.NewSysDictType.FindDataByType("sys_user_type")

	// 读取用户性别字典数据
	// dictSysUserSex := s.sysDictDataService.SelectDictDataByType("sys_user_sex")
	// 从第二行开始的数据
	dataCells := make([]map[string]any, 0)
	for i, row := range rows {
		idx := strconv.Itoa(i + 2)
		// 用户性别
		// sysUserSex := row.Sex
		// for _, v := range dictSysUserSex {
		// 	if row.Sex == v.DictValue {
		// 		sysUserSex = i18n.TKey(language, v.DictLabel)
		// 		break
		// 	}
		// }
		// 用户类型
		userType := row.UserType
		for _, v := range dictSysUserType {
			if row.UserType == v.DataValue && row.UserSource != "#" {
				userType = i18n.TKey(language, v.DataLabel) + " " + row.UserSource
				break
			}
		}
		// 帐号状态
		statusValue := i18n.TKey(language, "dictData.disable")
		if row.StatusFlag == "1" {
			statusValue = i18n.TKey(language, "dictData.normal")
		}
		// 用户角色, 默认导出首个
		userRole := ""
		if len(row.Roles) > 0 {
			arr := make([]string, 0)
			for _, v := range row.Roles {
				arr = append(arr, i18n.TKey(language, v.RoleName))
			}
			userRole = strings.Join(arr, ",")
		}
		dataCells = append(dataCells, map[string]any{
			"A" + idx: row.UserId,
			"B" + idx: row.UserName,
			"C" + idx: row.NickName,
			"D" + idx: userRole,
			"E" + idx: row.Dept.DeptName,
			"F" + idx: userType,
			"G" + idx: row.LoginIp,
			"H" + idx: date.ParseDateToStr(row.LoginTime, date.YYYY_MM_DDTHH_MM_SSZ),
			"I" + idx: statusValue,
			// "E" + idx: row.PhoneNumber,
			// "F" + idx: sysUserSex,
			// "D" + idx: row.Email,
			// "I" + idx: row.Dept.DeptID,
			// "K" + idx: row.Dept.Leader,
		})
	}

	// 导出文件名称
	fileName := fmt.Sprintf("user_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
	// 导出数据表格
	saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
	if err != nil {
		c.JSON(200, resp.ErrMsg(err.Error()))
		return
	}

	c.FileAttachment(saveFilePath, fileName)
}

// Template 用户信息列表导入模板下载
//
// GET /import/template
func (s *SysUserController) Template(c *gin.Context) {
	// 多语言处理
	language := reqctx.AcceptLanguage(c)
	asserPath := fmt.Sprintf("src/assets/template/excel/user_import_template_%s.xlsx", language)

	// 从 embed.FS 中读取内嵌文件
	assetsDir := config.GetAssetsDirFS()
	fileData, err := assetsDir.ReadFile(asserPath)
	if err != nil {
		c.String(400, "failed to read file")
		return
	}
	fileName := fmt.Sprintf("user_import_template_%d.xlsx", time.Now().UnixMilli())

	// 设置响应头
	c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
	c.Header("Content-Type", "application/octet-stream")

	// 返回响应体
	c.Data(200, "application/octet-stream", fileData)
}

// Import 用户信息列表导入
//
// POST /import
func (s *SysUserController) Import(c *gin.Context) {
	language := reqctx.AcceptLanguage(c)
	var body struct {
		FilePath string `json:"filePath" binding:"required"` // 上传的文件地址
		Update   bool   `json:"update"`                      // 允许进行更新
	}
	if err := c.ShouldBindBodyWithJSON(&body); err != nil {
		errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
		c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_PARSER, errMsgs))
		return
	}

	// 表格文件绝对地址
	filePath := file.ParseUploadFileAbsPath(body.FilePath)
	// 读取表格数据
	rows, err := file.ReadFileExecl(filePath, "")
	if err != nil {
		c.JSON(200, resp.ErrMsg(err.Error()))
		return
	}
	if len(rows) <= 0 {
		c.JSON(200, resp.ErrMsg("Imported user data cannot be empty!"))
		return
	}

	// 获取操作人名称
	operaName := reqctx.LoginUserToUserName(c)

	// 读取默认初始密码
	initPassword := service.NewSysConfig.FindValueByKey("sys.user.initPassword")
	// 读取用户性别字典数据
	dictSysUserSex := service.NewSysDictType.FindDataByType("sys_user_sex")

	// 导入记录
	successNum := 0
	failureNum := 0
	var successMsgArr []string
	var failureMsgArr []string
	mustItemArr := []string{"B", "C"}
	for _, row := range rows {
		// 检查必填列
		ownItem := true
		for _, item := range mustItemArr {
			if v, ok := row[item]; !ok || v == "" {
				ownItem = false
				break
			}
		}
		if !ownItem {
			mustItemArrStr := strings.Join(mustItemArr, "、")
			failureNum++
			// msg := fmt.Sprintf("表格中必填列表项，%s", mustItemArrStr)
			msg := i18n.TTemplate(language, "user.import.mustItem", map[string]any{"text": mustItemArrStr})
			failureMsgArr = append(failureMsgArr, msg)
			continue
		}

		// 用户性别转值
		sysUserSex := "0"
		for _, v := range dictSysUserSex {
			label := i18n.TKey(language, v.DataLabel)
			if row["F"] == label {
				sysUserSex = v.DataValue
				break
			}
		}
		// 用户状态
		sysUserStatus := constants.STATUS_NO
		if row["G"] == "正常" || row["G"] == "Normal" {
			sysUserStatus = constants.STATUS_YES
		}
		// 用户角色 拿编号
		sysUserRole := ""
		if v, ok := row["H"]; ok && v != "" {
			sysUserRole = strings.SplitN(v, "-", 2)[0]
			if sysUserRole == "1" {
				sysUserRole = ""
			}
		}
		var sysUserDeptId int64 = 101
		if row["I"] != "" {
			sysUserDeptId = parse.Number(row["I"])
		}

		// 验证是否存在这个用户
		newSysUser := s.sysUserService.FindByUserName(row["B"], "System", "#")
		newSysUser.Password = initPassword
		newSysUser.UserName = row["B"]
		newSysUser.NickName = row["C"]
		newSysUser.Phone = row["E"]
		newSysUser.Email = row["D"]
		newSysUser.StatusFlag = sysUserStatus
		newSysUser.Sex = sysUserSex
		newSysUser.DeptId = sysUserDeptId

		// 行用户编号
		rowNo := row["A"]

		// 检查手机号码格式并判断是否唯一
		if newSysUser.Phone != "" {
			if regular.ValidMobile(newSysUser.Phone) {
				uniquePhone := s.sysUserService.CheckUniqueByPhone(newSysUser.Phone, newSysUser.UserId)
				if !uniquePhone {
					// msg := fmt.Sprintf("用户编号：%s 手机号码：%s 已存在", rowNo, newSysUser.Phone)
					msg := i18n.TTemplate(language, "user.import.phoneExist", map[string]any{"id": rowNo, "phone": newSysUser.Phone})
					failureNum++
					failureMsgArr = append(failureMsgArr, msg)
					continue
				}
			} else {
				// msg := fmt.Sprintf("用户编号：%s 手机号码：%s 格式错误", rowNo, newSysUser.Phone)
				msg := i18n.TTemplate(language, "user.import.phoneFormat", map[string]any{"id": rowNo, "phone": newSysUser.Phone})
				failureNum++
				failureMsgArr = append(failureMsgArr, msg)
				continue
			}
		}

		// 检查邮箱格式并判断是否唯一
		if newSysUser.Email != "" {
			if regular.ValidEmail(newSysUser.Email) {
				uniqueEmail := s.sysUserService.CheckUniqueByEmail(newSysUser.Email, newSysUser.UserId)
				if !uniqueEmail {
					// msg := fmt.Sprintf("用户编号：%s 用户邮箱：%s 已存在", rowNo, newSysUser.Email)
					msg := i18n.TTemplate(language, "user.import.emailExist", map[string]any{"id": rowNo, "email": newSysUser.Email})
					failureNum++
					failureMsgArr = append(failureMsgArr, msg)
					continue
				}
			} else {
				// msg := fmt.Sprintf("用户编号：%s 用户邮箱：%s 格式错误", rowNo, newSysUser.Email)
				msg := i18n.TTemplate(language, "user.import.emailFormat", map[string]any{"id": rowNo, "email": newSysUser.Email})
				failureNum++
				failureMsgArr = append(failureMsgArr, msg)
				continue
			}
		}

		if newSysUser.UserId <= 0 {
			newSysUser.CreateBy = operaName
			if s.sysUserService.Insert(newSysUser) > 0 {
				// msg := fmt.Sprintf("用户编号：%s 登录名称：%s 导入成功", rowNo, newSysUser.UserName)
				msg := i18n.TTemplate(language, "user.import.success", map[string]any{"id": rowNo, "name": newSysUser.UserName})
				successNum++
				successMsgArr = append(successMsgArr, msg)
			} else {
				// msg := fmt.Sprintf("用户编号：%s 登录名称：%s 导入失败", rowNo, newSysUser.UserName)
				msg := i18n.TTemplate(language, "user.import.fail", map[string]any{"id": rowNo, "name": newSysUser.UserName})
				failureNum++
				failureMsgArr = append(failureMsgArr, msg)
			}
			continue
		}

		// 如果用户已存在 同时 是否更新支持
		if newSysUser.UserId > 0 && body.Update {
			newSysUser.Password = "" // 密码不更新
			newSysUser.UpdateBy = operaName
			rows := s.sysUserService.Update(newSysUser)
			if rows > 0 {
				// msg := fmt.Sprintf("用户编号：%s 登录名称：%s 更新成功", rowNo, newSysUser.UserName)
				msg := i18n.TTemplate(language, "user.import.successUpdate", map[string]any{"id": rowNo, "name": newSysUser.UserName})
				successNum++
				successMsgArr = append(successMsgArr, msg)
			} else {
				// msg := fmt.Sprintf("用户编号：%s 登录名称：%s 更新失败", rowNo, newSysUser.UserName)
				msg := i18n.TTemplate(language, "user.import.failUpdate", map[string]any{"id": rowNo, "name": newSysUser.UserName})
				failureNum++
				failureMsgArr = append(failureMsgArr, msg)
			}
			continue
		}
	}

	message := ""
	if failureNum > 0 {
		// msg := fmt.Sprintf("很抱歉，导入失败！共 %d 条数据格式不正确，错误如下：", failureNum)
		msg := i18n.TTemplate(language, "user.import.failTip", map[string]any{"num": failureNum})
		message = strings.Join(append([]string{msg}, failureMsgArr...), "<br/>")
	} else {
		// msg := fmt.Sprintf("恭喜您，数据已全部导入成功！共 %d 条，数据如下：", successNum)
		msg := i18n.TTemplate(language, "user.import.successTip", map[string]any{"num": successNum})
		message = strings.Join(append([]string{msg}, successMsgArr...), "<br/>")
	}
	c.JSON(200, resp.OkMsg(message))
}
