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

import com.neusoft.smart.pos.ota.persistent.domain.UpgradeStrategy;
import com.neusoft.smart.pos.ota.persistent.domain.UpgradeStrategyExample;
import com.neusoft.smart.pos.ota.persistent.domain.UpgradeStrategyGroup;
import com.neusoft.smart.pos.ota.persistent.domain.UpgradeStrategyGroupExample;
import com.neusoft.smart.pos.ota.persistent.ext.domain.UpgradeStrategyQuery;
import com.neusoft.smart.pos.ota.persistent.mapper.UpgradeStrategyGroupMapper;
import com.neusoft.smart.pos.ota.persistent.mapper.UpgradeStrategyMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Component
public class UpgradeStrategyRepository {
	
	@Autowired
	private UpgradeStrategyGroupMapper upgradeStrategyGroupMapper;
	
	@Autowired
	private UpgradeStrategyMapper upgradeStrategyMapper;
	
	
	/**
	 * @param upgradeStrategyGroup
	 */
	public void createGroup(UpgradeStrategyGroup upgradeStrategyGroup) {
		upgradeStrategyGroupMapper.insertSelective(upgradeStrategyGroup);
	}
	
	/**
	 * @param upgradeStrategy
	 */
	public void create(UpgradeStrategy upgradeStrategy) {
		upgradeStrategyMapper.insertSelective(upgradeStrategy);
	}
	
	/**
	 * @param upgradeStrategyQuery
	 * @return
	 */
	public List<UpgradeStrategyGroup> queryGroupList(UpgradeStrategyQuery upgradeStrategyQuery) {
		UpgradeStrategyGroupExample upgradeStrategyGroupExample = this.exampleProvider(upgradeStrategyQuery);
		upgradeStrategyGroupExample.setLimit(upgradeStrategyQuery.getLimit());
		upgradeStrategyGroupExample.setOffset(upgradeStrategyQuery.getOffset());
		return upgradeStrategyGroupMapper.selectByExample(upgradeStrategyGroupExample);
	}
	
	/**
	 * @param upgradeStrategyQuery
	 * @return
	 */
	public Integer queryGroupCount(UpgradeStrategyQuery upgradeStrategyQuery) {
		UpgradeStrategyGroupExample upgradeStrategyGroupExample = this.exampleProvider(upgradeStrategyQuery);
		return upgradeStrategyGroupMapper.countByExample(upgradeStrategyGroupExample);
	}
	
	/**
	 * @param groupId
	 * @return
	 */
	public UpgradeStrategyGroup getGroup(Integer groupId) {
		return upgradeStrategyGroupMapper.selectByPrimaryKey(groupId);
	}
	
	/**
	 * @param upgradeStrategyQuery
	 * @return
	 */
	public List<UpgradeStrategy> queryList(UpgradeStrategyQuery upgradeStrategyQuery) {
		UpgradeStrategyExample upgradeStrategyExample = new UpgradeStrategyExample();
		if (upgradeStrategyQuery.getIsPage()) {
			upgradeStrategyExample.setLimit(upgradeStrategyQuery.getLimit());
			upgradeStrategyExample.setOffset(upgradeStrategyQuery.getOffset());
		}
		UpgradeStrategyExample.Criteria criteria = upgradeStrategyExample.createCriteria();
		if (!StringUtils.isEmpty(upgradeStrategyQuery.getDeviceId())) {
			criteria.andDeviceIdEqualTo(upgradeStrategyQuery.getDeviceId());
		}
		if (!StringUtils.isEmpty(upgradeStrategyQuery.getGroupId())) {
			criteria.andGroupIdEqualTo(upgradeStrategyQuery.getGroupId());
		}
		if (null != upgradeStrategyQuery.getGroupIds() && !upgradeStrategyQuery.getGroupIds().isEmpty()) {
			criteria.andGroupIdIn(upgradeStrategyQuery.getGroupIds());
		}
		if (null!=upgradeStrategyQuery.getStatus()) {
			criteria.andStatusEqualTo(upgradeStrategyQuery.getStatus());
		}
		return upgradeStrategyMapper.selectByExample(upgradeStrategyExample);
	}
	

	public void updateGroup(UpgradeStrategyGroup upgradeStrategyGroup) {
		upgradeStrategyGroupMapper.updateByPrimaryKeySelective(upgradeStrategyGroup);
	}
	
	public void update(UpgradeStrategy upgradeStrategy) {
		upgradeStrategyMapper.updateByPrimaryKeySelective(upgradeStrategy);
	}
	
	public void updateByExampleAndId(Integer groupId, UpgradeStrategy upgradeStrategy) {
		UpgradeStrategyExample upgradeStrategyExample = new UpgradeStrategyExample();
		upgradeStrategyExample.createCriteria().andGroupIdEqualTo(groupId);
		upgradeStrategyMapper.updateByExampleSelective(upgradeStrategy, upgradeStrategyExample);
	}
	
	public void updateStatus(Integer packageId, Integer deviceId) {
		UpgradeStrategyExample upgradeStrategyExample = new UpgradeStrategyExample();
		upgradeStrategyExample.createCriteria().andPackageIdEqualTo(packageId).
				andDeviceIdEqualTo(deviceId).andStatusEqualTo(0);
		UpgradeStrategy upgradeStrategy = new UpgradeStrategy();
		upgradeStrategy.setStatus(1);
		upgradeStrategy.setUpdateTime(new Date());
		upgradeStrategyMapper.updateByExampleSelective(upgradeStrategy, upgradeStrategyExample);
	}
	
	private UpgradeStrategyGroupExample exampleProvider(UpgradeStrategyQuery upgradeStrategyQuery) {
		UpgradeStrategyGroupExample upgradeStrategyGroupExample = new UpgradeStrategyGroupExample();
		upgradeStrategyGroupExample.setOrderByClause("created_date desc");
		UpgradeStrategyGroupExample.Criteria criteria = upgradeStrategyGroupExample.createCriteria();
		if (!StringUtils.isEmpty(upgradeStrategyQuery.getStrategyName())) {
			criteria.andStrategyNameLike("%" + upgradeStrategyQuery.getStrategyName() + "%");
		}
		if (!StringUtils.isEmpty(upgradeStrategyQuery.getStartDate())) {
			try {
				criteria.andCreatedDateGreaterThanOrEqualTo(DateUtils.parseDate(upgradeStrategyQuery.getStartDate() + " 00:00:00", new String[]{"yyyy-MM-dd HH:mm:ss"}));
			} catch (ParseException ignored) {
			}
		}
		if (!StringUtils.isEmpty(upgradeStrategyQuery.getEndDate())) {
			try {
				criteria.andCreatedDateLessThanOrEqualTo(DateUtils.parseDate(upgradeStrategyQuery.getEndDate() + " 23:59:59", new String[]{"yyyy-MM-dd HH:mm:ss"}));
			} catch (ParseException ignored) {
			}
		}
		if (!StringUtils.isEmpty(upgradeStrategyQuery.getOrganizationId())) {
			criteria.andOrganizationIdEqualTo(upgradeStrategyQuery.getOrganizationId());
		}
		if (null != upgradeStrategyQuery.getPackageId()) {
			criteria.andPackageIdEqualTo(upgradeStrategyQuery.getPackageId());
		}
		criteria.andStatusNotEqualTo(1);//过滤删除的
		
		if (!"sa".equals(RequestHeaderUtil.getRole())) {
			criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
		}
		return upgradeStrategyGroupExample;
	}
}
