package com.huiju.estateagents.property.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.model.TpRentalHouse;
import com.huiju.estateagents.property.service.ITpRentalHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 出租公寓表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-13
 */
@RestController
@RequestMapping("/")

@Api(value = "长租公寓API", description = "长租公寓API")
public class TpRentalHouseController extends BaseController {
	
	@Autowired
	private ITpRentalHouseService tpRentalHouseService;
	
	@ApiOperation(value = "根据搜索条件获取公寓列表", notes = "根据搜索条件获取公寓列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "房间编号"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "houseName", value = "房间名称"),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "startPrice", value = "开始价格"),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "endPrice", value = "结束价格"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "rentalType", value = "租赁方式"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "houseType", value = "户型"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "houseStatus", value = "上架状态"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
	})
	@RequestMapping(value = "/rentals",method = RequestMethod.GET)
	public ResponseBean getRentalList(@RequestParam(value = "id") Integer id,
	                                @RequestParam(value = "houseName") String houseName,
	                                @RequestParam(value = "startPrice") Integer startPrice,
	                                @RequestParam(value = "endPrice") Integer endPrice,
	                                @RequestParam(value = "rentalType") String rentalType,
	                                @RequestParam(value = "houseType") String houseType,
	                                @RequestParam(value = "houseStatus") String houseStatus,
	                                @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
	                                @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
	                                HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = tpRentalHouseService.getRentalList(id,houseName,startPrice,endPrice,rentalType,houseType,houseStatus,userElement,pageNum,pageSize);
		return responseBean;
	}
	
	@ApiOperation(value = "添加公寓", notes = "添加公寓")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "TpRentalHouse", name = "rentalHouse", value = "公寓"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/rental/add",method = RequestMethod.POST)
	public ResponseBean saveRental(@RequestBody TpRentalHouse rentalHouse, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = tpRentalHouseService.saveRental(rentalHouse,userElement);
		return responseBean;
	}
	
	@ApiOperation(value = "更新公寓", notes = "更新公寓")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "TpRentalHouse", name = "rentalHouse", value = "公寓"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/rental/update",method = RequestMethod.POST)
	public ResponseBean updateRental(@RequestBody TpRentalHouse rentalHouse, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = tpRentalHouseService.updateRental(rentalHouse,userElement);
		return responseBean;
	}
	
	@ApiOperation(value = "获取公寓信息", notes = "获取公寓信息")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "公寓Id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/rental/get/{id}",method = RequestMethod.GET)
	public ResponseBean getRental(@PathVariable Integer id, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		TpRentalHouse rentalHouse = tpRentalHouseService.getRentalById(id);
		responseBean.addSuccess(rentalHouse);
		return responseBean;
	}
	
	@ApiOperation(value = "上架房间", notes = "上架房间")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "String", name = "jsonString", value = "房间id集合"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/rental/shelf",method = RequestMethod.POST)
	public ResponseBean shelfRental(@RequestBody String jsonString, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		JSONArray ids = jsonObject.getJSONArray("idArray");
		List<TpRentalHouse> rentalList = new ArrayList<>();
		for (Object id : ids){
			TpRentalHouse rentalHouse = new TpRentalHouse();
			rentalHouse.setId((Integer) id);
			rentalHouse.setHouseStatus("1");
			rentalHouse.setUpdateUser(userElement.getUserId());
			rentalHouse.setUpdateDate(LocalDateTime.now());
			rentalList.add(rentalHouse);
		}
		
		boolean state = tpRentalHouseService.updateBatchById(rentalList);
		if (state){
			responseBean.addSuccess("上架成功");
		}else{
			responseBean.addError("上架失败");
		}
		return responseBean;
	}
	
	@ApiOperation(value = "下架房间", notes = "下架房间")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "String", name = "jsonString", value = "房间id集合"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/rental/obtained",method = RequestMethod.POST)
	public ResponseBean obtainedRental(@RequestBody String jsonString, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		JSONArray ids = jsonObject.getJSONArray("idArray");
		List<TpRentalHouse> rentalList = new ArrayList<>();
		for (Object id : ids){
			TpRentalHouse rentalHouse = new TpRentalHouse();
			rentalHouse.setId((Integer) id);
			rentalHouse.setHouseStatus("2");
			rentalHouse.setUpdateUser(userElement.getUserId());
			rentalHouse.setUpdateDate(LocalDateTime.now());
			rentalList.add(rentalHouse);
		}
		
		boolean state = tpRentalHouseService.updateBatchById(rentalList);
		if (state){
			responseBean.addSuccess("下架成功");
		}else{
			responseBean.addError("下架失败");
		}
		return responseBean;
	}
}
