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.common.CommConstant;
import com.huiju.estateagents.common.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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 com.huiju.estateagents.service.ITpMessageEventService;
import com.huiju.estateagents.entity.TpMessageEvent;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
    *  前端控制器
    * </p>
 *
 * @author yansen
 * @since 2020-12-22
 */

@Api(tags = "")
@RestController
@RequestMapping("/api")
public class TpMessageEventController extends BaseController {

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

    @Autowired
    public ITpMessageEventService iTpMessageEventService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/tpMessageEvent",method= RequestMethod.GET)
    @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean tpMessageEventList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                           @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                           @ApiParam("事件名称") @RequestParam(value ="name", required = false) String name,
                                           @ApiParam("事件类型") @RequestParam(value ="evtType", required = false) String evtType,
                                           HttpServletRequest request) throws Exception {

		    IPage<TpMessageEvent> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TpMessageEvent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id", getOrgId(request));
            queryWrapper.gt("status", CommConstant.STATUS_DELETE);
            queryWrapper.like(!StringUtils.isEmpty(name), "name", "%"+name+"%");
            queryWrapper.like(!StringUtils.isEmpty(evtType), "evtType", "%"+evtType+"%");
            queryWrapper.orderByAsc("evt_type");
            queryWrapper.orderByAsc("evt_date");

            IPage<TpMessageEvent> result = iTpMessageEventService.page(pg, queryWrapper);
            return ResponseBean.success(result);
    }

    /**
     * 保存对象
     * @param tpMessageEvent 实体对象
     * @return
     */
    @RequestMapping(value="/admin/tpMessageEvent",method= RequestMethod.POST)
    @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
    public ResponseBean tpMessageEventAdd(@ApiParam("保存内容") @RequestBody TpMessageEvent tpMessageEvent,
                                          HttpServletRequest request) throws Exception{

        tpMessageEvent.setOrgId(getOrgId(request));

        if (iTpMessageEventService.save(tpMessageEvent)){
            return ResponseBean.success(tpMessageEvent);
        }else {
            return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @RequestMapping(value="/admin/tpMessageEvent/{id}", method= RequestMethod.DELETE)
    @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
    public ResponseBean tpMessageEventDelete(@ApiParam("对象ID") @PathVariable Integer id,
                                             HttpServletRequest request) throws Exception{
        TpMessageEvent tpMessageEvent = iTpMessageEventService.getById(id);

        if (null == tpMessageEvent || tpMessageEvent.getStatus().equals(CommConstant.STATUS_DELETE)) {
            return ResponseBean.success("success");
        }

        if (!tpMessageEvent.getOrgId().equals(getOrgId(request))) {
            return ResponseBean.error("您无权限操作当前数据", ResponseBean.ERROR_UNAVAILABLE);
        }

        tpMessageEvent.setStatus(CommConstant.STATUS_DELETE);

        return tpMessageEventUpdate(id, tpMessageEvent, request);
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param tpMessageEvent 实体对象
     * @return
     */
    @RequestMapping(value="/admin/tpMessageEvent/{id}",method= RequestMethod.PUT)
    @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
    public ResponseBean tpMessageEventUpdate(@ApiParam("对象ID") @PathVariable Integer id,
                                             @ApiParam("更新内容") @RequestBody TpMessageEvent tpMessageEvent,
                                             HttpServletRequest request) throws Exception{

        if (null == tpMessageEvent) {
            return ResponseBean.error("数据不存在", ResponseBean.ERROR_UNAVAILABLE);
        }

        if (!tpMessageEvent.getOrgId().equals(getOrgId(request))) {
            return ResponseBean.error("您无权限操作当前数据", ResponseBean.ERROR_UNAVAILABLE);
        }

        if (iTpMessageEventService.updateById(tpMessageEvent)){
            return ResponseBean.success(iTpMessageEventService.getById(id));
        }else {
            return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/tpMessageEvent/{id}",method= RequestMethod.GET)
    @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean tpMessageEventGet(@ApiParam("对象ID") @PathVariable Integer id,
                                          HttpServletRequest request) throws Exception{
        TpMessageEvent tpMessageEvent = iTpMessageEventService.getById(id);

        if (null == tpMessageEvent || tpMessageEvent.getStatus().equals(CommConstant.STATUS_DELETE)) {
            return ResponseBean.error("数据不存在", ResponseBean.ERROR_UNAVAILABLE);
        }

        if (!tpMessageEvent.getOrgId().equals(getOrgId(request))) {
            return ResponseBean.error("您无权限操作当前数据", ResponseBean.ERROR_UNAVAILABLE);
        }

        return ResponseBean.success(tpMessageEvent);
    }
}
