package com.hw.wms.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.hw.common.core.constant.WmsConstants;
import com.hw.common.core.utils.DateUtils;
import com.hw.common.core.utils.StringUtils;
import com.hw.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hw.wms.mapper.WmsWarehouseMaterialMapper;
import com.hw.wms.domain.WmsWarehouseMaterial;
import com.hw.wms.service.IWmsWarehouseMaterialService;

/**
 * 仓库可储存物料信息Service业务层处理
 *
 * @author xins
 * @date 2024-01-09
 */
@Service
public class WmsWarehouseMaterialServiceImpl implements IWmsWarehouseMaterialService {
    @Autowired
    private WmsWarehouseMaterialMapper wmsWarehouseMaterialMapper;

    /**
     * 查询仓库可储存物料信息
     *
     * @param warehouseId 仓库可储存物料信息主键
     * @return 仓库可储存物料信息
     */
    @Override
    public WmsWarehouseMaterial selectWmsWarehouseMaterialByWarehouseId(Long warehouseId) {
        return wmsWarehouseMaterialMapper.selectWmsWarehouseMaterialByWarehouseId(warehouseId);
    }

    /**
     * 查询仓库可储存物料信息列表
     *
     * @param wmsWarehouseMaterial 仓库可储存物料信息
     * @return 仓库可储存物料信息
     */
    @Override
    public List<WmsWarehouseMaterial> selectWmsWarehouseMaterialList(WmsWarehouseMaterial wmsWarehouseMaterial) {
        return wmsWarehouseMaterialMapper.selectWmsWarehouseMaterialList(wmsWarehouseMaterial);
    }

    /**
     * 新增仓库可储存物料信息
     *
     * @param wmsWarehouseMaterial 仓库可储存物料信息
     * @return 结果
     */
    @Override
    public int insertWmsWarehouseMaterial(WmsWarehouseMaterial wmsWarehouseMaterial) {
        wmsWarehouseMaterial.setCreateTime(DateUtils.getNowDate());
        return wmsWarehouseMaterialMapper.insertWmsWarehouseMaterial(wmsWarehouseMaterial);
    }

    /**
     * 修改仓库可储存物料信息
     *
     * @param wmsWarehouseMaterial 仓库可储存物料信息
     * @return 结果
     */
    @Override
    public int updateWmsWarehouseMaterial(WmsWarehouseMaterial wmsWarehouseMaterial) {
        return wmsWarehouseMaterialMapper.updateWmsWarehouseMaterial(wmsWarehouseMaterial);
    }

    /**
     * 批量删除仓库可储存物料信息
     *
     * @param wmsWarehouseMaterialIds 需要删除的仓库可储存物料信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsWarehouseMaterialByWarehouseMaterialIds(Long[] wmsWarehouseMaterialIds) {
        return wmsWarehouseMaterialMapper.deleteWmsWarehouseMaterialByWarehouseMaterialIds(wmsWarehouseMaterialIds);
    }

    /**
     * 删除仓库可储存物料信息信息
     *
     * @param warehouseMaterialId 仓库可储存物料信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsWarehouseMaterialByWarehouseMaterialId(Long warehouseMaterialId) {
        return wmsWarehouseMaterialMapper.deleteWmsWarehouseMaterialByWarehouseMaterialId(warehouseMaterialId);
    }


    /**
     * 查询仓库可储存物料信息列表,join material
     *
     * @param wmsWarehouseMaterial 仓库可储存物料信息
     * @return 仓库可储存物料信息
     */
    @Override
    public List<WmsWarehouseMaterial> selectWmsWarehouseMaterialJoinList(WmsWarehouseMaterial wmsWarehouseMaterial) {
        return wmsWarehouseMaterialMapper.selectWmsWarehouseMaterialJoinList(wmsWarehouseMaterial);
    }

    /**
     * 批量新增仓库配置物料信息
     *
     * @param warehouseId 仓库ID
     * @param materialIds 物料组
     */
    @Override
    public void batchInsertWarehouseMaterial(Long warehouseId, Long[] materialIds) {
        if (StringUtils.isNotEmpty(materialIds)) {
            String userName = SecurityUtils.getUsername();
            Date currentDate = new Date();
            // 新增仓库的物料配置信息
            List<WmsWarehouseMaterial> list = new ArrayList<WmsWarehouseMaterial>();
            for (Long materialId : materialIds) {
                WmsWarehouseMaterial warehouseMaterial = new WmsWarehouseMaterial();
                warehouseMaterial.setWarehouseId(warehouseId);
                warehouseMaterial.setStorageType(WmsConstants.WMS_WAREHOUSE_MATERIAL_STORAGE_TYPE_MATERIAL);
                warehouseMaterial.setStorageId(materialId);
                warehouseMaterial.setCreateBy(userName);
                warehouseMaterial.setCreateTime(currentDate);
                list.add(warehouseMaterial);
            }
            wmsWarehouseMaterialMapper.batchInsertWarehouseMaterial(list);
        }
    }


}
