package cn.com.qmth.stmms.admin.basic;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

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.RequestParam;

import cn.com.qmth.stmms.biz.basic.bean.RoleInfoQuery;
import cn.com.qmth.stmms.biz.basic.bean.RoleInfoVo;
import cn.com.qmth.stmms.biz.basic.bean.RolePrivilegeDomain;
import cn.com.qmth.stmms.biz.basic.bean.RolePrivilegeVo;
import cn.com.qmth.stmms.biz.basic.model.Privilege;
import cn.com.qmth.stmms.biz.basic.service.PrivilegeService;
import cn.com.qmth.stmms.biz.basic.service.RoleInfoService;
import cn.com.qmth.stmms.biz.basic.service.RolePrivilegeService;
import cn.com.qmth.stmms.common.controller.BaseController;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.utils.RequestUtils;
import net.sf.json.JSONArray;

@Controller
@RequestMapping("/admin/basic/role/info")
public class RoleInfoController extends BaseController {
	@Autowired
	private RoleInfoService roleInfoService;
	
	@Autowired
	private RolePrivilegeService rolePrivilegeService;
	
	@Autowired
	private PrivilegeService privilegeService;

	@RequestMapping(value = "list")
	public String roleLise(Model model, HttpServletRequest request, RoleInfoQuery query) {
		Integer totalCount = roleInfoService.countByQuery(query);
		if (totalCount > 0) {
			List<RoleInfoVo> list = roleInfoService.findByQuery(query);
			query.setResult(list);
			query.setTotalCount(totalCount);
		} else {
			query.setResult(new ArrayList<>());
			query.setTotalCount(0);
		}
		model.addAttribute("roleList", roleInfoService.allList(query.getSchoolId()));
		model.addAttribute("query", query);
		return "modules/basic/roleInfoList";
	}
	
	@RequestMapping(value = "privilege")
	public String privilege(Model model, HttpServletRequest request, RoleInfoQuery query) {
		List<RolePrivilegeVo> ps=rolePrivilegeService.findByQuery(query);
		if(ps!=null) {
			model.addAttribute("privilegeList", JSONArray.fromObject(ps).toString());
		}else {
			model.addAttribute("privilegeList", "[]");
		}
		model.addAttribute("schoolId", query.getSchoolId());
		model.addAttribute("roleCode", query.getRole());
		model.addAttribute("roleName", Role.valueOf(query.getRole()).getName());
		return "modules/basic/rolePrivilege";
	}

	@RequestMapping(value = "privilege/save")
	public String privilegeSave(Model model, HttpServletRequest request, RolePrivilegeDomain domain) {
		WebUser wu = RequestUtils.getWebUser(request);
		domain.setUserId(wu.getId());
		rolePrivilegeService.save(domain);
		return "redirect:/admin/basic/role/info/list?schoolId="+domain.getSchoolId();
	}
	
	@RequestMapping(value = "sub/page")
    public String subPage(HttpServletRequest request,@RequestParam String privilegeCode) {
        WebUser wu = RequestUtils.getWebUser(request);
        Set<String> codes=rolePrivilegeService.getRoleCodes(wu.getUser().getSchoolId(), wu.getRole());
        if(codes==null||codes.size()==0) {
        	return null;
        }
        List<Privilege> ps=privilegeService.getPagePrivilegesByParentCode(privilegeCode);
        if(ps==null||ps.size()==0) {
        	return null;
        }
        for(Privilege p:ps) {
        	if(codes.contains(p.getCode())) {
        		return "redirect:"+p.getPrivilegeUri();
        	}
        }
        return null;
    }
}
