package com.community.huiju.controller;

import com.community.commom.constant.Constant;
import com.community.commom.mode.ResponseBean;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.ToCommunities;
import com.community.huiju.service.CommunityServiceI;
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.cloud.context.config.annotation.RefreshScope;
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 org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author FXF
 * @date 2018-10-31
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "运营端社区API", description = "运营端社区API")
public class CommunityController extends BaseController {
	
	@Autowired
	private CommunityServiceI communityService;
	
	@ApiOperation(value = "根据搜索条件获取小区列表", notes = "根据搜索条件获取小区列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "communityId", value = "社区编号"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "communityName", value = "社区名称（模糊查询）"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "provinceId", value = "省份id"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "cityId", value = "城市id"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "districtId", value = "区县id"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度")
	})
	@RequestMapping(value = "/communitys",method = RequestMethod.GET)
	public ResponseBean getCommunitys(@RequestParam(value = "communityId", required = false) Integer communityId,
	                                  @RequestParam(value = "communityName", required = false) String communityName,
	                                  @RequestParam(value = "provinceId", required = false) Integer provinceId,
	                                  @RequestParam(value = "cityId", required = false) Integer cityId,
	                                  @RequestParam(value = "districtId", required = false) Integer districtId,
	                                  @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
	                                  @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
		ResponseBean responseBean = new ResponseBean();
		ToCommunities toCommunities = new ToCommunities();
		toCommunities.setId(communityId);
		toCommunities.setCommunityName(communityName);
		toCommunities.setProvinceId(provinceId);
		toCommunities.setCityId(cityId);
		toCommunities.setDistrictId(districtId);
		Map<String,Object> map = communityService.getCommunitys(toCommunities,pageNum,pageSize);
		responseBean.addSuccess(map);
		return responseBean;
	}
	
	@ApiOperation(value = "添加小区", notes = "添加小区")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", name = "toCommunities", dataType = "String",value =
					"communityName：社区名称,communityAlias：小区别名， provinceId:省份id,cityId:城市id,districtId:区县id，longitude: 经度,latitude: 纬度,userName:物业端用户名,loginName:物业端登录名")
	})
	@RequestMapping(value = "/community/add",method = RequestMethod.POST)
	public ResponseBean addCommunity(@RequestBody @Valid ToCommunities toCommunities){
		ResponseBean responseBean = new ResponseBean();
		String telRegex = "[1][3578]\\d{9}";
		if (!toCommunities.getLoginName().matches(telRegex)){
			responseBean.addError("请输入正确格式的手机号！");
			return responseBean;
		}
		Integer size = communityService.addCommunity(toCommunities);
		responseBean.addSuccess(size);
		return responseBean;
	}
	
	@ApiOperation(value = "根据id获取小区", notes = "根据id获取小区")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataType = "integer", name = "id", value = "小区id")
	})
	@RequestMapping(value = "/community/{id}",method = RequestMethod.GET)
	public ResponseBean getCommunityById(@PathVariable("id") Integer id){
		ResponseBean responseBean = new ResponseBean();
		ToCommunities communities = communityService.getCommunityById(id);
		responseBean.addSuccess(communities);
		return responseBean;
	}
	
	@ApiOperation(value = "根据id更新小区数据", notes = "根据id更新小区数据")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", name = "toCommunities", dataType = "String",value =
					"communityName：社区名称,communityAlias：小区别名， provinceId:省份id,cityId:城市id,districtId:区县id，longitude: 经度,latitude: 纬度,userName:物业端用户名,loginName:物业端登录名")
	})
	@RequestMapping(value = "/community/update",method = RequestMethod.POST)
	public ResponseBean updateCommunityById(@RequestBody ToCommunities toCommunities){
		ResponseBean responseBean = new ResponseBean();
		String telRegex = "[1][3578]\\d{9}";
		if (!toCommunities.getLoginName().matches(telRegex)){
			responseBean.addError("请输入正确格式的手机号！");
			return responseBean;
		}
		String message = communityService.updateCommunityById(toCommunities);
		if (message.equals(Constant.SUCCESS)){
			responseBean.addSuccess(message);
		}else {
			responseBean.addError(message);
		}
		
		return responseBean;
	}
}
