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.StringUtils;
import com.huiju.estateagents.entity.TaEventProperties;
import com.huiju.estateagents.service.ITaEventPropertiesService;
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 TaEventPropertiesController extends BaseController {

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

    @Autowired
    public ITaEventPropertiesService iTaEventPropertiesService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taEventProperties",method= RequestMethod.GET)
    public ResponseBean taEventPropertiesList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
									 @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                      @RequestParam(value ="eventType", required = false) String eventType,
                                      @RequestParam(value ="buildingId",required = false) String buildingId
    ){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaEventProperties> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaEventProperties> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(!StringUtils.isEmpty(eventType), "event_type", eventType);
            queryWrapper.eq(!StringUtils.isEmpty(buildingId), "building_id", buildingId);
            queryWrapper.groupBy("event_type");
            queryWrapper.orderByDesc("create_date");

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

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

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

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

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