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.TaRedPacketHistory;
import com.yunzhi.marketing.redpack.service.ITaRedPacketHistoryService;
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("/api")
public class TaRedPacketHistoryController extends BaseController {

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

    @Autowired
    public ITaRedPacketHistoryService iTaRedPacketHistoryService;


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

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

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

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

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

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