package com.huiju.estateagents.statistic.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.statistic.entity.TsPersonFromStatistic;
import com.huiju.estateagents.statistic.service.ITsPersonFromStatisticService;
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-01-04
 */
@RestController
@RequestMapping("/")
public class TsPersonFromStatisticController extends BaseController {

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

    @Autowired
    public ITsPersonFromStatisticService iTsPersonFromStatisticService;


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

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

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

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

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

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