/**
 * Copyright &copy; 2013-2017 <a href="http://www.rhcncpa.com/">瑞华会计师事务所</a> All rights reserved.
 */
package com.jeeplus.modules.sys.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.dao.CompanyDao;
import com.jeeplus.modules.sys.dao.RoleDao;
import com.jeeplus.modules.sys.entity.*;
import com.jeeplus.modules.sys.service.CompanyService;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 机构Controller
 *
 * @author jeeplus
 * @version 2013-5-15
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/officetest")
public class OfficeTestController extends BaseController {

    @Autowired
    private OfficeService officeService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private CompanyDao companyDao;


    @ModelAttribute("office")
    public Office get(@RequestParam(required = false) String id) {
        if (StringUtils.isNotBlank(id)) {
            return officeService.get(id);
        } else {
            return new Office();
        }
    }

    @RequiresPermissions("sys:office:index")
    @RequestMapping(value = {""})
    public String index(Office office, Model model) {
//        model.addAttribute("list", officeService.findAll());
        return "modules/sys/officeIndex1";
    }
    @RequiresPermissions("sys:office:index")
    @RequestMapping(value = "list2" )
    public String inde2(Office office, Model model) {
//        model.addAttribute("list", officeService.findAll());
        return "modules/sys/officeIndex2";
    }

    @RequiresPermissions("sys:office:index")
    @RequestMapping(value = {"list"})
    public String list(Office office, Model model) {
        if (office == null || office.getParentIds() == null || office.getParentIds().equals("")) {
            model.addAttribute("list", officeService.findList(false));
        } else {
            model.addAttribute("list", officeService.findListt(office));
        }
        return "modules/sys/officeList";
    }

