package com.fdream.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.fdream.entity.Advert;
import com.fdream.service.IAdvertService;
import com.fdream.util.DateFomat;
import com.fdream.util.RandomStringUtil;

import net.sf.json.JSONArray;

/**
 * 广告控制层
 * @author quanmin
 *
 */
@Controller
@RequestMapping("/advert")
public class AdvertController {
	@Autowired
	private IAdvertService advertService;
	
	/**
	 * 添加 修改
	 * @param advert
	 * @param request
	 * @return
	 */
	@RequestMapping("/save")
	public String save(Advert advert,HttpServletRequest request) throws Exception{
		String id = request.getParameter("aid");
		String aname = request.getParameter("aname");
		String aimg = request.getParameter("aimg");
		String aurl = request.getParameter("aurl");
		String acreatedate = DateFomat.getNowDate();
		if("".equals(id)){
			String aid = RandomStringUtil.getRandomCode(10, 2);
			advert.setAid(aid);
			advert.setAname(aname);
			advert.setAimg(aimg);
			advert.setAurl(aurl);
			advert.setAcreatedate(acreatedate);
			advertService.save(advert);
		}else{
			advert.setAname(aname);
			advert.setAimg(aimg);
			advert.setAurl(aurl); 
			advertService.update(advert);
		}
		return "redirect:/advertList.jsp";
	} 
	
	/**
	 * 查找所有
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findList")
	public String findList(HttpServletRequest request,HttpServletResponse response) throws Exception{
		List<Advert> advertList=advertService.findList();
		JSONArray jsonArray = JSONArray.fromObject(advertList);
		response.setContentType("application/json");
		try {
			PrintWriter out = response.getWriter();
			String json = jsonArray.toString();
			out.write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 前台广告显示
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/qt_show_adv")
	public String qt_show_adv(HttpServletRequest request,HttpServletResponse response) throws Exception{
		List<Advert> advertList=advertService.findList();
		Advert advert = advertList.get(0);
		JSONArray jsonArray = JSONArray.fromObject(advert);
		response.setContentType("application/json");
		try {
			PrintWriter out = response.getWriter();
			String json = jsonArray.toString();
			out.write(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 通过id查找分类信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public String findById(HttpServletRequest request) throws Exception{
		String id = request.getParameter("id");
		request.setAttribute("advert", advertService.findById(id));
		return "/advert";
	}
	
	/**
	 * 通过id删除
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/deletById")
	public String deletById(HttpServletRequest request) throws Exception{
		boolean result;
		String id = request.getParameter("id");
		result=advertService.delete(id);
		System.out.println("result:"+result);
		return "redirect:/advertList.jsp";
	}
}
最近下载更多
ruiqiweb  LV10 2023年3月23日
lilong007  LV20 2022年10月21日
xiaoqiaothq  LV2 2022年10月16日
qwer123546  LV13 2022年9月20日
2716804680  LV8 2022年6月21日
香魔芋的头  LV2 2022年5月12日
一杯美式  LV1 2022年4月21日
171337601  LV9 2022年4月18日
qlpqlp  LV7 2022年3月13日
夜猫子111  LV2 2022年3月11日
最近浏览更多
草原雄鹰  LV3 4月6日
CCCCWWWW  LV4 3月27日
admin_z  LV22 1月28日
lixinsi 1月15日
暂无贡献等级
李林112233  LV2 1月12日
gnnhka  LV10 2023年12月26日
limenghaoqwer  LV2 2023年11月27日
skook7  LV2 2023年11月21日
白菜白菜菜 2023年11月19日
暂无贡献等级
钝感力  LV1 2023年10月28日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友