cy
2023-11-27 20c30d26f8bab22a4cb6b6459962fa92d6da490f
consum-base/src/main/java/com/consum/base/controller/FinSysTenantUserController.java
@@ -1,16 +1,24 @@
package com.consum.base.controller;
import cn.hutool.core.util.ReflectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.consum.base.BaseController;
import com.consum.base.core.utils.MapperUtil;
import com.consum.base.pojo.FinSysTenantUserSearchParam;
import com.consum.base.pojo.FinSysTenantUserUpdParam;
import com.consum.base.pojo.response.DepartmentVO;
import com.consum.base.pojo.response.FinSysTenantUserVO;
import com.consum.base.pojo.response.FinSysTenantVO;
import com.consum.base.pojo.response.UserVO;
import com.consum.base.service.FinSysTenantDepartmentServiceImpl;
import com.consum.base.service.FinSysTenantServiceImpl;
import com.consum.base.service.FinSysTenantUserServiceImpl;
import com.consum.model.po.FinSysTenant;
import com.consum.model.po.FinSysTenantDepartment;
import com.consum.model.po.FinSysTenantUser;
import com.iplatform.base.ArgumentsConstants;
import com.iplatform.base.service.DeptServiceImpl;
@@ -25,12 +33,19 @@
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.*;
import com.walker.web.ResponseValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
/**
@@ -38,6 +53,7 @@
 * @Author wh
 * @Date 2023/7/17 14:16
 */
