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

import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.payorder.persistent.domain.PayRefundOrder;
import com.neusoft.smart.pos.payorder.persistent.domain.PayRefundOrderExample;
import com.neusoft.smart.pos.payorder.persistent.mapper.PayRefundOrderMapper;
import com.neusoft.smart.pos.utils.RequestHeaderUtil;
import lombok.val;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

@Component
public class PayRefundOrderRepository {

    @Resource
    private PayRefundOrderMapper mPayRefundOrderMapper;
    /**
     * 创建退款支付订单方法
     * @param payRefundOrder
     * @return
     */
    public Integer createPayRefundOrder(PayRefundOrder payRefundOrder){
        if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
            payRefundOrder.setCreatedBy(RequestHeaderUtil.getUsername());
        }
        return mPayRefundOrderMapper.insertSelective(payRefundOrder);
    }

    /**
     * 订单信息状态方法
     *
     * @param payRefundNum 退款支付单编号
     * @return 是否更新成功
     */
    public Boolean updatePayRefundOrderStatus(String payRefundNum, String refundStatus) {
        PayRefundOrder payRefundOrder = new PayRefundOrder();
        payRefundOrder.setRefundStatus(refundStatus);
        payRefundOrder.setUpdatedAt(new Date());
        if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
            payRefundOrder.setUpdatedBy(RequestHeaderUtil.getUsername());
        }

        if (DictionaryConstant.PAY_REFUND_STATUS__YTK.getCode().equals(refundStatus)) {
            payRefundOrder.setRefundedTime(payRefundOrder.getUpdatedAt());
        }
        PayRefundOrderExample example = new PayRefundOrderExample();
        example.createCriteria().andPayRefundOrderNumberEqualTo(payRefundNum);
        int result = mPayRefundOrderMapper.updateByExampleSelective(payRefundOrder, example);
        return result > 0 ? true : false;
    }



    /**
     * 退款支付订单更新方法
     * @param payRefundOrderNumber 业务系统订单号
     * @param payRefundOrder 更新订单对象
     * @return
     */
    public Boolean updatePayRefundOrder(String payRefundOrderNumber,PayRefundOrder payRefundOrder){
        payRefundOrder.setUpdatedAt(new Date());
        if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
            payRefundOrder.setUpdatedBy(RequestHeaderUtil.getUsername());
        }
        val example = new PayRefundOrderExample();
        example.createCriteria()
                .andOuterOrderNumberEqualTo(payRefundOrderNumber);
        int count = mPayRefundOrderMapper.updateByExampleSelective(payRefundOrder,example);
        return count > 0 ? true:false;
    }


    /**
     * 支付平台订单更新方法
     * @param mcode 商户号
     * @param tradeId 交易ID
     * @param refundNumber 退款单号
     * @param payRefundOrder 更新订单对象
     * @return
     */
    public Boolean updatePayOrder(String mcode,String tradeId,String refundNumber,PayRefundOrder payRefundOrder){
        payRefundOrder.setUpdatedAt(new Date());
        if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
            payRefundOrder.setUpdatedBy(RequestHeaderUtil.getUsername());
        }
        val example = new PayRefundOrderExample();
        example.createCriteria()
                .andMcodeEqualTo(mcode)
                .andTradeIdEqualTo(tradeId)
                .andRefundNumberEqualTo(refundNumber);
        int count = mPayRefundOrderMapper.updateByExampleSelective(payRefundOrder,example);
        return count > 0 ? true:false;
    }


    /**
     * 支付平台退款订单状态更新方法
     * @param payRefundOrder 退款订单信息
     * @param payRefundNumber 退款支付订单号
     * @return
     */
    public Boolean updatePayRefundOrderStatus(PayRefundOrder payRefundOrder,String payRefundNumber){
        payRefundOrder.setUpdatedAt(new Date());
        if(payRefundOrder.getRefundStatus() != null && payRefundOrder.getRefundStatus().equals(DictionaryConstant.PAY_REFUND_STATUS__YTK.getCode())){
            payRefundOrder.setRefundedTime(new Date());
        }
        if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
            payRefundOrder.setUpdatedBy(RequestHeaderUtil.getUsername());
        }
        val example = new PayRefundOrderExample();
        example.createCriteria()
                .andOuterOrderNumberEqualTo(payRefundNumber);
        int count = mPayRefundOrderMapper.updateByExampleSelective(payRefundOrder,example);
        return count > 0 ? true:false;
    }

    /**
     * 支付平台退款订单状态更新方法
     * @param payRefundOrder 退款订单信息
     * @param payRefundNumber 退款支付订单号
     * @return
     */
    public Boolean updatePayRefundOrderStatusById(PayRefundOrder payRefundOrder,String payRefundNumber){
        payRefundOrder.setUpdatedAt(new Date());
        if(payRefundOrder.getRefundStatus() != null && payRefundOrder.getRefundStatus().equals(DictionaryConstant.PAY_REFUND_STATUS__YTK.getCode())){
            payRefundOrder.setRefundedTime(new Date());
        }
        if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
            payRefundOrder.setUpdatedBy(RequestHeaderUtil.getUsername());
        }
        val example = new PayRefundOrderExample();
        example.createCriteria()
                .andPayRefundOrderNumberEqualTo(payRefundNumber);
        int count = mPayRefundOrderMapper.updateByExampleSelective(payRefundOrder,example);
        return count > 0 ? true:false;
    }
    
    /**
	 * 支付平台退款订单状态更新方法
	 * @param payRefundOrder 退款订单信息
	 * @param thirdRefundNumber 第三方平台退款单号
	 * @param refundNumber 商户退款单号
	 * @return
	 */
	public Boolean updatePayRefundOrderStatus(PayRefundOrder payRefundOrder,String thirdRefundNumber,String refundNumber){
		payRefundOrder.setUpdatedAt(new Date());
        if(payRefundOrder.getRefundStatus() != null && payRefundOrder.getRefundStatus().equals(DictionaryConstant.PAY_REFUND_STATUS__YTK.getCode())){
            payRefundOrder.setRefundedTime(new Date());
        }
        if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
            payRefundOrder.setUpdatedBy(RequestHeaderUtil.getUsername());
        }
		val example = new PayRefundOrderExample();
		example.createCriteria()
				.andThirdRefundNumberEqualTo(thirdRefundNumber)
				.andRefundNumberEqualTo(refundNumber);
		int count = mPayRefundOrderMapper.updateByExampleSelective(payRefundOrder,example);
		return count > 0 ? true:false;
	}
	


    /**
     * 订单信息列表查询方法
     * @return
     */
    public List<PayRefundOrder> queryPayRefundOrder(String refundNumber){
        val example = new PayRefundOrderExample();
        example.createCriteria()
                .andRefundNumberEqualTo(refundNumber);
        val payOrders = mPayRefundOrderMapper.selectByExample(example);
        return payOrders;
    }


    /**
     * 订单信息列表查询方法
     * @return
     */
    public PayRefundOrder getPayRefundOrder(String payRefundNumber){
        val example = new PayRefundOrderExample();
        example.createCriteria()
                .andPayRefundOrderNumberEqualTo(payRefundNumber);
        val payRefundOrders = mPayRefundOrderMapper.selectByExample(example);
        return payRefundOrders.size()>0?payRefundOrders.get(0):null;
    }

    /**
     * 退款订单信息查询方法
     * @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 = mPayRefundOrderMapper.selectByExample(example);
        return payRefundOrders.size()>0?payRefundOrders.get(0):null;
    }

    /**
     * 支付平台订单关闭方法
     * @param mcode 商户号
     * @param mOrderNumber 业务系统订单号
     * @param payType 更新订单支付方式
     * @return
     */
    public Boolean cancelPayOrder(String mcode,String mOrderNumber,String payType){
        val payRefundOrder = new PayRefundOrder();
        payRefundOrder.setUpdatedAt(new Date());
        if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
            payRefundOrder.setUpdatedBy(RequestHeaderUtil.getUsername());
        }
//		payOrder.setOrderStatus(DictionaryConstant.ORDER_STATUS_DDQX.getCode());
        val example = new PayRefundOrderExample();
        example.createCriteria()
                .andMcodeEqualTo(mcode)
                .andPayTypeEqualTo(payType)
                .andMOrderNumberEqualTo(mOrderNumber);
        int count = mPayRefundOrderMapper.updateByExampleSelective(payRefundOrder,example);
        return count > 0 ? true:false;
    }
}

