package com.hys.app.converter.erp;

import com.hys.app.framework.database.WebPage;
import com.hys.app.model.erp.dos.*;
import com.hys.app.model.erp.dto.StockUpdateDTO;
import com.hys.app.model.erp.dto.WarehouseOutDTO;
import com.hys.app.model.erp.enums.StockOperateEnum;
import com.hys.app.model.erp.vo.WarehouseOutAllowable;
import com.hys.app.model.erp.vo.WarehouseOutVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingConstants;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.hys.app.framework.util.CollectionUtils.convertList;
import static com.hys.app.framework.util.CollectionUtils.convertMap;

/**
 * 出库单 Convert
 *
 * @author 张崧
 * @since 2023-12-07 16:50:20
 */
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING)
public interface WarehouseOutConverter {

    WarehouseOutDO convert(WarehouseOutDTO warehouseOutDTO);

    WarehouseOutVO convert(WarehouseOutDO warehouseOutDO);

    List<WarehouseOutVO> convert(List<WarehouseOutDO> warehouseOutDO);

    default WebPage<WarehouseOutVO> convert(WebPage<WarehouseOutDO> webPage){
        List<WarehouseOutVO> convert = convert(webPage.getData());
        WebPage<WarehouseOutVO> resPage = new WebPage();
        resPage.setData(convert);
        resPage.setPageNo(webPage.getPageNo());
        resPage.setPageSize(webPage.getPageSize());
        resPage.setDataTotal(webPage.getDataTotal());
        return resPage;
    };

    default WebPage<WarehouseOutVO> combination(WebPage<WarehouseOutDO> webPage, Map<Long, String> deptNameMap, Map<Long, String> warehouseNameMap) {
        WebPage<WarehouseOutVO> page = convert(webPage);

        for (WarehouseOutVO warehouseOutVO : page.getData()) {
            warehouseOutVO.setDeptName(deptNameMap.get(warehouseOutVO.getDeptId()));
            warehouseOutVO.setWarehouseName(warehouseNameMap.get(warehouseOutVO.getWarehouseId()));
        }

        return page;
    }

    default WarehouseOutAllowable getAllowable(WarehouseOutDO warehouseOutDO){
        return new WarehouseOutAllowable(warehouseOutDO);
    }

    default List<StockUpdateDTO> convertStockUpdateList(List<WarehouseOutItemDO> itemList){
        return itemList.stream().map(warehouseOutItemDO -> {
            StockUpdateDTO stockUpdateDTO = new StockUpdateDTO();

            stockUpdateDTO.setBatchId(warehouseOutItemDO.getWarehouseEntryBatchId());
            stockUpdateDTO.setTip(warehouseOutItemDO.getWarehouseEntryBatchSn());
            stockUpdateDTO.setOperate(StockOperateEnum.Reduce);
            stockUpdateDTO.setChangeNum(warehouseOutItemDO.getOutNum());

            return stockUpdateDTO;
        }).collect(Collectors.toList());
    }


}

