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

import com.neusoft.smart.pos.advertisement.persistent.domain.MediaStrategy;
import com.neusoft.smart.pos.advertisement.persistent.domain.MediaStrategyExample;
import com.neusoft.smart.pos.advertisement.persistent.domain.MediaStrategyGroup;
import com.neusoft.smart.pos.advertisement.persistent.domain.MediaStrategyGroupExample;
import com.neusoft.smart.pos.advertisement.persistent.ext.domain.MediaResourceStrategyQuery;
import com.neusoft.smart.pos.advertisement.persistent.mapper.MediaStrategyGroupMapper;
import com.neusoft.smart.pos.advertisement.persistent.mapper.MediaStrategyMapper;
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 MediaResourceStrategyRepository {
	
	@Autowired
	private MediaStrategyGroupMapper mediaStrategyGroupMapper;
	
	@Autowired
	private MediaStrategyMapper mediaStrategyMapper;
	
	/**
	 * @param name
	 * @return
	 */
	public Integer countByName(String name) {
		MediaStrategyGroupExample mediaResourceExample = new MediaStrategyGroupExample();
		mediaResourceExample.createCriteria().andStrategyNameEqualTo(name).andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId()).andStatusEqualTo(0);
		return mediaStrategyGroupMapper.countByExample(mediaResourceExample);
	}
	
	/**
	 * 创建策略
	 *
	 * @param mediaStrategyGroup
	 * @return
	 */
	public MediaStrategyGroup createGroup(MediaStrategyGroup mediaStrategyGroup) {
		mediaStrategyGroupMapper.insertSelective(mediaStrategyGroup);
		return mediaStrategyGroup;
	}
	
	/**
	 * 创建具体机具和广告资源的关联
	 *
	 * @param mediaStrategy
	 * @return
	 */
	public MediaStrategy create(MediaStrategy mediaStrategy) {
		mediaStrategyMapper.insertSelective(mediaStrategy);
		return mediaStrategy;
	}
	
	public void batchCreate(List<MediaStrategy> mediaStrategyList) {
		mediaStrategyMapper.batchInsert(mediaStrategyList);
	}
	
	/**
	 * @param mediaResourceStrategyQuery
	 * @return
	 */
	public List<MediaStrategyGroup> getGroupList(MediaResourceStrategyQuery mediaResourceStrategyQuery) {
		MediaStrategyGroupExample mediaStrategyGroupExample = this.exampleProvider(mediaResourceStrategyQuery);
		if (mediaResourceStrategyQuery.getIsPage()) {
			mediaStrategyGroupExample.setLimit(mediaResourceStrategyQuery.getLimit());
			mediaStrategyGroupExample.setOffset(mediaResourceStrategyQuery.getOffset());
		}
		return mediaStrategyGroupMapper.selectByExample(mediaStrategyGroupExample);
	}
	
	/**
	 * @param mediaResourceStrategyQuery
	 * @return
	 */
	public Integer getGroupCount(MediaResourceStrategyQuery mediaResourceStrategyQuery) {
		MediaStrategyGroupExample mediaStrategyGroupExample = this.exampleProvider(mediaResourceStrategyQuery);
		return mediaStrategyGroupMapper.countByExample(mediaStrategyGroupExample);
	}
	
	private MediaStrategyGroupExample exampleProvider(MediaResourceStrategyQuery mediaResourceStrategyQuery) {
		MediaStrategyGroupExample mediaStrategyGroupExample = new MediaStrategyGroupExample();
		mediaStrategyGroupExample.setOrderByClause("created_date desc");
		MediaStrategyGroupExample.Criteria criteria = mediaStrategyGroupExample.createCriteria();
		if (!StringUtils.isEmpty(mediaResourceStrategyQuery.getStrategyName())) {
			criteria.andStrategyNameLike("%" + mediaResourceStrategyQuery.getStrategyName() + "%");
		}
		if (null != mediaResourceStrategyQuery.getStatus()) {
			criteria.andStatusEqualTo(mediaResourceStrategyQuery.getStatus());
		}
		if (!StringUtils.isEmpty(mediaResourceStrategyQuery.getStartDate())) {
			try {
				criteria.andCreatedDateGreaterThanOrEqualTo(DateUtils.parseDate(mediaResourceStrategyQuery.getStartDate() + " 00:00:00", new String[]{"yyyy-MM-dd HH:mm:ss"}));
			} catch (ParseException ignored) {
			}
		}
		if (!StringUtils.isEmpty(mediaResourceStrategyQuery.getEndDate())) {
			try {
				criteria.andCreatedDateLessThanOrEqualTo(DateUtils.parseDate(mediaResourceStrategyQuery.getEndDate() + " 23:59:59", new String[]{"yyyy-MM-dd HH:mm:ss"}));
			} catch (ParseException ignored) {
			}
		}
		if (null != mediaResourceStrategyQuery.getStoreId()) {
			criteria.andStoreIdEqualTo(mediaResourceStrategyQuery.getStoreId());
		}
		criteria.andStatusNotEqualTo(1);//过滤删除的
		switch (RequestHeaderUtil.getRole()) {
			case "admin":
				criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
				break;
			case "user":
				criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
				break;
			default:
			
		}
		return mediaStrategyGroupExample;
	}
	
	/**
	 * @param groupId
	 * @return
	 */
	public List<MediaStrategy> getList(Integer groupId) {
		MediaStrategyExample mediaStrategyExample = new MediaStrategyExample();
		mediaStrategyExample.createCriteria().andStrategyIdEqualTo(groupId);
		return mediaStrategyMapper.selectByExample(mediaStrategyExample);
	}
	
	/**
	 * @param resourceId
	 * @return
	 */
	public Integer queryStrategyCount(Integer resourceId) {
		MediaStrategyExample example = new MediaStrategyExample();
		example.createCriteria().andMediaResourceIdEqualTo(resourceId).andStatusEqualTo(0);
		return mediaStrategyMapper.countByExample(example);
	}
	
	/**
	 * @param mediaStrategyGroup
	 */
	public void updateGroup(MediaStrategyGroup mediaStrategyGroup) {
		mediaStrategyGroupMapper.updateByPrimaryKeySelective(mediaStrategyGroup);
	}
	
	/**
	 * @param strategyId
	 * @param mediaStrategy
	 */
	public void updateByStrategyId(Integer strategyId, MediaStrategy mediaStrategy) {
		MediaStrategyExample example = new MediaStrategyExample();
		example.createCriteria().andStrategyIdEqualTo(strategyId);
		mediaStrategyMapper.updateByExampleSelective(mediaStrategy, example);
	}
}
