package com.neusoft.smart.pos.payment.repository;

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.payment.persistent.domain.PayOrder;
import com.neusoft.smart.pos.payment.persistent.domain.PayOrderExample;
import com.neusoft.smart.pos.payment.persistent.domain.PayRefundOrder;
import com.neusoft.smart.pos.payment.persistent.domain.PayRefundOrderExample;
import com.neusoft.smart.pos.payment.persistent.mapper.PayOrderMapper;
import com.neusoft.smart.pos.payment.persistent.mapper.PayRefundOrderMapper;
import lombok.val;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zcf
 * @date 2018-01-09 10：43
 * @description 支付管理使用Repository
 */
@Component
public class PayOrderRepository {

	private final static Logger logger = LoggerFactory.getLogger(PayOrderRepository.class);

	@Resource
	private PayOrderMapper payOrderMapper;


	@Resource
	private PayRefundOrderMapper payRefundOrderMapper;

	/**
	 * 支付平台订单保存方法
	 * @param payOrder
	 * @return
	 */
	public Integer createPayOrder(PayOrder payOrder){
//		payOrder.setOrderStatus(DictionaryConstant.PAYMENT_ORDER_STATUS_DZF.getCode());
		payOrder.setPayStatus(DictionaryConstant.PAY_STATUS_WFK.getCode());
		return payOrderMapper.insertSelective(payOrder);
	}

	/**
     * 支付平台订单更新方法
	 * @param mcode 商户号
	 * @param tradeId 交易ID
	 * @param mOrderNumber 业务系统订单号
	 * @param payOrder 更新订单对象
	 * @return
     */
	public Boolean updatePayOrder(String mcode,String tradeId,String mOrderNumber,PayOrder payOrder){
		payOrder.setUpdatedAt(new Date());
		val payOrderExample = new PayOrderExample();
		payOrderExample.createCriteria()
				.andMcodeEqualTo(mcode)
				.andTradeIdEqualTo(tradeId)
				.andMOrderNumberEqualTo(mOrderNumber);
		int count = payOrderMapper.updateByExampleSelective(payOrder,payOrderExample);
		return count > 0 ? true:false;
	}

	/**
	 * 支付平台订单支付状态更新方法
	 * @param mcode 商户号
	 * @param payType 支付方式
	 * @param mOrderNumber 业务系统订单号
	 * @param payOrder 更新订单对象
	 * @return
	 */
	public Boolean updateOrderPayStatus(PayOrder payOrder,String mcode,String mOrderNumber,String payType){
//		payOrder.setOrderStatus(DictionaryConstant.PAYMENT_ORDER_STATUS_YZF.getCode());
		payOrder.setPayStatus(DictionaryConstant.PAY_STATUS_YFK.getCode());
		payOrder.setUpdatedAt(new Date());
		val payOrderExample = new PayOrderExample();
		payOrderExample.createCriteria()
				.andMcodeEqualTo(mcode)
				.andMOrderNumberEqualTo(mOrderNumber)
				.andPayStatusEqualTo(DictionaryConstant.PAY_STATUS_WFK.getCode())
				.andPayTypeEqualTo(payType);
		int count = payOrderMapper.updateByExampleSelective(payOrder,payOrderExample);
		return count > 0 ? true:false;
	}

	/**
	 * 支付平台订单退款状态更新方法
	 * @param mcode 商户号
	 * @param payType 支付方式
	 * @param mOrderNumber 业务系统订单号
	 * @param payOrder 更新订单对象
	 * @return
	 */
	public Boolean updateOrderRefundStatus(PayOrder payOrder,String mcode,String mOrderNumber,String payType){
//		payOrder.setOrderStatus(DictionaryConstant.PAYMENT_ORDER_STATUS_YTK.getCode());
		payOrder.setUpdatedAt(new Date());
		val payOrderExample = new PayOrderExample();
		payOrderExample.createCriteria()
				.andMcodeEqualTo(mcode)
				.andMOrderNumberEqualTo(mOrderNumber)
				.andPayStatusEqualTo(DictionaryConstant.ORDER_STATUS_YFK.getCode())
				.andPayTypeEqualTo(payType);
		int count = payOrderMapper.updateByExampleSelective(payOrder,payOrderExample);
		return count > 0 ? true:false;
	}

