package cn.estsh.i3plus.core.apiservice.controller.busi;

import cn.estsh.i3plus.core.api.iservice.busi.ICoreTreeService;
import cn.estsh.i3plus.core.api.iservice.busi.ISysOrganizeService;
import cn.estsh.i3plus.platform.common.util.PlatformConstWords;
import cn.estsh.i3plus.pojo.base.enumutil.ResourceEnumUtil;
import cn.estsh.i3plus.pojo.model.platform.CommonTreeModel;
import cn.estsh.i3plus.pojo.platform.bean.SysDepartment;
import cn.estsh.i3plus.pojo.platform.bean.SysMenu;
import cn.estsh.i3plus.pojo.platform.bean.SysOrganize;
import cn.estsh.i3plus.pojo.platform.bean.SysPosition;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.exception.ImppExceptionEnum;
import cn.estsh.impp.framework.boot.util.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description : 系统树服务
 * @Reference :
 * @Author : wei.peng
 * @Date : 2018-10-29 14:56
 * @Modify :
 **/
@RestController
@Api(description = "树服务")
@RequestMapping(PlatformConstWords.BASE_URL + "/sys-tree")
public class SysTreeController extends CoreBaseController {

    public static final Logger LOGGER = LoggerFactory.getLogger(SysLogSystemController.class);

    @Autowired
    private ICoreTreeService coreTreeService;

    @Autowired
    private ISysOrganizeService organizeService;

    @GetMapping(value="/position/get/{id}")
    @ApiOperation(value="查询岗位树",notes="通过部门主键获取岗位树，-1为根节点")
    public ResultBean listSysPositionTree(@PathVariable("id") long id) {
        try {
            SysPosition position = coreTreeService.getSysPositionTreeById(id);

            return ResultBean.success("查询部门树成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultObject(position);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/position/list/{parentId}")
    @ApiOperation(value="查询岗位树",notes="通过部门主键获取岗位树，-1为根节点")
    public ResultBean listSysPositionListTree(@PathVariable("parentId") long parentId) {
        try {
            List<SysPosition> positionList = coreTreeService.findSysPositionTreeByParentId(parentId);

            return ResultBean.success("查询部门树成功")
                    .setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode())
                    .setResultList(positionList);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/organize/get/{id}")
    @ApiOperation(value="查询组织树",notes="通过部门主键获取组织树，-1为根节点")
    public ResultBean listOrganizeTree(@PathVariable("id") long id) {
        try {
            SysOrganize obj = coreTreeService.getSysOrganizeTreeById(id);

            return ResultBean.success("查询部门树成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(obj);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/organize/list/{parentId}")
    @ApiOperation(value="查询组织树",notes="通过部门主键获取组织树，-1为根节点")
    public ResultBean listOrganizeListTree(@PathVariable("parentId") long parentId) {
        try {
            List<SysOrganize> resultList = coreTreeService.findSysOrganizeTreeByParentId(parentId);

            return ResultBean.success("查询部门树成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultList(resultList);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/department/get/{id}")
    @ApiOperation(value="部门树",notes="通过部门主键获取部门树，-1为根节点")
    public ResultBean listDepartmentTree(@PathVariable("id") long id) {
        try {
            SysDepartment obj = coreTreeService.getSysDepartmentTreeById(id);

            return ResultBean.success("查询部门树成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(obj);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/department/list/{parentId}")
    @ApiOperation(value="查询部门树",notes="通过部门主键获取部门树集合，-1为根节点")
    public ResultBean listDepartmentListTree(@PathVariable("parentId") long parentId) {
        try {
            List<SysDepartment> resultList = coreTreeService.findSysDepartmentTreeByParentId(parentId);

            return ResultBean.success("查询部门树成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultList(resultList);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/sys-menu/get/{id}")
    @ApiOperation(value="菜单树",notes="通过部门主键获取菜单树，-1为根节点")
    public ResultBean listSysMenuTree(@PathVariable("id") long id) {
        try {
            SysMenu obj = coreTreeService.getSysMenuTreeById(id);

            return ResultBean.success("查询部门树成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultObject(obj);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/sys-menu/list/{parentId}")
    @ApiOperation(value="查询菜单树",notes="通过组织主键获取菜单树集合，-1为根节点")
    public ResultBean listSysMenuListTree(@PathVariable("parentId") long parentId) {
        try {
            List<SysMenu> resultList = coreTreeService.findSysMenuTreeByParentId(parentId);

            return ResultBean.success("查询部门树成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultList(resultList);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }

    @GetMapping(value="/organize-department/list/{parentId}")
    @ApiOperation(value="查询组织-部门树",notes="通过组织主键获取组织-部门，-1为根节点")
    public ResultBean listOrganizeDepartmentListTree(@PathVariable("parentId") long parentId) {
        try {
            List<CommonTreeModel> resultList = coreTreeService.findOrganizeDepartment(parentId);

            return ResultBean.success("查询组织-部门树成功").setCode(ResourceEnumUtil.MESSAGE.SUCCESS.getCode()).setResultList(resultList);
        }catch(ImppBusiException busExcep){
            return ResultBean.fail(busExcep);
        }catch(Exception e){
            return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
        }
    }
}
