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.TdWxDict;
import com.huiju.estateagents.service.ITdWxDictService;
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-06
 */
@RestController
@RequestMapping("/api")
public class TdWxDictController extends BaseController {

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

    @Autowired
    public ITdWxDictService iTdWxDictService;


    /**
     * 分页查询列表
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/tdWxDict",method= RequestMethod.GET)
    public ResponseBean tdWxDictList(@RequestParam(value ="pageNumber",defaultValue = "1") Integer pageNumber,
                                     @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TdWxDict> pg = new Page<>(pageNumber, pageSize);
            QueryWrapper<TdWxDict> queryWrapper = new QueryWrapper<>();
            queryWrapper.groupBy("scene_type");

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

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

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

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

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