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


import com.neusoft.smart.pos.constants.DictionaryConstant;
import com.neusoft.smart.pos.framework.utils.IdGenerateUtils;
import com.neusoft.smart.pos.framework.utils.LocalDateTimeUtils;
import com.neusoft.smart.pos.order.persistent.domain.OrderRefund;
import com.neusoft.smart.pos.order.persistent.domain.OrderRefundExample;
import com.neusoft.smart.pos.order.persistent.mapper.OrderRefundMapper;
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;


/**
 * @author zcf
 * @date 2018-03-14 09：24
 * @description 订单退款管理使用Repository
 */
@Component
public class OrderRefundRepository {

	@Resource
	private OrderRefundMapper orderRefundMapper;
	
	/**
	 * 退款订单信息保存方法
	 * @return
	 */
	public Integer saveOrderRefund(OrderRefund orderRefund){
		if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
			orderRefund.setCreatedBy(RequestHeaderUtil.getUsername());
		}
		return orderRefundMapper.insertSelective(orderRefund);
	}

	/**
	 * 退款订单号生成方法
	 * @return
	 */
	public String generateRefundOrderNumber(){
		String refundOrderNumber;
		while(true){
			refundOrderNumber = IdGenerateUtils.generateRefundOrderNumber();
			val example = new OrderRefundExample();
			example.createCriteria()
					.andRefundNumberEqualTo(refundOrderNumber)
					.andCreatedAtGreaterThanOrEqualTo(LocalDateTimeUtils.convertLocalDateTimeToDate(LocalDateTimeUtils.getCurrentDayStart()));
			val orderRefunds = orderRefundMapper.selectByExample(example);
			if(null == orderRefunds || orderRefunds.size() == 0){
				break;
			}
		}
		return refundOrderNumber;
	}

	/**
	 * 获得退款订单方法
	 *
	 * @return
	 */
	public OrderRefund getOrderRefund(String refundNumber) {
		val example = new OrderRefundExample();
		example.createCriteria().andRefundNumberEqualTo(refundNumber);
		val orderRefunds = orderRefundMapper.selectByExample(example);
		return orderRefunds.size() > 0 ? orderRefunds.get(0) : null;
	}

	/**
	 * 获得订单全部退款单列表方法
	 * @return
	 */
	public List<OrderRefund> queryOrderRefunds(String orderNumber){
		val example = new OrderRefundExample();
		example.createCriteria().andOriginalOrderNumberEqualTo(orderNumber)
				.andRefundStatusNotEqualTo(DictionaryConstant.REFUND_STATUS_TKQX.getCode());
		return orderRefundMapper.selectByExample(example);
	}

	/**
	 * 订单退款成功信息更新方法
	 *
	 * @return
	 */
	public Boolean updateOrderRefundSuccess(OrderRefund orderRefund, String refundNumber) {
		orderRefund.setRefundStatus(DictionaryConstant.REFUND_STATUS_TKWC.getCode());
		orderRefund.setUpdatedAt(new Date());
		if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
			orderRefund.setUpdatedBy(RequestHeaderUtil.getUsername());
		}
		val example = new OrderRefundExample();
		example.createCriteria().andRefundNumberEqualTo(refundNumber);
		val result = orderRefundMapper.updateByExampleSelective(orderRefund, example);
		return result > 0 ? true : false;
	}

	/**
	 * 更新退款订单状态
	 * @param refundNumber
	 * @param state
	 * @return
	 */
	public boolean updateOrderRefundStatus(String refundNumber, String state) {
		OrderRefund orderRefund = new OrderRefund();
		orderRefund.setRefundStatus(state);
		orderRefund.setUpdatedAt(new Date());
		if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
			orderRefund.setUpdatedBy(RequestHeaderUtil.getUsername());
		}
		val example = new OrderRefundExample();
		example.createCriteria().andRefundNumberEqualTo(refundNumber);
		val result = orderRefundMapper.updateByExampleSelective(orderRefund, example);
		return result > 0 ? true : false;
	}

    public boolean updateOrderRefund(String refundNumber, OrderRefund updateOrderRefund) {
		updateOrderRefund.setUpdatedAt(new Date());
		if(!StringUtils.isEmpty(RequestHeaderUtil.getUsername())){
			updateOrderRefund.setUpdatedBy(RequestHeaderUtil.getUsername());
		}
		val example = new OrderRefundExample();
		example.createCriteria().andRefundNumberEqualTo(refundNumber);
		val result = orderRefundMapper.updateByExampleSelective(updateOrderRefund, example);
		return result > 0 ? true : false;
    }

	/**
	 * 根据原始订单编号查询订单退款信息
	 * @return
	 */
	public List<OrderRefund> selectByOriginalOrderNumber(String orderNumber) {
		val example = new OrderRefundExample();
		example.createCriteria().andOriginalOrderNumberEqualTo(orderNumber);
		return orderRefundMapper.selectByExample(example);
	}
}
