

package com.jeeplus.modules.sg.managementcenter.privilegePre.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sg.managementcenter.privilegePre.entity.PrivilegePre;
import com.jeeplus.modules.sg.managementcenter.privilegePre.service.PrivilegePreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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


@Controller
@RequestMapping(value = "${adminPath}/sg/managementcenter/privilegepre")
public class PrivilegePreController extends BaseController {

	@Autowired
	private PrivilegePreService privilegePreService;

	
/*	@ModelAttribute
	public Project get(@RequestParam(required=false) String id) {
		Project entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectService.get(id);
		}
		if (entity == null){
			entity = new Project();
		}
		return entity;
	}*/


	@RequestMapping(value = "list")
	public String list(PrivilegePre privilegePre, Model model) {
		model.addAttribute("privilege", privilegePre);
		return "modules/sg/managementcenter/privilege/privilege";
	}

	@RequestMapping(value = "privilegeProcdef")
	public String privilegeProcdef(PrivilegePre privilegePre, Model model) {
		model.addAttribute("privilegePre", privilegePre);
		return "modules/sg/managementcenter/privilege/privilegeProcdef";
	}


	@RequestMapping(value = "allProcDef")
	public String allProcDef(PrivilegePre privilegePre, Model model) {
		model.addAttribute("privilegePre", privilegePre);
		return "modules/sg/managementcenter/privilege/allProcDef";
	}

	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(PrivilegePre privilegePre, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PrivilegePre> page = privilegePreService.findPage(new Page<PrivilegePre>(request, response), privilegePre);
		return getBootstrapData(page);
	}


	@ResponseBody
	@RequestMapping(value = "allProcData")
	public Map<String, Object> allProcData(PrivilegePre privilegePre, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PrivilegePre> page = privilegePreService.findAllDefPage(new Page<PrivilegePre>(request, response), privilegePre);
		return getBootstrapData(page);
	}

	/**
	 * 新增权限
	 */
	@ResponseBody
	@RequestMapping(value = "addPerson")
	public AjaxJson add(PrivilegePre privilegePre) {
		AjaxJson j = new AjaxJson();
		String pid = privilegePre.getPid();
		if (pid == null || "".equals(pid)) {
			j.setSuccess(false);
			j.setMsg("请选择需要添加的项目");
		} else {
			String uid = privilegePre.getUid();
			PrivilegePre listUser = privilegePreService.findListUser(uid);
			if (listUser == null || "".equals(listUser)) {
				privilegePre.preInsert();
				String ling=";";
				pid = pid.replace(",", ling);
				privilegePre.setPid(pid);
				privilegePreService.addPersonal(privilegePre);
			} else {
				String lpid = listUser.getPid();
				String[] split = pid.split(",");
				String newPid = "";
				for (int i = 0; i < split.length; i++) {
					newPid += ";" + split[i];
				}
				lpid += newPid;
				privilegePreService.updatePersonal(lpid, uid);
			}
			j.setMsg("取消权限成功");
		}
		return j;
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping(value = "deleteAll")
	public AjaxJson delete(PrivilegePre privilegePre) {
		AjaxJson j = new AjaxJson();
		String uid = privilegePre.getUid();
		PrivilegePre listUser = privilegePreService.findListUser(uid);
		String pid = listUser.getPid();
		String[] split = pid.split(";");
		List<String> compareList = new ArrayList<>();
		for (int i = 0; i < split.length; i++) {
			if (!privilegePre.getPid().equals(split[i])) {
				compareList.add(split[i]);
			}
		}
		String newPid = "";
		for (String a :
				compareList) {
			newPid += a+";";
		}
		privilegePreService.updatePersonal(newPid, uid);
		j.setMsg("取消权限成功");
		return j;
	}

/*
@ResponseBody
	@RequestMapping(value = {"userList"})
	public Map<String, Object> list(User user, HttpServletRequest request, HttpServletResponse response, Model model,String roleNeedName) {
		if (roleNeedName != null&&!roleNeedName.equals("")) {
			Role role = null;
			String[] split = roleNeedName.split(";");
			List<Role> roleList = new ArrayList<>();
			for (int i = 0; i < split.length; i++) {
				role = new Role();
				role.setName(split[i]);
				roleList.add(role);
			}
			user.setRoleList(roleList);
		}
//		Page<User> page = privilegeService.findUser(new Page<User>(request, response), user);
//		return getBootstrapData(page);
	}
*/


}
