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.TaBuildingDynamicType;
import com.huiju.estateagents.service.ITaBuildingDynamicTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
    * 活动类型表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-11-28
 */
@RestController
@RequestMapping("/api")
public class TaBuildingDynamicTypeController extends BaseController {

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

    @Autowired
    public ITaBuildingDynamicTypeService iTaBuildingDynamicTypeService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taBuildingDynamicType",method= RequestMethod.GET)
    public ResponseBean taBuildingDynamicTypeList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                                  @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer orgId = getOrgId(request);
            //使用分页插件
		    IPage<TaBuildingDynamicType> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaBuildingDynamicType> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(orgId != null,"org_id", orgId);
            queryWrapper.eq("status", 1);
            queryWrapper.orderByDesc("create_date");

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

    /**
     * 保存对象
     * @param taBuildingDynamicType 实体对象
     * @return
     */
    @RequestMapping(value="/admin/taBuildingDynamicType",method= RequestMethod.POST)
    public ResponseBean taBuildingDynamicTypeAdd(@RequestBody TaBuildingDynamicType taBuildingDynamicType, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            taBuildingDynamicType.setCreateDate(LocalDateTime.now());
            taBuildingDynamicType.setStatus(1);
            taBuildingDynamicType.setOrgId(getOrgId(request));
            if (iTaBuildingDynamicTypeService.save(taBuildingDynamicType)){
                responseBean.addSuccess(taBuildingDynamicType);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taBuildingDynamicTypeAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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