package org.jeecg.modules.system.controller;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basedata.entity.FdPersonnel;
import org.jeecg.modules.basedata.service.IFdPersonnelService;
import org.jeecg.modules.system.entity.*;
import org.jeecg.modules.system.model.DepartIdModel;
import org.jeecg.modules.system.model.SysUserDepartsVO;
import org.jeecg.modules.system.service.ISysUserDepartService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author scott
 * @since 2018-12-20
 */
@Slf4j
@RestController
@RequestMapping("/sys/user")
public class SysUserController {

	@Autowired
	private ISysUserService sysUserService;

	@Autowired
	private ISysUserRoleService sysUserRoleService;

	@Autowired
	private ISysUserDepartService sysUserDepartService;

	@Autowired
	private ISysUserRoleService userRoleService;
	@Autowired
	private IFdPersonnelService fdPersonnelService;

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result<IPage<SysUserSelect>> queryPageList(SysUserSelect user, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
		Result<IPage<SysUserSelect>> result = new Result<IPage<SysUserSelect>>();
		//用户真实姓名模糊查询
        String realname = user.getRealname();
        if (StringUtils.isNotBlank(user.getRealname())) {
            user.setRealname(null);
        }
        //账号
        String username = user.getUsername();
        if (StringUtils.isNotBlank(user.getUsername())) {
            user.setUsername(null);
        }
        QueryWrapper<SysUserSelect> queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap());
        queryWrapper.eq("su.del_flag","0");
        queryWrapper.orderByDesc("su.create_time");
        if (StringUtils.isNotBlank(realname)) {
            queryWrapper.like("su.realname", realname);
        }
        if (StringUtils.isNotBlank(username)) {
            queryWrapper.like("su.username", username);
        }
		Page<SysUserSelect> page = new Page<SysUserSelect>(pageNo, pageSize);
		IPage<SysUserSelect> pageList = sysUserService.getPageList(page, queryWrapper);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

