package com.goafanti.admin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goafanti.admin.bo.AdminVideoBo;
import com.goafanti.admin.service.AdminVideoService;
import com.goafanti.common.dao.JtVideoMapper;
import com.goafanti.common.model.JtVideo;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;

@Service
public class AdminVideoServiceImpl extends BaseMybatisDao<JtVideoMapper> implements AdminVideoService {

	@Autowired
	private JtVideoMapper jtVideoMapper;
	
	@Override
	public Pagination<AdminVideoBo> getVideoList(JtVideo video, Integer pageNo,
			Integer pageSize, Integer ob) {
		Map<String,Object> params = new HashMap<String, Object>();
		if(null != video) params.put("v", video);
		params.put("ob", ob);//按创建时间排序
		@SuppressWarnings("unchecked")
		Pagination<AdminVideoBo> videos = (Pagination<AdminVideoBo>) findPage("getVideoListBySome","getVideoCountBySome",params,pageNo,pageSize);
		return videos;
	}

	@Override
	public AdminVideoBo getVideoById(JtVideo jtVideo) {
		return jtVideoMapper.selectById(jtVideo);
	}

	@Override
	public int insertVideo(JtVideo jtVideo) {
		//生成id
		jtVideo.setId(UUID.randomUUID().toString());
		//默认状态
		if(null == jtVideo.getStatus()) jtVideo.setStatus(0);
		//创建时间
		jtVideo.setCreateTime(new Date());
		//创建人
		jtVideo.setOwner(TokenManager.getUserId());
		return jtVideoMapper.insertSelective(jtVideo);
	}

	@Override
	public int updateVideo(JtVideo jtVideo) {
		//如果状态更改成已经发布需要修改发布时间
		if(jtVideo.getStatus() == 1){
			jtVideo.setReleaseTime(new Date());
		}
		return jtVideoMapper.updateByPrimaryKeySelective(jtVideo);
	}

	@Override
	public int updateByUrl(JtVideo jtVideo) {
		return jtVideoMapper.updateByUrl(jtVideo);
	}

	@Override
	public AdminVideoBo getIndexVideo(Integer position) {
		return jtVideoMapper.getIndexVideo(position);
	}

	@Override
	public int nameCheck(String name) {
		return jtVideoMapper.nameCheck(name);
	}

	@Override
	public List<JtVideo> getAllVideoExcludeId(String id) {
		return jtVideoMapper.getAllVideoExcludeId(id);
	}

	

}
