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

/**
 * <p>
    * 微信支付设置  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-02-26
 */
@RestController
@RequestMapping("/")
public class TaWxPayConfigController extends BaseController {

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

    @Autowired
    public ITaWxPayConfigService iTaWxPayConfigService;


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

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

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

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

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

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