    @RequestMapping(value = "/list1", method = RequestMethod.GET)
    @RequiresPermissions("url:user:view")//有权限
    @PermissionData(pageComponent = "system/UserList")
    public Result<IPage<SysUserSelect>> queryPageList1(SysUserSelect user,@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,HttpServletRequest req) {
        Result<IPage<SysUserSelect>> result = new Result<IPage<SysUserSelect>>();
        //用户真实姓名模糊查询
        String realname = user.getRealname();
        if (StringUtils.isNotBlank(user.getRealname())) {
            user.setRealname(null);
        }
        //账号
        String username = user.getUsername();
        if (StringUtils.isNotBlank(user.getUsername())) {
            user.setUsername(null);
        }
        //角色
        List<String> roleIdList=user.getRoleIdList();
        List<String> userIdList=new ArrayList<>();//储存用户id
        if(roleIdList!=null&&roleIdList.size()>0){
            //根据角色查询用户角色数据
            LambdaQueryWrapper<SysUserRole> sysUserRoleLambdaQueryWrapper=new LambdaQueryWrapper<>();
            sysUserRoleLambdaQueryWrapper.in(SysUserRole::getRoleId,roleIdList);
            List<SysUserRole> sysUserRoleList=sysUserRoleService.list(sysUserRoleLambdaQueryWrapper);
            //判断是否为空
            if(sysUserRoleList!=null&&sysUserRoleList.size()>0){
                //循环获取用户id
                for(SysUserRole sysUserRole:sysUserRoleList){
                    userIdList.add(sysUserRole.getUserId());
                }
                //去重
                removeDuplicate(userIdList);
            }
            user.setRoleIdList(null);
        }else{
            user.setRoleIdList(null);
        }

        QueryWrapper<SysUserSelect> queryWrapper = QueryGenerator.initQueryWrapper(user, req.getParameterMap());
        queryWrapper.eq("su.del_flag","0");
        queryWrapper.orderByDesc("su.create_time");
        if (StringUtils.isNotBlank(realname)) {
            queryWrapper.like("su.realname", realname);
        }
        if (StringUtils.isNotBlank(username)) {
            queryWrapper.like("su.username", username);
        }
        if(roleIdList!=null&&roleIdList.size()>0){
            if(userIdList.size()>0){
                queryWrapper.in("su.id",userIdList);
            }else{
                queryWrapper.eq("su.id","无");
            }
        }
        Page<SysUserSelect> page = new Page<SysUserSelect>(pageNo, pageSize);
        IPage<SysUserSelect> pageList = sysUserService.getPageList(page, queryWrapper);
        getUserRoleName(pageList.getRecords());
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
    * @Author chenchuang
    * @Description //TODO 集合去重
    * @Date 2020/6/29 14:44
    * @Param [list]
    * @return java.util.List
    */
    public   static   List  removeDuplicate(List list)  {
        for  ( int  i  =   0 ; i  <  list.size()  -   1 ; i ++ )  {
            for  ( int  j  =  list.size()  -   1 ; j  >  i; j -- )  {
                if  (list.get(j).equals(list.get(i)))  {
                    list.remove(j);
                }
            }
        }
        return list;
    }

    //获取角色名称
    public void getUserRoleName(List<SysUserSelect> sysUserSelectList){
        //获取全部人员的角色
        List<SysUser> sysUserList=sysUserService.getUserRole();
        //是否为空
        if(sysUserList!=null&&sysUserList.size()>0){
            //查询的全部人员
            for(SysUserSelect sysUserSelect:sysUserSelectList){
                String roleName="";//角色名称
                for(SysUser sysUser:sysUserList){
                    //找到对应用户的角色
                    if(sysUserSelect.getId().equals(sysUser.getId())){
                        if(roleName.equals("")){//第一次找到
                            roleName=sysUser.getRoleName();
                        }else{//依次找到
                            roleName=roleName+"、"+sysUser.getRoleName();
                        }
                    }
                }
                //获取全部角色的名称
                sysUserSelect.setRoleName(roleName);
            }
        }
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RequiresPermissions("user:add")
	public Result<SysUser> add(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		String selectedRoles = jsonObject.getString("selectedroles");
		try {
			SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
			user.setCreateTime(new Date());//设置创建时间
			String salt = oConvertUtils.randomGen(8);
			user.setSalt(salt);
            String password = user.getPassword();
            if(password==null || "".equals(password)){
                password="123456";//设置初始密码
            }
			String passwordEncode = PasswordUtil.encrypt(user.getUsername(), password, salt);
			user.setPassword(passwordEncode);
			user.setStatus(1);
			user.setDelFlag("0");
			LambdaQueryWrapper<SysUser> queryWrapper=new LambdaQueryWrapper<>();
			queryWrapper.eq(SysUser::getUsername,user.getUsername());
            List<SysUser> userList = sysUserService.list(queryWrapper);
            if (userList.size()>0){
                result.error500("用户账号重复");
                return result;
            }
            sysUserService.addUserWithRole(user, selectedRoles);
			result.success("添加成功！");
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @RequiresPermissions("user:edit")
	public Result<SysUser> edit(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		try {
			SysUser sysUser = sysUserService.getById(jsonObject.getString("id"));
			if(sysUser==null) {
				result.error500("未找到对应实体");
			}else {
				SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
				user.setUpdateTime(new Date());
				//String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), sysUser.getSalt());
				user.setPassword(sysUser.getPassword());
				String roles = jsonObject.getString("selectedroles");
				sysUserService.editUserWithRole(user, roles);
				result.success("修改成功!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			result.error500("操作失败");
		}
		return result;
	}

    /*
     * @Title:agentPerson
     * @Description:TODO 为用户代理人员
     * @param [jsonObject]
     * @return org.jeecg.common.api.vo.Result<org.jeecg.modules.system.entity.SysUser>
     * @exception
     * @author licc
     * @date 2019/6/13 15:39
     * @updatehistory:
     *   2019/6/13 15:39 新增
     */
    @RequiresPermissions("user:edit")
    @RequestMapping(value = "/agentPerson", method = RequestMethod.PUT)
    public Result<SysUser> agentPerson(@RequestBody JSONObject jsonObject) {
        Result<SysUser> result = new Result<SysUser>();
        try {
            SysUser sysUser = sysUserService.getById(jsonObject.getString("id"));
            FdPersonnel personnelId = fdPersonnelService.getById(jsonObject.getString("personnelId"));
            if(sysUser==null || personnelId==null) {
                result.error500("未找到对应实体");
            }else {
                SysUser sysUser1 = new SysUser();
                sysUser1.setId(jsonObject.getString("id"));
                sysUser1.setRealname(personnelId.getName());
                sysUser1.setEmail(personnelId.getEmail());
                sysUser1.setPhone(personnelId.getMobile());
                sysUser1.setSex(Integer.parseInt(personnelId.getGender())+1);
                sysUser1.setPersonnelId(jsonObject.getString("personnelId"));
                sysUserService.updateById(sysUser1);
                result.success("关联成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.info(e.getMessage());
            result.error500("操作失败");
        }
        return result;
    }

	@RequestMapping(value = "/delete", method = RequestMethod.DELETE)
	
	public Result<SysUser> delete(@RequestParam(name="id",required=true) String id) {
		Result<SysUser> result = new Result<SysUser>();

		// 定义SysUserDepart实体类的数据库查询LambdaQueryWrapper
		LambdaQueryWrapper<SysUserDepart> query = new LambdaQueryWrapper<SysUserDepart>();
		SysUser sysUser = sysUserService.getById(id);
		if(sysUser==null) {
			result.error500("未找到对应实体");
		}else {
//			// 当某个用户被删除时,删除其ID下对应的部门数据
//			query.eq(SysUserDepart::getUserId, id);
//			boolean ok = sysUserService.removeById(id);
//			sysUserDepartService.remove(query);
//            // 当某个用户被删除时,删除其ID下对应的人员信息
//            String personnelId = sysUser.getPersonnelId();
//            LambdaQueryWrapper<FdPersonnel> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(FdPersonnel::getId,personnelId);
//            fdPersonnelService.remove(queryWrapper);
//			if(ok) {
//				result.success("删除成功!");
//			}
            //删除用户
            sysUser.setDelFlag("1");
            boolean ok=sysUserService.updateById(sysUser);
            //当某个用户被删除时,删除其ID下对应的人员信息
//            String personnelId = sysUser.getPersonnelId();
//            FdPersonnel fdPersonnel=fdPersonnelService.getById(personnelId);
//            fdPersonnel.setDelFlag("1");
//            fdPersonnelService.updateById(fdPersonnel);
            //是否删除成功
            if(ok) {
				result.success("删除成功!");
			}
        }

		return result;
	}

	@RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
	
	public Result<SysUser> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		// 定义SysUserDepart实体类的数据库查询对象LambdaQueryWrapper
		LambdaQueryWrapper<SysUserDepart> query = new LambdaQueryWrapper<SysUserDepart>();
		String[] idArry = ids.split(",");
		Result<SysUser> result = new Result<SysUser>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.sysUserService.removeByIds(Arrays.asList(ids.split(",")));
			// 当批量删除时,删除在SysUserDepart中对应的所有部门数据
			for(String id : idArry) {
				query.eq(SysUserDepart::getUserId, id);
				this.sysUserDepartService.remove(query);
			}
			result.success("删除成功!");
		}
		return result;
	}

	/**
	  * 冻结&解冻用户
	 * @param jsonObject
	 * @return
	 */
	
	@RequestMapping(value = "/frozenBatch", method = RequestMethod.PUT)
	public Result<SysUser> frozenBatch(@RequestBody JSONObject jsonObject) {
		Result<SysUser> result = new Result<SysUser>();
		try {
			String ids = jsonObject.getString("ids");
			String status = jsonObject.getString("status");
			String[] arr = ids.split(",");
			for (String id : arr) {
				if(oConvertUtils.isNotEmpty(id)) {
					this.sysUserService.update(new SysUser().setStatus(Integer.parseInt(status)),
							new UpdateWrapper<SysUser>().lambda().eq(SysUser::getId,id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.error500("操作失败"+e.getMessage());
		}
		result.success("操作成功!");
		return result;

    }

    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<SysUser> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<SysUser> result = new Result<SysUser>();
        SysUser sysUser = sysUserService.getById(id);
        if (sysUser == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(sysUser);
            result.setSuccess(true);
        }
        return result;
    }

    @RequestMapping(value = "/queryUserRole", method = RequestMethod.GET)
    public Result<List<String>> queryUserRole(@RequestParam(name = "userid", required = true) String userid) {
        Result<List<String>> result = new Result<>();
        List<String> list = new ArrayList<String>();
        List<SysUserRole> userRole = sysUserRoleService.list(new QueryWrapper<SysUserRole>().lambda().eq(SysUserRole::getUserId, userid));
        if (userRole == null || userRole.size() <= 0) {
            result.error500("未找到用户相关角色信息");
        } else {
            for (SysUserRole sysUserRole : userRole) {
                list.add(sysUserRole.getRoleId());
            }
            result.setSuccess(true);
            result.setResult(list);
        }
        return result;
    }


    /**
     * 校验用户账号是否唯一<br>
     * 可以校验其他 需要检验什么就传什么。。。
     *
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "/checkOnlyUser", method = RequestMethod.GET)
    public Result<Boolean> checkUsername(SysUser sysUser) {
        Result<Boolean> result = new Result<>();
        result.setResult(true);//如果此参数为false则程序发生异常
        String id = sysUser.getId();
        log.info("--验证用户信息是否唯一---id:" + id);
        try {
            SysUser oldUser = null;
            if (oConvertUtils.isNotEmpty(id)) {
                oldUser = sysUserService.getById(id);
            } else {
                sysUser.setId(null);
            }
            //通过传入信息查询新的用户信息
            SysUser newUser = sysUserService.getOne(new QueryWrapper<SysUser>(sysUser));
            if (newUser != null) {
                //如果根据传入信息查询到用户了，那么就需要做校验了。
                if (oldUser == null) {
                    //oldUser为空=>新增模式=>只要用户信息存在则返回false
                    result.setSuccess(false);
                    result.setMessage("用户账号已存在");
                    return result;
                } else if (!id.equals(newUser.getId())) {
                    //否则=>编辑模式=>判断两者ID是否一致-
                    result.setSuccess(false);
                    result.setMessage("用户账号已存在");
                    return result;
                }
            }

        } catch (Exception e) {
            result.setSuccess(false);
            result.setResult(false);
            result.setMessage(e.getMessage());
            return result;
        }
        result.setSuccess(true);
        return result;
    }

    /**
     * 修改密码
     */
    @RequestMapping(value = "/changPassword", method = RequestMethod.PUT)
    
    public Result<SysUser> changPassword(@RequestBody SysUser sysUser) {
        Result<SysUser> result = new Result<SysUser>();
        String password = sysUser.getPassword();
        sysUser = this.sysUserService.getOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, sysUser.getUsername()));
        if (sysUser == null) {
            result.error500("未找到对应实体");
        } else {
            String salt = oConvertUtils.randomGen(8);
            sysUser.setSalt(salt);
            String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), password, salt);
            sysUser.setPassword(passwordEncode);
            this.sysUserService.updateById(sysUser);
            result.setResult(sysUser);
            result.success("密码修改完成！");
        }
        return result;
    }

    /**
     * 查询指定用户和部门关联的数据
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/userDepartList", method = RequestMethod.GET)
    public Result<List<DepartIdModel>> getUserDepartsList(@RequestParam(name = "userId", required = true) String userId) {
        Result<List<DepartIdModel>> result = new Result<>();
        try {
            List<DepartIdModel> depIdModelList = this.sysUserDepartService.queryDepartIdsOfUser(userId);
            if (depIdModelList != null || depIdModelList.size() > 0) {
                result.setSuccess(true);
                result.setMessage("查找成功");
                result.setResult(depIdModelList);
            } else {
                result.setSuccess(false);
                result.setMessage("查找失败");
            }
            return result;
        } catch (Exception e) {
            e.fillInStackTrace();
            result.setSuccess(false);
            result.setMessage("查找过程中出现了异常: " + e.getMessage());
            return result;
        }

    }

    /**
     * 给指定用户添加对应的部门
     *
     * @param sysUserDepartsVO
     * @return
     */
    @RequestMapping(value = "/addUDepartIds", method = RequestMethod.POST)
    public Result<String> addSysUseWithrDepart(@RequestBody SysUserDepartsVO sysUserDepartsVO) {
        boolean ok = this.sysUserDepartService.addSysUseWithrDepart(sysUserDepartsVO);
        Result<String> result = new Result<String>();
        try {
            if (ok) {
                result.setMessage("添加成功!");
                result.setSuccess(true);
            } else {
                throw new Exception("添加失败!");
            }
            return result;
        } catch (Exception e) {
            e.fillInStackTrace();
            result.setSuccess(true);
            result.setMessage("添加数据的过程中出现市场了: " + e.getMessage());
            return result;
        }

    }

    /**
     * 根据用户id编辑对应的部门信息
     *
     * @param sysUserDepartsVO
     * @return
     */
    @RequestMapping(value = "/editUDepartIds", method = RequestMethod.PUT)
    public Result<String> editSysUserWithDepart(@RequestBody SysUserDepartsVO sysUserDepartsVO) {
        Result<String> result = new Result<String>();
        boolean ok = sysUserDepartService.editSysUserWithDepart(sysUserDepartsVO);
        if (ok) {
            result.setMessage("更新成功!");
            result.setSuccess(true);
            return result;
        }
        result.setMessage("更新失败!");
        result.setSuccess(false);
        return result;
    }

    /**
     * 生成在添加用户情况下没有主键的问题,返回给前端,根据该id绑定部门数据
     *
     * @return
     */
    @RequestMapping(value = "/generateUserId", method = RequestMethod.GET)
    public Result<String> generateUserId() {
        Result<String> result = new Result<>();
        System.out.println("我执行了,生成用户ID==============================");
        String userId = UUID.randomUUID().toString().replace("-", "");
        result.setSuccess(true);
        result.setResult(userId);
        return result;
    }

    /**
     * 根据部门id查询用户信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryUserByDepId", method = RequestMethod.GET)
    public Result<List<SysUser>> queryUserByDepId(@RequestParam(name = "id", required = true) String id) {
        Result<List<SysUser>> result = new Result<>();
        List<SysUser> userList = sysUserDepartService.queryUserByDepId(id);
        try {
            result.setSuccess(true);
            result.setResult(userList);
            return result;
        } catch (Exception e) {
            e.fillInStackTrace();
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 查询所有用户所对应的角色信息
     *
     * @return
     */
    @RequestMapping(value = "/queryUserRoleMap", method = RequestMethod.GET)
    public Result<Map<String, String>> queryUserRole() {
        Result<Map<String, String>> result = new Result<>();
        Map<String, String> map = userRoleService.queryUserRole();
        result.setResult(map);
        result.setSuccess(true);
        return result;
    }

    /**
     * 导出excel
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HttpServletResponse response) {
        // Step.1 组装查询条件
        QueryWrapper<SysUser> queryWrapper = null;
        try {
            String paramsStr = request.getParameter("paramsStr");
            if (oConvertUtils.isNotEmpty(paramsStr)) {
                String deString = URLDecoder.decode(paramsStr, "UTF-8");
                SysUser sysUser = JSON.parseObject(deString, SysUser.class);
                queryWrapper = QueryGenerator.initQueryWrapper(sysUser, request.getParameterMap());
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //Step.2 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        List<SysUser> pageList = sysUserService.list(queryWrapper);
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "用户列表");
        mv.addObject(NormalExcelConstants.CLASS, SysUser.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("用户列表数据", "导出人:Jeecg", "导出信息"));
        mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
        return mv;
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile file = entity.getValue();// 获取上传文件对象
            ImportParams params = new ImportParams();
            params.setTitleRows(2);
            params.setHeadRows(1);
            params.setNeedSave(true);
            try {
                List<SysUser> listSysUsers = ExcelImportUtil.importExcel(file.getInputStream(), SysUser.class, params);
                for (SysUser sysUserExcel : listSysUsers) {
                    if (sysUserExcel.getPassword() == null) {
                        // 密码默认为“123456”
                        sysUserExcel.setPassword("123456");
                    }
                    sysUserService.save(sysUserExcel);
                }
                return Result.ok("文件导入成功！数据行数：" + listSysUsers.size());
            } catch (Exception e) {
                log.error(e.getMessage());
                return Result.error("文件导入失败！");
            } finally {
                try {
                    file.getInputStream().close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return Result.ok("文件导入失败！");
    }


    /**
     * 根据username查询realName
     *
     * @param username
     * @return
     */
    @RequestMapping(value = "/queryRealNameByUsername", method = RequestMethod.GET)
    public Result<SysUser> queryRealNameByUsername(@RequestParam(name = "username", required = true) String username) {
        Result<SysUser> result = new Result<>();
        SysUser user = sysUserService.queryRealNameByUsername(username);
        try {
            result.setSuccess(true);
            result.setResult(user);
            return result;
        } catch (Exception e) {
            e.fillInStackTrace();
            result.setSuccess(false);
            return result;
        }
    }

    /**
    * @Author chenchuang
    * @Description //TODO 查询角色下的人员
    * @Date 2020/7/20 11:22
    * @Param [roleId]
    * @return org.jeecg.common.api.vo.Result<java.util.List<org.jeecg.modules.system.entity.SysUser>>
    */
    @RequestMapping(value = "/getUserByRoleId")
    public Result<List<SysUser>> getUserByRoleId(@RequestParam(name = "roleId", required = true)String roleId){
        Result<List<SysUser>> result=new Result<>();
        try {
            List<SysUser> sysUserList=sysUserService.getUserByRoleId(roleId);
            result.setResult(sysUserList);
            result.success("操作成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.error500("操作失败："+e.getMessage());
        }
        return result;
    }
}
