package com.jwipc.inquiry.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jwipc.constant.FlagConst;
import com.jwipc.inquiry.client.PurchaseApplyBillDetailClient;
import com.jwipc.inquiry.convert.PurchaseApplyBillDetailConvert;
import com.jwipc.inquiry.dto.PurchaseApplyBillDetailClientDTO;
import com.jwipc.inquiry.entity.PurchaseApplyBillDetail;
import com.jwipc.inquiry.logic.PurchaseApplyBillDetailLogic;
import com.jwipc.inquiry.service.IPurchaseApplyBillDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * @author shenyang.ou
 * @date 2024/4/24 10:13
 * @desc
 */
@Service
@RequiredArgsConstructor
public class PurchaseApplyBillDetailClientImpl implements PurchaseApplyBillDetailClient {
    private final IPurchaseApplyBillDetailService purchaseApplyBillDetailService;
    private final PurchaseApplyBillDetailLogic purchaseApplyBillDetailLogic;
    private final PurchaseApplyBillDetailConvert purchaseApplyBillDetailConvert;

    @Override
    public void updatePurchaseAppliyBillDetailForTender(String code, Collection<? extends Serializable> sourceRowIds) {
        List<PurchaseApplyBillDetail> purchaseApplyBillDetails = purchaseApplyBillDetailService.listByIds(sourceRowIds);
        purchaseApplyBillDetails.forEach(item -> {
            item.setTenderFlag(1);
            item.setTenderCode(code);
            item.setPurchaseMethod("Z002");
        });
        purchaseApplyBillDetailService.updateBatchById(purchaseApplyBillDetails);
    }

    @Override
    public void cancelPurchaseAppliyBillDetailTender(String code) {
        purchaseApplyBillDetailService.update(new LambdaUpdateWrapper<PurchaseApplyBillDetail>()
                .set(PurchaseApplyBillDetail::getTenderFlag, FlagConst.N.getCode())
                .set(PurchaseApplyBillDetail::getTenderCode,"")
                .set(PurchaseApplyBillDetail::getPurchaseMethod,"")
                .eq(PurchaseApplyBillDetail::getTenderCode,code));
    }

    @Override
    public PurchaseApplyBillDetailClientDTO getByBillCodeAndItemCode(String billCode, String itemCode) {
        return purchaseApplyBillDetailLogic.getByBillCodeAndItemCode(billCode,itemCode);
    }

    @Override
    public PurchaseApplyBillDetailClientDTO getByBillCode(String billCode) {
        return purchaseApplyBillDetailLogic.getByBillCode(billCode);
    }

    @Override
    public void updatePuchaseMethode(String purhaseCode,Integer transferStatus,Collection<? extends Serializable> detailIds) {
        if (CollectionUtil.isNotEmpty(detailIds)){
            List<PurchaseApplyBillDetail> purchaseApplyBillDetails = purchaseApplyBillDetailService.listByIds(detailIds);
            purchaseApplyBillDetails.forEach(item -> {
                if(!StringUtils.isEmpty(purhaseCode)){
                    item.setPurchaseMethod(purhaseCode);
                }
                item.setTransferStatus(transferStatus);
            });
            purchaseApplyBillDetailService.updateBatchById(purchaseApplyBillDetails);
        }
    }

    @Override
    public void updatePuchaseMethodeById(Integer transferStatus, Collection<? extends Serializable> detailIds) {
        List<PurchaseApplyBillDetail> purchaseApplyBillDetails = purchaseApplyBillDetailService.listByIds(detailIds);
        purchaseApplyBillDetails.forEach(item -> {
            item.setTransferStatus(transferStatus);
            item.setPurchaseMethod("Z004");
        });
        purchaseApplyBillDetailService.updateBatchById(purchaseApplyBillDetails);
    }

    @Override
    public void updatePurchaseAppliyBillDetailForShoppingY(String code, BigDecimal cartQty) {
        List<PurchaseApplyBillDetail> purchaseApplyBillDetailList = purchaseApplyBillDetailService.list();
//        for (String prCode : codeList) {
            for (PurchaseApplyBillDetail purchaseApplyBillDetail : purchaseApplyBillDetailList) {

                if((purchaseApplyBillDetail.getBillCode() + " - " + purchaseApplyBillDetail.getItemCode()).equals(code)){
                    //转单状态
                    //purchaseApplyBillDetail.setTransferStatus(2);
            BigDecimal remainQuantity = purchaseApplyBillDetail.getRemainQuantity() == null ? BigDecimal.ZERO : purchaseApplyBillDetail.getRemainQuantity();

                    purchaseApplyBillDetail.setRemainQuantity(remainQuantity.subtract(cartQty));
//                    purchaseApplyBillDetailService.updateById(purchaseApplyBillDetail);
                }

//            }
        }
        purchaseApplyBillDetailService.updateBatchById(purchaseApplyBillDetailList);
    }

    @Override
    public void  updatePurchaseAppliyBillDetailForShoppingW(String code, BigDecimal cartQty) {
        List<PurchaseApplyBillDetail> purchaseApplyBillDetailList = purchaseApplyBillDetailService.list();
//        List<CartPurchaseDTO> cartPurchaseDTOList = cartInfoService.getByCartId(id);
//        for (String prCode : codeList) {
            for (PurchaseApplyBillDetail purchaseApplyBillDetail : purchaseApplyBillDetailList) {
                if((purchaseApplyBillDetail.getBillCode() + " - " + purchaseApplyBillDetail.getItemCode()).equals(code)){
                    //转单状态
                    //purchaseApplyBillDetail.setTransferStatus(1);
                    BigDecimal remainQuantity = purchaseApplyBillDetail.getRemainQuantity() == null ? BigDecimal.ZERO : purchaseApplyBillDetail.getRemainQuantity();

                    purchaseApplyBillDetail.setRemainQuantity(remainQuantity.add(cartQty));
//                    purchaseApplyBillDetailService.updateById(purchaseApplyBillDetail);
                }

            }
//        }
        purchaseApplyBillDetailService.updateBatchById(purchaseApplyBillDetailList);
    }

    @Override
    public void updateRemainQuantity(List<PurchaseApplyBillDetailClientDTO> applyBillDetailList) {
        List<PurchaseApplyBillDetail> purchaseApplyBillDetail = purchaseApplyBillDetailConvert.clientDto2Entities(applyBillDetailList);
        purchaseApplyBillDetailService.updateBatchById(purchaseApplyBillDetail);
    }


}
