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.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.entity.TdLiveDict;
import com.huiju.estateagents.service.ITdLiveDictService;
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-20
 */
@RestController
@RequestMapping("/api")
public class TdLiveDictController extends BaseController {

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

    @Autowired
    public ITdLiveDictService iTdLiveDictService;


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

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

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

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

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

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