package com.zbf.system.service.impl;

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

import com.zbf.common.annotation.DataSource;
import com.zbf.common.enums.DataSourceType;
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.TRkWareNoticeBak;
import com.zbf.system.domain.TRkWareNoticeTabBak;
import com.zbf.system.mapper.TRkWareNoticeBakMapper;
import com.zbf.system.service.ITRkWareNoticeBakService;

/**
 * 入库通知单历史表Service业务层处理
 *
 * @author tony
 * @date 2024-06-18
 */
@Service
public class TRkWareNoticeBakServiceImpl extends ServiceImpl<TRkWareNoticeBakMapper, TRkWareNoticeBak> implements ITRkWareNoticeBakService //修改
{
    @Autowired
    private TRkWareNoticeBakMapper tRkWareNoticeBakMapper;

    /**
     * 查询入库通知单历史表
     *
     * @param id 入库通知单历史表主键
     * @return 入库通知单历史表
     */
    @Override
    public TRkWareNoticeBak selectTRkWareNoticeBakById(String id)
    {
        return tRkWareNoticeBakMapper.selectTRkWareNoticeBakById(id);
    }

    /**
     * 查询入库通知单历史表列表
     *
     * @param tRkWareNoticeBak 入库通知单历史表
     * @return 入库通知单历史表
     */
    @Override
    @DataSource(DataSourceType.MASTER)
    public List<TRkWareNoticeBak> selectTRkWareNoticeBakList(TRkWareNoticeBak tRkWareNoticeBak)
    {
        return tRkWareNoticeBakMapper.selectTRkWareNoticeBakList(tRkWareNoticeBak);
    }

    /**
     * 新增入库通知单历史表
     *
     * @param tRkWareNoticeBak 入库通知单历史表
     * @return 结果
     */
        @Transactional
    @Override
    public int insertTRkWareNoticeBak(TRkWareNoticeBak tRkWareNoticeBak)
    {
        tRkWareNoticeBak.setCreateTime(DateUtils.getNowDate());
        int rows = tRkWareNoticeBakMapper.insertTRkWareNoticeBak(tRkWareNoticeBak);
        insertTRkWareNoticeTabBak(tRkWareNoticeBak);
        return rows;
    }

    /**
     * 修改入库通知单历史表
     *
     * @param tRkWareNoticeBak 入库通知单历史表
     * @return 结果
     */
        @Transactional
    @Override
    public int updateTRkWareNoticeBak(TRkWareNoticeBak tRkWareNoticeBak)
    {
                tRkWareNoticeBak.setUpdateTime(DateUtils.getNowDate());
        tRkWareNoticeBakMapper.deleteTRkWareNoticeTabBakByNoticeId(tRkWareNoticeBak.getId());
        insertTRkWareNoticeTabBak(tRkWareNoticeBak);
        return tRkWareNoticeBakMapper.updateTRkWareNoticeBak(tRkWareNoticeBak);
    }

    /**
     * 批量删除入库通知单历史表
     *
     * @param ids 需要删除的入库通知单历史表主键
     * @return 结果
     */
        @Transactional
    @Override
    public int deleteTRkWareNoticeBakByIds(String[] ids)
    {
        tRkWareNoticeBakMapper.deleteTRkWareNoticeTabBakByNoticeIds(ids);
        return tRkWareNoticeBakMapper.deleteTRkWareNoticeBakByIds(ids);
    }

    /**
     * 删除入库通知单历史表信息
     *
     * @param id 入库通知单历史表主键
     * @return 结果
     */
        @Transactional
    @Override
    public int deleteTRkWareNoticeBakById(String id)
    {
        tRkWareNoticeBakMapper.deleteTRkWareNoticeTabBakByNoticeId(id);
        return tRkWareNoticeBakMapper.deleteTRkWareNoticeBakById(id);
    }


    /**
     * 批量禁用
     *
     * @param id 入库通知单历史表主键
     * @return 结果
     */
        @Transactional
    @Override
    public int disabledTRkWareNoticeBakByIds(String[] ids) {
        return tRkWareNoticeBakMapper.disabledTRkWareNoticeBakByIds(ids);
    }

    /**
     * 批量启用
     *
     * @param id 入库通知单历史表主键
     * @return 结果
     */
        @Transactional
    @Override
    public int enableTRkWareNoticeBakByIds(String[] ids) {
        return tRkWareNoticeBakMapper.enableTRkWareNoticeBakByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tRkWareNoticeBak 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TRkWareNoticeBak tRkWareNoticeBak) {
        return tRkWareNoticeBakMapper.updateTRkWareNoticeBak(tRkWareNoticeBak);
    }


        /**
         * 新增入库通知单明细历史表信息
         *
         * @param tRkWareNoticeBak 入库通知单历史表对象
         */
        public void insertTRkWareNoticeTabBak(TRkWareNoticeBak tRkWareNoticeBak)
        {
            List<TRkWareNoticeTabBak> tRkWareNoticeTabBakList = tRkWareNoticeBak.getTRkWareNoticeTabBakList();
            String id = tRkWareNoticeBak.getId();
            if (StringUtils.isNotNull(tRkWareNoticeTabBakList))
            {
                List<TRkWareNoticeTabBak> list = new ArrayList<TRkWareNoticeTabBak>();
                for (TRkWareNoticeTabBak tRkWareNoticeTabBak : tRkWareNoticeTabBakList)
                {
                    tRkWareNoticeTabBak.setNoticeId(id);
                    list.add(tRkWareNoticeTabBak);
                }
                if (list.size() > 0)
                {
                        tRkWareNoticeBakMapper.batchTRkWareNoticeTabBak(list);
                }
            }
        }
}
