package com.goafanti.user.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goafanti.common.bo.PublishPageBo;
import com.goafanti.common.dao.ExpertPublishMapper;
import com.goafanti.common.model.ExpertPublish;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.user.bo.ExpertPublishBo;
import com.goafanti.user.service.ExpertPublishPageService;
import com.goafanti.user.service.ExpertPublishService;


@Service
public class ExpertPublishServiceImpl extends BaseMybatisDao<ExpertPublishMapper> implements ExpertPublishService {

	@Autowired
	private ExpertPublishMapper	expertPublishMapper;

	private static final Logger				logger	= LoggerFactory.getLogger(ExpertPublishServiceImpl.class);

	@Override
	public int insertExpertPublish(ExpertPublish e) {
		String [] aid=e.getExpertId().split(",");
		for (String s : aid) {
			e.setExpertId(s);
			if (expertPublishMapper.checkExisting(e)>0) {
				continue;
			}
			e.setId(UUID.randomUUID().toString());
			if (null!=TokenManager.getAdminId()) {
				e.setPublisher(TokenManager.getAdminId());
			}
			if (null==e.getShowNumber()) {
				e.setShowNumber(9999999);
			}
			if (null==e.getTopNumber()) {
				e.setTopNumber(9999999);
			}
			e.setPublishTime(new Date());
			expertPublishMapper.insertSelective(e);
		}
		return 1;
	}

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

	@Override
	public int updatePublish(ExpertPublish e) {
		ExpertPublish e1=expertPublishMapper.selectByPrimaryKey(e.getId());
		if (e1.getIfTop()!=e.getIfTop()&&e.getIfTop()==1) {
			e.setTopNumber(9999999);
		}
		return expertPublishMapper.updateByPrimaryKeySelective(e);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<ExpertPublishBo> listPublish(String name, String publishPlatform, Integer publishClient,
			String publishPage, Integer ifTop, Integer pageNo, Integer pageSize) {
		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);
		}																					
		Pagination<ExpertPublishBo> p=(Pagination<ExpertPublishBo>) findPage("findExpertPublishByPage", "findExpertPublishCount",
				params,pageNo, pageSize);
		List<ExpertPublishBo> ep=(List<ExpertPublishBo>) p.getList();
		List <PublishPageBo> pp=ExpertPublishPageService.getBranchInformation();
		for (ExpertPublishBo a : ep) {
			for (PublishPageBo b : pp) {
				if (a.getPublishPage().equals(b.getId())) {
					a.setPublishPageName(b.getName());
				}
				
			}
		}
		return p;
	}
	

}
