package com.yunzhi.marketing.xlk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.entity.TaBuilding;
import com.yunzhi.marketing.service.ITaBuildingService;
import com.yunzhi.marketing.xlk.dto.BuildingSpecialRoomDTO;
import com.yunzhi.marketing.xlk.entity.BuildingSpecialRoom;
import com.yunzhi.marketing.xlk.service.IBuildingSpecialRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
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 2021-07-12
 */
@RestController
@RequestMapping("/api")
@Api(value = "特价房源表", tags = "xlk-特价房源表")
public class BuildingSpecialRoomController extends BaseController {

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

    @Autowired
    public IBuildingSpecialRoomService iBuildingSpecialRoomService;

    @Autowired
    public ITaBuildingService iTaBuildingService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "admin-查询特价房源列表", notes = "admin-查询特价房源列表")
    @RequestMapping(value="/{plat}/buildingSpecialRoom",method= RequestMethod.GET)
    public ResponseBean buildingSpecialRoomList(@PathVariable("plat") String plat,
                                                @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                                @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                                @RequestParam(value ="buildingId") String buildingId,
                                                @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<BuildingSpecialRoom> pg = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<BuildingSpecialRoom> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BuildingSpecialRoom::getOrgId,getOrgId(request));
            queryWrapper.eq(BuildingSpecialRoom::getBuildingId,buildingId);
            queryWrapper.gt("wx".equals(plat), BuildingSpecialRoom::getEndTime, LocalDateTime.now());
            queryWrapper.orderByDesc(BuildingSpecialRoom::getCreateDate);

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

    @GetMapping("/wx/buildingSpecialRoom/building")
    public ResponseBean getListOfBuilding(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                          @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                          @RequestParam(value ="cityId", required = false) String cityId,
                                          HttpServletRequest request) {
        Integer orgId = getOrgId(request);
        IPage<TaBuilding> page = new Page<>(pageNum, pageSize);

        IPage<TaBuilding> result = iTaBuildingService.getListOfSpecial(page, orgId, cityId);

        return ResponseBean.success(result);
    }

    /**
     * 保存对象
     * @param buildingSpecialRoomDTO 实体对象
     * @return
     */
    @ApiOperation(value = "admin-保存特价房源", notes = "admin-保存特价房源")
    @RequestMapping(value="/admin/buildingSpecialRoom",method= RequestMethod.POST)
    public ResponseBean buildingSpecialRoomAdd(@RequestBody BuildingSpecialRoomDTO buildingSpecialRoomDTO, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            Integer orgId = getOrgId(request);
            BuildingSpecialRoom buildingSpecialRoom = new BuildingSpecialRoom();
            BeanUtils.copyProperties(buildingSpecialRoomDTO,buildingSpecialRoom);
            buildingSpecialRoom.setOrgId(orgId);
            buildingSpecialRoom.setCreateDate(LocalDateTime.now());
            if (iBuildingSpecialRoomService.save(buildingSpecialRoom)){
                responseBean.addSuccess(buildingSpecialRoom);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("buildingSpecialRoomAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ApiOperation(value = "admin-删除特价房源", notes = "admin-删除特价房源")
    @ResponseBody
    @RequestMapping(value="/admin/buildingSpecialRoom/{id}", method= RequestMethod.DELETE)
    public ResponseBean buildingSpecialRoomDelete(@PathVariable String id, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iBuildingSpecialRoomService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("buildingSpecialRoomDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param buildingSpecialRoomDTO 实体对象
     * @return
     */
    @ApiOperation(value = "admin-修改特价房源", notes = "admin-修改特价房源")
    @RequestMapping(value="/admin/buildingSpecialRoom/{id}",method= RequestMethod.PUT)
    public ResponseBean buildingSpecialRoomUpdate(@PathVariable String id,
                                                  @RequestBody BuildingSpecialRoomDTO buildingSpecialRoomDTO, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            BuildingSpecialRoom buildingSpecialRoom = new BuildingSpecialRoom();
            BeanUtils.copyProperties(buildingSpecialRoomDTO,buildingSpecialRoom);
            buildingSpecialRoom.setSpecialRoomId(id);
            if (iBuildingSpecialRoomService.updateById(buildingSpecialRoom)){
                responseBean.addSuccess(buildingSpecialRoom);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("buildingSpecialRoomUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @ApiOperation(value = "admin-获取特价房源详情", notes = "admin-获取特价房源详情")
    @RequestMapping(value="/admin/buildingSpecialRoom/{id}",method= RequestMethod.GET)
    public ResponseBean buildingSpecialRoomGet(@PathVariable String id, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iBuildingSpecialRoomService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("buildingSpecialRoomDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
