OrganizationController.java 5.72 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 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;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Zheng Xiajun
 * @ClassName: OrganizationController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2019年1月22日
 */
@RestController
@RequestMapping("/organization")
public class OrganizationController extends BaseController {


    @Autowired
    private ISysOrganizationService organizationService;

    @RequestMapping(value = "/getByParams", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultObject getByParams(SysOrganization sysOrganization) {

        SysUser sysUser = ShiroUtil.getUser();

        Integer id = sysOrganization.getId();
        String name = sysOrganization.getName();
        Byte orgStatus = sysOrganization.getOrgStatus();

        logger.info("接收到的id参数值为:" + id);
        if (!StringUtils.isNull(name)) {
            name = name.replaceAll(" ", "");
        }

        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNull(name) && orgStatus == null && id == null) {
            // 菜单状态:1,正常、0.停用、2.注销
            // 只查询一级菜单
            return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(),
                    organizationService.selectByExample(null, null, sysUser));
        } else {
            if (StringUtils.isNull(name) && (orgStatus != null || id != null)) {
                if (orgStatus == null) {
                    // 根据id递归查询子菜单
                    return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(),
                            organizationService.findOrgById(id));
                } else if (id != null) {
                    // // 根据id和状态递归查询子菜单
                    map.put("orgStatus", orgStatus);
                    map.put("id", id);
                    return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(),
                            organizationService.findOrgsByParams(map));
                }
            }
            // 根据名字和状态搜索
            return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(),
                    organizationService.selectByExample(name, orgStatus, sysUser));
        }
    }

    @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);
    }

}