/**
 * 
 */
package org.cs.mgr.admin.ctl;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.cs.constants.Constants;
import org.cs.core.ctl.BaseCtl;
import org.cs.core.model.Dic;
import org.cs.core.service.IDicService;
import org.cs.mgr.admin.model.TbNews;
import org.cs.mgr.admin.service.ITbNewsService;
import org.cs.util.*;
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 org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName NewsCtrl 
 * @Description TODO(网站新闻动态)
 * @author Administrator
 * @Date 2017-4-18上午9:36:23
 * @version 1.0.0
 */
@Controller
@RequestMapping("/news/")
public class NewsCtrl extends BaseCtl {

	@Autowired
	private ITbNewsService tbNewsService;
	@Autowired
	private IDicService dicService;
	
	@RequestMapping("list")
	public String list(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		List<Dic> dicList = dicService.getByType("news_type");
		mm.put("dicList", dicList);
		return "mgr/news/list";	
	}
	
    @RequestMapping(value="uploadPic", method = RequestMethod.POST)
    @ResponseBody
    public String uploadPic( ModelMap mm, HttpServletRequest request,  @RequestParam(required = false) MultipartFile Filedata){
        String fileName = "";
        JSONObject json = new JSONObject();
    	try{
            if(Filedata != null){
                try {
                    fileName = FileUtil.uploadFile(PropUtil.getString("file.path.design"), Filedata) ; 
                    json.put("fileName", fileName);
                    json.put("name", fileName);
                    json.put("path", fileName);
                    json.put("thumb", fileName);
//                    json.put("rootPath", Constants.HTTP_IMG_PREFIX+"/"+ Constants.IMG_CONTEXTPATH);
                    json.put("status", 1);
                } catch (IOException e) {
                    json.put("fileName",  fileName);
                    json.put("name",  fileName);
                    json.put("path",  fileName);
                    json.put("thumb", fileName);
//                    json.put("rootPath",  Constants.HTTP_IMG_PREFIX+"/"+ Constants.IMG_CONTEXTPATH);
                    json.put("status", 0);
                    e.printStackTrace();
                } 
            }     		
    	}catch(Exception e){
    		e.printStackTrace();
    	}
        return JSONObject.toJSONString(json, features);
    }	
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject list2(int page, int pageSize, String search, ModelMap mm){
		Pager pager = new Pager(page, pageSize);		
		pager = this.tbNewsService.getList(pager, search, "",null);
		JSONObject json = new JSONObject();		
		json.put("rows", pager.getResults());
		json.put("total", pager.getTotal());
		
		return json;
	}
	
	@RequestMapping("transfer")
	public String transfer(String id, String action, ModelMap mm){
		TbNews bean = null;
		if(StringUtil.isNotBlank(id)){
			bean = this.tbNewsService.findById(Integer.valueOf(id.trim()));
			mm.put("bean", bean);
			mm.put("newsContent", bean.getNewsContent());
		}
		List<Dic> dicList = dicService.getByType("news_type");
		mm.put("dicList", dicList);
		return "mgr/news/" + action;
	}
	
	@RequestMapping(value="edit", method = RequestMethod.POST)
	@ResponseBody
	public String edit(TbNews bean, ModelMap mm){
		TbNews tnold = this.tbNewsService.findById(bean.getId());
		tnold.setNewsTitle(bean.getNewsTitle());
		tnold.setNewsAbstract(bean.getNewsAbstract());
		tnold.setNewsSort(bean.getNewsSort());
		if(StringUtil.isNotBlank(bean.getNewsImage())){
			tnold.setNewsImage(bean.getNewsImage().split(",")[0]);
		}
		tnold.setNewsShow(bean.getNewsShow());
		tnold.setNewsContent(bean.getNewsContent());
		tnold.setOpenFlag(bean.getOpenFlag());
		tnold.setNewsContent(XMLUtil.getNewContent(bean.getNewsContent()));
		this.tbNewsService.update(tnold);
		return AJAX_SUCCESS;
	}
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(TbNews bean, ModelMap mm){
		bean.setClassCode("1");
		bean.setNewsImage(bean.getNewsImage().trim().replaceAll(",", ""));
		bean.setCreateTime(new Date());
		bean.setCreateTime(new Date());
		bean.setNewsContent(XMLUtil.getNewContent(bean.getNewsContent()));
		this.tbNewsService.add(bean);
		if(bean.getOpenFlag()){  //公开
			//发推送消息
			Map<String,String> param = new HashMap<String,String>();
			param.put("type", "10");
			param.put("id", bean.getId().toString());
			param.put("content", bean.getNewsTitle());
			JPush.sendAll(param);
		}
		return AJAX_SUCCESS;
	}
	
	
	/** 
	* @Title: del 
	* @Description: TODO(删除商品类别) 
	* @param @param id
	* @param @param mm
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	* @author:     Jzw
	* Create at:   2017年2月15日 上午11:26:11
	*/
	@RequestMapping("del")
	@ResponseBody
	public String del(String id, ModelMap mm){
//		HzyHelp bean = null;
        if(StringUtil.isNotBlank(id)){
            this.tbNewsService.deleteById(id);
		}
		return AJAX_SUCCESS;
	}
}
