package com.goafanti.achievement.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.achievement.bo.AchievementPublishBo;
import com.goafanti.achievement.service.AchievementPublishPageService;
import com.goafanti.achievement.service.AchievementPublishService;
import com.goafanti.common.bo.PublishPageBo;
import com.goafanti.common.dao.AchievementPublishMapper;
import com.goafanti.common.model.AchievementPublish;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;


@Service
public class AchievementPublishServiceImpl extends BaseMybatisDao<AchievementPublishMapper> implements AchievementPublishService {

	@Autowired
	private AchievementPublishMapper	achievementPublishMapper;


	@Override
	public int insertachievementPublish(AchievementPublish a) {
		String [] aid=a.getAchievementId().split(",");
		for (String s : aid) {
			a.setAchievementId(s);
			if (achievementPublishMapper.checkExisting(a)>0) {
				continue;
			}
			a.setId(UUID.randomUUID().toString());
			if (null!=TokenManager.getAdminId()) {
				a.setPublisher(TokenManager.getAdminId());
			}
			if (null==a.getShowNumber()) {
				a.setShowNumber(9999999);
			}
			if (null==a.getTopNumber()) {
				a.setTopNumber(9999999);
			}
			a.setPublishTime(new Date());
			achievementPublishMapper.insertSelective(a);
		}
		return 1;
	}

	@Override
	public int deletePublish(String id) {
		return achievementPublishMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updatePublish(AchievementPublish a) {
		AchievementPublish a1=achievementPublishMapper.selectByPrimaryKey(a.getId());
		if (a1.getIfTop()!=a.getIfTop()&&a.getIfTop()==1) {
			a.setTopNumber(9999999);
		}
		
		return achievementPublishMapper.updateByPrimaryKeySelective(a);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<AchievementPublishBo> listPublish(String name, String publishPlatform, Integer publishClient,
			String publishPage, Integer ifTop, Integer pageNo, Integer pageSize,String ownerName) {
		Map<String, Object> params=new HashMap<String, Object>();
		if (null!=name) {
			params.put("name", name);
		}
		if (null!=publishPlatform) {
			params.put("publishPlatform", publishPlatform);
		}
		if (null!=publishClient) {
			params.put("publishClient", publishClient);
		}
		if (null!=publishPage) {
			params.put("publishPage", publishPage);
		}
		if (null!=ifTop) {
			params.put("ifTop", ifTop);
		}
		if (null!=ownerName) {
			params.put("ownerName", ownerName);
		}
		
		Pagination<AchievementPublishBo> p=(Pagination<AchievementPublishBo>) findPage("findAchievementPublishByPage", "findAchievementPublishCount",
				params,pageNo, pageSize);
		List<AchievementPublishBo> ap=(List<AchievementPublishBo>) p.getList();
		List <PublishPageBo> pp=AchievementPublishPageService.getBranchInformation();
		for (AchievementPublishBo a : ap) {
			for (PublishPageBo b : pp) {
				if (a.getPublishPage().equals(b.getId())) {
					a.setPublishPageName(b.getName());
				}
			}
		}
		return p;
	}

	
	

}
