package com.lerx.handlers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lerx.annotation.Token;
import com.lerx.dao.iface.IAlbgenreDao;
import com.lerx.dao.iface.IAlbumArticleClassifyDao;
import com.lerx.entities.Albgenre;
import com.lerx.entities.AlbumArticleClassify;

@RequestMapping("/action_album_article_classify")
@Controller
public class AlbumArticleClassifyHandler {
	
	@Autowired
	private IAlbgenreDao albgenreDaoImpl;
	
	@Autowired
	private IAlbumArticleClassifyDao albumArticleClassifyDaoImpl;
	
	private static final String FAILED = "jsp/result/failed";
	private static final String ADMINFAILRETURN = "_admin.login.redirect_";
	
	@RequestMapping(value = {"/listByGid/{gid}"})
	@Token(ajax = false, admin = true, failedPage = FAILED, msgKey = "fail.permission",failedReturnUrl = ADMINFAILRETURN)
	public String listByGid(@PathVariable(value = "gid", required = true) Long gid,HttpServletRequest request, HttpSession session,Map<String, Object> map) {
		List<AlbumArticleClassify> list=albumArticleClassifyDaoImpl.findByGID(gid);
		map.put("list", list);
		
		return "jsp/album/genre/aacs";
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/addOnGenre/{gid}")
	@Token(ajax = false, admin = true, failedPage = FAILED, msgKey = "fail.permission",failedReturnUrl = ADMINFAILRETURN)
	public int addOnGenre(@PathVariable(value = "gid", required = true) Long gid,String name) {
		Albgenre genre=albgenreDaoImpl.findByID(gid);
		AlbumArticleClassify aac=new AlbumArticleClassify();
		aac.setDeled(false);
		aac.setGenre(genre);
		aac.setName(name);
		aac.setMain(false);
		int maxOrdn=albumArticleClassifyDaoImpl.maxOrdn(gid);
		aac.setOrdn(maxOrdn+1);
		albumArticleClassifyDaoImpl.add(aac);
		return 0;
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/delOnGenre/{cid}")
	@Token(ajax = false, admin = true, failedPage = FAILED, msgKey = "fail.permission",failedReturnUrl = ADMINFAILRETURN)
	public int delOnGenre(@PathVariable(value = "cid", required = true) Long cid) {
		albumArticleClassifyDaoImpl.delByID(cid);
		return 0;
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/modifyOnGenre/{cid}")
	@Token(ajax = false, admin = true, failedPage = FAILED, msgKey = "fail.permission",failedReturnUrl = ADMINFAILRETURN)
	public int modifyOnGenre(@PathVariable(value = "cid", required = true) Long cid,String name) {
		AlbumArticleClassify aac=albumArticleClassifyDaoImpl.findByID(cid);
		aac.setName(name);
		
		albumArticleClassifyDaoImpl.modify(aac);
		return 0;
		
	}
	
	@ResponseBody
	@RequestMapping(value = "/setMain/{cid}")
	@Token(ajax = false, admin = true, failedPage = FAILED, msgKey = "fail.permission",failedReturnUrl = ADMINFAILRETURN)
	public int setMain(@PathVariable(value = "cid", required = true) Long cid) {
		
		albumArticleClassifyDaoImpl.setMain(cid);
		return 0;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/modifyOrdn/{cid}/{ordn}")
	@Token(ajax = false, admin = true, failedPage = FAILED, msgKey = "fail.permission",failedReturnUrl = ADMINFAILRETURN)
	public int modifyOrdn(@PathVariable(value = "cid", required = true) Long cid,@PathVariable(value = "ordn", required = true) Integer ordn) {
		if (ordn==null || ordn<=0) {
			ordn=99;
		}
		AlbumArticleClassify aac=albumArticleClassifyDaoImpl.findByID(cid);
		aac.setOrdn(ordn);
		albumArticleClassifyDaoImpl.modify(aac);
		return 0;
		
	}
	
	
	
	

}
