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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springblade.community.entity.Agency;
import org.springblade.community.feign.IAgencyClient;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.device.entity.Gb28181Device;
import org.springblade.device.service.IGbDeviceChannelService;
import org.springblade.device.vo.Gb28181DeviceVO;
import org.springblade.device.mapper.GbDeviceMapper;
import org.springblade.device.service.IGbDeviceService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.device.wrapper.GbDeviceWrapper;
import org.springblade.third.videodevice.gb28181.entity.GbDevice;
import org.springblade.third.videodevice.gb28181.feign.IThirdGbDeviceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.util.Assert;

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

/**
 *  服务实现类
 *
 * @author BladeX
 * @since 2021-12-06
 */
@Service
public class GbDeviceServiceImpl extends BaseServiceImpl<GbDeviceMapper, Gb28181Device> implements IGbDeviceService {

	@Autowired
	private IAgencyClient agencyClient;
	@Autowired
	private IThirdGbDeviceClient thirdGbDeviceClient;
	@Autowired
	private IGbDeviceChannelService deviceChannelService;

	@Override
	public IPage<Gb28181DeviceVO> selectGbDevicePage(IPage<Gb28181DeviceVO> page, Gb28181DeviceVO gbDevice) {
		return page.setRecords(baseMapper.selectGbDevicePage(page, gbDevice));
	}

	@Override
	public Gb28181Device getByDeviceId(String deviceId, Long agencyId) {
		LambdaQueryWrapper<Gb28181Device> lambda = new LambdaQueryWrapper<>();
		lambda.eq(Gb28181Device::getDeviceId, deviceId);
		lambda.eq(Gb28181Device::getAgencyId, agencyId);
		return baseMapper.selectOne(lambda);
	}

	@Override
	public R bind(Gb28181Device gb28181Device) {
		Long agencyId = gb28181Device.getAgencyId();
		Assert.notNull(agencyId, "缺少必要参数agencyId");
		String deviceId = gb28181Device.getDeviceId();
		Assert.hasText(deviceId, "设备编号必传");
		Gb28181Device device = this.getByDeviceId(gb28181Device.getDeviceId(), gb28181Device.getAgencyId());
		if(device == null){
			Agency agency = agencyClient.getById(gb28181Device.getAgencyId());
			gb28181Device.setTenantId(agency.getTenantId());
			return R.status(this.save(gb28181Device));
		}else{
			Gb28181Device copy = BeanUtil.copy(gb28181Device, Gb28181Device.class);
			copy.setId(device.getId());
			copy.setUpdateTime(new Date());
			return R.status(this.updateById(copy));
		}
	}

	@Override
	public R sync(Gb28181Device gb28181Device) {
		Long agencyId = gb28181Device.getAgencyId();
		Assert.notNull(agencyId, "缺少必要参数agencyId");
		String deviceId = gb28181Device.getDeviceId();
		Assert.hasText(deviceId, "设备编号必传");
		R<GbDevice> sync = thirdGbDeviceClient.sync(deviceId);
		if(sync.isSuccess()){
			Gb28181Device dbDevice = this.getByDeviceId(deviceId, agencyId);
			if(dbDevice != null){
				BeanUtil.copyProperties(sync.getData(), dbDevice);
				dbDevice.setId(gb28181Device.getId());
				dbDevice.setUpdateTime(new Date());
				this.updateById(dbDevice);
				return R.data(GbDeviceWrapper.build().entityVO(dbDevice));
			}else{
				return R.fail("设备不存在");
			}
		}else{
			return sync;
		}
	}

	@Override
	public R deleteDevice(List<Long> idList) {
		for(Long id : idList){
			Gb28181Device device = this.getById(id);
			String deviceId = device.getDeviceId();
			String tenantId = device.getTenantId();
			R r = this.deviceChannelService.deleteChannel(deviceId, tenantId);
			if(r.isSuccess()){
				this.baseMapper.deleteDevice(id);
			}else{
				return R.status(false);
			}
		}
		return R.status(true);
	}

}
