package com.sv.service.oms;

import com.common.DeviceDTO;
import com.enums.DeviceStatusEnum;
import com.github.pagehelper.PageHelper;
import com.sv.entity.Device;
import com.sv.exception.oms.OmsException;
import com.sv.mapper.DeviceMapper;
import com.ydd.framework.core.common.Pagination;
import com.ydd.framework.core.common.dto.ResponseDTO;
import com.ydd.framework.core.service.impl.BaseServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * Service - 门禁设备
 *
 * @author zhaoziyu
 * @since 2018-08-09
 */
@Service
@Transactional(readOnly = true)
public class DeviceService extends BaseServiceImpl  {

	private final Logger logger = LoggerFactory.getLogger(DeviceService.class);

	@Resource
	private DeviceMapper deviceMapper;

	@Resource
	private RestTemplate restTemplate;


	/**
	 * 创建门禁设备
	 *
	 * @param device 门禁设备
	 */
	
	@Transactional
    public void save(Device device) {
		if(deviceMapper.countByStream(device.getStream(),device.getId()) > 0){
			throw new OmsException("该视频流已经存在");
		}
		if (device.getId() != null && device.getId() > 0) {
			// 更新
			deviceMapper.update(device);
		} else {
			// 新建
			deviceMapper.insert(device);
		}
	}

	/**
	 * 更新门禁设备
 	 *
	 * @param device 门禁设备
	 */
	
	@Transactional
    public void update(Device device) {
		deviceMapper.update(device);
	}

	/**
	 * 删除门禁设备
	 *
	 * @param id 编号
   	 * @return 删除数量
	 */
	
	@Transactional
    public Integer delete(Integer id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return deviceMapper.delete(id);
	}

	/**
	 * 删除门禁设备
	 *
	 * @param ids 编号数组
	 * @return 删除数量
	 */
	
	@Transactional
    public Integer deleteByIds(Integer[] ids) {
		if (ids == null || ids.length == 0) {
			return 0;
		}
		return deviceMapper.deleteByIds(ids);
	}

	/**
	 * 查询门禁设备
	 *
	 * @param id 编号
	 * @return 门禁设备
	 */
	public Device findById(Integer id) {
		return deviceMapper.findById(id);
	}

	/**
	 * 查询门禁设备
	 *
	 * @param pagination 分页信息
	 * @return 分页结果
	 */
	
	public Pagination findPage(Pagination pagination) {
		PageHelper.startPage(pagination.getPage(), pagination.getPageSize());
		pagination.setQueryResult(deviceMapper.findAllDTO());
		return pagination;
	}

	public List<DeviceDTO> findAll(){
		return deviceMapper.findAllDTO();
	}

	/**
	 * 设备连接断开
	 * @param deviceId
	 */
	@Transactional
	public void offline(Integer deviceId){
		Device device = new Device();
		device.setId(deviceId);
		device.setStatus(DeviceStatusEnum.OFFLINE.value);
		deviceMapper.update(device);
	}

	/**
	 * 设备连接成功
	 * @param deviceId
	 */
	@Transactional
	public void online(Integer deviceId){
		Device device = new Device();
		device.setId(deviceId);
		device.setStatus(DeviceStatusEnum.ONLINE.value);
		deviceMapper.update(device);
	}

	/**
	 * 设备正在重连
	 * @param deviceId
	 */
	@Transactional
	public void setReconnect(Integer deviceId){
		Device device = new Device();
		device.setId(deviceId);
		device.setStatus(DeviceStatusEnum.RECONNECT.value);
		deviceMapper.update(device);
	}

	public void reconnect(Integer deviceId){
		try {
			HttpHeaders headers = new HttpHeaders();
			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
			HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(null,headers);
			ResponseDTO responseDTO = restTemplate.postForObject("http://127.0.0.1:8000/device/reconnect/"+deviceId, request, ResponseDTO.class);
			Integer code = (Integer) responseDTO.get(ResponseDTO.ERR_CODE);
			if(0 != code){
				throw new OmsException("重启失败！");
			}
		}catch (Exception e){
			throw new OmsException("连接失败！");
		}

	}
}

