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

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 红包助力设置  前端控制器
 * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@RestController
@RequestMapping("/api")
public class TaRedPacketHelpController extends BaseController {

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

    @Autowired
    public ITaRedPacketHelpService iTaRedPacketHelpService;


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

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

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

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

    /**
     * 修改对象
     *
     * @param id              实体ID
     * @param taRedPacketHelp 实体对象
     * @return
     */
    @RequestMapping(value = "/admin/redPacket/help/{id}", method = RequestMethod.PUT)
    public ResponseBean taRedPacketHelpUpdate(@RequestBody TaRedPacketHelp taRedPacketHelp) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaRedPacketHelpService.updateRedPackHelp(taRedPacketHelp);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taRedPacketHelpUpdate -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     *
     * @param id 实体ID
     */
    @RequestMapping(value = "/admin/redPacket/help/{id}", method = RequestMethod.GET)
    public ResponseBean taRedPacketHelpGet(@PathVariable String id, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaRedPacketHelpService.getRedPacketHelpById(id,getOrgId(request));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taRedPacketHelpDelete -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
