FrontUserController.java 11.1 KB
package com.lhcredit.project.business.frontUser.controller;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.pagehelper.PageHelper;
import com.lhcredit.common.constant.UserConstants;
import com.lhcredit.common.utils.JsonUtils;
import com.lhcredit.common.utils.StringUtils;
import com.lhcredit.common.utils.security.ShiroUtils;
import com.lhcredit.framework.web.page.PageDomain;
import com.lhcredit.framework.web.page.TableSupport;
import com.lhcredit.project.business.frontUser.domain.EmailAttachment;
import com.lhcredit.project.business.frontRole.domain.FrontRole;
import com.lhcredit.project.business.frontUser.domain.TempConf;
import com.lhcredit.project.business.frontRole.service.IFrontRoleService;
import com.lhcredit.project.business.frontUser.domain.FrontUserMon;
import com.lhcredit.project.business.templateConfiguration.service.ITemplateConfigurationService;
import com.lhcredit.project.system.dict.service.IDictDataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.lhcredit.framework.aspectj.lang.annotation.Log;
import com.lhcredit.framework.aspectj.lang.enums.BusinessType;
import com.lhcredit.project.business.frontUser.domain.FrontUser;
import com.lhcredit.project.business.frontUser.service.IFrontUserService;
import com.lhcredit.framework.web.controller.BaseController;
import com.lhcredit.framework.web.page.TableDataInfo;
import com.lhcredit.framework.web.domain.AjaxResult;
import com.lhcredit.common.utils.poi.ExcelUtil;

import javax.annotation.Resource;

/**
 * 前端用户信息操作处理
 *
 * @author lhcredit
 * @date 2024-05-13
 */
@Controller
@RequestMapping("/business/frontUser")
public class FrontUserController extends BaseController {
    private String prefix = "business/frontUser";

    @Autowired
    private IFrontUserService frontUserService;

    @Autowired
    private IFrontRoleService frontRoleService;

    @Resource
    private ITemplateConfigurationService templateConfigurationService;

    @Resource
    private IDictDataService dictDataService;

    @RequiresPermissions("business:frontUser:view")
    @GetMapping()
    public String frontUser() {
        return prefix + "/frontUser";
    }

    /**
     * 查询前端用户列表
     */
    @RequiresPermissions("business:frontUser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FrontUser frontUser) {
        //startPage();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNull(pageNum)) {
            pageNum = 1;
        }
        if (StringUtils.isNull(pageSize)) {
            pageSize = 10;
        }

        if (StringUtils.isNotEmpty(pageDomain.getOrderByColumn())){
            if (Objects.equals(pageDomain.getOrderByColumn(),"deptName")){
                PageHelper.startPage(pageNum, pageSize,pageDomain.getOrderBy());
            }else {
                PageHelper.startPage(pageNum, pageSize,pageDomain.getOrderByColumn() + " " + pageDomain.getIsAsc());
            }
        }else {
            PageHelper.startPage(pageNum, pageSize, "creditTime desc");
        }

