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


import com.neusoft.smart.pos.framework.utils.BigDecimalUtils;
import com.neusoft.smart.pos.order.persistent.domain.OrderTransaction;
import com.neusoft.smart.pos.order.persistent.domain.OrderTransactionExample;
import com.neusoft.smart.pos.order.persistent.mapper.OrderTransactionMapper;
import lombok.val;
import org.springframework.stereotype.Component;

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


/**
 * @author tongmg
 * @date 2019-12-13 10：43
 * @description 订单商品管理使用Repository
 */
@Component
public class OrderTransactionRepository {

	@Resource
	private OrderTransactionMapper orderTransactionMapper;
	
	/**
	 * 订单信息保存方法
	 * @return
	 */
	public Integer saveOrderDetails(OrderTransaction orderTransaction){
		if(orderTransaction.getFinalPrice() == null) {
			orderTransaction.setFinalPrice(BigDecimalUtils.roundByScale(orderTransaction.getUnitPrice().subtract(orderTransaction.getDiscountAmount())).multiply(orderTransaction.getQuantity()));
		}
		return orderTransactionMapper.insertSelective(orderTransaction);
	}
	
	/**
	 * 订单信息保存方法
	 * @return
	 */
	public Integer saveRecordOrderDetails(OrderTransaction orderTransaction){
		return orderTransactionMapper.insertSelective(orderTransaction);
	}
	
	/**
	 * 订单信息保存方法
	 * @return
	 */
	public void saveRecordOrderDetailsBatch(List<OrderTransaction> orderTransactionList){
		orderTransactionMapper.batchInsert(orderTransactionList);
	}

	/**
	 * 订单明细信息查询方法
	 * @return
	 */
	public List<OrderTransaction> queryOrderDetails(String orderNumber){
		val example = new OrderTransactionExample();
		example.createCriteria().andOrderNumberEqualTo(orderNumber).andCategoryNumberNotEqualTo("-2");
		return orderTransactionMapper.selectByExample(example);
	}

	/**
	 * 订单明细信息查询方法
	 * @return
	 */
	public List<OrderTransaction> queryVariableFees(String orderNumber){
		val example = new OrderTransactionExample();
		example.createCriteria().andOrderNumberEqualTo(orderNumber).andCategoryNumberEqualTo("-2");
		return orderTransactionMapper.selectByExample(example);
	}

	/**
	 * 订单删除使用方法
	 * @return
	 */
	public Integer deleteOrderTransactionByOrderNumber(String orderNumber){
		OrderTransactionExample example = new OrderTransactionExample();
		example.createCriteria().andOrderNumberEqualTo(orderNumber).andCategoryNumberNotEqualTo("-2");
		return orderTransactionMapper.deleteByExample(example);
	}
	
	/**
	 * 订单删除使用方法
	 * @return
	 */
	public Integer deleteOrderTransactionByOrderNumber(List<String> orderNumberList){
		OrderTransactionExample example = new OrderTransactionExample();
		example.createCriteria().andOrderNumberIn(orderNumberList);
		return orderTransactionMapper.deleteByExample(example);
	}
}
