package org.cs.mgr.admin.ctl;

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

import org.apache.log4j.Logger;
import org.cs.core.ctl.BaseCtl;
import org.cs.mgr.admin.model.DCProject;
import org.cs.mgr.admin.model.Menu;
import org.cs.mgr.admin.service.IDCProjectService;
import org.cs.mgr.admin.vo.ZTree;
import org.cs.util.DateUtil;
import org.cs.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping("/dcproject")
public class DCProjectCtl extends BaseCtl {
	
	Logger log = Logger.getLogger(DCProjectCtl.class);
	
	@Autowired
	IDCProjectService dcprojectService;
	
	
	@RequestMapping("list")
	public String list(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		return "mgr/dcproject/list";	
	}
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String list2(int page, int pageSize, String search){
		
		Map<String, String> map = getMapFromJson(search)
				;
		Pager pager 	= this.dcprojectService.findDCProject(new Pager(page, pageSize), map);
		JSONObject json = new JSONObject();
		json.put("rows", pager.getResults());
		json.put("total", pager.getTotal());
		return JSONObject.toJSONString(json, features);
	}
	
	@RequestMapping("transfer")
	public String transfer(Integer id, String action, ModelMap mm){
		DCProject dcproject = null;
		if(id !=null && id != 0){
			dcproject = this.dcprojectService.findById(id);
			mm.put("dcproject", dcproject);
		}
		
		return "mgr/dcproject/" + action;
	}
	
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(DCProject dcproject, ModelMap mm){
		if(dcproject != null ){
			dcproject.setCreateDate(DateUtil.getCurrentDate());
			this.dcprojectService.add(dcproject);
		}
		return AJAX_SUCCESS;
	}
	
	
	@RequestMapping(value="edit", method = RequestMethod.POST)
	@ResponseBody
	public String edit(DCProject dcproject, ModelMap mm){
		if(dcproject != null ){
			DCProject dcproject1 = this.dcprojectService.findById(dcproject.getId());
			dcproject1.setName(dcproject.getName());
			dcproject1.setInfo(dcproject.getInfo());
			dcproject1.setProvince(dcproject.getProvince());
			dcproject1.setProvinceCode(dcproject.getProvinceCode());
			dcproject1.setCity(dcproject.getCity());
			dcproject1.setCityCode(dcproject.getCityCode());
			this.dcprojectService.update(dcproject1);
//			redisClientTemplate.set("menu_name", menu.getName());
//			System.out.println(redisClientTemplate.get("menu_name"));
		}
		return AJAX_SUCCESS;
	}
	
	@RequestMapping("del")
	@ResponseBody
	public String del(int id, ModelMap mm){
		
		if(id != 0){
			this.dcprojectService.deleteById(id);
		}
		return AJAX_SUCCESS;
	}
	/*
	@RequestMapping("modify")
	public String modify(Menu menu, ModelMap mm){
		menu.setDisplay(true);
		this.menuService.update(menu);
		
		mm.put("superMenuList", this.menuService.getSuperMenuList());
		
		return "redirect:/list.cs"; 
	}
	
	
	
	@RequestMapping("delAction")
	@ResponseBody
	public String delAction(int id, ModelMap mm){
		
//		mm.put("superMenuList", this.menuService.getSuperMenuList());
		if(id != 0){
			this.actionService.deleteById(id / 100);
		}
		return AJAX_SUCCESS;
	}
	*/
	
	
	
}
