package com.goafanti.message.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.error.BusinessException;
import com.goafanti.common.model.MessageFromSystem;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.message.bo.MessageListBo;
import com.goafanti.message.enums.ConsumerType;
import com.goafanti.message.enums.RecommendType;
import com.goafanti.message.enums.SubjectType;
import com.goafanti.message.service.MessageService;

/**
 *  消息推送
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/api/admin/message")
public class AdminMessageController extends CertifyApiController{
	@Resource
	private MessageService messageService;
	
	/**
	 * 进入创建系统消息
	 */
	@RequestMapping(value = "/toCreateSystemMessage", method = RequestMethod.GET)
	public Result toCreateSystemMessage(){
		Result res = new Result();
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("createTime", DateUtils.formatDate(new Date(), AFTConstants.YYYYMMDDHHMMSS));
		res.setData(jsonObject);
		return res;
	}
	/**
	 * 
	 * @param companyIds 公司集
	 * @param userIds 用户集
	 * @param adminIds 运营人员
	 * @param societyTags 社会属性集
	 * @param provinceIds 省份集
	 * @param cityIds 城市集
	 * @param areas 区域集
	 * @param roles 角色集
	 * @param industryIds 行业
	 * @param subject  0-运营消息 , 1-客户消息 , 2-技淘推荐
	 * @param isDraft 是否草稿
	 * @throws ParseException 
	 */
	@RequestMapping(value = "/createSystemMessage", method = RequestMethod.POST)
	public Result createSystemMessage(String personalUserIds,String organizationUserIds, String adminIds,String companyIds,String societyTags,String provinceIds,String cityIds,String areaIds,
			String roles,String industryIds,String title,String body,Integer subject,Integer targetType,String createTime,Integer isDraft) throws ParseException{
		Result res = new Result();
		if(null == targetType){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定人员类型"));
			return res;
		}
		if(null == subject){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定消息类型"));
			return res;
		}
		if(StringUtils.isBlank(title) || StringUtils.isBlank(body)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息是的标题和内容不能为空"));
			return res;
		}
		if(null == isDraft){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须说明是否为草稿"));
			return res;
		}
		checkParam(personalUserIds,organizationUserIds,adminIds,companyIds,societyTags,provinceIds,cityIds,areaIds,roles,industryIds);
		Map<Integer,String> targetMap = new HashMap<Integer, String>();
		if(targetType == ConsumerType.PERSON_CUSTOMER_RETAIL.getTypeCode()) targetMap.put(targetType, personalUserIds);
		if(targetType == ConsumerType.ORGANIZATION_CUSTOMER_RETAIL.getTypeCode()) targetMap.put(targetType, organizationUserIds);
		if(targetType == ConsumerType.USER_RETAIL.getTypeCode()) targetMap.put(targetType, adminIds);
		if(targetType == ConsumerType.SOCIETY_TAG.getTypeCode()) targetMap.put(targetType, societyTags);
		if(targetType == ConsumerType.LOCATION.getTypeCode()){
			 targetMap.put(targetType, ConsumerType.LOCATION.getTypeName());
			 targetMap.put(ConsumerType.LOCATION_PROVINCE.getTypeCode(), provinceIds);
			 targetMap.put(ConsumerType.LOCATION_CITY.getTypeCode(), cityIds);
			 targetMap.put(ConsumerType.LOCATION_AREA.getTypeCode(), areaIds);
		}
		if(targetType == ConsumerType.COMPANY.getTypeCode()) targetMap.put(targetType, companyIds);
		if(targetType == ConsumerType.ROLE.getTypeCode()) targetMap.put(targetType, roles);
		if(targetType == ConsumerType.INDUSTRY.getTypeCode()) targetMap.put(targetType, industryIds);
		if(StringUtils.isBlank(targetMap.get(targetType))){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须选定目标群体"));
			return res;
		}
		messageService.insertSystemMessage(targetMap,targetType, title, body, subject,
				DateUtils.parseDate(createTime,AFTConstants.YYYYMMDDHHMMSS),isDraft);
		return res;
	}
	
	/**
	 * 
	 * @param projectIds 项目
	 * @param personalAchievementIds 个人成果
	 * @param organizationAchievementIds 组织成果
	 * @param personalDemandIds 个人需求
	 * @param organizationDemandIds 组织需求
	 * @param userIds 专家
	 * @param newsIds 政策
	 * @param sourceType 资源类型
	 * @param userIds 用户类型
	 * @param societyTags 社会属性
	 * @param provinceIds 省
	 * @param cityIds 市
	 * @param areaIds 区
	 * @param targetType 目标类型
	 * @param isDraft 是否草稿
	 * @return
	 */
	@RequestMapping(value = "/createRecommendMessage", method = RequestMethod.POST)
	public Result createRecommendMessage(String projectIds,String personalAchievementIds,String organizationAchievementIds,
			String personalDemandIds,String organizationDemandIds, String expertIds, String newsIds,Integer sourceType,
			String personalUserIds,String organizationUserIds,String societyTags,String provinceIds,String cityIds,String areaIds,String industryIds,Integer targetType,Integer isDraft){
		Result res = new Result();
		if(null == sourceType){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定推荐类型"));
			return res;
		}
		if(null == targetType){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定目标群体"));
			return res;
		}
		if(null == isDraft){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须说明是否为草稿"));
			return res;
		}
		checkParam(projectIds,projectIds,personalAchievementIds,organizationAchievementIds,personalDemandIds,organizationDemandIds,
				expertIds,newsIds,personalUserIds,organizationUserIds,provinceIds,cityIds,areaIds,industryIds);
		Map<Integer,String> sourceMap = new HashMap<Integer, String>();
		if(sourceType == RecommendType.TECH_SERVICE.getTypeCode()) sourceMap.put(sourceType, projectIds);
		if(sourceType == RecommendType.PERSON_ACHIEVEMENT.getTypeCode()) sourceMap.put(sourceType, personalAchievementIds);
		if(sourceType == RecommendType.ORGANIZATION_ACHIEVEMENT.getTypeCode()) sourceMap.put(sourceType, organizationAchievementIds);
		if(sourceType == RecommendType.PERSONAL_DEMAND.getTypeCode()) sourceMap.put(sourceType, personalDemandIds);
		if(sourceType == RecommendType.ORGANIZATION_DEMAND.getTypeCode()) sourceMap.put(sourceType, organizationDemandIds);
		if(sourceType == RecommendType.EXPERT.getTypeCode()) sourceMap.put(sourceType, expertIds);
		if(sourceType == RecommendType.NEWS.getTypeCode()) sourceMap.put(sourceType, newsIds);
		if(StringUtils.isBlank(sourceMap.get(sourceType))){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须选定推荐内容"));
			return res;
		}
		Map<Integer,String> targetMap = new HashMap<Integer, String>();
		if(targetType == ConsumerType.PERSON_CUSTOMER_RETAIL.getTypeCode()) targetMap.put(targetType, personalUserIds);
		if(targetType == ConsumerType.ORGANIZATION_CUSTOMER_RETAIL.getTypeCode()) targetMap.put(targetType, organizationUserIds);
		if(targetType == ConsumerType.SOCIETY_TAG.getTypeCode()) targetMap.put(targetType, societyTags);
		if(targetType == ConsumerType.LOCATION.getTypeCode()){
			 targetMap.put(targetType, ConsumerType.LOCATION.getTypeName());
			 targetMap.put(ConsumerType.LOCATION_PROVINCE.getTypeCode(), provinceIds);
			 targetMap.put(ConsumerType.LOCATION_CITY.getTypeCode(), cityIds);
			 targetMap.put(ConsumerType.LOCATION_AREA.getTypeCode(), areaIds);
		}
		if(targetType == ConsumerType.INDUSTRY.getTypeCode()) targetMap.put(targetType, industryIds);
		if(StringUtils.isBlank(targetMap.get(targetType))){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定目标群体"));
			return res;
		}
		messageService.insertRecommendMessage(sourceMap,sourceType,targetMap,targetType,SubjectType.TUI_JIAN.getTypeCode(),isDraft);
		return res;
		
	}
	
	/**
	 * 查看消息列表
	 * @param isDraft 是否未草稿
	 * @param subject 消息类型
	 * @return
	 */
	@RequestMapping(value = "/listSystemMessage", method = RequestMethod.GET)
	public Result listSystemMessage(Integer isDraft,Integer subject,Integer page,Integer pageNo,Integer pageSize){
		Result res = new Result();
		if(null == isDraft){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息发送状态不能为空"));
			return res;
		}
		/*if(null == subject){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "消息类型不能为空"));
			return res;
		}*/
		res.setData(messageService.listSystemMessage(isDraft, subject,pageNo,pageSize));
		return res;
	}
	
	/**
	 * 更新消息
	 * @param messageId 消息ID
	 * @param title 消息标题
	 * @param body 消息内容
	 * @return
	 */
	@RequestMapping(value = "/updateSystemMessage", method = RequestMethod.POST)
	public Result updateSystemMessage(String messageId,String title,String body,Integer isDraft){
		Result res = new Result();
		if(StringUtils.isBlank(messageId)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号不能为空"));
			return res;
		}
		if(null == isDraft){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息发送状态不能为空"));
			return res;
		}
		if(StringUtils.isBlank(title) || StringUtils.isBlank(body)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息标题和消息内容不能为空"));
			return res;
		}
		messageService.updateSystemMessage(messageId, title, body, isDraft);
		return res;
	}
	
	/**
	 * 删除消息
	 * @param messageId 消息ID
	 * @return
	 */
	@RequestMapping(value = "/deleteSystemMessage", method = RequestMethod.GET)
	public Result deleteSystemMessage(String messageId){
		Result res = new Result();
		if(StringUtils.isBlank(messageId)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号为空"));
			return res;
		}
		messageService.deleteSystemMessage(messageId);
		return res;
	}
	
	/**
	 * 读取消息详情
	 * @param messageId 消息ID
	 * @return
	 */
	@RequestMapping(value = "/getSystemMessageDetail", method = RequestMethod.GET)
	public Result getSystemMessageDetail(String messageId){
		Result res = new Result();
		if(StringUtils.isBlank(messageId)){
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号为空"));
			return res;
		}
		// 读取消息详情
		MessageFromSystem mfs = messageService.selectSystemMessageDetail(messageId);
		MessageListBo bo = new MessageListBo();
		bo.setCreateTime(mfs.getCreateTime() == null ? ""
				: DateUtils.formatDate(mfs.getCreateTime(), AFTConstants.YYYYMMDDHHMMSS));
		bo.setResourceType(String.valueOf(mfs.getResourceType()));
		bo.setSubject(String.valueOf(mfs.getSubject()));
		bo.setMessageId(mfs.getId());
		bo.setTitle(mfs.getTitle());
		bo.setBody(mfs.getBody());
		bo.setResourceId(mfs.getResourceId());
		bo.setConsumerType(""+mfs.getConsumerType());
		res.setData(bo);
		return res;
	}
	
	/**
	 * 校验
	 * @param ids
	 */
	public void checkParam(String... ids){
		String regex = "^[0-9a-z-,]*$";
		for(String s:ids){
			if(StringUtils.isNotBlank(s) && !Pattern.matches(regex, s)){
				throw new BusinessException(buildError(ErrorConstants.PARAM_ERROR, "参数不合法", s));
			}
		}
	} 
}
