/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.community.residential.service.impl;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.vo.ResidentialVO;
import org.springblade.community.residential.mapper.ResidentialMapper;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.openinterface.facedevice.qinlin.beans.QinLinApiResultBean;
import org.springblade.openinterface.facedevice.qinlin.util.QinLinHttpUtil;
import org.springblade.openinterface.facedevice.qinlin.vo.QLinCommunityDevices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 小区表，新建小区的时候，小区也要作为一个机构存入到机构表里面，机构的编号就使用当前小区的编号 服务实现类
 *
 * @author BladeX
 * @since 2020-10-16
 */
@Service
@Slf4j
public class ResidentialServiceImpl extends BaseServiceImpl<ResidentialMapper, Residential> implements IResidentialService {

	@Autowired
	private QinLinHttpUtil qinLinHttpUtil;

	@Override
	public IPage<ResidentialVO> selectResidentialPage(IPage<ResidentialVO> page, ResidentialVO residential) {
		return page.setRecords(baseMapper.selectResidentialPage(page, residential));
	}

	@Override
	public List<Residential> getListWithYtelevatorXq() {
		return baseMapper.getListWithYtelevatorXq();
	}

	@Override
	public Boolean updateNumberById(String residentialId) {
		return baseMapper.updateNumberById(residentialId);
	}

	@Override
	public R registerToThird(Residential residential) {
		String communityName = residential.getName();
		QLinCommunityDevices qLinCommunityDevices = new QLinCommunityDevices();
		qLinCommunityDevices.setCommunityName(communityName);
		QinLinApiResultBean resultBean = null;
		try {
			resultBean = qinLinHttpUtil.addCommunity(qLinCommunityDevices);
			log.info("========================注册区域响应数据======================");
			log.info(JSONUtil.toJsonStr(resultBean));
			boolean addSuccess = resultBean.getSuccess();
			if(addSuccess){
				residential.setThirdResidentialId(resultBean.getData().toString());
				this.updateById(residential);
				return R.success("注册区域成功");
			}else{
				return R.fail("注册区域失败：" + resultBean.getMessage());
			}
		}catch (Exception e){
			e.printStackTrace();
			return R.fail("注册区域异常：" + resultBean.getMessage());
		}
	}

}