	/**
	 * 支付平台订单支付状态更新方法
	 * @param mcode 商户号
	 * @param payType 支付方式
	 * @param mOrderNumber 业务系统订单号
	 * @param payOrder 更新订单对象
	 * @return
	 */
	public Boolean updateOrderPayStatus(String orderStatus,PayOrder payOrder,String mcode,String mOrderNumber,String payType){
//		payOrder.setOrderStatus(orderStatus);
		payOrder.setPayStatus(orderStatus);
		payOrder.setUpdatedAt(new Date());
		val payOrderExample = new PayOrderExample();
		payOrderExample.createCriteria()
				.andMcodeEqualTo(mcode)
				.andMOrderNumberEqualTo(mOrderNumber)
				.andPayStatusEqualTo(DictionaryConstant.PAY_STATUS_WFK.getCode())
				.andPayTypeEqualTo(payType);
		int count = payOrderMapper.updateByExampleSelective(payOrder,payOrderExample);
		return count > 0 ? true:false;
	}

	/**
	 * 订单信息查询方法
	 * @return
	 */
	public PayOrder queryPayOrder(String mcode,String mOrderNumber,String payType){
		val example = new PayOrderExample();
		example.createCriteria()
				.andMcodeEqualTo(mcode)
				.andMOrderNumberEqualTo(mOrderNumber)
				.andPayTypeEqualTo(payType);
		val payOrders = payOrderMapper.selectByExample(example);
		return payOrders.size()>0?payOrders.get(0):null;
	}

	/**
	 * 订单信息查询方法
	 * @return
	 */
	public PayOrder queryPayOrderByTimeExpire(String mOrderNumber,String timeExpire,String payType){
		val example = new PayOrderExample();
		example.createCriteria()
				.andMOrderNumberEqualTo(mOrderNumber)
				.andTimeExpireEqualTo(timeExpire)
				.andPayTypeEqualTo(payType);
		val payOrders = payOrderMapper.selectByExample(example);
		return payOrders.size()>0?payOrders.get(0):null;
	}

	/**
	 * 订单信息列表查询方法
	 * @return
	 */
	public List<PayOrder> queryPayOrder(String mcode, String mOrderNumber){
		val example = new PayOrderExample();
		example.createCriteria()
				.andMcodeEqualTo(mcode)
				.andMOrderNumberEqualTo(mOrderNumber);
		val payOrders = payOrderMapper.selectByExample(example);
		return payOrders;
	}
	
	/**
	 * 订单信息查询方法
	 * @return
	 */
	public PayOrder queryPayOrderByNumber(String mOrderNumber,String payType){
		val example = new PayOrderExample();
		example.createCriteria()
				.andMOrderNumberEqualTo(mOrderNumber)
				.andPayTypeEqualTo(payType);
		val payOrders = payOrderMapper.selectByExample(example);
		return payOrders.size()>0?payOrders.get(0):null;
	}

	/**
	 * 支付平台订单关闭方法
	 * @param mcode 商户号
	 * @param mOrderNumber 业务系统订单号
	 * @param payType 更新订单支付方式
	 * @return
	 */
	public Boolean cancelPayOrder(String mcode,String mOrderNumber,String payType){
		val payOrder = new PayOrder();
		payOrder.setUpdatedAt(new Date());
//		payOrder.setOrderStatus(DictionaryConstant.ORDER_STATUS_DDQX.getCode());
		val payOrderExample = new PayOrderExample();
		payOrderExample.createCriteria()
				.andMcodeEqualTo(mcode)
				.andPayTypeEqualTo(payType)
				.andMOrderNumberEqualTo(mOrderNumber);
		int count = payOrderMapper.updateByExampleSelective(payOrder,payOrderExample);
		return count > 0 ? true:false;
	}

