package com.chinacreator.process.service;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.chinacreator.common.exception.BusinessException;
import com.chinacreator.common.util.MD5;
import com.chinacreator.common.util.URLUtil;
import com.chinacreator.process.bean.ActivityConfigBean;
import com.chinacreator.process.bean.OrderBean;
import com.chinacreator.process.dao.ActiveHandleDao;
import com.chinacreator.process.dao.ActivityConfigDao;
import com.chinacreator.process.util.DesUtil;
import com.chinacreator.process.util.JsonUtil;

@Component
public class ActivityHandleService {

	private static Logger log = Logger.getLogger(ActivityHandleService.class);

	@Autowired
	private ActiveHandleDao activeHandleDao;

	@Autowired
	private ActivityConfigDao activityConfigDao;

	private static String url = "http://10.199.99.144:8090/video-activity/joinActivity";

	public void handle(OrderBean orderBean) {
		log.info("活动" + JsonUtil.objectToJson(orderBean));
		try {
			if (orderBean.getOrderType().equals("0")) {
				order(orderBean);
				givevip(orderBean);
			} else {
				cancel(orderBean);
			}
		} catch (Exception e) {
			log.error(orderBean.getUserid(), e);
		}
	}

	public void order(OrderBean orderBean) throws Exception {
		OrderBean oldOrderBean = activeHandleDao.existUser(orderBean.getUserid(), orderBean.getCpid(),
				orderBean.getSpid(), String.valueOf(orderBean.getActiveType()));
		ActivityConfigBean activityConfigBean = activityConfigDao.findByCallerheadtype(orderBean.getCpid(),
				orderBean.getSpid(), orderBean.getProvince(), String.valueOf(orderBean.getActiveType()));
		System.out.println(JsonUtil.objectToJson(activityConfigBean));
		if (oldOrderBean == null) {
			if (activityConfigBean.getVip() == 0) {
				orderBean.setVipstatus("2");
			} else {
				orderBean.setVipstatus("5");
			}
			if (activityConfigBean.getFlow() == 0) {
				orderBean.setFlowstatus("1");
			} else {
				orderBean.setFlowstatus("8");
			}
			activeHandleDao.insertBeans(orderBean);
		} else {
			if (activityConfigBean.getCanrepeat() == 1) {
				throw new BusinessException("10001", "该活动不能重复参加");
			}
		}
	}

	public void cancel(OrderBean orderBean) throws Exception {
		OrderBean oldOrderBean = activeHandleDao.existUser(orderBean.getUserid(), orderBean.getCpid(),
				orderBean.getSpid(), String.valueOf(orderBean.getActiveType()));
		if (oldOrderBean == null) {
			throw new BusinessException("10002", "该用户未参加活动");
		} else {
			if (oldOrderBean.getOrderType().equals("1")) {
				throw new BusinessException("10003", "用户已退订");
			} else {
				orderBean.setId(oldOrderBean.getId());
				System.out.println("====>"+JsonUtil.objectToJson(orderBean));
				activeHandleDao.cancelOrder(orderBean);
			}
		}
	}

	public void givevip(OrderBean orderBean) {
		try {
			if (orderBean.getCpid().equals("youtu") && orderBean.getSpid().equals("21156")) {
				String vipurl = url;
				String userid = orderBean.getUserid();
				String pwd = "ea044e50";
				userid = DesUtil.encode(userid, pwd);
				String timestamp = (System.currentTimeMillis() / 1000) + "";
				String activetype = "21";
				String usertype = "4";
				String userval = orderBean.getUserid();
				String vipname = "";
				vipname = URLEncoder.encode(vipname, "UTF-8");
				String signature = MD5.MD5Encode(userid + userval + pwd + timestamp);
				vipurl = vipurl + "?userid=" + URLEncoder.encode(userid, "utf-8") + "&signature=" + signature + "&timestamp="
						+ timestamp + "&activetype=" + activetype + "&usertype=" + usertype + "&userval=" + userval
						+ "&vipname=" + vipname;
				String result = URLUtil.get(vipurl);
				log.info("活动会员赠送结果" +orderBean.getUserid()+","+result);
				Map<?,?> map = JsonUtil.jsonToMap(result);
				if(((String)map.get("resultcode")).equals("0")){
					inserSmstMq(orderBean);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void inserSmstMq(OrderBean orderBean) {
//			try {
//				Map<String, String> map = new HashMap<String, String>();
//				map.put("userid", orderBean.getUserid());
//				map.put("cpid", "youtu");
//				map.put("spid", "21156");
//				map.put("result", "0");
//				map.put("channel", "");
//				map.put("style", "50");
//				map.put("times", "");
//				map.put("orderType", "");
//				map.put("type", "cssms");
//				URLUtil.post("http://10.199.99.144:8090/mq-service/recive.do", JsonUtil.objectToJson(map));
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
	}
}
