package com.ym.mec.collectfee.service.impl;

import com.thoughtworks.xstream.XStream;
import com.ym.mec.collectfee.common.dao.BaseDAO;
import com.ym.mec.collectfee.common.service.impl.BaseServiceImpl;
import com.ym.mec.collectfee.dao.OrderDao;
import com.ym.mec.collectfee.entity.*;
import com.ym.mec.collectfee.service.CourseGroupInfoService;
import com.ym.mec.collectfee.service.OrderService;
import com.ym.mec.collectfee.service.SchoolService;
import com.ym.mec.collectfee.utils.HttpUtil;
import com.ym.mec.collectfee.utils.XStreamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.io.Writer;
import java.util.Base64;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl extends BaseServiceImpl<Integer, Order> implements OrderService {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private CourseGroupInfoService courseGroupInfoService;

	//公共密钥
	@Value("${common.properties.mec-publicKey}")
	private String publicKey;

	@Value("${common.properties.mec-url}")
	private String url;

	@Override
	public BaseDAO<Integer, Order> getDAO() {
		return orderDao;
	}

	@Override
	public List<Order> getOrderByUserId(Integer userId) {
		return orderDao.getOrderByUserId(userId);
	}

	@Override
	public String pushOrder(String batchNum){
		try {
			RequestParamBean requestParamBean = new RequestParamBean();
			List<Order> orderList = orderDao.getOrderByBatch(batchNum);
			if(orderList == null || orderList.size() < 1){
				return null;
			}
			XStream xs = new XStream();
			xs.autodetectAnnotations(true);
			Writer writer = new StringWriter();
			Orders orders = new Orders(orderList);
			xs.toXML(orders,writer);
			String body = writer.toString();
			body = body.substring(body.indexOf("<body>")+6,body.indexOf("</body>"));

			body = Base64.getEncoder().encodeToString(body.getBytes());
			requestParamBean.setBody(body);
			requestParamBean.setHead(XStreamUtil.getOrdersHead(body,121512,publicKey,null,null));

			xs.autodetectAnnotations(true);
			writer = new StringWriter();
			writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
			xs.toXML(requestParamBean,writer);
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


	@Override
	public int countOrder(String poName, String voicePort) {
		return orderDao.countOrder(poName,voicePort);
	}


	@Override
	public Order getOrderByOrderNo(String orderNo) {
		return orderDao.getOrderByOrderNo(orderNo);
	}

	@Override
	public Object getSchoolDetail(Integer schoolId,Integer clazzId) {
		try {
			RequestParamBean requestParamBean = new RequestParamBean();
			String body = "<classId>" + clazzId + "</classId><schoolId>" + schoolId + "</schoolId>";
			body = Base64.getEncoder().encodeToString(body.getBytes());
			requestParamBean.setBody(body);
			requestParamBean.setHead(XStreamUtil.getOrdersHead(body,124005,publicKey,null,null));
			XStream xs = new XStream();
			xs.autodetectAnnotations(true);
			Writer writer = new StringWriter();
			writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
			xs.toXML(requestParamBean,writer);
			String parseBody = parseBody(HttpUtil.postXmlData(writer.toString(), url));
			ResponseCourseEntity xmlToObject = XStreamUtil.xmlToObject("body", ResponseCourseEntity.class, parseBody);
			courseGroupInfoService.upSetCourseGroup(xmlToObject);
			return schoolService.upsetSchool(xmlToObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public String parseBody(String body){
		body = body.substring(body.indexOf("<body>")+6,body.indexOf("</body>"));
		return "<body><courses>" + new String(Base64.getDecoder().decode(body)) + "</courses></body>";
	}

	@Override
	public int getPayOrderNums() {
		return orderDao.getPayOrderNums();
	}

	@Override
	public List<Order> findPayingOrders(){
		return orderDao.findPayingOrders();
	}


}