package com.huiju.estateagents.property.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.AnnouncementTel;
import com.huiju.estateagents.property.service.IAnnouncementTelService;
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 javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 物业号码表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-03-05
 */
@RestController
@RequestMapping("/")

@Api(value = "物业端号码 API", description = "物业端号码 API")
public class AnnouncementTelController extends BaseController {
	
	@Autowired
	private IAnnouncementTelService announcementTelService;
	
	@ApiOperation(value = "根据搜索条件获取物业端号码", notes = "根据搜索条件获取物业端号码")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "name", value = "名称（模糊查询）"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "tel", value = "号码（模糊查询）"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "remark", 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 = "/tels",method = RequestMethod.GET)
	public ResponseBean getList(@RequestParam(value = "name", required = false) String name,
                                @RequestParam(value = "tel", required = false) String tel,
                                @RequestParam(value = "remark", required = false) String remark,
                                @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		AnnouncementTel announcementTel = new AnnouncementTel();
		announcementTel.setCommunityId(userElement.getOrgId());
		announcementTel.setName(name);
		announcementTel.setTel(tel);
		announcementTel.setRemark(remark);
		responseBean = announcementTelService.getTelList(announcementTel,pageNum,pageSize);
		return responseBean;
	}
	
	@ApiOperation(value = "添加物业端号码", notes = "添加物业端号码")
	@ApiImplicitParams({
			@ApiImplicitParam(dataTypeClass = String.class, paramType = "AnnouncementTel", name = "announcementTel", value = "电话号码实体类"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/tel/add",method = RequestMethod.POST)
	public ResponseBean addTel(@Valid @RequestBody AnnouncementTel announcementTel, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();

//		if (!AccountValidatorUtil.isNumber(announcementTel.getTel())) {
//			responseBean.addError("输入的号码不正确!");
//			return responseBean;
//		}

		TaUser userElement = getTaUser(request);
		QueryWrapper<AnnouncementTel> announcementTelQueryWrapper = new QueryWrapper<>();
		announcementTelQueryWrapper.eq("tel", announcementTel.getTel());
		announcementTelQueryWrapper.eq("community_id", userElement.getOrgId());
		AnnouncementTel telServiceOne = announcementTelService.getOne(announcementTelQueryWrapper);
		if (null != telServiceOne) {
			responseBean.addError("此号码已存在");
			return responseBean;
		}
		LocalDateTime time = LocalDateTime.now();
		announcementTel.setCommunityId(userElement.getOrgId());
		announcementTel.setCreateUser(userElement.getUserId());
		announcementTel.setUpdateUser(userElement.getUserId());
		announcementTel.setCreateDate(time);
		announcementTel.setUpdateDate(time);
		boolean state = announcementTelService.save(announcementTel);
		if (state){
			responseBean.addSuccess("添加物业端号码成功");
			return responseBean;
		}
		responseBean.addError("添加物业端号码失败");
		return responseBean;
	}
	
	@ApiOperation(value = "根据ID修改物业端号码", notes = "根据ID修改物业端号码")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "AnnouncementTel", name = "announcementTel", value = "电话号码实体类"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/tel/update", method = RequestMethod.PUT)
	public ResponseBean updateTel(@Valid @RequestBody AnnouncementTel announcementTel, HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		boolean isPhone = true;
		boolean isLandline = true;
//		if (!AccountValidatorUtil.isLandline(announcementTel.getTel())){
//			isLandline = false;
//		}
//		if (!AccountValidatorUtil.isPhone(announcementTel.getTel())){
//			isPhone = false;
//		}
//		if (isPhone == false && isLandline == false) {
//			responseBean.addError("输入的号码不正确!");
//			return responseBean;
//		}
//		if (!AccountValidatorUtil.isNumber(announcementTel.getTel())) {
//			responseBean.addError("输入的号码不正确!");
//			return responseBean;
//		}
		TaUser userElement = getTaUser(request);
		QueryWrapper<AnnouncementTel> announcementTelQueryWrapper = new QueryWrapper<>();
		announcementTelQueryWrapper.eq("tel", announcementTel.getTel());
		announcementTelQueryWrapper.eq("community_id", userElement.getOrgId());
		AnnouncementTel telServiceOne = announcementTelService.getOne(announcementTelQueryWrapper);
		if (null != telServiceOne && announcementTel.getTel().equals(telServiceOne.getTel()) && announcementTel.getId().equals(telServiceOne.getId())) {
			announcementTel.setUpdateUser(userElement.getUserId());
			announcementTel.setUpdateDate(LocalDateTime.now());
			announcementTelService.updateById(announcementTel);
			responseBean.addSuccess("修改物业端号码成功");
			return responseBean;
		}
		if (null != telServiceOne) {
			responseBean.addError("此号码已存在");
			return responseBean;
		}
		announcementTel.setUpdateUser(userElement.getUserId());
		announcementTel.setUpdateDate(LocalDateTime.now());
		boolean state = announcementTelService.updateById(announcementTel);
		if (state){
			responseBean.addSuccess("修改物业端号码成功");
			return responseBean;
		}
		responseBean.addError("修改物业端号码失败");
		return responseBean;
	}
	
	@ApiOperation(value = "获取Id获取物业端号码详情", notes = "获取Id获取物业端号码详情")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "Path", dataType = "integer", name = "id", value = "主键Id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/tel/{id}",method = RequestMethod.GET)
	public ResponseBean getNoticeById(@PathVariable(value ="id") Integer id, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		AnnouncementTel announcementTel = announcementTelService.getById(id);
		responseBean.addSuccess(announcementTel);
		return responseBean;
	}
	
	@ApiOperation(value = "根据ID批量删除物业端号码", notes = "根据ID批量删除物业端号码")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "integer", name = "ids", value = "主键Id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/tel/delete", method = RequestMethod.POST)
	public ResponseBean deleteNotice(@RequestBody List<Integer> ids, HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		boolean state = announcementTelService.removeByIds(ids);
		if (state){
			responseBean.addSuccess("删除物业端号码成功");
			return responseBean;
		}
		responseBean.addError("删除物业端号码失败");
		return responseBean;
	}
}
