package com.yunzhi.marketing.xlk.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.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.xlk.entity.HouseProperty;
import com.yunzhi.marketing.xlk.service.IHousePropertyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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 2021-06-15
 */
@RestController
@RequestMapping("/api")
@Api(value = "帮我找房问卷问题表", tags = "xlk-帮我找房问卷问题表")
public class HousePropertyController extends BaseController {

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

    @Autowired
    public IHousePropertyService iHousePropertyService;


    @ApiOperation(value = "wx-获取帮我找房问卷问题表", notes = "admin-获取帮我找房问卷问题表")
    @RequestMapping(value="/wx/houseProperty",method= RequestMethod.GET)
    public ResponseBean housePropertyList(){
        return ResponseBean.success(iHousePropertyService.list());
    }

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

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

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

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