package com.hys.app.converter.erp;

import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.util.CurrencyUtil;
import com.hys.app.model.erp.dos.ProcurementContractProduct;
import com.hys.app.model.erp.dos.WarehouseEntryProductDO;
import com.hys.app.model.erp.dto.WarehouseEntryProductDTO;
import com.hys.app.model.erp.vo.WarehouseEntryProductVO;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 入库单商品明细 Convert
 *
 * @author 张崧
 * @since 2023-12-05 15:30:10
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface WarehouseEntryProductConverter {

    WarehouseEntryProductDO convert(WarehouseEntryProductDTO warehouseEntryProductDTO);

    WarehouseEntryProductVO convert(WarehouseEntryProductDO warehouseEntryProductDO);

    List<WarehouseEntryProductVO> convert(List<WarehouseEntryProductDO> list);

    WebPage<WarehouseEntryProductVO> convert(WebPage<WarehouseEntryProductDO> webPage);

    default List<WarehouseEntryProductDO> combination(List<WarehouseEntryProductDTO> dtolist, Long warehouseEntryId, List<ProcurementContractProduct> contractProductList, Double priceRatio) {
        List<WarehouseEntryProductDO> list = new ArrayList<>();

        for (WarehouseEntryProductDTO warehouseEntryProductDTO : dtolist) {
            // 合同商品信息
            for (ProcurementContractProduct procurementContractProduct : contractProductList) {
                if(procurementContractProduct.getGoodsId().equals(warehouseEntryProductDTO.getProductId())) {
                    WarehouseEntryProductDO entryProductDO = convert(warehouseEntryProductDTO);
                    entryProductDO.setWarehouseEntryId(warehouseEntryId);
                    entryProductDO.setGoodsId(procurementContractProduct.getGoodsId());
                    entryProductDO.setSn(procurementContractProduct.getProductSn());
                    entryProductDO.setName(procurementContractProduct.getProductName());
                    entryProductDO.setSpecification(procurementContractProduct.getSpecification());
                    entryProductDO.setUnit(procurementContractProduct.getUnit());
                    entryProductDO.setProductBarcode(procurementContractProduct.getBarcode());
                    entryProductDO.setCategoryId(procurementContractProduct.getCategoryId());
                    entryProductDO.setCategoryName(procurementContractProduct.getCategoryName());
                    entryProductDO.setContractNum(procurementContractProduct.getNum());
                    entryProductDO.setContractPrice(procurementContractProduct.getPrice());
                    entryProductDO.setCostPrice(CurrencyUtil.mul(procurementContractProduct.getPrice(), priceRatio));
                    entryProductDO.setTotalPrice(CurrencyUtil.mul(entryProductDO.getCostPrice(), entryProductDO.getNum()));
                    entryProductDO.setTaxRate(procurementContractProduct.getTaxRate());
                    list.add(entryProductDO);
                }
            }

        }

        return list;
    }
}

