OrganizationController.java 6.69 KB
/*
 * Copyright (c) 2018, 2028, glsx and/or its affiliates. All rights reserved.
 * GLSX PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.glsx.usercenter.controller;

import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
import com.glsx.common.annotation.AuthCheck;
import com.glsx.common.annotation.Log;
import com.glsx.common.constants.Constants;
import com.glsx.common.constants.ResultObject;
import com.glsx.common.utils.StringUtils;
import com.glsx.usercenter.constants.ResultCodeEnum;
import com.glsx.usercenter.pojo.menu.SysOrganization;
import com.glsx.usercenter.pojo.user.SysUser;
import com.glsx.usercenter.service.ISysOrganizationService;
import com.glsx.usercenter.utils.ShiroUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Zheng Xiajun
 * @ClassName: OrganizationController
 * @date 2019年1月22日
 */
@AuthCheck
@RestController
@RequestMapping("/organization")
public class OrganizationController extends BaseController {
    @Autowired
    private ISysOrganizationService organizationService;

    /**
     * 根据条件查询组织结构
     *
     * @param organization 请求体
     * @return 结果
     */
    @RequestMapping(value = "/getByParams", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultObject getListByParam(SysOrganization organization) {
        logger.info("/getListByParam param: {}", JSON.toJSONString(organization));

        ResultObject result;
        SysUser currentUser = ShiroUtil.getUser();

        // 如果当前用户为服务商则指定查找该服务商根组织的组织
        if (currentUser.getUserType() == Constants.USER_TYPE_SP && organization.getId() == null) {
            this.logger.info("==== /getListByParam id not specified, set to `groupId`.");
            organization.setId(currentUser.getGroupId());
        }

        List<SysOrganization> organizationList = this.organizationService.getListByParam(
                organization, currentUser);
        result = new ResultObject(ResultCodeEnum.SUCCESS.getCode(),
                ResultCodeEnum.SUCCESS.getMsg(), organizationList);

        return result;
    }

    /**
     * 根据条件查询组织结构
     *
     * @param organization 请求体
     * @return 结果
     * @deprecated V2.2.0 已废弃,使用 {@link #getListByParam(SysOrganization)}
     */
    @RequestMapping(value = "/getByParams2", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultObject getByParams2(SysOrganization organization) {
        logger.info("/getByParams2 param: {}", JSON.toJSONString(organization));

        ResultObject result;
        SysUser currentUser = ShiroUtil.getUser();
        Integer orgId = organization.getId();
        String orgName = StringUtils.isNotBlank(organization.getName())
                ? organization.getName().replaceAll(" ", "") : "";
        Byte orgStatus = organization.getOrgStatus();

        List<SysOrganization> organizationList;
        if (StringUtils.isNotBlank(orgName)) {
            // 根据 name 和 orgStatus 搜索
            organizationList = organizationService.selectByExample(orgName, orgStatus, currentUser);
        } else if (orgId != null && orgStatus != null) {
            // 根据 id 和 orgStatus 递归查询子菜单
            SysOrganization record = new SysOrganization();
            record.setId(orgId);
            record.setOrgStatus(orgStatus);
            organizationList = organizationService.findOrgByRecord(record);
        } else if (orgId != null) {
            // 根据 id 递归查询子菜单
            organizationList = organizationService.findOrgById(orgId);
        } else {
            // orgStatus 不为空则根据状态查询,如果为空则只查询一级菜单
            organizationList = organizationService.selectByExample(null, orgStatus,
                    currentUser);
        }

        result = new ResultObject(ResultCodeEnum.SUCCESS.getCode(),
                ResultCodeEnum.SUCCESS.getMsg(), organizationList);

        return result;
    }

    @Log(menuName = "系统管理-组织管理", operateType = Constants.SYS_LOG_OPERATE_TYPE_SAVE)
    @RequestMapping(value = "/add", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultObject add(SysOrganization sysOrganization) {
        Integer userId = ShiroUtil.getUserId();
        sysOrganization.setCreateTime(new Date());
        sysOrganization.setCreateBy(userId);
        if (sysOrganization.getOrgType() == null) {
            sysOrganization.setOrgType((byte) 1);
        }
        return organizationService.insertSelective(sysOrganization, userId);
    }

    @Log(menuName = "系统管理-组织管理", operateType = Constants.SYS_LOG_OPERATE_TYPE_DELETE)
    @RequestMapping(value = "/deleteById", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultObject deleteById(SysOrganization organization) {
        if (organization == null || organization.getId() == null) {
            return new ResultObject(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL);
        }
        return organizationService.deleteByPrimaryKey(organization.getId());
    }

    @Log(menuName = "系统管理-组织管理", operateType = Constants.SYS_LOG_OPERATE_TYPE_UPDATE)
    @RequestMapping(value = "/update", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultObject update(SysOrganization sysOrganization) {
        sysOrganization.setUpdateTime(new Date());
        organizationService.updateByPrimaryKeySelective(sysOrganization);
        return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg());
    }

    @RequestMapping(value = "/selectDetailById", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultObject selectDetailById(SysOrganization organization) {
        if (organization == null) {
            return new ResultObject(ResultCodeEnum.FAIL.getCode(), ResultCodeEnum.FAIL);
        }
        SysOrganization sysOrganization = organizationService.selectByPrimaryKey(organization.getId());
        SysOrganization parent = organizationService.selectByPrimaryKey(sysOrganization.getParentId());
        if (parent != null) {
            sysOrganization.setParentName(parent.getName());
        } else {
            sysOrganization.setParentName("无");
        }

        return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), sysOrganization);
    }

}