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.common.CommConstant;
import com.huiju.estateagents.entity.TdReport;
import com.huiju.estateagents.service.ITdReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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-08
 */
@RestController
@RequestMapping("/api")
@Api(value = "客户留言", tags = "客户留言")
public class TdReportController extends BaseController {

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

    @Autowired
    public ITdReportService iTdReportService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "后台报表列表", notes = "后台报表列表")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageNum", paramType = "query",value = "第几页"),
            @ApiImplicitParam(dataTypeClass = Integer.class, name = "pageSize", paramType = "query",value = "一页多少行")
    })
    @RequestMapping(value="/admin/tdReports",method= RequestMethod.GET)
    public ResponseBean tdReportList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TdReport> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TdReport> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            queryWrapper.orderByDesc("create_date");

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

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

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

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

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