        List<FrontUser> list = frontUserService.selectFrontUserList(frontUser);
        return getDataTable(list);
    }


    /**
     * 导出前端用户列表
     */
    @RequiresPermissions("business:frontUser:export")
    @Log(title = "前端用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FrontUser frontUser) {
        List<FrontUser> list = frontUserService.selectFrontUserList(frontUser);
        ExcelUtil<FrontUser> util = new ExcelUtil<FrontUser>(FrontUser. class);
        return util.exportExcel(list, "frontUser");
    }

    /**
     * 新增前端用户
     */
    @GetMapping("/add")
    public String add( ModelMap mmap) {
        mmap.put("roles", frontRoleService.selectFrontRoleList(new FrontRole()));
        return prefix + "/add";
    }

    /**
     * 新增前端用户
     */
    @GetMapping("/addVx")
    public String addVx(@RequestParam("openId") String openId, ModelMap mmap) {
        mmap.put("roles", frontRoleService.selectFrontRoleList(new FrontRole()));
        mmap.put("openId", openId);
        return prefix + "/add";
    }

    /**
     * 增加用户信息,根据选择的部门orgId,查询对应的模板名称
     * 机构名称 org
     * 类型 type 1:增加用户 2:编辑用户
     */
    @GetMapping("/getTempConfByOrgId")
    @ResponseBody
    public AjaxResult getTempConfByOrgId(Long orgId, Long userId) throws IOException {
        return AjaxResult.success(frontUserService.getTempConfByOrgId(orgId,userId));
    }

    /**
     * 新增保存前端用户
     */
    @RequiresPermissions("business:frontUser:add")
    @Log(title = "前端用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FrontUser frontUser) {
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(frontUserService.checkLoginNameUnique(frontUser.getLoginName()))) {
            return error("保存用户'" + frontUser.getLoginName() + "'失败,登录账号已存在");
        }
        frontUser.setCreateBy(ShiroUtils.getLoginName());
        frontUser.setCreateTime(new Date());
        frontUser.setUpdateBy(ShiroUtils.getLoginName());
        frontUser.setUpdateTime(new Date());
        return toAjax(frontUserService.insertFrontUser(frontUser));
    }

    /**
     * 新增保存前端用户
     */
    @RequiresPermissions("business:frontUser:add")
    @Log(title = "前端用户", businessType = BusinessType.INSERT)
    @PostMapping("/addNew")
    @ResponseBody
    public AjaxResult addSaveNew(FrontUser frontUser) {
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(frontUserService.checkLoginNameUnique(frontUser.getLoginName()))) {
            return error("保存用户'" + frontUser.getLoginName() + "'失败,登录账号已存在");
        }
        frontUser.setCreateBy(ShiroUtils.getLoginName());
        frontUser.setCreateTime(new Date());
        frontUser.setUpdateBy(ShiroUtils.getLoginName());
        frontUser.setUpdateTime(new Date());
        return toAjax(frontUserService.insertFrontUserNew(frontUser));
    }

    @Log(title = "批量创建用户监控默认分组、模板")
    @GetMapping("/createGroupTemplate")
    @ResponseBody
    public AjaxResult createGroupTemplate() {
        Date date = new Date();
        List<FrontUser> frontUsers = frontUserService.selectFrontUserList(new FrontUser());
        // 使用并行流替代原来的forEach
        frontUsers.parallelStream().forEach(frontUser -> {
            frontUserService.createGroupTemplate(frontUser, frontUser.getId(), date);
        });
        return AjaxResult.success();
    }

    /**
     * 修改前端用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) throws IOException {
        FrontUser frontUser =frontUserService.selectFrontUserById(id);
        /*if (StringUtils.isNotEmpty(frontUser.getReports())){
            List<TempConf> tempConfByOrg = frontUserService.getTempConfByOrgId(frontUser.getOrgId(),null);
            List<TempConf> tempConfList = JsonUtils.fromJson(frontUser.getReports(), new TypeReference<List<TempConf>>() {});

            // 将 tempConfList 转换为 Map,以便快速查找
            Map<String, TempConf> tempConfMap = tempConfList.stream()
                    .collect(Collectors.toMap(TempConf::getName, conf -> conf));

            // 遍历 tempConfByOrg,如果存在相同id的对象,则拷贝属性
            for (TempConf orgConf : tempConfByOrg) {
                TempConf sourceConf = tempConfMap.get(orgConf.getName());
                if (sourceConf != null) {
                    // 使用 Spring 的 BeanUtils 拷贝非空属性
                    BeanUtils.copyProperties(sourceConf, orgConf);
                }
            }
            mmap.put("tempConfList", tempConfByOrg);
        }
        if (StringUtils.isNotEmpty(frontUser.getEmailAttachmentInfo())){
            List<EmailAttachment> emailAttachmentList = JsonUtils.fromJson(frontUser.getReports(), new TypeReference<List<EmailAttachment>>() {});
            mmap.put("emailAttachmentList", emailAttachmentList);
        }*/
        mmap.put("frontUser", frontUser);
        mmap.put("roles", frontRoleService.selectRolesByUserId(Long.parseLong(id+"")));
        return prefix + "/edit";
    }

    /**
     * 修改保存前端用户
     */
    @RequiresPermissions("business:frontUser:edit")
    @Log(title = "前端用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FrontUser frontUser) {
        frontUser.setUpdateBy(ShiroUtils.getLoginName());
        frontUser.setUpdateTime(new Date());
        return toAjax(frontUserService.updateFrontUser(frontUser));
    }

    /**
     * 删除前端用户
     */
    @RequiresPermissions("business:frontUser:remove")
    @Log(title = "前端用户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(frontUserService.deleteFrontUserByIds(ids));
    }


    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(FrontUser user) {
        return frontUserService.checkLoginNameUnique(user.getLoginName());
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(FrontUser user) {
        return frontUserService.checkPhoneUnique(user);
    }


    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @GetMapping("/resetPwd/{id}")
    public String resetPwd(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("user", frontUserService.selectFrontUserById(id));
        return prefix + "/resetPwd";
    }


    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwd(FrontUserMon user) {
        user.setModifyPsdPage(1);
        return toAjax(frontUserService.resetUserPwd(user));
    }

    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(FrontUser user) {

        return toAjax(frontUserService.changeStatus(user));
    }

    /**
     * 根据部门 id  查询前台用户
     * @param orgId
     * @return
     */
    @GetMapping("/getFrontUserByOrgId/{orgId}")
    @ResponseBody
    public AjaxResult getFrontUserByOrgId(@PathVariable("orgId") Long orgId) {
        if (orgId == null) {
            return AjaxResult.error("orgId 不能为空");
        }
        List<FrontUser> frontUserList = frontUserService.getFrontUserByOrgId(orgId);
        return AjaxResult.success(frontUserList);
    }

}