package services

import "eta/eta_mini_crm_ht/models"

func GetSysUserList(condition string, pars []interface{}, startSize, pageSize int) (items []*models.SysUserMapping, err error) {
	// 查询系统用户
	sysUserList, err := models.GetSysUserListByCondition(condition, pars, startSize, pageSize)
	if err != nil {
		return
	}
	// 建立部门id和名字的映射
	departmentList, err := models.GetSysDepartments()
	if err != nil {
		return
	}
	departmentIdToName := make(map[int]string)
	for _, dep := range departmentList {
		departmentIdToName[dep.SysDepartmentId] = dep.SysDepartmentName
	}
	// 匹配系统用户
	items = make([]*models.SysUserMapping, 0)
	for _, user := range sysUserList {
		items = append(items,
			&models.SysUserMapping{
				SysUserId:          user.SysUserId,
				SysUserName:        user.SysUserName,
				SysRealName:        user.SysRealName,
				Email:              user.Email,
				Phone:              user.Phone,
				AreaCode:           user.AreaCode,
				SysRoleId:          user.SysRoleId,
				SysRoleName:        user.SysRoleName,
				SysDepartmentId:    user.SysDepartmentId,
				SysDepartmentName1: departmentIdToName[user.SysDepartmentId1],
				SysDepartmentName2: departmentIdToName[user.SysDepartmentId2],
				SysDepartmentName3: departmentIdToName[user.SysDepartmentId3],
				Province:           user.Province,
				City:               user.City,
				PositionName:       user.PositionName,
				PositionCode:       user.PositionCode,
				IsEnabled:          user.IsEnabled,
				CreateTime:         user.CreateTime,
				ModifyTime:         user.ModifyTime,
			})
	}
	return
}
