package com.yunzhi.marketing.redpack.controller;

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 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.redpack.entity.TaH5Images;
import com.yunzhi.marketing.redpack.service.ITaH5ImagesService;

/**
 * <p>
    * H5图片列表  前端控制器
    * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/")
public class TaH5ImagesController extends BaseController {

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

    @Autowired
    public ITaH5ImagesService iTaH5ImagesService;


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

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

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

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

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

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