package com.yunzhi.marketing.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.common.StringUtils;
import com.yunzhi.marketing.entity.TaOfficeNews;
import com.yunzhi.marketing.service.ITaOfficeNewsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.bind.annotation.RestController;

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

/**
 * <p>
    * 资讯  前端控制器
    * </p>
 *
 * @author yansen
 * @since 2020-06-11
 */
@RestController
@RequestMapping("/api")
public class TaOfficeNewsController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaOfficeNewsController.class);

    @Autowired
    public ITaOfficeNewsService iTaOfficeNewsService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/channel/taOfficeNews",method= RequestMethod.GET)
    public ResponseBean taOfficeNewsList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                         @RequestParam(value ="title",required = false) String title,
                                         @RequestParam(value ="status",required = false) Integer status){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaOfficeNews> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaOfficeNews> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(!StringUtils.isEmpty(title),"title",title);
            queryWrapper.eq(null != status,"status",status);
            queryWrapper.gt("status", CommConstant.STATUS_DELETE);
            queryWrapper.orderByDesc("weight","create_date");

            IPage<TaOfficeNews> result = iTaOfficeNewsService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taOfficeNewsList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taOfficeNews 实体对象
     * @return
     */
    @RequestMapping(value="/channel/taOfficeNews",method= RequestMethod.POST)
    public ResponseBean taOfficeNewsAdd(@RequestBody TaOfficeNews taOfficeNews){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaOfficeNewsService.save(taOfficeNews)){
                responseBean.addSuccess(taOfficeNews);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taOfficeNewsAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/taOfficeNews/{id}", method= RequestMethod.DELETE)
    public ResponseBean taOfficeNewsDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTaOfficeNewsService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taOfficeNewsDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taOfficeNews 实体对象
     * @return
     */
    @RequestMapping(value="/channel/taOfficeNews/{id}",method= RequestMethod.PUT)
    public ResponseBean taOfficeNewsUpdate(@PathVariable Integer id,
                                        @RequestBody TaOfficeNews taOfficeNews){
        ResponseBean responseBean = new ResponseBean();
        try {
            taOfficeNews.setNewsId(id);
            if (iTaOfficeNewsService.updateById(taOfficeNews)){
                responseBean.addSuccess(taOfficeNews);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taOfficeNewsUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/channel/taOfficeNews/{id}",method= RequestMethod.GET)
    public ResponseBean taOfficeNewsGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaOfficeNewsService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taOfficeNewsDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }



    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/official/taOfficeNews",method= RequestMethod.GET)
    public ResponseBean getOfficialNews(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                        @RequestParam(value ="pageSize",defaultValue = "100") Integer pageSize,
                                        HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","*");
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TaOfficeNews> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaOfficeNews> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            queryWrapper.orderByDesc("weight","create_date");

            IPage<TaOfficeNews> result = iTaOfficeNewsService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taOfficeNewsList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
