package com.weaver.seconddev.interfaces.oactrip;

import com.alibaba.fastjson.JSONObject;
import com.weaver.seconddev.interfaces.prop.ConfigProp;
import corp.openapicalls.contract.Authentification;
import corp.openapicalls.contract.setapproval.request.*;
import corp.openapicalls.contract.setapproval.response.SetApprovalResponse;
import corp.openapicalls.contract.ticket.TicketResponse;
import corp.openapicalls.service.setapproval.SetApprovalService;
import corp.openapicalls.service.ticket.CorpTicketService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.weaver.verupgrade.file.Prop;
import com.weaver.verupgrade.general.BaseBean;
import org.springframework.stereotype.Component;

@Component
public class OaCtrip extends BaseBean {

	private static final org.slf4j.Logger logger_359344e8 = LoggerFactory.getLogger(OaCtrip.class);

	private ArrayList<PassengerDetail> passengerdetails = new ArrayList<PassengerDetail>();

	private ArrayList<PassengerDetail> gwpassengerdetails = new ArrayList<PassengerDetail>();

	public void setPassengerDetails(ArrayList<PassengerDetail> details) {
		this.passengerdetails = details;
	}

	public void setGWPassengerDetails(ArrayList<PassengerDetail> gwdetails) {
		this.gwpassengerdetails = gwdetails;
	}
	@Autowired
	private ConfigProp configProp;
	public String[] call(String approvalnumber, String employeeid, String expiredtime, Date begindate, Date enddate, String lx) throws ParseException {
		String[] rtInfo = new String[3];
		// 出差类型0国内，1国外
		String cclx = lx;
		// Date beginDateTime = new Date(); //根据陈利要求开始日期调整为申请日期--20170323
		if (begindate != null) {
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(begindate);
			rightNow.add(Calendar.DAY_OF_YEAR, -3);
			begindate = rightNow.getTime();
		}
		if (enddate != null) {
			Calendar rightNow = Calendar.getInstance();
			rightNow.setTime(enddate);
			rightNow.add(Calendar.DAY_OF_YEAR, 3);
			enddate = rightNow.getTime();
		}
		// Date endDateTime = enddate;
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		// Calendar rightNow = Calendar.getInstance();
		// rightNow.setTime(endDateTime);
		// rightNow.add(Calendar.DAY_OF_YEAR,7);
		// Date dt1=rightNow.getTime();
		String reStr = df.format(enddate);
		rtInfo[0] = "E";
		// 接口验证
		// 测试环境测试完成后注释账号密码
		// TicketResponse ticketResponse=CorpTicketService.getOrderAuditTicket("","", "1.0");

//		String appKey = I18nContextUtilcus.getBean(Environment.class).getProperty("htctripmt.appkey");
//		String appsecurity = I18nContextUtilcus.getBean(Environment.class).getProperty("htctripmt.appsecurity");

		String appKey = configProp.getWeaver_secondev_htctripmt_appkey();
		String appsecurity = configProp.getWeaver_secondev_htctripmt_appsecurity();

		TicketResponse ticketResponse = CorpTicketService.getOrderAuditTicket(appKey, appsecurity, "1.0");
		rtInfo[1] = ticketResponse.getStatus().getMessage();
		rtInfo[2] = ticketResponse.getStatus().getErrorCode() + "";
		if (ticketResponse != null && ticketResponse.getStatus() != null && ticketResponse.getStatus().getSuccess()) {
			SetApprovalService setapprovalService = new SetApprovalService();
			SetApprovalServiceRequest setApprovalServiceRequest = new SetApprovalServiceRequest();
			SetApprovalRequest setApprovalRequest = new SetApprovalRequest();
			Authentification authInfo = new Authentification(appKey, ticketResponse.getTicket());
			// 接入账号及KEY
			setApprovalRequest.setAuth(authInfo);
			// 审批单号requestid
			setApprovalRequest.setApprovalNumber(approvalnumber);
			// 0为无效1为有效默认为1
			setApprovalRequest.setStatus(1);
			// setApprovalRequest.setCtripCardNO("2113315393");
			setApprovalRequest.setEmployeeID(employeeid);
			setApprovalRequest.setExpiredTime(reStr);
			// 机票信息
			ArrayList<FlightEndorsementDetail> flightEndorsementDetails = new ArrayList<FlightEndorsementDetail>();
			FlightEndorsementDetail flightEndorsementDetail = new FlightEndorsementDetail();
			// flightEndorsementDetail.setAirline("MU");
			flightEndorsementDetail.setCurrency(CurrencyType.RMB);
			flightEndorsementDetail.setFlightWay(FlightWayType.RoundTrip);
			flightEndorsementDetail.setDepartBeginDate(begindate);
			flightEndorsementDetail.setDepartEndDate(enddate);
			flightEndorsementDetail.setReturnBeginDate(begindate);
			flightEndorsementDetail.setReturnEndDate(enddate);
			flightEndorsementDetail.setPassengerList(passengerdetails);
			// 国内
			flightEndorsementDetail.setProductType(ProductType.forValue(1));
			flightEndorsementDetail.setSkipFields(0);
			flightEndorsementDetails.add(flightEndorsementDetail);
			// 酒店信息
			ArrayList<HotelEndorsementDetail> hotelEndorsementDetails = new ArrayList<HotelEndorsementDetail>();
			HotelEndorsementDetail hotelEndorsementDetail = new HotelEndorsementDetail();
			hotelEndorsementDetail.setProductType(HotelProductType.forValue(3));
			hotelEndorsementDetail.setCheckInBeginDate(begindate);
			hotelEndorsementDetail.setCheckInEndDate(enddate);
			hotelEndorsementDetail.setPassengerList(passengerdetails);
			hotelEndorsementDetail.setSkipFields(0);
			hotelEndorsementDetail.setCheckOutBeginDate(begindate);
			hotelEndorsementDetail.setCheckOutEndDate(enddate);
			hotelEndorsementDetails.add(hotelEndorsementDetail);
			if (cclx.equals("1")) {
				FlightEndorsementDetail flightEndorsementDetail2 = new FlightEndorsementDetail();
				// flightEndorsementDetail.setAirline("MU");
				flightEndorsementDetail2.setCurrency(CurrencyType.RMB);
				flightEndorsementDetail2.setFlightWay(FlightWayType.RoundTrip);
				flightEndorsementDetail2.setDepartBeginDate(begindate);
				flightEndorsementDetail2.setDepartEndDate(enddate);
				flightEndorsementDetail2.setReturnBeginDate(begindate);
				flightEndorsementDetail2.setReturnEndDate(enddate);
				flightEndorsementDetail2.setPassengerList(gwpassengerdetails);
				// 国际
				flightEndorsementDetail2.setProductType(ProductType.forValue(2));
				flightEndorsementDetail2.setSkipFields(0);
				flightEndorsementDetails.add(flightEndorsementDetail2);
				HotelEndorsementDetail hotelEndorsementDetail2 = new HotelEndorsementDetail();
				hotelEndorsementDetail2.setProductType(HotelProductType.forValue(4));
				hotelEndorsementDetail2.setCheckInBeginDate(begindate);
				hotelEndorsementDetail2.setCheckInEndDate(enddate);
				hotelEndorsementDetail2.setPassengerList(gwpassengerdetails);
				hotelEndorsementDetail2.setSkipFields(0);
				hotelEndorsementDetail2.setCheckOutBeginDate(begindate);
				hotelEndorsementDetail2.setCheckOutEndDate(enddate);
				hotelEndorsementDetails.add(hotelEndorsementDetail2);
			}
			setApprovalRequest.setFlightEndorsementDetails(flightEndorsementDetails);
			setApprovalRequest.setHotelEndorsementDetails(hotelEndorsementDetails);
			// 火车票信息
			ArrayList<TrainEndorsementDetail> trainEndorsementDetails = new ArrayList<TrainEndorsementDetail>();
			TrainEndorsementDetail trainEndorsementDetail = new TrainEndorsementDetail();
			trainEndorsementDetail.setProductType(TrainProductType.forValue(5));
			trainEndorsementDetail.setTripType(TripTypeEnum.forValue(1));
			trainEndorsementDetail.setDepartBeginDate(begindate);
			trainEndorsementDetail.setDepartEndDate(enddate);
			trainEndorsementDetail.setReturnBeginDate(begindate);
			trainEndorsementDetail.setReturnEndDate(enddate);
			trainEndorsementDetail.setPassengerList(passengerdetails);
			trainEndorsementDetail.setSkipFields(0);
			trainEndorsementDetails.add(trainEndorsementDetail);
			setApprovalRequest.setTrainEndorsementDetails(trainEndorsementDetails);
			setApprovalServiceRequest.setRequest(setApprovalRequest);
			SetApprovalResponse setApprovalResponse = setapprovalService.SetApproval(setApprovalServiceRequest);
			logger_359344e8.error("携程接口报文----" + JSONObject.toJSONString(setApprovalServiceRequest.getRequest()));
			if (setApprovalResponse != null && setApprovalResponse.getStatus() != null) {
				if (setApprovalResponse.getStatus().getSuccess()) {
					rtInfo[0] = "S";
					rtInfo[1] = setApprovalResponse.getStatus().getMessage();
					rtInfo[2] = setApprovalResponse.getStatus().getErrorCode() + "";
				} else {
					rtInfo[0] = "E";
					rtInfo[1] = setApprovalResponse.getStatus().getMessage();
					rtInfo[2] = setApprovalResponse.getStatus().getErrorCode() + "";
				}
			} else {
				rtInfo[0] = "E";
			}
		}
		return rtInfo;
	}
}
