Newer
Older
huludao / src / main / java / com / newfiber / api / pc / controller / News / ZhhzNewsController.java
package com.newfiber.api.pc.controller.News;

import com.baomidou.mybatisplus.plugins.Page;
import com.newfiber.api.core.bean.ReqBodyObj;
import com.newfiber.api.core.bean.RespBodyObj;
import com.newfiber.api.core.utils.UUIDPK;
import com.newfiber.api.pc.dao.News.NewsDao;
import com.newfiber.api.pc.model.EventsManage.News;
import com.newfiber.api.pc.model.EventsManage.NewsQuery;
import com.newfiber.api.pc.model.UserManager.SysOrganization;
import com.newfiber.api.pc.service.RestTemplateService;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by XuChengChao on 2017/7/20.
 */
@Controller
public class ZhhzNewsController {



    @Autowired
    NewsDao newsDao;

    @Resource
    private RestTemplateService restTemplateService;

    /**
     * 展示新闻信息
     * @return
     */
    @PostMapping(value = "news/showNewsList")
    @ResponseBody
    public RespBodyObj selectAllNews(@RequestBody ReqBodyObj<Map<String,Object>> param) {



        String area = (String) param.getData().get("area");
        String str = (String) param.getData().get("str");
        Integer page = param.getCurrent();
        Integer rows = param.getSize();
        String sort = (String) param.getData().get("sort");
        String order = (String) param.getData().get("order");
        NewsQuery query = new NewsQuery();
        if (area!=null &&area!=""){

            query.createCriteria().andAreaEqualTo(area);
        }
        if (str!=null && str!=""){
            try {
                query.setStr(URLDecoder.decode(str,"UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        if (page!=null && rows !=null){
            query.setPageNo(page);
            query.setPageSize(rows);
        }
        if (sort!=null && order!=null){
            query.setOrder(order);
            query.setSort(sort);
        }
        List<News> list = newsDao.selectByExample(query);

        int count = newsDao.countByExample(query);
        Page page1 = new Page();
        page1.setTotal(count);
        page1.setRecords(list);
        return RespBodyObj.ok(page1);
    }


    /**
     * 新增新闻信息
     * @param request
     * @return
     */
    @PostMapping(value = "news/addNews",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj addNews(HttpServletRequest request,@RequestBody ReqBodyObj<Map<String,Object>> param){


        String title = (String) param.getData().get("title");
        String titleImg = (String) param.getData().get("titleImg");
        String content = (String) param.getData().get("content");
        String area = (String) param.getData().get("area");
        String abstracts = (String) param.getData().get("abstracts");

        News news = new News();

        String uuid = UUIDPK.UUIDGenerator.getUUID();
        news.setNewsNo(uuid);
        news.setContent(content);
        news.setTitle(title);
        news.setArea(area);
        news.setPublishTime(new Date());

        String userNo = request.getHeader("userNo");

        String userName = restTemplateService.getUserInfo(userNo).getUserName();
        //根据用户编号查询机构名称
        SysOrganization organizations = restTemplateService.getOrgByUserNo(userNo);
        if (null != organizations){
            news.setOrgNo(organizations.getOrgNo());
        }
        news.setPublishUsername(userName);
        news.setPublishUserid(userNo);
        news.setAbstracts(abstracts);
        news.setTitleImg(titleImg);
        int flag = 0;
        try {
            flag  = newsDao.insertSelective(news);
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }
        if (flag>0){
            return RespBodyObj.ok("添加成功");

        }else return RespBodyObj.ok("添加失败");
    }


    /**
     * 修改新闻信息
     * @return
     */
    @PostMapping(value = "news/updateNews",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj updateNews(@RequestBody ReqBodyObj<Map<String,Object>> param){


        String newsNo = (String) param.getData().get("newsNo");
        String title = (String) param.getData().get("title");
        String titleImg = (java.lang.String) param.getData().get("titleImg");
        String content = (String) param.getData().get("content");
        String area = (String) param.getData().get("area");
        String abstracts = (String) param.getData().get("abstracts");
        NewsQuery query = new NewsQuery();
        query.createCriteria().andNewsNoEqualTo(newsNo);


        News news = new News();
        news.setAbstracts(abstracts);
        news.setContent(content);
        news.setTitle(title);
        news.setArea(area);

        try {
            if(!(titleImg.isEmpty())){
                news.setTitleImg(titleImg);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }



        int flag=0;
        try {
            flag = newsDao.updateByExampleSelective(news,query);
        }catch (Exception e){
            e.printStackTrace();
            return RespBodyObj.error();
        }
        if (flag>0){
            return RespBodyObj.ok("修改成功");
        }else return RespBodyObj.ok("修改失败");
    }


    /**
     * 批量删除新闻信息
     * @return
     */
    @PostMapping(value = "news/deleteNews",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public RespBodyObj deleteNews(@RequestBody ReqBodyObj<Map<String,String []>> param){
        String[] newsNos = param.getData().get("newsNos");
        if (newsNos.length!=0){
            int flag = 0;
            for(int i=0;i<newsNos.length;i++){
                NewsQuery query = new NewsQuery();
                query.createCriteria().andNewsNoEqualTo(newsNos[i]);
                try {
                    flag = newsDao.deleteByExample(query);
                }catch (Exception e){
                    e.printStackTrace();
                    return RespBodyObj.error();
                }

            }
            if (flag>0){
                return RespBodyObj.ok("删除成功");
            }else return RespBodyObj.ok("删除失败");

        }else return RespBodyObj.ok("未选中数据");
    }

}