@Api(value = "机构用户信息", tags = "机构用户信息")
@RestController
@RequestMapping("/pc/fin/sys/tenant/user")
public class FinSysTenantUserController extends BaseController {
@@ -54,6 +70,9 @@
    private RoleServiceImpl roleService;
    @Autowired
    private FinSysTenantDepartmentServiceImpl finSysTenantDepartmentService;
    @Autowired
    public void setUserService(UserServiceImpl userService) {
@@ -63,7 +82,8 @@
    private S_config sConfig;
    @Autowired
    public FinSysTenantUserController(FinSysTenantServiceImpl finSysTenantService, FinSysTenantUserServiceImpl finSysTenantUserService, RoleServiceImpl roleService) {
    public FinSysTenantUserController(FinSysTenantServiceImpl finSysTenantService, FinSysTenantUserServiceImpl finSysTenantUserService,
        RoleServiceImpl roleService) {
        this.finSysTenantUserService = finSysTenantUserService;
        this.finSysTenantService = finSysTenantService;
        this.roleService = roleService;
@@ -87,9 +107,11 @@
        }
        GenericPager<FinSysTenantUser> pager = this.finSysTenantUserService.queryAllPageUser(param);
        List<FinSysTenantUserVO> resultList = Lists.newArrayList();
        List<FinSysTenantUser> datas = pager.getDatas();
        if (datas.size() > 0) {
            for (FinSysTenantUser finSysTenantUser : datas) {
                FinSysTenantUserVO userVO = new FinSysTenantUserVO();
                if (finSysTenantUser.getUserPhone() != null) {
                    // 解密并脱敏
                    String tel = null;
@@ -125,9 +147,38 @@
                } else {
                    finSysTenantUser.setIsSupplier("否");
                }
                BeanUtils.copyProperties(finSysTenantUser, userVO);
                Long sysDeptId = finSysTenantUser.getSysDeptId();
                FinSysTenantDepartment department = finSysTenantDepartmentService.getById(sysDeptId);
                if (Objects.nonNull(department)) {
                    userVO.setSysDeptName(department.getName());
                }
                resultList.add(userVO);
            }
        }
        try {
            Field fieldDatas = GenericPager.class.getDeclaredField("datas");
//            fieldDatas.setAccessible(true);
//            fieldDatas.set(pager, resultList);
            ReflectUtil.setFieldValue(pager, fieldDatas, resultList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseValue.success(pager);
    }
    @ApiOperation(value = "根据机构id查询用户", notes = "根据机构id查询用户")
    @ApiImplicitParam(name = "tenantId", value = "机构id", required = true, dataType = "Long", paramType = "query")
    @GetMapping("/query/user")
    public ResponseValue queryUserByTenantId(Long tenantId) {
        FinSysTenantUser finSysTenantUser = new FinSysTenantUser();
        finSysTenantUser.setTenantId(String.valueOf(tenantId));
        List<FinSysTenantUser> result = finSysTenantUserService.select(finSysTenantUser);
        return ResponseValue.success(result);
    }
@@ -159,16 +210,15 @@
        user.setUpdateTime(DateUtils.getDateTimeNumber(System.currentTimeMillis()));
        user.setUpdateBy(this.getCurrentUser().getUser_name());
        //user.setStatus(1);// 0禁用 1启用
        user.setIsDelete(0);// 是否删除 0是 1否
        user.setIsDelete(0);
        user.setSysUserId(NumberGenerator.getLongSequenceNumber());
        // 加密手机号
        String key = PlatformRSAUtils.AES_KEY;
        String pwdPhone = "";
        String encPhone = "";
        if (user.getUserPhone() != null) {
            pwdPhone = AESUtils.encryptStrAES(user.getUserPhone(), key);
            encPhone = AESUtils.encryptStrAES(user.getUserPhone(), key);
        }
        user.setUserPhone(pwdPhone);
        user.setIsDelete(1);
        user.setUserPhone(encPhone);
        //怎么获取左侧机构树数据
        user.setSysDeptId(3L); //对应平台机构id  默认为平台管理
        this.finSysTenantUserService.insert(user);
@@ -182,7 +232,7 @@
        userCore.setNick_name(user.getUserName());
        //用户类型 先默认设置为2
        userCore.setUser_type(2);
        userCore.setPhonenumber(pwdPhone);
        userCore.setPhonenumber(encPhone);
        userCore.setSex(user.getSex().toString());
        //初始化密码 123456
        String pws = this.getArgumentVariable(ArgumentsConstants.KEY_SECURITY_PASSWORD_INIT).getStringValue();
@@ -229,8 +279,8 @@
    }
    public static class ImportUserParam {
    public static class ImportUserParam{
        @ExcelProperty("用户姓名")
        private String userName;
        @ExcelProperty("用户编号")
@@ -249,42 +299,55 @@
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getUserCode() {
            return userCode;
        }
        public void setUserCode(String userCode) {
            this.userCode = userCode;
        }
        public String getUserPhone() {
            return userPhone;
        }
        public void setUserPhone(String userPhone) {
            this.userPhone = userPhone;
        }
        public Integer getSex() {
            return sex;
        }
        public void setSex(Integer sex) {
            this.sex = sex;
        }
        public String getEmail() {
            return email;
        }
        public void setEmail(String email) {
            this.email = email;
        }
        public Long getSeq() {
            return seq;
        }
        public void setSeq(Long seq) {
            this.seq = seq;
        }
        public String getRoleName() {
            return roleName;
        }
        public void setRoleName(String roleName) {
            this.roleName = roleName;
        }
@@ -296,35 +359,35 @@
     * @Date 2023/10/12 15:56
     */
    @PostMapping("/import")
    public ResponseValue importUser(@RequestParam String tenantId,@RequestParam String tenantCode,MultipartFile file) throws IOException {
    public ResponseValue importUser(@RequestParam String tenantId, @RequestParam String tenantCode, MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        if (!"xls".equals(originalFilename.substring(originalFilename.lastIndexOf(".") + 1))){
        if (!"xls".equals(originalFilename.substring(originalFilename.lastIndexOf(".") + 1))) {
            return ResponseValue.error("文件格式有误!");
        }
        S_user_core currentUser = this.getCurrentUser();
        if (currentUser==null){
        if (currentUser == null) {
            return ResponseValue.error("当前登录用户为空");
        }
        if (StringUtils.isEmpty(tenantId)||StringUtils.isEmpty(tenantCode)){
        if (StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(tenantCode)) {
            return ResponseValue.error("添加时请选择区县");
        }
        List<S_role> roles = this.roleService.select(new S_role());
        ArrayList<FinSysTenantUser> finSysTenantUsers = new ArrayList<>();
        EasyExcel.read(file.getInputStream(), ImportUserParam.class, new AnalysisEventListener<ImportUserParam>(){
        EasyExcel.read(file.getInputStream(), ImportUserParam.class, new AnalysisEventListener<ImportUserParam>() {
            @Override
            public void invoke(ImportUserParam data, AnalysisContext context) {
                if (StringUtils.isEmpty(data.getUserName()) || StringUtils.isEmpty(data.getUserCode())  || StringUtils.isEmpty(data.getUserPhone()) || null==data.getSex()
                    || StringUtils.isEmpty(data.getEmail()) || null==data.getSeq() || StringUtils.isEmpty(data.getRoleName())){
                if (StringUtils.isEmpty(data.getUserName()) || StringUtils.isEmpty(data.getUserCode()) || StringUtils.isEmpty(data.getUserPhone())
                    || null == data.getSex() || StringUtils.isEmpty(data.getEmail()) || null == data.getSeq() || StringUtils.isEmpty(data.getRoleName())) {
                    throw new ImportUserParamVerifyException("单元格不能为空!");
                }
                //根据员工编号查询,如果存在则提示
                if ( finSysTenantUserService.getByUserCode(data.getUserCode()) > 0) {
                if (finSysTenantUserService.getByUserCode(data.getUserCode()) > 0) {
                    throw new ImportUserParamVerifyException("员工编号为空或已存在!");
                }
                if (data.getSex()<1&&data.getSex()>0){
                if (data.getSex() < 1 && data.getSex() > 0) {
                    throw new ImportUserParamVerifyException("性别码只能为 0 或 1");
                }
                FinSysTenantUser user = new FinSysTenantUser();
@@ -351,30 +414,34 @@
                user.setRemark("批量导入");
                // 加密手机号
                String key = PlatformRSAUtils.AES_KEY;
                String pwdPhone = AESUtils.encryptStrAES(data.getUserPhone(), key);
                user.setUserPhone(pwdPhone);
                String encPhone = AESUtils.encryptStrAES(data.getUserPhone(), key);
                user.setUserPhone(encPhone);
                user.setIsDelete(1);
                //怎么获取左侧机构树数据
                user.setSysDeptId(3L); //对应平台机构id  默认为平台管理
                finSysTenantUsers.add(user);
            }
            @Override
            public void doAfterAllAnalysed(AnalysisContext context) {
            }
            @Override
            public void onException(Exception exception, AnalysisContext context) throws Exception {
                if (exception!=null){
                if (exception != null) {
                    // 如果是某一个单元格的转换异常 能获取到具体行号 封装时异常
                    if (exception instanceof ExcelDataConvertException) {
                        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException)exception;
                        logger.error("第{}行,第{}列解析异常,数据为:{}",excelDataConvertException.getRowIndex(),
                                excelDataConvertException.getColumnIndex()+1, excelDataConvertException.getCellData().getStringValue());
                        throw new IllegalStateException("第"+(excelDataConvertException.getRowIndex()+1)+"行,第"+(excelDataConvertException.getColumnIndex()+1)+"列解析异常,异常数据为:[ "+excelDataConvertException.getCellData().getStringValue()+" ]");
                        ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
                        logger.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(), excelDataConvertException.getColumnIndex() + 1,
                            excelDataConvertException.getCellData().getStringValue());
                        throw new IllegalStateException(
                            "第" + (excelDataConvertException.getRowIndex() + 1) + "行,第" + (excelDataConvertException.getColumnIndex() + 1) + "列解析异常,异常数据为:[ "
                                + excelDataConvertException.getCellData().getStringValue() + " ]");
                    }
                    if (exception instanceof ImportUserParamVerifyException){
                        throw new IllegalStateException("第"+context.readSheetHolder().getRowIndex()+"行, 解析错误:[ "+ exception.getMessage()+" ]");
                    if (exception instanceof ImportUserParamVerifyException) {
                        throw new IllegalStateException("第" + context.readSheetHolder().getRowIndex() + "行, 解析错误:[ " + exception.getMessage() + " ]");
                    }
                    throw exception;
                }
@@ -390,7 +457,7 @@
     * 批量插入用户表和平台用户表
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertUserAndUserCoreBatch(List<FinSysTenantUser> finSysTenantUsers){
    public void insertUserAndUserCoreBatch(List<FinSysTenantUser> finSysTenantUsers) {
        ArrayList<S_user_core> sUserCores = new ArrayList<>();
        for (FinSysTenantUser user : finSysTenantUsers) {
            // 插入平台用户
@@ -447,9 +514,15 @@
        this.finSysTenantUserService.execInsertRoleUserList(finSysTenantUsers);
    }
    public static class ImportUserParamVerifyException extends RuntimeException{
        public ImportUserParamVerifyException(){super();}
        public ImportUserParamVerifyException(String message){super(message);}
    public static class ImportUserParamVerifyException extends RuntimeException {
        public ImportUserParamVerifyException() {
            super();
        }
        public ImportUserParamVerifyException(String message) {
            super(message);
        }
    }
@@ -465,11 +538,11 @@
            return ResponseValue.error("参数为空");
        }
        String key = PlatformRSAUtils.AES_KEY;
        String pwdPhone = "";
        String encPhone = "";
        if (user.getUserPhone() != null) {
            pwdPhone = AESUtils.encryptStrAES(user.getUserPhone(), key);
            encPhone = AESUtils.encryptStrAES(user.getUserPhone(), key);
        }
        user.setUserPhone(pwdPhone);
        user.setUserPhone(encPhone);
        // 1.更新系统用户   FIN_SYS_TENANT_USER
        finSysTenantUserService.update(user);
        // 2.更新平台用户  S_USER_CORE
@@ -593,8 +666,7 @@
    }
    /**
     * 得到 当前登录用户的信息
     * 增加 父类机构的code
     * 得到 当前登录用户的信息 增加 父类机构的code
     *
     * @param
     * @return
@@ -701,10 +773,10 @@
        // 加密手机号
        String key = PlatformRSAUtils.AES_KEY;
        String userPhone = user.getUserPhone();
        String pwdPhone = "";
        String encPhone = "";
        if (userPhone != null) {
            pwdPhone = AESUtils.encryptStrAES(userPhone, key);
            user.setUserPhone(pwdPhone);
            encPhone = AESUtils.encryptStrAES(userPhone, key);
            user.setUserPhone(encPhone);
        }
        this.finSysTenantUserService.insert(user);
@@ -798,12 +870,12 @@
        updUser.setEmail(user.getEmail());
        updUser.setUserName(user.getUserName());
        updUser.setSex(user.getSex());
        String pwdPhone = "";
        String encPhone = "";
        if (user.getUserPhone() != null) {
            String key = PlatformRSAUtils.AES_KEY;
            pwdPhone = AESUtils.encryptStrAES(user.getUserPhone(), key);
            encPhone = AESUtils.encryptStrAES(user.getUserPhone(), key);
        }
        updUser.setUserPhone(pwdPhone);
        updUser.setUserPhone(encPhone);
        // 1.更新系统用户   FIN_SYS_TENANT_USER
        finSysTenantUserService.update(updUser);
        // 2.更新平台用户  S_USER_CORE
@@ -875,4 +947,55 @@
        // 2. 修改平台用户(不用修改)
        return ResponseValue.success(1);
    }
    @ApiOperation(value = "根据机构id查询部门中的用户", notes = "根据机构id查询部门中的用户")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "agencyId", value = "机构id", required = true, dataType = "Long", paramType = "query")
    })
    @GetMapping("/select/department")
    public ResponseValue selectDepartment(Long agencyId) {
        FinSysTenantVO finSysTenantVO = new FinSysTenantVO();
        FinSysTenant finSysTenant = new FinSysTenant();
        finSysTenant.setId(agencyId);
        FinSysTenant sysTenant = finSysTenantService.select(finSysTenant).get(0);
        finSysTenantVO.setId(sysTenant.getId());
        finSysTenantVO.setName(sysTenant.getName());
        finSysTenantVO.setType("tenant");
        // 部门
        FinSysTenantDepartment finSysTenantDepartment = new FinSysTenantDepartment();
        finSysTenantDepartment.setTenantId(agencyId);
        List<FinSysTenantDepartment> tenantDepartmentList = finSysTenantDepartmentService.select(finSysTenantDepartment);
        List<DepartmentVO> departmentVOList = Lists.newArrayList();
        tenantDepartmentList.forEach(department -> {
            DepartmentVO departmentVO = new DepartmentVO();
            //用户
            Long id = department.getId();
            departmentVO.setId(id);
            departmentVO.setName(department.getName());
            departmentVO.setType("department");
            String sql = "SELECT SYS_USER_ID,USER_NAME FROM "
                + "fin_sys_tenant_user "
                + "WHERE SYS_DEPT_ID =:deptId AND SYS_USER_ID NOT IN(SELECT MANAGER_ID FROM base_warehouse_manager)";
            Map<String, Object> param = new HashMap<>();
            param.put("deptId", id);
            List<Map<String, Object>> select = finSysTenantUserService.select(sql, param, new MapperUtil());
            List<UserVO> userVOList = Lists.newArrayList();
            select.forEach(user -> {
                UserVO userVO = new UserVO();
                userVO.setId((Long) user.get("sysUserId"));
                userVO.setName(user.get("userName").toString());
                userVO.setType("user");
                userVOList.add(userVO);
            });
            departmentVO.setChildren(userVOList);
            departmentVOList.add(departmentVO);
        });
        finSysTenantVO.setChildren(departmentVOList);
        return ResponseValue.success(finSysTenantVO);
    }
}