package com.neusoft.smart.pos.ota.service;

import com.neusoft.smart.pos.dto.ota.request.UpgradeHistoryQueryRequest;
import com.neusoft.smart.pos.dto.ota.request.UpgradeHistoryRequest;
import com.neusoft.smart.pos.dto.ota.response.UpgradeHistoryExtResponse;
import com.neusoft.smart.pos.dto.ota.response.UpgradeHistoryResponse;
import com.neusoft.smart.pos.framework.utils.BeanUtil;
import com.neusoft.smart.pos.ota.persistent.domain.DeviceUpgradeHistory;
import com.neusoft.smart.pos.ota.persistent.ext.domain.UpgradeHistoryQuery;
import com.neusoft.smart.pos.ota.persistent.repository.UpgradeHistoryRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
public class UpgradeHistoryService {
	
	@Autowired
	private UpgradeHistoryRepository upgradeHistoryRepository;
	
	/**
	 * @param upgradeHistoryRequest
	 * @return
	 */
	@Transactional
	public UpgradeHistoryResponse create(UpgradeHistoryRequest upgradeHistoryRequest) {
		UpgradeHistoryQuery upgradeHistoryQuery = new UpgradeHistoryQuery();
		upgradeHistoryQuery.setCurVersion(upgradeHistoryRequest.getCurVersion());
		upgradeHistoryQuery.setPrevVersion(upgradeHistoryRequest.getPrevVersion());
		upgradeHistoryQuery.setDeviceId(upgradeHistoryRequest.getDeviceId());
		if (upgradeHistoryRepository.queryCount(upgradeHistoryQuery) > 0) {
			return null;
		}
		DeviceUpgradeHistory deviceUpgradeHistory = new DeviceUpgradeHistory();
		BeanUtils.copyProperties(upgradeHistoryRequest, deviceUpgradeHistory);
		upgradeHistoryRepository.create(deviceUpgradeHistory);
		UpgradeHistoryResponse upgradeHistoryResponse = new UpgradeHistoryResponse();
		BeanUtils.copyProperties(deviceUpgradeHistory, upgradeHistoryResponse);
		return upgradeHistoryResponse;
	}
	
	/**
	 * @param upgradeHistoryQueryRequest
	 * @return
	 */
	public List<UpgradeHistoryResponse> getList(UpgradeHistoryQueryRequest upgradeHistoryQueryRequest) {
		List<UpgradeHistoryResponse> result = new ArrayList<>();
		List<DeviceUpgradeHistory> poList = upgradeHistoryRepository.queryList(BeanUtil.parse(upgradeHistoryQueryRequest, UpgradeHistoryQuery.class));
		poList.forEach(po -> {
			UpgradeHistoryResponse response = new UpgradeHistoryResponse();
			BeanUtils.copyProperties(po, response);
			result.add(response);
		});
		return result;
	}
	
	/**
	 * @param deviceIds
	 * @return
	 */
	public List<UpgradeHistoryExtResponse> getLastUpdateTime(List<Integer> deviceIds) {
		return upgradeHistoryRepository.queryLastUpdateTime(deviceIds).stream().collect(ArrayList<UpgradeHistoryExtResponse>::new, (list, deviceUpgradeHistoryExt) -> {
			list.add(BeanUtil.parse(deviceUpgradeHistoryExt, UpgradeHistoryExtResponse.class));
		}, ArrayList::addAll);
	}
	
	/**
	 * @param upgradeHistoryQueryRequest
	 * @return
	 */
	public Integer queryCount(UpgradeHistoryQueryRequest upgradeHistoryQueryRequest) {
		return upgradeHistoryRepository.queryCount(BeanUtil.parse(upgradeHistoryQueryRequest, UpgradeHistoryQuery.class));
	}
}
