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.aspectj.weaver.ast.And;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.goafanti.app.bo.AppDiscoveryBo;
import com.goafanti.common.dao.JtCollectSearchMapper;
import com.goafanti.common.dao.PolicyMapper;
import com.goafanti.common.dao.PolicyPublishMapper;
import com.goafanti.common.enums.collectType;
import com.goafanti.common.model.JtCollectSearch;
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;
@Autowired
JtCollectSearchMapper	jtCollectSearchMapper;
@Value(value="${collect_flag}")
private Boolean collectFlag ;

public Policy saveNewPolicy(Policy policy,List<String>webPages,List<String>appPages) {
	if (null!=policy&&policy.getAuditStatus()!=null&&policy.getAuditStatus()==2) {
		addJtCollectSearch(policy);
	}
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) {
		if(collectFlag)jtCollectSearchMapper.deleteByrowId(String.valueOf(id));
		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,Integer provinceId){
	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);
	if(provinceId!=null)params.put("provinceId", provinceId);
	return (Pagination<PolicyResult>) findPage("findPolicyByPageAndPublishPages","findPolicyCountByPageAndPublishPages",params, pageNo, pageSize); 
}

@SuppressWarnings("unchecked")
public Pagination<AppDiscoveryBo> appListPolicy(Integer pageNo, Integer pageSize) {
	Map<String, Object> params = new HashMap<>();
	return (Pagination<AppDiscoveryBo>) findPage("findPolicyAppList","findPolicyAppCount",params, pageNo, pageSize);
}

public PolicyResult policyDetail(String id) {
	PolicyResult policyResult =policyMapper.selectPolicyDetail(id);
	return policyResult;
}

@SuppressWarnings("unchecked")
public Pagination<PolicyResult> searchPolicyAndNewsList(Policy policy, Integer pageNo, Integer pageSize){
	Map<String, Object> params = new HashMap<>();
	params.put("policy", policy);
	return (Pagination<PolicyResult>) findPage("searchPolicyAndNewsList","searchPolicyAndNewsCount",params, pageNo, pageSize); 
}
public void addJtCollectSearch(Policy policy) {
	if(collectFlag){
		JtCollectSearch r=new JtCollectSearch();
		r.setFromTable(collectType.POLICY.getDesc());
		r.setKeyword(policy.getKeyword());
		r.setTitle(policy.getTitle());
		r.setRowId(policy.getId().toString());
		String sum = policy.getSummary();
		if(null != sum && sum.length() > 40){
			sum = sum.substring(0,40) + "...";
		}
		r.setSummary(sum);
		jtCollectSearchMapper.insertSelective(r);
	}
	
}

public void updateJtCollectSearch(Policy policy) {
	if(collectFlag)jtCollectSearchMapper.deleteByrowId(policy.getId().toString());
	}
}
