package com.yunzhi.marketing.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.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.entity.TdBizEvent;
import com.yunzhi.marketing.service.ITdBizEventService;
import com.yunzhi.marketing.base.BaseController;
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 javax.servlet.http.HttpServletRequest;

/**
 * <p>
    * 事件字典  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-06
 */
@RestController
@RequestMapping("/api")
public class TdBizEventController extends BaseController {

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

    @Autowired
    public ITdBizEventService iTdBizEventService;


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

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


    /**
     * 分页查询列表，并携带意向值
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/tdBizEventIntention",method= RequestMethod.GET)
    public ResponseBean tdBizEventAndIntention(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                               @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "buildingId", required = false) String buildingId,
                                               HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
            IPage<TdBizEvent> pg = new Page<>(pageNum, pageSize);
            responseBean = iTdBizEventService.getBizEventAndIntention(pg, buildingId, getOrgId(request));
        }catch (Exception e){
            logger.error("tdBizEventList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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

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

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

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