    @RequiresPermissions(value = {"sys:office:view", "sys:office:add", "sys:office:edit"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(Office office, Model model, HttpSession session, HttpServletRequest request) {
        String view = request.getParameter("view");
        if (!StringUtils.isBlank(view)) {
            if (view.equals("view")) {
                return "modules/sys/officeView";
            }
        }
        User user = UserUtils.getUser();
        if (office.getParent() == null || office.getParent().getId() == null) {
            office.setParent(user.getOffice());
        }
        office.setParent(officeService.get(office.getParent().getId()));
        if (office.getArea() == null) {
            office.setArea(user.getOffice().getArea());
        }
        if (!UserUtils.getSelectRole().get(0).getId().equals("1") && !user.getId().equals("1")) {
            Office selectCompany = UserUtils.getSelectCompany();
            if(!isSelectComp(selectCompany,office)){
                office.setParent(selectCompany);//非admin 只能创建部门 以及 当前公司的分公司
            }
        }
        if (StringUtils.isNotBlank(office.getMaster())) {
            office.setMasterName(UserUtils.get(office.getMaster()).getName());
        }
        // 自动获取排序号
        if (StringUtils.isBlank(office.getId()) && office.getParent() != null) {
            int size = 0;
            List<Office> list = officeService.findAll();
            for (int i = 0; i < list.size(); i++) {
                Office e = list.get(i);
                if (e.getParent() != null && e.getParent().getId() != null
                        && e.getParent().getId().equals(office.getParent().getId())) {
                    size++;
                }
            }
            office.setCode(office.getParent().getCode() + StringUtils.leftPad(String.valueOf(size > 0 ? size + 1 : 1), 3, "0"));
        }
        if (StringUtils.isBlank(office.getId())) {
            office.setType(null);
        } else if (StringUtils.isNotBlank(office.getId())) {
            session.setAttribute("state", "disabled");//禁止修改类型级别
            /*if(office.getParentIds().equals("0")||office.getCode().equals("100000")||office.getParentIds().equals("0,")){
				session.setAttribute("cname","no");//禁止修改公司名称
			}*/
        }

        model.addAttribute("office", office);
        return "modules/sys/officeForm";
    }

    private boolean isSelectComp(Office selectCompany, Office office) {
        if (office.getParent()==null||StringUtils.isBlank(office.getParent().getId())) {
            return false;
        }
        if (office.getParent().getId()==selectCompany.getId())return true;
        if(StringUtils.isNotBlank(office.getParent().getParentIds())){
            String[] split = office.getParent().getParentIds().split(",");
            for (String s : split) {
                if (s.equals(selectCompany.getId()))return true;
            }
        }
        return false;
    }

    @RequiresPermissions(value = {"sys:office:add", "sys:office:edit"}, logical = Logical.OR)
    @RequestMapping(value = "save")
    public String save(Office office, Model model, RedirectAttributes redirectAttributes, HttpSession session, HttpServletRequest request) throws Exception {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/sys/office/";
        }
        if (!beanValidator(model, office)) {
            return form(office, model, session, request);
        }
        if (office.getType().equals("1")) {
            if (office.getParent() == null || office.getParent().getId() == null) {
                office.setGrade("1");
            } else {
                office.setGrade("2");//分公司等级
            }
        } else if (office.getType().equals("2")) {
            office.setGrade("2");
        } else if (office.getType().equals("3")) {
            office.setGrade("2");
        } else {
            office.setGrade("3");
        }
        if (!office.getIsNewRecord()) {//编辑表单保存
            Office t = officeService.get(office.getId());//从数据库取出记录的值
            MyBeanUtils.copyBeanNotNull2Bean(office, t);//将编辑表单中的非NULL值覆盖数据库记录中的值
            officeService.save(office);
            addMessage(redirectAttributes, "修改成功");
            String id = "0".equals(office.getParentId()) ? "" : office.getParentId();
            return "redirect:" + adminPath + "/sys/office/list?id=" + id + "&parentIds=" + office.getParentIds();
        } else {//新增表单保存
            officeService.save(office);//保存
        }
        String str = "0";
        //如果是添加分公司，则分配相应部门和岗位
        try {
            Company company = new Company();
            company.setName(office.getName());
            company.setEmail(office.getEmail());
            company.setPhone(office.getPhone());
            company.setTuser(new User(office.getMaster()));
            company.setUSEABLE("1");
            String logo = "";
            if (office.getLogo() != null) {
                logo = office.getLogo();
            }
            company.setLogo(logo);
            if (office.getType().equals("1")) {
                str = companyService.applyCompanyOR(office, company, request);
            } else if (office.getType().equals("2")){
                Boolean b = companyService.applyOffice(office);
            } else if (office.getType().equals("3")) {
                Office com = new Office();
                if (!UserUtils.getUser().isAdmin()) {
                    com = UserUtils.getSelectCompany();
                } else {
                    com = office.getParent();
                }

                str = companyService.applyCompanyAnd(com, office, company, request);
            }
            if (office.getChildDeptList() != null) {

                Office childOffice = null;
                for (String id : office.getChildDeptList()) {
                    childOffice = new Office();
                    childOffice.setName(DictUtils.getDictLabel(id, "sys_office_common", "未知"));
                    childOffice.setParent(office);
                    childOffice.setArea(office.getArea());
                    childOffice.setType("2");
                    childOffice.setGrade(String.valueOf(Integer.valueOf(office.getGrade()) + 1));
                    childOffice.setUseable(Global.YES);
                    officeService.save(childOffice);
                }
            }
            if (str != null && "1".equals(str)) {
                addMessage(redirectAttributes, "保存机构'" + office.getName() + "'成功");
                logger.info("添加成功");
            } else if ("2".equals(str)) {
                addMessage(redirectAttributes, "创建公司管理员岗位失败");
                logger.info("创建公司管理员岗位失败");
            } else if ("3".equals(str)) {
                addMessage(redirectAttributes, "所选负责人数据异常");
            } else if ("0".equals(str)) {
                addMessage(redirectAttributes, "保存机构'" + office.getName() + "'成功");
            } else {
                logger.info("添加失败");
            }
        } catch (Exception e) {
            officeService.delete(office);
            addMessage(redirectAttributes, "创建公司管理员岗位失败" + e.getMessage());
        }
        String id = "0".equals(office.getParentId()) ? "" : office.getParentId();
		/*if(UserUtils.getUser().getId().equals(office.getMaster())&&office.getType().equals("1")){
			request.getSession().invalidate();
		}*/
        return "redirect:" + adminPath + "/sys/office/list?id=" + id + "&parentIds=" + office.getParentIds();
    }

    @RequiresPermissions("sys:office:del")
    @RequestMapping(value = "delete")
    public String delete(Office office, RedirectAttributes redirectAttributes, HttpServletRequest request) {
        if (Global.isDemoMode()) {
            addMessage(redirectAttributes, "演示模式，不允许操作！");
            return "redirect:" + adminPath + "/sys/office/list";
        }
        if (office.getParent() == null || office.getParent().getId() == null || office.getParent().getId().equals("0")) {
            addMessage(redirectAttributes, "删除机构失败,不能删除当前公司！");
            //return "redirect:" + adminPath + "/sys/office/list?id=" + office.getParentId() + "&parentIds=" + office.getParentIds();
            return "redirect:" + Global.getAdminPath() + "/sys/office/list";
        }
        if ("1".equals(office.getType()) || "3".equals(office.getType())) {
            addMessage(redirectAttributes, "删除机构失败,不能删除类型为公司的机构！");
            return "redirect:" + Global.getAdminPath() + "/sys/office/list";
        }
        //判断机构类型
        Company company = companyService.getByName(office.getName());
        if (company == null) {
            //机构非公司
            List<User> userList = systemService.findUserByOfficeId(office.getId());
            if (userList != null && userList.size() != 0) {
                addMessage(redirectAttributes, "删除机构失败,当前部门下存在用户！");
                //return "redirect:" + adminPath + "/sys/office/list?id=" + office.getParentId() + "&parentIds=" + office.getParentIds();
                return "redirect:" + Global.getAdminPath() + "/sys/office/list";
            } else {
//		if (Office.isRoot(id)){
//			addMessage(redirectAttributes, "删除机构失败, 不允许删除顶级机构或编号空");
//		}else{
                officeService.delete(office);
                addMessage(redirectAttributes, "删除机构成功");
//		}
            }
        } else {
            //获取当前用户公司
            Office officeo = UserUtils.getSelectCompany();
            Company companySign = companyService.getByName(officeo.getName());
            int sign = 0;
            //判断是否有子公司
            List<Office> office2s = officeService.getSon(company);
            if (office2s == null || office2s.size() == 0) {
                if ("1".equals(company.getUSEABLE())) {
                    //companyService.delete(company,1);
                    List<Useroffice> userOfficeList = companyService.getUserCompanyId(company);
                    List<Useroffice> userOfficeList2 = new ArrayList<Useroffice>();
                    //无用户情况
                    if (userOfficeList.size() == 0 || userOfficeList == null) {
                        //对目前公司记录数据进行逻辑删除
                        companyDao.updateCompanyId(company);
                        companyDao.deleteCompanyInfo(company);
                        //对岗位表进行逻辑删除sys_role
                        officeService.delete(office);
                        companyDao.updateRoleId(company);
                    } else {
                        //除去管理员岗位以外的用户
                        if (userOfficeList != null && userOfficeList.size() > 0) {
                            //for (Useroffice userOffice : userOfficeList) {
                            for (int i = 0; i < userOfficeList.size(); i++) {
                                Useroffice userOffice = userOfficeList.get(i);
                                User u = UserUtils.get(userOffice.getUserId());
                                List<Role> rlist = roleDao.findList(new Role(u));
                                for (Role role :
                                        rlist) {
                                    if (role.getCompany().getId().equals(company.getCompanyId())) {
                                        if (org.apache.commons.lang3.StringUtils.isNotBlank(role.getEnname()) && role.getEnname().contains("gly")) {
                                            userOfficeList.remove(userOffice);
                                        }
                                    }
                                }
                            }
                            userOfficeList2 = userOfficeList;
                        }
                        //判断是否存在用户
                        if (userOfficeList2 != null && userOfficeList2.size() > 0) {
                            addMessage(redirectAttributes, "该企业下存在用户，禁止删除");
                        } else {
                            //执行删除操作
                            if (companySign != null && !companySign.equals("")) {
                                if (companySign.getId().equals(company.getId())) {
                                    sign++;
                                }
                            }
                            companyService.delete(company, 1);
                            officeService.delete(office);
                            addMessage(redirectAttributes, "企业删除成功");
                        }
                    }
                } else {
                    companyService.deleteCompanyId(company);
                    if (companySign != null && !companySign.equals("")) {
                        if (companySign.getId().equals(company.getId())) {
                            sign++;
                        }
                    }
                    officeService.delete(office);
                    addMessage(redirectAttributes, "企业删除成功");
                }
            } else {
                //addMessage(redirectAttributes, "该企业下存在用户，禁止删除");

                for (Office office2:office2s) {
                    //删除子公司
                    Company company1 = companyService.getByName(office2.getName());
                    if ("1".equals(company1.getUSEABLE())) {
                        //companyService.delete(company,1);
                        List<Useroffice> userOfficeList = companyService.getUserCompanyId(company1);
                        List<Useroffice> userOfficeList2 = new ArrayList<Useroffice>();
                        //无用户情况
                        if (userOfficeList.size() == 0 || userOfficeList == null) {
                            //对目前公司记录数据进行逻辑删除
                            companyDao.updateCompanyId(company1);
                            companyDao.deleteCompanyInfo(company1);
                            //对岗位表进行逻辑删除sys_role
                            companyDao.updateRoleId(company1);
                            officeService.delete(office);
                        } else {
                            //除去管理员岗位以外的用户
                            if (userOfficeList != null && userOfficeList.size() > 0) {
                                //for (Useroffice userOffice : userOfficeList) {
                                for (int i = 0; i < userOfficeList.size(); i++) {
                                    Useroffice userOffice = userOfficeList.get(i);
                                    User u = UserUtils.get(userOffice.getUserId());
                                    List<Role> rlist = roleDao.findList(new Role(u));
                                    for (Role role :
                                            rlist) {
                                        if (role.getCompany().getId().equals(company1.getCompanyId())) {
                                            if (org.apache.commons.lang3.StringUtils.isNotBlank(role.getEnname()) && role.getEnname().contains("gly")) {
                                                userOfficeList.remove(userOffice);
                                            }
                                        }
                                    }
                                }
                                userOfficeList2 = userOfficeList;
                            }
                            //判断是否存在用户
                            if (userOfficeList2 != null && userOfficeList2.size() > 0) {
                                addMessage(redirectAttributes, "该企业下分公司存在用户，禁止删除");
                            } else {
                                //-------删除母公司------
                                if ("1".equals(company.getUSEABLE())) {
                                    //companyService.delete(company,1);
                                    List<Useroffice> userOfficeList3 = companyService.getUserCompanyId(company);
                                    List<Useroffice> userOfficeList22 = new ArrayList<Useroffice>();
                                    //无用户情况
                                    if (userOfficeList3.size() == 0 || userOfficeList3 == null) {
                                        //对目前公司记录数据进行逻辑删除
                                        companyDao.updateCompanyId(company);
                                        companyDao.deleteCompanyInfo(company);
                                        //对岗位表进行逻辑删除sys_role
                                        companyDao.updateRoleId(company);
                                        officeService.delete(office);
                                    } else {
                                        //除去管理员岗位以外的用户
                                        if (userOfficeList3 != null && userOfficeList3.size() > 0) {
                                            //for (Useroffice userOffice : userOfficeList3) {
                                            for (int i = 0; i < userOfficeList3.size(); i++) {
                                                Useroffice userOffice = userOfficeList3.get(i);
                                                User u = UserUtils.get(userOffice.getUserId());
                                                List<Role> rlist = roleDao.findList(new Role(u));
                                                for (Role role :
                                                        rlist) {
                                                    if (role.getCompany().getId().equals(company.getCompanyId())) {
                                                        if (org.apache.commons.lang3.StringUtils.isNotBlank(role.getEnname()) && role.getEnname().contains("gly")) {
                                                            userOfficeList3.remove(userOffice);
                                                        }
                                                    }
                                                }
                                            }
                                            userOfficeList22 = userOfficeList3;
                                        }
                                        //判断是否存在用户
                                        if (userOfficeList22 != null && userOfficeList22.size() > 0) {
                                            addMessage(redirectAttributes, "该企业下存在用户，禁止删除");
                                        } else {
                                            //执行删除操作
                                            companyService.delete(company1, 1);//删除分公司
                                            //执行删除操作
                                            companyService.delete(company, 1);
                                            if (companySign != null && !companySign.equals("")) {
                                                if (companySign.getId().equals(company.getId()) || companySign.getId().equals(company1.getId())) {
                                                    sign++;
                                                }
                                            }
                                            officeService.delete(office);
                                            addMessage(redirectAttributes, "企业删除成功");
                                        }
                                    }
                                } else {
                                    //执行删除操作
                                    companyService.delete(company1, 1);//删除分公司
                                    companyService.deleteCompanyId(company);
                                    if (companySign != null && !companySign.equals("")) {
                                        if (companySign.getId().equals(company.getId()) || companySign.getId().equals(company1.getId())) {
                                            sign++;
                                        }
                                    }
                                    officeService.delete(office);
                                    addMessage(redirectAttributes, "企业删除成功");
                                }
                            }
                        }
                    } else {
                        companyService.deleteCompanyId(company1);
                        officeService.delete(office);
                        addMessage(redirectAttributes, "企业删除成功");
                    }
                }
            }
            //删除当前登录企业时的处理
            if (sign != 0) {
                request.getSession().invalidate();
            }
        }
        //return "redirect:" + adminPath + "/sys/office/list?id=" + office.getParentId() + "&parentIds=" + office.getParentIds();
        return "redirect:" + Global.getAdminPath() + "/sys/office/list";
    }

    /**
     * 获取机构JSON数据。
     *
     * @param extId    排除的ID
     * @param type     类型（1：公司；2：部门/小组/其它：3：用户）
     * @param grade    显示级别
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(@RequestParam(required = false) String extId, @RequestParam(required = false) String type,
                                              @RequestParam(required = false) Long grade, @RequestParam(required = false) Boolean isAll, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<Office> list = officeService.findList(isAll);
        for (int i = 0; i < list.size(); i++) {
            Office e = list.get(i);
            if ((StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1))
                    && (type == null || (type != null && (type.equals("1") ? type.equals(e.getType()) : true)))
                    && (grade == null || (grade != null && Integer.parseInt(e.getGrade()) <= grade.intValue()))
                    && Global.YES.equals(e.getUseable())) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("pIds", e.getParentIds());
                map.put("name", e.getName());
                if (type != null && "3".equals(type)) {
                    map.put("isParent", true);
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    /**
     * 获取机构JSON数据。
     *
     * @param extId    排除的ID
     * @param type     类型（1：公司；2：部门/小组/其它：3：用户）
     * @param grade    显示级别
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeDataAll")
    public List<Map<String, Object>> treeDataAll(@RequestParam(required = false) String extId, @RequestParam(required = false) String type,
                                                 @RequestParam(required = false) Long grade, @RequestParam(required = false) Boolean isAll, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        Office office = new Office();
        office.setParentIds(UserUtils.getSelectCompany().getId());
        List<Office> list = officeService.findByParentIdsLike(office);
        for (int i = 0; i < list.size(); i++) {
            Office e = list.get(i);
            if ((StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1))
                    && (type == null || (type != null && (type.equals("1") ? type.equals(e.getType()) : true)))
                    && (grade == null || (grade != null && Integer.parseInt(e.getGrade()) <= grade.intValue()))
                    && Global.YES.equals(e.getUseable())) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("pIds", e.getParentIds());
                map.put("name", e.getName());
                if (type != null && "3".equals(type)) {
                    map.put("isParent", true);
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    /**
     * 获取岗位JSON数据。
     *
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeDataRole")
    public List<Map<String, Object>> treeDataRole(@RequestParam(required = false) String userId,@RequestParam(required = false) String extId, @RequestParam(required = false) String type,
                                                  @RequestParam(required = false) Long grade, @RequestParam(required = false) Boolean isAll, HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        Office office = new Office();
        office.setParentIds(UserUtils.getSelectCompany().getId());
        List<Office> list = officeService.findByParentIdsLike(office);
        for (int i = 0; i < list.size(); i++) {
            Office e = list.get(i);
            if ((StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId()) && e.getParentIds().indexOf("," + extId + ",") == -1))
                    && (type == null || (type != null && (type.equals("1") ? type.equals(e.getType()) : true)))
                    && (grade == null || (grade != null && Integer.parseInt(e.getGrade()) <= grade.intValue()))
                    && Global.YES.equals(e.getUseable())) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("id", e.getId());
                map.put("pId", e.getParentId());
                map.put("pIds", e.getParentIds());
                map.put("name", e.getName());
                if (type != null && "3".equals(type)) {
                    map.put("isParent", true);
                }
                mapList.add(map);
            }
        }
        Office company = new Office();
        if (StringUtils.isNotBlank(userId)) {
            company = UserUtils.get(userId).getCompany();
        } else {
            company = UserUtils.getSelectCompany();
        }
        Role roles = new Role();
        roles.setCompany(company);
        List<Role> roleList = UserUtils.getRoleListByCompany(roles);
        for (int i = 0; i < roleList.size(); i++) {
            Role e = roleList.get(i);
            Office o = officeService.get(e.getOffice());
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            map.put("name", e.getName());
            map.put("pId", o.getId());
            map.put("pIds", o.getParentIds()+","+o.getId());
            map.put("isParent", false);
            mapList.add(map);
        }
        return mapList;
        /*Office company = new Office();
        if (StringUtils.isNotBlank(userId)) {
            company = UserUtils.get(userId).getCompany();
        } else {
            company = UserUtils.getSelectCompany();
        }
        Role roles = new Role();
        roles.setCompany(company);
        List<Role> roleList = UserUtils.getRoleListByCompany(roles);
        for (int i = 0; i < roleList.size(); i++) {
            Role e = roleList.get(i);
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            map.put("name", e.getName());
            map.put("isParent", false);
            mapList.add(map);
        }*/
    }
    /**
     * 获取角色JSON数据。
     *
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeDataRoleActivity")
    public List<Map<String, Object>> treeDataRoleActivity(HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        Office company = UserUtils.getSelectCompany();
        SysRoleActivity roleActivity = new SysRoleActivity();
        roleActivity.setCompanyId(company.getId());
        List<SysRoleActivity> roleList = UserUtils.getRoleActivityListByCompany(roleActivity);
        for (int i = 0; i < roleList.size(); i++) {
            SysRoleActivity e = roleList.get(i);
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            map.put("name", e.getName());
            map.put("isParent", false);
            mapList.add(map);
        }
        return mapList;
    }
    /**
     * 获取角色JSON数据。
     *
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeDataRoleActivityEnname")
    public List<Map<String, Object>> treeDataRoleActivityEnname(HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        Office company = UserUtils.getSelectCompany();
        SysRoleActivity roleActivity = new SysRoleActivity();
        roleActivity.setCompanyId(company.getId());
        List<SysRoleActivity> roleList = UserUtils.getRoleActivityListByCompany(roleActivity);
        for (int i = 0; i < roleList.size(); i++) {
            SysRoleActivity e = roleList.get(i);
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getEnname());
            map.put("name", e.getName());
            map.put("isParent", false);
            mapList.add(map);
        }
        return mapList;
    }

    @RequestMapping("compInfo")
    @ResponseBody
    public Office queryCompInfo(String id){
        Office office = officeService.get(id);
        return  office;
    }
}
