package cn.hmsoft.skill.control.cf;

import java.net.URLDecoder;

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.control.FrameControl;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.skill.data.dao.school.SchoolAspectDao;
import cn.hmsoft.skill.data.dao.school.SchoolRegDao;
import cn.hmsoft.skill.data.model.school.SchoolAspect;
import cn.hmsoft.skill.helper.ArtRegexHelper;
import cn.hmsoft.skill.service.cf.SchoolAspectService;

@RestController
public class CfAspectControl extends FrameControl {

	@Autowired
	private SchoolRegDao daoSchool;
	@Autowired
	private SchoolAspectDao daoAspect;
	@Autowired
	private SchoolAspectService sService;

	@RequestMapping("conf/aspect/page")
	public Ajax page(String query, Integer limit, Integer start, String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "aspect_code";
		return new Ajax(this.daoAspect.pageSchoolAspect(this.getFrameOptr().getOrg_id(), start, limit, 
				query, getQueryOrder(order, type)));
	}

	@RequestMapping("conf/aspect/all")
	public Ajax all(String order, String type) {
		if (StringHelper.isEmpty(order))
			order = "aspect_code";
		return new Ajax(this.daoAspect.all(this.getQueryOrder(order, type)));
	}

	@RequestMapping("conf/aspect/find")
	public Ajax find(int aspect_id) {
		return new Ajax(this.daoAspect.find(aspect_id));
	}

	@RequestMapping("conf/aspect/add")
	public Ajax add(SchoolAspect aspect, String aspect_notice) {
		if (aspect_notice != null) {
			try {
				if (!ArtRegexHelper.isPositiveDigit(aspect.getAspect_code())) {
					throw new BusinessException("专业代码必须是数字!");
				}
				aspect.setAspect_notice(URLDecoder.decode(aspect_notice, "UTF-8"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		aspect.setSchool_id(this.getFrameOptr().getOrg_id());
		aspect.setAspect_id(Integer.valueOf(aspect.getAspect_code()));
		this.sService.addSchoolAspect(aspect, this.getFrameOptr());
		return new Ajax(aspect);
	}

	@RequestMapping("conf/aspect/edit")
	public Ajax edit(SchoolAspect aspect, String aspect_notice) {
		if (aspect_notice != null) {
			try {
				aspect.setAspect_notice(URLDecoder.decode(aspect_notice, "UTF-8"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		this.sService.editSchoolAspect(aspect, this.getFrameOptr());
		return new Ajax(aspect);
	}

	@RequestMapping("conf/aspect/delete")
	public Ajax delete(int aspect_id) {
		this.sService.deleteAspect(aspect_id, this.getFrameOptr());
		return new Ajax();
	}

}
