package com.huiju.estateagents.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.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.redpack.entity.TaRedPacketMoney;
import com.huiju.estateagents.redpack.service.ITaRedPacketMoneyService;
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 TaRedPacketMoneyController extends BaseController {

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

    @Autowired
    public ITaRedPacketMoneyService iTaRedPacketMoneyService;


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

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

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

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

    /**
     * 修改对象
     *
     * @param id               实体ID
     * @param taRedPacketMoney 实体对象
     * @return
     */
    @RequestMapping(value = "/admin/redPacket/money/{id}", method = RequestMethod.PUT)
    public ResponseBean taRedPacketMoneyUpdate(@PathVariable("id") String id, @RequestBody TaRedPacketMoney taRedPacketMoney) {
        ResponseBean responseBean = new ResponseBean();
        try {
            taRedPacketMoney.setActivityId(id);
            responseBean = iTaRedPacketMoneyService.updateRedPacketMoney(taRedPacketMoney);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taRedPacketMoneyUpdate -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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