package com.goafanti.demand.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.DemandPublishMapper;
import com.goafanti.common.model.DemandPublish;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.demand.bo.DemandPublishBo;
import com.goafanti.demand.service.DemandPublishPageService;
import com.goafanti.demand.service.DemandPublishService;


@Service
public class DemandPublishServiceImpl extends BaseMybatisDao<DemandPublishMapper> implements DemandPublishService {

	@Autowired
	private DemandPublishMapper	demandPublishMapper;

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

	@Override
	public int insertDemandPublish(DemandPublish d) {
		String [] did=d.getDemandId().split(",");
		for (String s : did) {
			d.setDemandId(s);
			if (demandPublishMapper.checkExisting(d)>0) {
				continue;
			}
			d.setId(UUID.randomUUID().toString());
			if (null==d.getShowNumber()) {
				d.setShowNumber(9999999);
			}
			if (null==d.getTopNumber()) {
				d.setTopNumber(9999999);
			}
			d.setPublishTime(new Date());
			demandPublishMapper.insertSelective(d);
		}
		return 1;
	}

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

	@Override
	public int updatePublish(DemandPublish d) {
		DemandPublish d1=demandPublishMapper.selectByPrimaryKey(d.getId());
		if (d1.getIfTop()!=d.getIfTop()&&d.getIfTop()==1) {
			d.setTopNumber(9999999);
		}
		return demandPublishMapper.updateByPrimaryKeySelective(d);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination<DemandPublishBo> listPublish(String name, String publishPlatform, Integer publishClient,
			String publishPage, Integer ifTop, Integer pageNo, Integer pageSize,String employerName) {
		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!=employerName) {
			params.put("employerName", employerName);
		}								
		Pagination<DemandPublishBo> p=(Pagination<DemandPublishBo>) findPage("findDemandPublishByPage", "findDemandPublishCount",
				params,pageNo, pageSize);
		List<DemandPublishBo> dp=(List<DemandPublishBo>) p.getList();
		List <PublishPageBo> pp=DemandPublishPageService.getBranchInformation();
		for (DemandPublishBo d : dp) {
			for (PublishPageBo b : pp) {
				if (d.getPublishPage().equals(b.getId())) {
					d.setPublishPageName(b.getName());
				}
				
			}
		}
		return p;
	}
	

}
