/**
* Copyright © 2013-2017 瑞华会计师事务所 All rights reserved.
*/
package com.jeeplus.modules.sys.web;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.MyBeanUtils;
import com.jeeplus.modules.sys.dao.CompanyDao;
import com.jeeplus.modules.sys.dao.OfficeDao;
import com.jeeplus.modules.sys.dao.RoleDao;
import com.jeeplus.modules.sys.dao.UserDao;
import com.jeeplus.modules.sys.entity.*;
import com.jeeplus.modules.sys.service.CompanyService;
import com.jeeplus.modules.sys.service.SystemService;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import com.jeeplus.modules.workclientinfo.entity.WorkClientLinkman;
import com.jeeplus.modules.workstaff.entity.WorkStaffBasicInfo;
import com.jeeplus.modules.workstaff.service.WorkStaffBasicInfoService;
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 com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.sys.service.OfficeService;
import com.jeeplus.modules.sys.utils.DictUtils;
import com.jeeplus.modules.sys.utils.UserUtils;
/**
* 机构Controller
*
* @author jeeplus
* @version 2013-5-15
*/
@Controller
@RequestMapping(value = "${adminPath}/sys/office")
public class OfficeController extends BaseController {
@Autowired
private OfficeService officeService;
@Autowired
private SystemService systemService;
@Autowired
private CompanyService companyService;
@Autowired
private RoleDao roleDao;
@Autowired
private CompanyDao companyDao;
@Autowired
private WorkStaffBasicInfoService workStaffBasicInfoService;
@Autowired
private OfficeDao officeDao;
@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 = {"list",""})
public String index(Office office, Model model) {
User user=UserUtils.get(UserUtils.getUser().getId());
List offices = new ArrayList<>();
if (office == null || office.getParentIds() == null || office.getParentIds().equals("")) {
offices = officeService.findListIndex(office);
if (offices!=null && offices.size()!=0){
boolean b = true;
for (Office office1:offices){
if (office1.getType().equals("1")){
office1.setParent(new Office("0"));
b = false;
break;
}
}
List roleList = roleDao.getManagerRoleList(UserUtils.getUser().getId(),UserUtils.getSelectCompany().getId());
if(roleList!=null&&roleList.size()>0){
for (Office office1:offices){
if (office1.getType().equals("3") && b){
for(Role role:roleList){
if(office1.getId().equals(role.getOffice().getId())){
office1.setParent(new Office("0"));
}
}
}
}
}else{
for (Office office1:offices){
if (office1.getType().equals("3") && b){
office1.setParent(new Office("0"));
}
}
}
}
} else {
offices = officeService.findListt(office);
}
model.addAttribute("list", offices);
return "modules/sys/officeList";
}
/* @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")) {
if (StringUtils.isNotBlank(office.getMaster())) {
office.setMasterName(UserUtils.get(office.getMaster()).getName());
}
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 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/";
}
Office office1 = office.getParent();
if(office1 != null && office1.getName() != null) {
Office o = officeService.get(office.getParent().getId());
if (o.getType().equals("2")) {
if (!"2".equals(office.getType())) {
addMessage(redirectAttributes, "部门下不能添加分公司!");
return "redirect:" + adminPath + "/sys/office/";
}
}
}
if (!beanValidator(model, office)) {
return form(office, model, session, request);
}
Office c = UserUtils.getSelectCompany();
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值覆盖数据库记录中的值
t.setIsSon(c.getIsSon());
officeService.save(t);
addMessage(redirectAttributes, "修改成功");
String id = "0".equals(office.getParentId()) ? "" : office.getParentId();
return "redirect:" + adminPath + "/sys/office/list";
} else {//新增表单保存
office.setIsSon(c.getIsSon());
officeService.save(office);//保存
}
String str = "0";
//如果是添加分公司,则分配相应部门和岗位
try {
Company company = new Company();
company.setName(office.getTopCompany());
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.getTopCompany() + "'成功");
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.getTopCompany() + "'成功");
} else {
logger.info("添加失败");
}
} catch (Exception e) {
logger.info("Exception e:"+e);
officeService.delete(office);
addMessage(redirectAttributes, "创建公司管理员岗位失败");
}
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/?repage";
}
@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 ("1".equals(office.getId())||"1".equals(office.getParentId())) {
addMessage(redirectAttributes, "删除机构失败,不能删除当前机构!");
//return "redirect:" + adminPath + "/sys/office/list?id=" + office.getParentId() + "&parentIds=" + office.getParentIds();
return "redirect:" + Global.getAdminPath() + "/sys/office/?repage";
}
int cou = systemService.countByOffice(office);
if (cou > 0) {
addMessage(redirectAttributes, "删除机构失败,当前机构下存在用户!");
return "redirect:" + Global.getAdminPath() + "/sys/office/?repage";
}
officeService.logicDelete(office);
if(UserUtils.getSelectCompany().getId().equals(office.getId())){
systemService.removeSession(UserUtils.getUser());
return "modules/sys/sysLogin";
}
addMessage(redirectAttributes, "删除机构成功");
return "redirect:" + Global.getAdminPath() + "/sys/office/?repage";
}
@RequiresPermissions("sys:office:del")
@RequestMapping(value = "deleteUseable")
public String deleteUseable(Office office, RedirectAttributes redirectAttributes, HttpServletRequest request) {
try{
Office company = UserUtils.getSelectCompany();
if ("1".equals(company.getId())) {
if (UserUtils.getUser().isAdmin()) {
String[] ids = office.getParentIds().split(",");
if (ids.length>1) {
int lenght = ids.length - 1;
for (int i = lenght; i > 0; i--) {
Office o = officeService.get(ids[1]);
if (o.getType().equals("1")) {
company = o;
break;
}
}
}else{
company = new Office("1");
}
}else {
addMessage(redirectAttributes, "无操作权限!");
}
}
String str = "成功";
if ("1".equals(office.getUseable())){
officeService.updateUseable(office);
addMessage(redirectAttributes, "该机构及所有子机构项启用"+str);
}else {
List users = systemService.findUserByOffices(office,company);
if (users==null||users.size()==0){
officeService.updateUseable(office);
}else {
str = "失败,机构下存在用户不能禁用!";
}
addMessage(redirectAttributes, "该机构及所有子机构项禁用"+str);
}
}catch (Exception e){
logger.error("Exception e:"+e);
if ("1".equals(office.getUseable())){
addMessage(redirectAttributes, "该机构及所有子机构项启用失败");
}else {
addMessage(redirectAttributes, "该机构及所有子机构项禁用失败");
}
}
return "redirect:" + Global.getAdminPath() + "/sys/office/?repage";
}
@ResponseBody
@RequestMapping(value = "userCompany")
public List