package com.controller.admin;


import com.controller.CommonController;
import com.entity.News;
import com.service.NewsService;
import com.service.UsersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import com.util.Info;
import org.springframework.web.multipart.MultipartFile;

/**
 *  新闻广告控制类
 * @ignore
 * @Author:Alcedo QQ:815657032
 * @Date 2020/4/15 11:05
 */
@Controller
@RequestMapping("/admin/")
public class NewsController {
    @Resource
    private NewsService newsService;
    @Resource
    private UsersService usersService;

    @Resource
    private CommonController commonController;
    /*设置访问路径*/
    private String path="admin/views/news/";



    public static final String TABLE_NAME = "新闻广告";



    /**
     * ===后台新闻广告管理===
     *
     * @return
     */

    /*处理添加新闻广告*/
    @RequestMapping("doAddNews.do")
    public String doAddNews(HttpServletRequest request, ModelMap map) {
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"news_add";
    }

    // 添加新闻广告
    @RequestMapping("addNews.do")
	public String addNews(@RequestParam(value="file",required=false)MultipartFile file,String img,
        HttpServletRequest request,News news, HttpSession session){
             img=Info.fileUpload(file, request, img);
             news.setImg(img);
            news.setPubtime(new Timestamp(System.currentTimeMillis()).toString().substring(0,19));
            newsService.add(news);
        return "redirect:newsList.do";
	}




    /*处理修改新闻广告*/
    @RequestMapping("doUpdateNews.do")
    public String doUpdateNews(HttpServletRequest request, int id, ModelMap map) {
        map.put("news", newsService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"news_update";
    }

    /*处理修改新闻广告*/
    @RequestMapping("doUpdateNew.do")
    public String doUpdateNew(HttpServletRequest request, int id, ModelMap map) {
        map.put("news", newsService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"new_update";
    }

    /*查看新闻广告*/
    @RequestMapping("doLookNews.do")
    public String doLookNews(HttpServletRequest request, int id, ModelMap map) {
        map.put("news", newsService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"news_look";
    }

    /*保存新闻广告*/
    @RequestMapping("updateNews.do")
    public String updateNews(@RequestParam(value="file",required=false)MultipartFile file,String img,
    HttpServletRequest request,News news) {
            img=Info.fileUpload(file, request, img);
            if(img!=null&&!img.equals("")){
                news.setImg(img);
            }
        newsService.update(news);
        return "redirect:newsList.do";
    }


    // 分页查询新闻广告信息的列表
    @RequestMapping("newsList.do")
    public String newsList(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                            HttpSession session) {
        Map<String, Object> bMap = new HashMap<>();

        List<News> list = newsService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"news_list";
    }

    // 分页查询新闻广告信息的列表--模糊查询
    @RequestMapping("newsListSearch.do")
    public String newsListSearch(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                                  HttpSession session, News news) {
        Map<String, Object> bMap = new HashMap<>();

        if (news.getName() != null && !news.getName().equals("")) {
            bMap.put("name",news.getName());
        }
        if (news.getBtype() != null && !news.getBtype().equals("")) {
            bMap.put("btype",news.getBtype());
        }
        List<News> list = newsService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"news_list";
    }

    /*删除新闻广告*/
    @RequestMapping("deleteNews.do")
    public String deleteNews(int id) {
        newsService.delete(id);
        return "redirect:newsList.do";
    }

}

