package com.hw.wms.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.hw.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.hw.wms.domain.WmsMoveDetail;
import com.hw.wms.mapper.WmsMoveMapper;
import com.hw.wms.domain.WmsMove;
import com.hw.wms.service.IWmsMoveService;

/**
 * 移库合库记录Service业务层处理
 * 
 * @author xins
 * @date 2024-01-09
 */
@Service
public class WmsMoveServiceImpl implements IWmsMoveService 
{
    @Autowired
    private WmsMoveMapper wmsMoveMapper;

    /**
     * 查询移库合库记录
     * 
     * @param moveId 移库合库记录主键
     * @return 移库合库记录
     */
    @Override
    public WmsMove selectWmsMoveByMoveId(Long moveId)
    {
        return wmsMoveMapper.selectWmsMoveByMoveId(moveId);
    }

    /**
     * 查询移库合库记录列表
     * 
     * @param wmsMove 移库合库记录
     * @return 移库合库记录
     */
    @Override
    public List<WmsMove> selectWmsMoveList(WmsMove wmsMove)
    {
        return wmsMoveMapper.selectWmsMoveList(wmsMove);
    }

    /**
     * 新增移库合库记录
     * 
     * @param wmsMove 移库合库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsMove(WmsMove wmsMove)
    {
        int rows = wmsMoveMapper.insertWmsMove(wmsMove);
        insertWmsMoveDetail(wmsMove);
        return rows;
    }

    /**
     * 修改移库合库记录
     * 
     * @param wmsMove 移库合库记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsMove(WmsMove wmsMove)
    {
        wmsMoveMapper.deleteWmsMoveDetailByMoveId(wmsMove.getMoveId());
        insertWmsMoveDetail(wmsMove);
        return wmsMoveMapper.updateWmsMove(wmsMove);
    }

    /**
     * 批量删除移库合库记录
     * 
     * @param moveIds 需要删除的移库合库记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsMoveByMoveIds(Long[] moveIds)
    {
        wmsMoveMapper.deleteWmsMoveDetailByMoveIds(moveIds);
        return wmsMoveMapper.deleteWmsMoveByMoveIds(moveIds);
    }

    /**
     * 删除移库合库记录信息
     * 
     * @param moveId 移库合库记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsMoveByMoveId(Long moveId)
    {
        wmsMoveMapper.deleteWmsMoveDetailByMoveId(moveId);
        return wmsMoveMapper.deleteWmsMoveByMoveId(moveId);
    }

    /**
     * 新增移库合库记录明细;原材料入库记录对应的明细信息信息
     * 
     * @param wmsMove 移库合库记录对象
     */
    public void insertWmsMoveDetail(WmsMove wmsMove)
    {
        List<WmsMoveDetail> wmsMoveDetailList = wmsMove.getWmsMoveDetailList();
        Long moveId = wmsMove.getMoveId();
        if (StringUtils.isNotNull(wmsMoveDetailList))
        {
            List<WmsMoveDetail> list = new ArrayList<WmsMoveDetail>();
            for (WmsMoveDetail wmsMoveDetail : wmsMoveDetailList)
            {
                wmsMoveDetail.setMoveId(moveId);
                list.add(wmsMoveDetail);
            }
            if (list.size() > 0)
            {
                wmsMoveMapper.batchWmsMoveDetail(list);
            }
        }
    }
}
