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

/**
 * <p>
    * 红包客户端消息配置  前端控制器
    * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/")
public class TaRedPacketClientController extends BaseController {

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

    @Autowired
    public ITaRedPacketClientService iTaRedPacketClientService;


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

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

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

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

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

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