package com.cloudcross.ssp.web;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

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

import com.cloudcross.ssp.back.model.Account;
import com.cloudcross.ssp.back.model.Resources;
import com.cloudcross.ssp.back.service.IAccountService;
import com.cloudcross.ssp.common.utils.Common;
import com.cloudcross.ssp.common.utils.Md5Tool;
//import com.cloudcross.ssp.common.utils.POIUtils;
import com.cloudcross.ssp.pulgin.mybatis.plugin.PageView;
/*import javax.inject.Inject;*/

/**
 * 
 * @author lanyuan
 * 2013-11-19
 * @Email: mmm333zzz520@163.com
 * @version 1.0v
 */
@Controller
@RequestMapping("/security/account/")
public class AccountController extends BaseController{
	@Autowired
	private IAccountService accountService;
	
	@RequestMapping("list")
	public String list(Model model, Resources menu, String pageNow) {
		return "/security/account/list";
	}
	/**
	 * @param model
	 * 存放返回界面的model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("query")
	public PageView query(Account account,String pageNow,String pagesize) {
		pageView = accountService.query(getPageView(pageNow,pagesize), account);
		return pageView;
	}
	/**
	 * 保存数据
	 * 
	 * @param model
	 * @param videoType
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("add")
	@ResponseBody
	public Map<String, Object> add(Account account) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			account.setPassword(Md5Tool.getMd5(account.getPassword()));
			accountService.add(account);
			map.put("flag", "true");
		} catch (Exception e) {
			map.put("flag", "false");
		}
		return map;
	}

	
	/**
	 * 跑到新增界面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("addUI")
	public String addUI() {
		return Common.BACKGROUND_PATH+"/account/add";
	}
	
	/**
	 * 账号角色页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("accRole")
	public String accRole(Model model,String accountName,String roleName) {

		try {
			accountName=java.net.URLDecoder.decode(accountName,"UTF-8");  
			roleName= java.net.URLDecoder.decode(roleName,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			
		} 
		model.addAttribute("accountName", accountName);
		model.addAttribute("roleName", roleName);
		
		return Common.BACKGROUND_PATH+"/account/acc_role";
	}
	
	/**
	 * 跑到新增界面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping("editUI")
	public String editUI(Model model,String accountId) {
		Account account = accountService.getById(accountId);
		model.addAttribute("account", account);
		return Common.BACKGROUND_PATH+"/account/edit";
	}
	
	/**
	 * 验证账号是否存在
	 * @author lanyuan
	 * Email：mmm333zzz520@163.com
	 * date：2014-2-19
	 * @param name
	 * @return
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String name){
		Account account = accountService.isExist(name);
		if(account == null){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 删除
	 * 
	 * @param model
	 * @param videoTypeId
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("deleteById")
	public Map<String, Object> deleteById(Model model, String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id[] = ids.split(",");
			for (String string : id) {
				if(!Common.isEmpty(string)){
				accountService.delete(string);
				}
			}
			map.put("flag", "true");
		} catch (Exception e) {
			map.put("flag", "false");
		}
		return map;
	}
	/**
	 * 删除
	 * 
	 * @param model
	 * @param videoTypeId
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("updateState")
	public Map<String, Object> updateState(Model model, String ids,String state) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String id[] = ids.split(",");
			for (String string : id) {
				if(!Common.isEmpty(string)){
					Account account = new Account();
					account.setId(Integer.parseInt(string));
					account.setState(state);
					accountService.update(account);
				}
			}
			map.put("flag", "true");
		} catch (Exception e) {
			map.put("flag", "false");
		}
		return map;
	}
	/**
	 * 更新类型
	 * 
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("update")
	public Map<String, Object> update(Model model, Account account) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
	
			account.setPassword(Md5Tool.getMd5(account.getPassword()));
			accountService.update(account);
			map.put("flag", "true");
		} catch (Exception e) {
			map.put("flag", "false");
		}
		return map;
	}
}