package cn.hmsoft.frame.control;

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

import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.frame.data.dao.FrameDictDao;
import cn.hmsoft.frame.data.dao.FrameDictNameDao;
import cn.hmsoft.frame.data.model.FrameDict;
import cn.hmsoft.frame.data.model.FrameDictName;
import cn.hmsoft.frame.service.FrameDictNameService;
import cn.hmsoft.frame.service.FrameDictService;
import cn.hmsoft.frame.util.FrameDictUtil;
import cn.hmsoft.jdbc.entity.QueryOrder;

@RestController
public class FrameDictControl extends FrameControl{

	@Autowired
	private FrameDictService dictService;
	@Autowired
	private FrameDictNameService sName;
	@Autowired
	private FrameDictNameDao daoName;
	@Autowired
	private FrameDictDao daoDict;

	@RequestMapping("frame/dict/all")
	public Ajax all() {
		return new Ajax(FrameDictUtil.allDict());
	}

	@RequestMapping("frame/dict/type/page")
	public Ajax pageDictType(String query, Integer limit, Integer start, String order, String type) {
		return new Ajax(this.daoName.pageDictType(start, limit, query, getQueryOrder(order, type)));
	}

	@RequestMapping("frame/dict/type/add")
	public Ajax addType(FrameDictName type) {
		this.authAdminRole();
		this.sName.addDictType(type);
		return new Ajax();
	}

	@RequestMapping("frame/dict/type/edit")
	public Ajax editType(FrameDictName config) {
		this.authAdminRole();
		this.sName.editDictType(config);
		return new Ajax();
	}

	@RequestMapping("frame/dict/type/delete")
	public Ajax delType(String dict_name) {
		this.authAdminRole();
		this.sName.deleteDictType(dict_name);
		return new Ajax();
	}

	@RequestMapping("frame/dict/add")
	public Ajax add(FrameDict dict) {
		this.authAdminRole();
		this.dictService.addFrameDict(dict, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("frame/dict/edit")
	public Ajax edit(FrameDict dict) {
		this.authAdminRole();
		this.dictService.editFrameDict(dict, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("frame/dict/delete")
	public Ajax delete(String dict_name, String dict_value) {
		this.authAdminRole();
		FrameDict config = new FrameDict();
		config.setDict_name(dict_name);
		config.setDict_value(dict_value);
		this.dictService.deleteFrameDict(config, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("frame/dict/page")
	public Ajax pageConfig(String query, String dict_name, Integer limit, Integer start, String order, String type) {
		return new Ajax(this.daoDict.pageDict(start, limit, dict_name, query, new QueryOrder("dict_order")));
	}

	@RequestMapping("frame/dict/reload")
	public Ajax reload() {
		this.authAdminRole();
		FrameDictUtil.allDict(true);
		return new Ajax();
	}

}
