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

import com.neusoft.smart.pos.advertisement.persistent.domain.MediaResource;
import com.neusoft.smart.pos.advertisement.persistent.domain.MediaResourceExample;
import com.neusoft.smart.pos.advertisement.persistent.ext.domain.MediaResourceQuery;
import com.neusoft.smart.pos.advertisement.persistent.mapper.MediaResourceMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class MediaResourceRepository {
	
	@Autowired
	private MediaResourceMapper mediaResourceMapper;
	
	/**
	 * 通过名称查询个数
	 *
	 * @param name
	 * @return
	 */
	public Integer countByName(String name) {
		MediaResourceExample mediaResourceExample = new MediaResourceExample();
		mediaResourceExample.createCriteria().andMediaNameEqualTo(name).andStatusEqualTo(0);
		return mediaResourceMapper.countByExample(mediaResourceExample);
	}
	
	/**
	 * @param mediaResource
	 * @return
	 */
	public MediaResource create(MediaResource mediaResource) {
		mediaResourceMapper.insertSelective(mediaResource);
		return mediaResource;
	}
	
	/**
	 * @param mediaResourceQuery
	 * @return
	 */
	public List<MediaResource> getList(MediaResourceQuery mediaResourceQuery) {
		MediaResourceExample mediaResourceExample = this.exampleProvider(mediaResourceQuery);
		if (mediaResourceQuery.getIsPage()) {
			mediaResourceExample.setLimit(mediaResourceQuery.getLimit());
			mediaResourceExample.setOffset(mediaResourceQuery.getOffset());
		}
		return mediaResourceMapper.selectByExample(mediaResourceExample);
	}
	
	/**
	 * @param mediaResourceQuery
	 * @return
	 */
	public Integer getCount(MediaResourceQuery mediaResourceQuery) {
		MediaResourceExample mediaResourceExample = this.exampleProvider(mediaResourceQuery);
		return mediaResourceMapper.countByExample(mediaResourceExample);
	}
	
	/**
	 * @param mediaResourceQuery
	 * @return
	 */
	private MediaResourceExample exampleProvider(MediaResourceQuery mediaResourceQuery) {
		MediaResourceExample mediaResourceExample = new MediaResourceExample();
		mediaResourceExample.setOrderByClause("create_time desc");
		MediaResourceExample.Criteria criteria = mediaResourceExample.createCriteria();
		if (!StringUtils.isEmpty(mediaResourceQuery.getMediaName())) {
			criteria.andMediaNameLike("%" + mediaResourceQuery.getMediaName() + "%");
		}
		if (null != mediaResourceQuery.getMediaType()) {
			criteria.andMediaTypeIn(mediaResourceQuery.getMediaType());
		}
		if (!StringUtils.isEmpty(mediaResourceQuery.getFileType())) {
			criteria.andFileTypeEqualTo(mediaResourceQuery.getFileType());
		}
		if (null != mediaResourceQuery.getStatus()) {
			criteria.andStatusEqualTo(mediaResourceQuery.getStatus());
		}
		if (null != mediaResourceQuery.getIds() && !mediaResourceQuery.getIds().isEmpty()) {
			criteria.andIdIn(mediaResourceQuery.getIds());
		}
		if (null != mediaResourceQuery.getStoreId()) {
			criteria.andStoreIdEqualTo(mediaResourceQuery.getStoreId());
		}
		criteria.andStatusNotEqualTo(1);//过滤删除的
		switch (RequestHeaderUtil.getRole()) {
			case "admin":
				criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
				break;
			case "user":
				criteria.andOrganizationIdEqualTo(RequestHeaderUtil.getOrgId());
				break;
			
		}
		return mediaResourceExample;
	}
	
	public void updateStatus(MediaResource mediaResource) {
		mediaResourceMapper.updateByPrimaryKeySelective(mediaResource);
	}
}
