package com.huiju.estateagents.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.ResponseBean;
import com.huiju.estateagents.entity.TdBizEventType;
import com.huiju.estateagents.service.ITdBizEventTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.huiju.estateagents.base.BaseController;

/**
 * <p>
    * 事件类型字典  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-06
 */
@RestController
@RequestMapping("/api")
public class TdBizEventTypeController extends BaseController {

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

    @Autowired
    public ITdBizEventTypeService iTdBizEventTypeService;


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

            IPage<TdBizEventType> result = iTdBizEventTypeService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            logger.error("tdBizEventTypeList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/wx/tdBizEventType",method= RequestMethod.GET)
    public ResponseBean tdBizEventTypeListWx(@RequestParam(value ="pageNumber",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TdBizEventType> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TdBizEventType> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("type_id");

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

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

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

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

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