/*
 *      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.smartapplication.facedevice.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
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.springblade.smartapplication.facedevice.entity.FaceDevice;
import org.springblade.smartapplication.facedevice.mapper.FaceDeviceMapper;
import org.springblade.smartapplication.facedevice.service.IFaceDeviceService;
import org.springblade.smartapplication.facedevice.vo.FaceDeviceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2020-12-04
 */
@Service
@Slf4j
public class FaceDeviceServiceImpl extends BaseServiceImpl<FaceDeviceMapper, FaceDevice> implements IFaceDeviceService {

	@Autowired
	private IResidentialService residentialService;
	@Autowired
	private QinLinHttpUtil qinLinHttpUtil;


		@Override
	public IPage<FaceDeviceVO> selectFaceDevicePage(IPage<FaceDeviceVO> page, FaceDeviceVO faceDevice) {
		return page.setRecords(baseMapper.selectFaceDevicePage(page, faceDevice));
	}

	@Override
	public List<FaceDevice> selectByAgencyOrXqOrUnitPositionList(String agencyPos, String residentialPos, String unitPos) {
		QueryWrapper<FaceDevice> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().likeRight(FaceDevice::getOrgPosition, agencyPos)
			.or().like(FaceDevice::getOrgPosition, residentialPos)
			.or().likeRight(FaceDevice::getOrgPosition, unitPos);
		List<FaceDevice> faceDeviceList = baseMapper.selectList(queryWrapper);
		return faceDeviceList;
	}

	@Override
	public FaceDevice selectByMacAddress(String macAddress) {
		QueryWrapper<FaceDevice> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(FaceDevice::getMacAddress, macAddress);
		FaceDevice faceDevice = baseMapper.selectOne(queryWrapper);
		return faceDevice;
	}

	@Override
	public R registerToThird(FaceDevice faceDevice) {
		if(faceDevice != null){
			String deviceFactory = faceDevice.getDeviceFactory();
			switch (deviceFactory){
				case "qinlin":
					Long residentialId = faceDevice.getResidentialId();
					Residential residential = residentialService.getById(residentialId);
					log.info("============================注册亲邻设备==============================");
					log.info(JSONUtil.toJsonStr(residential));
					if(StringUtil.isBlank(residential.getThirdResidentialId())){
						return R.fail("请先在平台创建区域");
					}
					QLinCommunityDevices qLinCommunityDevices = new QLinCommunityDevices();
					qLinCommunityDevices.setMac(Func.toStrList(faceDevice.getMacAddress()));
					qLinCommunityDevices.setCommunityId(Long.valueOf(residential.getThirdResidentialId()));
					QinLinApiResultBean resultBean = null;
					try {
						resultBean = qinLinHttpUtil.addDevices(qLinCommunityDevices);
						log.info("========================注册设备响应数据======================");
						log.info(JSONUtil.toJsonStr(resultBean));
						boolean addSuccess = resultBean.getSuccess();
						if(addSuccess){
							return R.success("新增门禁设备成功");
						}else{
							return R.success("门禁设备注册失败：" + resultBean.getMessage());
						}
					}catch (Exception e){
						e.printStackTrace();
						return R.success("门禁注册系统异常：" + resultBean.getMessage());
					}
				case "JVT":
					break;
			}
		}
		return R.status(true);
	}

}