	/**
	 * 支付平台订单关闭方法
	 * @param payOrder 支付单
	 * @param refundNumber 业务系统退款订单号
	 * @param thirdRefundNumber 第三方支付平台退款单号
	 * @param refundNotifyUrl 退款成功回调URL
	 * @return
	 */
	public Boolean refundPayOrder(PayOrder payOrder,String refundNumber,Long refundAmount,String thirdRefundNumber,String refundNotifyUrl,String orderStatus){
		//1.更新订单状态信息
		val payOrderTemp = new PayOrder();
		payOrderTemp.setUpdatedAt(new Date());
//		payOrderTemp.setOrderStatus(orderStatus);
		val payOrderExample = new PayOrderExample();
		payOrderExample.createCriteria()
				.andMcodeEqualTo(payOrder.getMcode())
				.andPayTypeEqualTo(payOrder.getPayType())
				.andMOrderNumberEqualTo(payOrder.getmOrderNumber())
				.andTradeIdEqualTo(payOrder.getTradeId());
		int count = payOrderMapper.updateByExampleSelective(payOrderTemp,payOrderExample);

		//2.生成聚合支付平台退款单信息
		if(count > 0){
			val payRefundOrder = new PayRefundOrder();
			BeanUtils.copyProperties(payOrder,payRefundOrder);
			payRefundOrder.setRefundNotifyUrl(refundNotifyUrl);
			payRefundOrder.setThirdRefundNumber(thirdRefundNumber);
			payRefundOrder.setRefundNumber(refundNumber);
			payRefundOrder.setRefundStatus(DictionaryConstant.REFUND_STATUS_TKZ.getCode());
			payRefundOrder.setRefundedAmount(refundAmount);

			payRefundOrder.setCreatedAt(new Date());
			payRefundOrder.setUpdatedAt(new Date());
			count = payRefundOrderMapper.insertSelective(payRefundOrder);
		}
		return count > 0 ? true:false;
	}

	/**
	 * 退款订单信息查询方法
	 * @param mOrderNumber 商户订单号
	 * @param thirdRefundNumber 第三方平台退款单号
	 * @param refundNumber 商户退款单号
	 * @return
	 */
	public PayRefundOrder queryPayRefundOrder(String mOrderNumber,String thirdRefundNumber,String refundNumber){
		val example = new PayRefundOrderExample();
		example.createCriteria()
				.andMOrderNumberEqualTo(mOrderNumber)
				.andThirdRefundNumberEqualTo(thirdRefundNumber)
				.andRefundNumberEqualTo(refundNumber);
		val payRefundOrders = payRefundOrderMapper.selectByExample(example);
		return payRefundOrders.size()>0?payRefundOrders.get(0):null;
	}

	/**
	 * 支付平台退款订单状态更新方法
	 * @param payRefundOrder 退款订单信息
	 * @param mOrderNumber 商户订单号
	 * @param thirdRefundNumber 第三方平台退款单号
	 * @param refundNumber 商户退款单号
	 * @return
	 */
	public Boolean updatePayRefundOrderStatus(PayRefundOrder payRefundOrder,String mOrderNumber,String thirdRefundNumber,String refundNumber){
		payRefundOrder.setUpdatedAt(new Date());
		val example = new PayRefundOrderExample();
		example.createCriteria()
				.andMOrderNumberEqualTo(mOrderNumber)
				.andThirdRefundNumberEqualTo(thirdRefundNumber)
				.andRefundNumberEqualTo(refundNumber);
		int count = payRefundOrderMapper.updateByExampleSelective(payRefundOrder,example);
		return count > 0 ? true:false;
	}
}
