package com.neusoft.smart.pos.gateway.task;

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.dto.member.request.MemberTransactionRequest;
import com.neusoft.smart.pos.dto.member.response.DetailedMemberResponse;
import com.neusoft.smart.pos.dto.trade.request.PostRecordOrderRequest;
import com.neusoft.smart.pos.dto.trade.request.PostTradeRecordOrderRequest;
import com.neusoft.smart.pos.dto.trade.response.PostTradeRecordOrderResponse;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.framework.task.RedisTask;
import com.neusoft.smart.pos.framework.utils.IdGenerateUtils;
import com.neusoft.smart.pos.framework.utils.MapBeanUtils;
import com.neusoft.smart.pos.framework.utils.SignUtils;
import com.neusoft.smart.pos.framework.utils.SpringUtils;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteMemberService;
import com.neusoft.smart.pos.gateway.service.feignService.RemoteOrderService;
import com.neusoft.smart.pos.utils.PosMemberRedisUtils;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.util.StringUtils;

import java.util.Date;

@Slf4j
public class RecordOrderTask extends RedisTask {
	
	@Setter
	private PostRecordOrderRequest recordOrderRequest;
	
	private RemoteMemberService remoteMemberService = SpringUtils.getBean(RemoteMemberService.class);
	
	private RemoteOrderService remoteOrderService = SpringUtils.getBean(RemoteOrderService.class);
	
	@Override
	public void executeTask() {
		log.info("订单补录任务开始执行");
		if (null != recordOrderRequest.getTradeRecordOrderList()) {
			recordOrderRequest.getTradeRecordOrderList().forEach(tradeRecordOrder -> {
				//0.卡内号与会员账号转换
				if(StringUtils.isEmpty(tradeRecordOrder.getMemberCardNo()) && !StringUtils.isEmpty(tradeRecordOrder.getHardwareCardNo())){
					DetailedMemberResponse detailedMemberData = PosMemberRedisUtils.getMemberByCardHidCode(tradeRecordOrder.getHardwareCardNo());
					if(detailedMemberData == null) {
						detailedMemberData = remoteMemberService.getDetailByHidCode(tradeRecordOrder.getHardwareCardNo()).getResult();
					}
					if(detailedMemberData != null) {
						//注意：这里的使用查询的card_num对应唯一卡号而非MemberCode，防止MemberCode与MemberCardNo不同
						tradeRecordOrder.setMemberCardNo(detailedMemberData. getCardNum());
					}
				}
				//1.补录会员卡相关信息(需将获得积分进行设置)
				if (!org.springframework.util.StringUtils.isEmpty(tradeRecordOrder.getMemberCardNo())) {
					if(SignUtils.isSignatureValidSHA256(tradeRecordOrder)) {
						DetailedMemberResponse memberResponse = trade(tradeRecordOrder).getResult();
						tradeRecordOrder.setScore(memberResponse.getPoint());
					}
				}
				//2.补录订单信息
				recordOrderHandle(tradeRecordOrder);
			});
		}
		log.info("订单补录任务执行完成");
	}
	
	/**
	 * 订单补录方法
	 */
	private ResponseData<PostTradeRecordOrderResponse> recordOrderHandle(PostTradeRecordOrderRequest tradeRecordOrderRequest) {
		val recordOrderResponseData = remoteOrderService.recordOrder(tradeRecordOrderRequest);
		return recordOrderResponseData;
	}
	
	/**
	 * 会员卡扣款
	 *
	 * @param orderRequest
	 * @return
	 */
	private ResponseData<DetailedMemberResponse> trade(PostTradeRecordOrderRequest orderRequest) {
		MemberTransactionRequest memberTransactionRequest = new MemberTransactionRequest();
		memberTransactionRequest.setCardNum(orderRequest.getMemberCardNo());
		memberTransactionRequest.setTransactionOrderNo(orderRequest.getOrderNumber());
		memberTransactionRequest.setTransactionAmount(orderRequest.getRealPayAmount());
		memberTransactionRequest.setTransactionMode(Integer.parseInt(DictionaryConstant.PAY_TYPE_FKSY.getCode()));
		memberTransactionRequest.setTransactionTime(new Date(orderRequest.getCreatedAt()));
		memberTransactionRequest.setCanBeNegative(true);
		return remoteMemberService.trade(memberTransactionRequest);
	}
	
}
