package com.zbf.system.service.impl;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; //添加
import com.zbf.common.utils.DateUtils;
import com.zbf.common.utils.StringUtils;
import com.zbf.system.domain.TRkReceivingGoods;
import com.zbf.system.domain.TRkReceivingGoodsTab;
import com.zbf.system.mapper.TRkReceivingGoodsMapper;
import com.zbf.system.service.ITRkReceivingGoodsService;

/**
 * 收货单Service业务层处理
 *
 * @author tony
 * @date 2024-06-18
 */
@Service
public class TRkReceivingGoodsServiceImpl extends ServiceImpl<TRkReceivingGoodsMapper, TRkReceivingGoods> implements ITRkReceivingGoodsService //修改
{
    @Autowired
    private TRkReceivingGoodsMapper tRkReceivingGoodsMapper;

    /**
     * 查询收货单
     *
     * @param id 收货单主键
     * @return 收货单
     */
    @Override
    public TRkReceivingGoods selectTRkReceivingGoodsById(String id)
    {
        return tRkReceivingGoodsMapper.selectTRkReceivingGoodsById(id);
    }

    /**
     * 查询收货单列表
     *
     * @param tRkReceivingGoods 收货单
     * @return 收货单
     */
    @Override
    public List<TRkReceivingGoods> selectTRkReceivingGoodsList(TRkReceivingGoods tRkReceivingGoods)
    {
        return tRkReceivingGoodsMapper.selectTRkReceivingGoodsList(tRkReceivingGoods);
    }

    /**
     * 新增收货单
     *
     * @param tRkReceivingGoods 收货单
     * @return 结果
     */
        @Transactional
    @Override
    public int insertTRkReceivingGoods(TRkReceivingGoods tRkReceivingGoods)
    {
        tRkReceivingGoods.setCreateTime(DateUtils.getNowDate());
        int rows = tRkReceivingGoodsMapper.insertTRkReceivingGoods(tRkReceivingGoods);
        insertTRkReceivingGoodsTab(tRkReceivingGoods);
        return rows;
    }

    /**
     * 修改收货单
     *
     * @param tRkReceivingGoods 收货单
     * @return 结果
     */
        @Transactional
    @Override
    public int updateTRkReceivingGoods(TRkReceivingGoods tRkReceivingGoods)
    {
                tRkReceivingGoods.setUpdateTime(DateUtils.getNowDate());
        tRkReceivingGoodsMapper.deleteTRkReceivingGoodsTabByNoticeId(tRkReceivingGoods.getId());
        insertTRkReceivingGoodsTab(tRkReceivingGoods);
        return tRkReceivingGoodsMapper.updateTRkReceivingGoods(tRkReceivingGoods);
    }

    /**
     * 批量删除收货单
     *
     * @param ids 需要删除的收货单主键
     * @return 结果
     */
        @Transactional
    @Override
    public int deleteTRkReceivingGoodsByIds(String[] ids)
    {
        tRkReceivingGoodsMapper.deleteTRkReceivingGoodsTabByNoticeIds(ids);
        return tRkReceivingGoodsMapper.deleteTRkReceivingGoodsByIds(ids);
    }

    /**
     * 删除收货单信息
     *
     * @param id 收货单主键
     * @return 结果
     */
        @Transactional
    @Override
    public int deleteTRkReceivingGoodsById(String id)
    {
        tRkReceivingGoodsMapper.deleteTRkReceivingGoodsTabByNoticeId(id);
        return tRkReceivingGoodsMapper.deleteTRkReceivingGoodsById(id);
    }


    /**
     * 批量禁用
     *
     * @param id 收货单主键
     * @return 结果
     */
        @Transactional
    @Override
    public int disabledTRkReceivingGoodsByIds(String[] ids) {
        return tRkReceivingGoodsMapper.disabledTRkReceivingGoodsByIds(ids);
    }

    /**
     * 批量启用
     *
     * @param id 收货单主键
     * @return 结果
     */
        @Transactional
    @Override
    public int enableTRkReceivingGoodsByIds(String[] ids) {
        return tRkReceivingGoodsMapper.enableTRkReceivingGoodsByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tRkReceivingGoods 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TRkReceivingGoods tRkReceivingGoods) {
        return tRkReceivingGoodsMapper.updateTRkReceivingGoods(tRkReceivingGoods);
    }


        /**
         * 新增收货单明细信息
         *
         * @param tRkReceivingGoods 收货单对象
         */
        public void insertTRkReceivingGoodsTab(TRkReceivingGoods tRkReceivingGoods)
        {
            List<TRkReceivingGoodsTab> tRkReceivingGoodsTabList = tRkReceivingGoods.getTRkReceivingGoodsTabList();
            String id = tRkReceivingGoods.getId();
            if (StringUtils.isNotNull(tRkReceivingGoodsTabList))
            {
                List<TRkReceivingGoodsTab> list = new ArrayList<TRkReceivingGoodsTab>();
                for (TRkReceivingGoodsTab tRkReceivingGoodsTab : tRkReceivingGoodsTabList)
                {
                    tRkReceivingGoodsTab.setNoticeId(id);
                    list.add(tRkReceivingGoodsTab);
                }
                if (list.size() > 0)
                {
                        tRkReceivingGoodsMapper.batchTRkReceivingGoodsTab(list);
                }
            }
        }
}
