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.entity.TaDocumentImg;
import com.yunzhi.marketing.service.ITaDocumentImgService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
    * 资料图片库  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-10-23
 */
@RestController
@RequestMapping("/")
public class TaDocumentImgController extends BaseController {

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

    @Autowired
    public ITaDocumentImgService iTaDocumentImgService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/documentImg",method= RequestMethod.GET)
    public ResponseBean documentImgList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                        @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaDocumentImg> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaDocumentImg> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

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

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

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

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

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