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.WmsInventoryCheckDetail;
import com.hw.wms.mapper.WmsInventoryCheckMapper;
import com.hw.wms.domain.WmsInventoryCheck;
import com.hw.wms.service.IWmsInventoryCheckService;

/**
 * 盘点记录Service业务层处理
 * 
 * @author xs
 * @date 2023-12-20
 */
@Service
public class WmsInventoryCheckServiceImpl implements IWmsInventoryCheckService 
{
    @Autowired
    private WmsInventoryCheckMapper wmsInventoryCheckMapper;

    /**
     * 查询盘点记录
     * 
     * @param inventoryCheckId 盘点记录主键
     * @return 盘点记录
     */
    @Override
    public WmsInventoryCheck selectWmsInventoryCheckByInventoryCheckId(Long inventoryCheckId)
    {
        return wmsInventoryCheckMapper.selectWmsInventoryCheckByInventoryCheckId(inventoryCheckId);
    }

    /**
     * 查询盘点记录列表
     * 
     * @param wmsInventoryCheck 盘点记录
     * @return 盘点记录
     */
    @Override
    public List<WmsInventoryCheck> selectWmsInventoryCheckList(WmsInventoryCheck wmsInventoryCheck)
    {
        return wmsInventoryCheckMapper.selectWmsInventoryCheckList(wmsInventoryCheck);
    }

    /**
     * 新增盘点记录
     * 
     * @param wmsInventoryCheck 盘点记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsInventoryCheck(WmsInventoryCheck wmsInventoryCheck)
    {
        int rows = wmsInventoryCheckMapper.insertWmsInventoryCheck(wmsInventoryCheck);
        insertWmsInventoryCheckDetail(wmsInventoryCheck);
        return rows;
    }

    /**
     * 修改盘点记录
     * 
     * @param wmsInventoryCheck 盘点记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsInventoryCheck(WmsInventoryCheck wmsInventoryCheck)
    {
        wmsInventoryCheckMapper.deleteWmsInventoryCheckDetailByInventoryCheckId(wmsInventoryCheck.getInventoryCheckId());
        insertWmsInventoryCheckDetail(wmsInventoryCheck);
        return wmsInventoryCheckMapper.updateWmsInventoryCheck(wmsInventoryCheck);
    }

    /**
     * 批量删除盘点记录
     * 
     * @param inventoryCheckIds 需要删除的盘点记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsInventoryCheckByInventoryCheckIds(Long[] inventoryCheckIds)
    {
        wmsInventoryCheckMapper.deleteWmsInventoryCheckDetailByInventoryCheckIds(inventoryCheckIds);
        return wmsInventoryCheckMapper.deleteWmsInventoryCheckByInventoryCheckIds(inventoryCheckIds);
    }

    /**
     * 删除盘点记录信息
     * 
     * @param inventoryCheckId 盘点记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsInventoryCheckByInventoryCheckId(Long inventoryCheckId)
    {
        wmsInventoryCheckMapper.deleteWmsInventoryCheckDetailByInventoryCheckId(inventoryCheckId);
        return wmsInventoryCheckMapper.deleteWmsInventoryCheckByInventoryCheckId(inventoryCheckId);
    }

    /**
     * 新增盘点记录明细信息
     * 
     * @param wmsInventoryCheck 盘点记录对象
     */
    public void insertWmsInventoryCheckDetail(WmsInventoryCheck wmsInventoryCheck)
    {
        List<WmsInventoryCheckDetail> wmsInventoryCheckDetailList = wmsInventoryCheck.getWmsInventoryCheckDetailList();
        Long inventoryCheckId = wmsInventoryCheck.getInventoryCheckId();
        if (StringUtils.isNotNull(wmsInventoryCheckDetailList))
        {
            List<WmsInventoryCheckDetail> list = new ArrayList<WmsInventoryCheckDetail>();
            for (WmsInventoryCheckDetail wmsInventoryCheckDetail : wmsInventoryCheckDetailList)
            {
                wmsInventoryCheckDetail.setInventoryCheckId(inventoryCheckId);
                list.add(wmsInventoryCheckDetail);
            }
            if (list.size() > 0)
            {
                wmsInventoryCheckMapper.batchWmsInventoryCheckDetail(list);
            }
        }
    }
}
