package com.neusoft.smart.pos.ota.persistent.repository;

import com.neusoft.smart.pos.ota.persistent.domain.DeviceUpgradeHistory;
import com.neusoft.smart.pos.ota.persistent.domain.DeviceUpgradeHistoryExample;
import com.neusoft.smart.pos.ota.persistent.domain.DeviceUpgradeHistoryExt;
import com.neusoft.smart.pos.ota.persistent.ext.domain.UpgradeHistoryQuery;
import com.neusoft.smart.pos.ota.persistent.mapper.DeviceUpgradeHistoryMapper;
import com.neusoft.smart.pos.ota.persistent.mapper.DeviceUpgradeHistoryMapperExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class UpgradeHistoryRepository {
	
	@Autowired
	private DeviceUpgradeHistoryMapper deviceUpgradeHistoryMapper;
	
	@Autowired
	private DeviceUpgradeHistoryMapperExt deviceUpgradeHistoryMapperExt;
	
	/**
	 * @param deviceUpgradeHistory
	 */
	public void create(DeviceUpgradeHistory deviceUpgradeHistory) {
		deviceUpgradeHistoryMapper.insertSelective(deviceUpgradeHistory);
	}
	
	/**
	 * @param upgradeHistoryQuery
	 * @return
	 */
	public List<DeviceUpgradeHistory> queryList(UpgradeHistoryQuery upgradeHistoryQuery) {
		DeviceUpgradeHistoryExample deviceUpgradeHistoryExample = this.exampleProvider(upgradeHistoryQuery);
		if (upgradeHistoryQuery.getIsPage()) {
			deviceUpgradeHistoryExample.setLimit(upgradeHistoryQuery.getLimit());
			deviceUpgradeHistoryExample.setOffset(upgradeHistoryQuery.getOffset());
		}
		
		return deviceUpgradeHistoryMapper.selectByExample(deviceUpgradeHistoryExample);
	}
	
	/**
	 * @param upgradeHistoryQuery
	 * @return
	 */
	public Integer queryCount(UpgradeHistoryQuery upgradeHistoryQuery) {
		DeviceUpgradeHistoryExample deviceUpgradeHistoryExample = this.exampleProvider(upgradeHistoryQuery);
		return deviceUpgradeHistoryMapper.countByExample(deviceUpgradeHistoryExample);
	}
	
	/**
	 * @param deviceIds
	 * @return
	 */
	public List<DeviceUpgradeHistoryExt> queryLastUpdateTime(List<Integer> deviceIds) {
		return deviceUpgradeHistoryMapperExt.selectLastUpdateTimeByDeviceIds(deviceIds);
	}
	
	/**
	 * @param upgradeHistoryQuery
	 * @return
	 */
	private DeviceUpgradeHistoryExample exampleProvider(UpgradeHistoryQuery upgradeHistoryQuery) {
		DeviceUpgradeHistoryExample deviceUpgradeHistoryExample = new DeviceUpgradeHistoryExample();
		DeviceUpgradeHistoryExample.Criteria criteria = deviceUpgradeHistoryExample.createCriteria();
		deviceUpgradeHistoryExample.setOrderByClause("update_time desc");
		if (null != upgradeHistoryQuery.getDeviceIdList() && !upgradeHistoryQuery.getDeviceIdList().isEmpty()) {
			criteria.andDeviceIdIn(upgradeHistoryQuery.getDeviceIdList());
		}
		if (null != upgradeHistoryQuery.getDeviceId()) {
			criteria.andDeviceIdEqualTo(upgradeHistoryQuery.getDeviceId());
		}
		if (!StringUtils.isEmpty(upgradeHistoryQuery.getCurVersion())) {
			criteria.andCurVersionEqualTo(upgradeHistoryQuery.getCurVersion());
		}
		if (!StringUtils.isEmpty(upgradeHistoryQuery.getPrevVersion())) {
			criteria.andPrevVersionEqualTo(upgradeHistoryQuery.getPrevVersion());
		}
		return deviceUpgradeHistoryExample;
	}
}
