package com.goafanti.news.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.constant.PageConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.model.News;
import com.goafanti.common.model.Policy;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.core.mybatis.JDBCIdGenerator;
import com.goafanti.news.bo.InputNews;
import com.goafanti.news.bo.newsInterestBo;
import com.goafanti.news.enums.NewsFields;
import com.goafanti.news.service.PolicyService;


@RestController
@RequestMapping(value = "/api/admin/policy")
public class AdminPolicyApiController extends BaseApiController {
	
	@Autowired
	private JDBCIdGenerator	idGenerator;
	@Resource
	private PolicyService policyService;
	/*
	 * 新增
	 * 
	 * */
		@RequestMapping(value="/apply",method=RequestMethod.POST)
		public Result insertPolicy(@Valid Policy policy,String[]publishPages,BindingResult bindingResult) {
			Result result=new Result();
			if (bindingResult.hasErrors()) {
				result.getError().add(
						buildErrorByMsg(bindingResult.getFieldError()
								.getDefaultMessage(), NewsFields 
								.getFieldDesc(bindingResult.getFieldError()
										.getField())));
				return result;
			}
			disposePolicyParams(policy,result);
			if(result.getError().size()>0)return result;
			if(publishPages==null || publishPages.length<1) {
				result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","发布位置"));
				return result;
			}
			List<String>webPages=new ArrayList<>();
			List<String>appPages=new ArrayList<>();
			PageConstants.putPolicy(publishPages,webPages,appPages);
			if(webPages.size()<1 && appPages.size()<1) {
				result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","发布位置"));
				return result;
			}
			policy.setCreateTime(new Date());
			policy.setEditTime(new Date());
			policy.setId(idGenerator.generateId());
			policy.setType(0);
			if(policy.getAuditStatus()!=null &&policy.getAuditStatus() == 2) {
				policy.setReleaseDate(new Date());
			
			}
			result.setData(policyService.saveNewPolicy(policy, webPages, appPages));
			return result;
		}
		

		private void disposePolicyParams(Policy policy,Result result) {
			if(StringUtils.isBlank(policy.getTitle())) {
				result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","标题"));
				return ;
			}
			if(StringUtils.isBlank(policy.getContent()))
			{
				result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","内容"));
				return ;
			}
			if(StringUtils.isBlank(policy.getSource()))
			{
				result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","来源"));
				return ;
			}
			if(policy.getAuditStatus() == null )
			{
				result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","审核状态"));
				return ;
			}
		}
		/*
		 * 删除
		 * 
		 * */
		
		@RequestMapping(value="/delete",method=RequestMethod.POST)
		public Result deleteById(Long id) {
			Result result=new Result();
			if(id ==null) {
				result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","id不可为空,"));return result;
			}
			int res=policyService.deletePolicyById(id);
			result.setData(res);
			if(res==-1) {
				result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","id不可为空"));
			}
			return result;
		}
		
		/*
		 * 详情
		 * */
		@RequestMapping(value="/detail",method=RequestMethod.GET)
		public Result getDetailById(Long id) {
			Result result=new Result();
			if(id==null) {
				result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","id不可为空,"));return result;
			}
			result.setData(policyService.getPolicyDetail(id));
			return result;
		}
		
		/*
		 * 修改
		 * 
		 * */
			@RequestMapping(value="/update",method=RequestMethod.POST)
		public Result updateNews(@Valid Policy policy,BindingResult bindingResult,String[] publishPages) {
				Result result=new Result();
				if (bindingResult.hasErrors()) {
					result.getError().add(
							buildErrorByMsg(bindingResult.getFieldError()
									.getDefaultMessage(), NewsFields 
									.getFieldDesc(bindingResult.getFieldError()
											.getField())));
					return result;
				}
				List<String>webPages=new ArrayList<>();
				List<String>appPages=new ArrayList<>();
				PageConstants.putPolicy(publishPages, webPages, appPages);
					if(webPages.size()<1 && appPages.size()<1) {
						result.getError().add(buildError(ErrorConstants.PARAM_ERROR,"","发布位置"));
						return result;
					}
				disposePolicyParams(policy, result);
				if(result.getError().size()>0) {
					return result;
				}
				if(policy.getAuditStatus()!=null ) {
					policy.setReleaseDate(new Date());
					if(policy.getAuditStatus() ==2){
						policy.setReleaseDate(new Date());
						policyService.addJtCollectSearch(policy);
						}
					if(policy.getAuditStatus()==4) {
						policyService.updateJtCollectSearch(policy);
					}
					
				}
				policy.setEditTime(new Date());
		
				result.setData(policyService.updateSelectively( policy,webPages,appPages)); 
	
				return result;
			}
			
			
			/*
			 * 变更发布状态、发布时间
			 * */
				@RequestMapping(value="/updateStatus",method=RequestMethod.POST)
				public Result updateStatus(Long id,Integer auditStatus,Integer refresh) {
					Result result=new Result();
					if(id==null) {
						result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","未找到该政策,"));
						return result;
					}
					Policy policy=policyService.getPolicyDetail(id);
					if(policy==null) {
						result.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"","未找到该政策,"));
						return result;
					}
					if(refresh!=null &&refresh==1){
						if( auditStatus!=2 && policy.getAuditStatus()!=2) {
							result.getError().add(buildError("","","当前未发布"));
							return result;
						}
						policy.setReleaseDate(new Date());}
					if(auditStatus!=null) {
						policy.setAuditStatus(auditStatus); 
					if(auditStatus ==2){
						policy.setReleaseDate(new Date());
						policyService.updateJtCollectSearch(policy);
						policyService.addJtCollectSearch(policy);
						}
					if (auditStatus==4) {
						policyService.updateJtCollectSearch(policy);
					}
					}
					
					result.setData(policyService.updateSelectivelyWithoutPages(policy));  
					return result;
					
				}
				
				/*
				 * 政策搜索
				 * */
				@RequestMapping(value="/list",method=RequestMethod.GET)
				public Result searchPolicy(String title,Integer auditStatus,String startReleaseDate,String endReleaseDate,String publishPage,Integer pageNo,Integer pageSize) {
					Result result=new Result();
					if(pageNo==null || pageNo<1)pageNo=1;
					if(pageSize==null || pageSize<1)pageSize=10;
					result.setData(policyService.searchPolicy(title,auditStatus,startReleaseDate,endReleaseDate,publishPage,pageNo,pageSize,null));
					return result;
				}
				
				
		
		
}
