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

import com.neusoft.smart.pos.device.persistent.domain.ConfigStrategy;
import com.neusoft.smart.pos.device.persistent.domain.ConfigStrategyExample;
import com.neusoft.smart.pos.device.persistent.domain.ConfigStrategyGroup;
import com.neusoft.smart.pos.device.persistent.domain.ConfigStrategyGroupExample;
import com.neusoft.smart.pos.device.persistent.ext.domain.ConfigStrategyQuery;
import com.neusoft.smart.pos.device.persistent.mapper.ConfigStrategyGroupMapper;
import com.neusoft.smart.pos.device.persistent.mapper.ConfigStrategyMapper;
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.List;

@Component
public class ConfigStrategyRepository {
	
	@Autowired
	private ConfigStrategyGroupMapper configStrategyGroupMapper;
	
	@Autowired
	private ConfigStrategyMapper configStrategyMapper;
	
	/**
	 * @param configStrategyGroup
	 * @return
	 */
	public Integer createGroup(ConfigStrategyGroup configStrategyGroup) {
		configStrategyGroupMapper.insertSelective(configStrategyGroup);
		return configStrategyGroup.getId();
	}
	
	/**
	 * @param configStrategy
	 */
	public void create(ConfigStrategy configStrategy) {
		configStrategyMapper.insertSelective(configStrategy);
	}
	
	/**
	 * @param configStrategyList
	 */
	public void batchCreate(List<ConfigStrategy> configStrategyList) {
		configStrategyMapper.batchInsert(configStrategyList);
	}
	
	/**
	 * @param configStrategyQuery
	 * @return
	 */
	public List<ConfigStrategyGroup> queryGroupList(ConfigStrategyQuery configStrategyQuery) {
		ConfigStrategyGroupExample configStrategyGroupExample = this.exampleProvider(configStrategyQuery);
		if (configStrategyQuery.getIsPage()) {
			configStrategyGroupExample.setLimit(configStrategyQuery.getLimit());
			configStrategyGroupExample.setOffset(configStrategyQuery.getOffset());
		}
		return configStrategyGroupMapper.selectByExample(configStrategyGroupExample);
	}
	
	/**
	 * @param configStrategyQuery
	 * @return
	 */
	public Integer queryGroupCount(ConfigStrategyQuery configStrategyQuery) {
		ConfigStrategyGroupExample configStrategyGroupExample = this.exampleProvider(configStrategyQuery);
		return configStrategyGroupMapper.countByExample(configStrategyGroupExample);
	}
	
	/**
	 * @param strategyId
	 * @return
	 */
	public List<ConfigStrategy> queryByStrategyId(Integer strategyId) {
		ConfigStrategyExample configStrategyExample = new ConfigStrategyExample();
		configStrategyExample.createCriteria().andStrategyIdEqualTo(strategyId);
		return configStrategyMapper.selectByExample(configStrategyExample);
	}
	
	/**
	 * @param configStrategyQuery
	 * @return
	 */
	private ConfigStrategyGroupExample exampleProvider(ConfigStrategyQuery configStrategyQuery) {
		ConfigStrategyGroupExample configStrategyGroupExample = new ConfigStrategyGroupExample();
		configStrategyGroupExample.setOrderByClause("created_date desc");
		ConfigStrategyGroupExample.Criteria criteria = configStrategyGroupExample.createCriteria();
		if (null != configStrategyQuery.getStrategyId()) {
			criteria.andIdEqualTo(configStrategyQuery.getStrategyId());
		}
		if (!StringUtils.isEmpty(configStrategyQuery.getStrategyName())) {
			criteria.andStrategyNameLike("%" + configStrategyQuery.getStrategyName() + "%");
		}
		if (!StringUtils.isEmpty(configStrategyQuery.getStartDate())) {
			try {
				criteria.andCreatedDateGreaterThanOrEqualTo(DateUtils.parseDate(configStrategyQuery.getStartDate() + " 00:00:00", new String[]{"yyyy-MM-dd HH:mm:ss"}));
			} catch (ParseException ignored) {
			}
		}
		if (!StringUtils.isEmpty(configStrategyQuery.getEndDate())) {
			try {
				criteria.andCreatedDateLessThanOrEqualTo(DateUtils.parseDate(configStrategyQuery.getEndDate() + " 23:59:59", new String[]{"yyyy-MM-dd HH:mm:ss"}));
			} catch (ParseException ignored) {
			}
		}
		if (!StringUtils.isEmpty(configStrategyQuery.getOrganizationId())) {
			criteria.andOrganizationIdEqualTo(configStrategyQuery.getOrganizationId());
		}
		criteria.andStatusNotEqualTo(1);//过滤删除的
		if (!"sa".equals(RequestHeaderUtil.getRole())) {
			criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
		}
		return configStrategyGroupExample;
	}
}
