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.annotation.DataScope;
import com.zbf.common.annotation.DataSource;
import com.zbf.common.enums.DataSourceType;
import com.zbf.common.utils.DateUtils;
import com.zbf.common.utils.StringUtils;
import com.zbf.system.domain.TckOrderdetailBak;
import com.zbf.system.domain.TckOrdersBak;
import com.zbf.system.mapper.TckOrdersBakMapper;
import com.zbf.system.service.ITckOrdersBakService;

/**
 * 出库单历史表Service业务层处理
 *
 * @author tony
 * @date 2024-07-03
 */
@Service
public class TckOrdersBakServiceImpl extends ServiceImpl<TckOrdersBakMapper, TckOrdersBak> implements ITckOrdersBakService //修改
{
    @Autowired
    private TckOrdersBakMapper tckOrdersBakMapper;

    /**
     * 查询出库单历史表
     *
     * @param id 出库单历史表主键
     * @return 出库单历史表
     */
    @Override
    public TckOrdersBak selectTckOrdersBakById(String id)
    {
        return tckOrdersBakMapper.selectTckOrdersBakById(id);
    }

    /**
     * 查询出库单历史表列表
     *
     * @param tckOrdersBak 出库单历史表
     * @return 出库单历史表
     */
    @DataSource(value=DataSourceType.MASTER)
    @Override
    public List<TckOrdersBak> selectTckOrdersBakList(TckOrdersBak tckOrdersBak)
    {
        return tckOrdersBakMapper.selectTckOrdersBakList(tckOrdersBak);
    }

    /**
     * 新增出库单历史表
     *
     * @param tckOrdersBak 出库单历史表
     * @return 结果
     */
        @Transactional
    @Override
    public int insertTckOrdersBak(TckOrdersBak tckOrdersBak)
    {
        tckOrdersBak.setCreateTime(DateUtils.getNowDate());
        int rows = tckOrdersBakMapper.insertTckOrdersBak(tckOrdersBak);
        insertTckOrderdetailBak(tckOrdersBak);
        return rows;
    }

    /**
     * 修改出库单历史表
     *
     * @param tckOrdersBak 出库单历史表
     * @return 结果
     */
        @Transactional
    @Override
    public int updateTckOrdersBak(TckOrdersBak tckOrdersBak)
    {
                tckOrdersBak.setUpdateTime(DateUtils.getNowDate());
        tckOrdersBakMapper.deleteTckOrderdetailBakByOrderId(tckOrdersBak.getId());
        insertTckOrderdetailBak(tckOrdersBak);
        return tckOrdersBakMapper.updateTckOrdersBak(tckOrdersBak);
    }

    /**
     * 批量删除出库单历史表
     *
     * @param ids 需要删除的出库单历史表主键
     * @return 结果
     */
        @Transactional
    @Override
    public int deleteTckOrdersBakByIds(String[] ids)
    {
        tckOrdersBakMapper.deleteTckOrderdetailBakByOrderIds(ids);
        return tckOrdersBakMapper.deleteTckOrdersBakByIds(ids);
    }

    /**
     * 删除出库单历史表信息
     *
     * @param id 出库单历史表主键
     * @return 结果
     */
        @Transactional
    @Override
    public int deleteTckOrdersBakById(String id)
    {
        tckOrdersBakMapper.deleteTckOrderdetailBakByOrderId(id);
        return tckOrdersBakMapper.deleteTckOrdersBakById(id);
    }


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

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

    /**
     * 修改状态
     *
     * @param tckOrdersBak 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TckOrdersBak tckOrdersBak) {
        return tckOrdersBakMapper.updateTckOrdersBak(tckOrdersBak);
    }


        /**
         * 新增出库物料明细信息
         *
         * @param tckOrdersBak 出库单历史表对象
         */
        public void insertTckOrderdetailBak(TckOrdersBak tckOrdersBak)
        {
            List<TckOrderdetailBak> tckOrderdetailBakList = tckOrdersBak.getTckOrderdetailBakList();
            String id = tckOrdersBak.getId();
            if (StringUtils.isNotNull(tckOrderdetailBakList))
            {
                List<TckOrderdetailBak> list = new ArrayList<TckOrderdetailBak>();
                for (TckOrderdetailBak tckOrderdetailBak : tckOrderdetailBakList)
                {
                    tckOrderdetailBak.setOrderId(id);
                    list.add(tckOrderdetailBak);
                }
                if (list.size() > 0)
                {
                        tckOrdersBakMapper.batchTckOrderdetailBak(list);
                }
            }
        }

        @Override
        @DataScope(deptAlias = "a")
        public List<TckOrderdetailBak> selectReturnTckOrdersBakList(TckOrderdetailBak tckOrderdetailBak) {
            return tckOrdersBakMapper.selectReturnTckOrdersBakList(tckOrderdetailBak);
        }
}
