package com.yueqian.xingyun.sc.impl.sale;

import com.lframework.starter.common.exceptions.impl.DefaultClientException;
import com.lframework.starter.common.utils.Assert;
import com.lframework.starter.common.utils.NumberUtil;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.yueqian.xingyun.basedata.entity.Product;
import com.yueqian.xingyun.basedata.service.product.ProductService;
import com.yueqian.xingyun.sc.dto.sale.out.SaleOutSheetDetailLotDto;
import com.yueqian.xingyun.sc.entity.SaleOutSheetDetail;
import com.yueqian.xingyun.sc.entity.SaleOutSheetDetailLot;
import com.yueqian.xingyun.sc.mappers.SaleOutSheetDetailLotMapper;
import com.yueqian.xingyun.sc.service.sale.SaleOutSheetDetailLotService;
import com.yueqian.xingyun.sc.service.sale.SaleOutSheetDetailService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SaleOutSheetDetailLotServiceImpl
    extends BaseMpServiceImpl<SaleOutSheetDetailLotMapper, SaleOutSheetDetailLot>
    implements SaleOutSheetDetailLotService {

  @Autowired
  private ProductService productService;

  @Autowired
  private SaleOutSheetDetailService saleOutSheetDetailService;

  @Override
  public SaleOutSheetDetailLotDto findById(String id) {

    return getBaseMapper().findById(id);
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void addReturnNum(String id, BigDecimal num) {

    Assert.notBlank(id);
    Assert.greaterThanZero(num);

    SaleOutSheetDetailLot detail = getBaseMapper().selectById(id);

    BigDecimal remainNum = NumberUtil.sub(detail.getOrderNum(), detail.getReturnNum());
    if (NumberUtil.lt(remainNum, num)) {
      SaleOutSheetDetail sheetDetail = saleOutSheetDetailService.getById(detail.getDetailId());

      Product product = productService.findById(sheetDetail.getProductId());

      throw new DefaultClientException(
          "（" + product.getCode() + "）" + product.getName() + "剩余退货数量为" + remainNum
              + "个，本次退货数量不允许大于"
              + remainNum + "个！");
    }

    if (getBaseMapper().addReturnNum(detail.getId(), num) != 1) {
      SaleOutSheetDetail sheetDetail = saleOutSheetDetailService.getById(detail.getDetailId());

      Product product = productService.findById(sheetDetail.getProductId());

      throw new DefaultClientException(
          "（" + product.getCode() + "）" + product.getName() + "剩余退货数量不足，不允许继续退货！");
    }

    saleOutSheetDetailService.addReturnNum(detail.getDetailId(), num);
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public void subReturnNum(String id, BigDecimal num) {

    Assert.notBlank(id);
    Assert.greaterThanZero(num);

    SaleOutSheetDetailLot detail = getBaseMapper().selectById(id);

    if (NumberUtil.lt(detail.getReturnNum(), num)) {
      SaleOutSheetDetail sheetDetail = saleOutSheetDetailService.getById(detail.getDetailId());

      Product product = productService.findById(sheetDetail.getProductId());

      throw new DefaultClientException(
          "（" + product.getCode() + "）" + product.getName() + "已退货数量为" + detail.getReturnNum()
              + "个，本次取消退货数量不允许大于" + detail.getReturnNum() + "个！");
    }

    if (getBaseMapper().subReturnNum(detail.getId(), num) != 1) {
      SaleOutSheetDetail sheetDetail = saleOutSheetDetailService.getById(detail.getDetailId());

      Product product = productService.findById(sheetDetail.getProductId());

      throw new DefaultClientException(
          "（" + product.getCode() + "）" + product.getName() + "已退货数量不足，不允许取消退货！");
    }

    saleOutSheetDetailService.subReturnNum(detail.getDetailId(), num);
  }
}
