package org.cs.mgr.setting.ctl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.cs.core.ctl.BaseCtl;
import org.cs.util.DateUtil;
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.cs.mgr.setting.service.ISysAdvService;
import org.cs.util.Pager;
import org.cs.mgr.setting.model.SysAdv;
import org.cs.util.StringUtil;
import com.alibaba.fastjson.JSONObject;


/**
 * 
 * @ClassName: SysAdv 
 * @Description:  
 * @author: sunny_shi
 * @date: 2018-06-09 13:29:15
 */
@Controller
@RequestMapping("/sysAdvCtl")
public class SysAdvCtl extends BaseCtl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ISysAdvService sysAdvService;
	
	@RequestMapping("list")
	public String list(ModelMap mm, @RequestParam(required=false)String menuCode){
		mm.put(MENU_CODE, menuCode);
		this.set(MENU_CODE, menuCode);
		return "mgr/sysAdv/list";	
	}
	
	@RequestMapping(value="list2", method = RequestMethod.GET, produces="application/json;charset=UTF-8")
	@ResponseBody
	public String list2(int page, int pageSize){
		Pager pager = this.sysAdvService.getSysAdvs(new Pager(page, pageSize));
		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){
		SysAdv sysAdv = null;
		if(id != null){
			sysAdv = this.sysAdvService.findById(id);
			mm.put("sysAdv", sysAdv);
		}
		return "mgr/sysAdv/" + action;
	}
	
	@RequestMapping(value="add", method = RequestMethod.POST)
	@ResponseBody
	public String add(SysAdv sysAdv){
		if(sysAdv != null ){
			if(sysAdv.getAdvPic().indexOf(",")>=0){
				sysAdv.setAdvPic(sysAdv.getAdvPic().substring(0, sysAdv.getAdvPic().length() - 1));
			}
			this.sysAdvService.addSysAdv(sysAdv);
		}
		return AJAX_SUCCESS;
	}

    @RequestMapping(value="edit", method = RequestMethod.POST)
    @ResponseBody
    public String edit(SysAdv sysAdv){
        if(sysAdv != null ){
        	SysAdv sa = this.sysAdvService.findById(sysAdv.getId());
        	sa.setAdvTitle(sysAdv.getAdvTitle());
        	sa.setAdvSrc(sysAdv.getAdvSrc());
        	sa.setAdvType(sysAdv.getAdvType());
        	sa.setAdvSort(sysAdv.getAdvSort());
        	if(sysAdv.getAdvPic().indexOf(",")>=0){
        		sa.setAdvPic(sysAdv.getAdvPic().substring(0, sysAdv.getAdvPic().length() - 1));
			}
            this.sysAdvService.updateSysAdv(sa);
        }
        return AJAX_SUCCESS;
    }

	
	
	
	@RequestMapping("del")
	@ResponseBody
	public String del(Integer id){
		if(id != null){
			this.sysAdvService.delSysAdv(id);
		}
		
		return AJAX_SUCCESS;
	}
	
	
	
		
}

