package cn.hmsoft.frame.control;

import java.time.LocalDateTime;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.frame.constants.FrameResIdConstants;
import cn.hmsoft.frame.data.dao.FrameRoleDao;
import cn.hmsoft.frame.data.model.FrameRole;
import cn.hmsoft.frame.service.FrameRoleService;

@RestController
public class FrameRoleControl extends FrameControl {

	@Autowired
	private FrameRoleService sFrameRole;
	@Autowired
	private FrameRoleDao daoRole;

	@RequestMapping("frame/role/page")
	public Ajax page(Integer optr_id, final int start, final int limit, final String query, final String order, final String type) {
		return new Ajax(this.daoRole.pageRole(optr_id, start, limit, query, this.getQueryOrder(order, type)));
	}

	@RequestMapping("frame/role/all")
	public Ajax all(String order, String type) {
		return new Ajax(this.daoRole.all(this.getQueryOrder(order, type)));
	}


	@RequestMapping("frame/role/add")
	public Ajax add(final FrameRole role) {
		this.authOptrRole(FrameResIdConstants.Role_Manage);
		role.setCreate_optr(this.getFrameOptr().getOptr_id());
		role.setCreate_time(LocalDateTime.now());
		role.setThread_max(this.getFrameOptr().getThread_max());
		this.sFrameRole.addRole(role);
		return new Ajax(role);
	}
	@RequestMapping("frame/role/edit")
	public Ajax edit(final FrameRole role) {
		this.authOptrRole(FrameResIdConstants.Role_Manage);
		this.sFrameRole.editRole(role);
		return new Ajax();
	}

	@RequestMapping("frame/role/find")
	public Ajax find(final int role_id) {
		return new Ajax(this.daoRole.find(role_id));
	}

	@RequestMapping("frame/role/delete")
	public Ajax delete(final int role_id) {
		this.authOptrRole(FrameResIdConstants.Role_Manage);
		this.sFrameRole.deleteRole(role_id);
		return new Ajax();
	}

	
	@RequestMapping("frame/role/res/list")
	public Ajax listRes(int role_id) {
		return new Ajax(this.daoRole.listRoleRes(role_id));
	}
	@RequestMapping("frame/role/res/grant")
	public Ajax grantRes(int role_id, @RequestParam(value = "res_ids") Integer[] res_ids) {
		this.authOptrRole(FrameResIdConstants.Role_Manage);
		this.sFrameRole.grantRole(role_id, res_ids);
		return new Ajax();
	}

	
}
