package com.goafanti.news.service;

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 org.springframework.util.IdGenerator;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import com.goafanti.common.dao.PolicyMapper;
import com.goafanti.common.dao.PolicyPublishMapper;
import com.goafanti.common.model.Policy;
import com.goafanti.common.model.PolicyPublish;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.news.bo.PolicyResult;

@Service
public class PolicyService extends BaseMybatisDao<PolicyMapper> {
@Autowired
PolicyMapper policyMapper;
@Autowired
PolicyPublishMapper policyPublishMapper;

public Policy saveNewPolicy(Policy policy,List<String>webPages,List<String>appPages) {
policyMapper.insertSelective(policy);
PolicyPublish pp=null;
if(webPages!=null)
for(String s:webPages)
{
	pp=new PolicyPublish();
	pp.setId(UUID.randomUUID().toString());
	pp.setIfTop(0);
	pp.setPolicyId(policy.getId()+"");
	pp.setPublishClient(0);
	pp.setPublisher("管理员");
	pp.setPublishPage(s);
	pp.setPublishPlatform("");
	pp.setPublishTime(new Date());
	pp.setShowNumber(1);
	pp.setTopNumber(1);
	policyPublishMapper.insert(pp);
	}
if(appPages!=null)
	for(String s:appPages)
	{pp=new PolicyPublish();
		pp.setId(UUID.randomUUID().toString());
		pp.setIfTop(0);
		pp.setPolicyId(policy.getId()+"");
		pp.setPublishClient(1);
		pp.setPublisher("管理员");
		pp.setPublishPage(s);
		pp.setPublishPlatform("");
		pp.setPublishTime(new Date());
		pp.setShowNumber(1);
		pp.setTopNumber(1);
		policyPublishMapper.insert(pp);
		}
	return policy;
}

public int deletePolicyById(Long id) {
	if(id!=null) {
		policyPublishMapper.deleteByPolicyId(id);
		return policyMapper.deleteByPrimaryKey(id);
	}
	else return -1;
	
}

public PolicyResult getPolicyDetail(Long id) {
	PolicyResult policyResult=policyMapper.selectByPrimaryKey(id);
	List<String> publishPages=policyPublishMapper.getPagesByPolicyId(id); 
	if(policyResult!=null)
	policyResult.setPublishPages(publishPages);
	return policyResult;
}

public int updateSelectively(Policy policy, List<String> webPages, List<String> appPages) {
	// TODO Auto-generated method stub
	policyPublishMapper.deleteByPolicyId(policy.getId());
	PolicyPublish pp=null;
	if(webPages!=null)
	for(String s:webPages)
	{
		pp=new PolicyPublish();
		pp.setId(UUID.randomUUID().toString());
		pp.setIfTop(0);
		pp.setPolicyId(policy.getId()+"");
		pp.setPublishClient(0);
		pp.setPublisher("管理员");
		pp.setPublishPage(s);
		pp.setPublishPlatform("");
		pp.setPublishTime(new Date());
		pp.setShowNumber(1);
		pp.setTopNumber(1);
		policyPublishMapper.insert(pp);
		}
	if(appPages!=null)
		for(String s:appPages)
		{pp=new PolicyPublish();
			pp.setId(UUID.randomUUID().toString());
			pp.setIfTop(0);
			pp.setPolicyId(policy.getId()+"");
			pp.setPublishClient(1);
			pp.setPublisher("管理员");
			pp.setPublishPage(s);
			pp.setPublishPlatform("");
			pp.setPublishTime(new Date());
			pp.setShowNumber(1);
			pp.setTopNumber(1);
			policyPublishMapper.insert(pp);
			}
	
	return policyMapper.updateByPrimaryKeySelective(policy);
}

public int updateSelectivelyWithoutPages(Policy policy)
{
	if(policy==null || policy.getId() ==null )
return 	-1;
	else return policyMapper.updateByPrimaryKeySelective(policy);
}

@SuppressWarnings("unchecked")
public Pagination<PolicyResult>searchPolicy(String title,Integer auditStatus,String startReleaseDate,String endReleaseDate,String publishPage,Integer pageNo,Integer pageSize){
	Map<String, Object> params = new HashMap<>();
	if(title!=null)params.put("title", title);
	if(auditStatus!=null &&auditStatus>=0 && auditStatus<=4)params.put("auditStatus", auditStatus);
	if(startReleaseDate!=null) {
		params.put("startDate", startReleaseDate);  
	}
	if(endReleaseDate!=null)params.put("endDate", endReleaseDate); 
	if(publishPage!=null)params.put("publishPage", publishPage);
	return (Pagination<PolicyResult>) findPage("findPolicyByPageAndPublishPages","findPolicyCountByPageAndPublishPages",params, pageNo, pageSize); 
}
}
