package com.zbf.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zbf.common.exception.ServiceException;
import com.zbf.common.utils.DateUtils;
import com.zbf.common.utils.StringUtils;
import com.zbf.system.domain.TMiXnStock;
import com.zbf.system.domain.TRkWareNoticeTabBak;
import com.zbf.system.domain.TRkXnWareNoticeTabBak;
import com.zbf.system.mapper.TRkWareNoticeTabBakMapper;
import com.zbf.system.mapper.TRkXnWareNoticeTabBakMapper;
import com.zbf.system.service.ITRkWareNoticeTabBakService;
import com.zbf.system.service.ITRkXnWareNoticeTabBakService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 入库通知单明细备份Service业务层处理
 *
 * @author tony
 * @date 2024-06-14
 */
@Service
public class TRkXnWareNoticeTabBakServiceImpl extends ServiceImpl<TRkXnWareNoticeTabBakMapper, TRkXnWareNoticeTabBak> implements ITRkXnWareNoticeTabBakService //修改
{
    @Autowired
    private TRkXnWareNoticeTabBakMapper tRkWareNoticeTabBakMapper;

    /**
     * 查询入库通知单明细备份
     *
     * @param id 入库通知单明细备份主键
     * @return 入库通知单明细备份
     */
    @Override
    public TRkXnWareNoticeTabBak selectTRkWareNoticeTabBakById(String id)
    {
        return tRkWareNoticeTabBakMapper.selectTRkWareNoticeTabBakById(id);
    }



    /**
     * 查询入库通知单明细备份列表
     *
     * @param tRkWareNoticeTabBak 入库通知单明细备份
     * @return 入库通知单明细备份
     */
    @Override
    public List<TRkXnWareNoticeTabBak> selectTRkWareNoticeTabBakList(TRkXnWareNoticeTabBak tRkWareNoticeTabBak)
    {
        return tRkWareNoticeTabBakMapper.selectTRkWareNoticeTabBakList(tRkWareNoticeTabBak);
    }

    /**
     * 新增入库通知单明细备份
     *
     * @param tRkWareNoticeTabBak 入库通知单明细备份
     * @return 结果
     */
    @Override
    public int insertTRkWareNoticeTabBak(TRkXnWareNoticeTabBak tRkWareNoticeTabBak)
    {
        return tRkWareNoticeTabBakMapper.insertTRkWareNoticeTabBak(tRkWareNoticeTabBak);
    }

    /**
     * 修改入库通知单明细备份
     *
     * @param tRkWareNoticeTabBak 入库通知单明细备份
     * @return 结果
     */
    @Override
    public int updateTRkWareNoticeTabBak(TRkXnWareNoticeTabBak tRkWareNoticeTabBak)
    {
        tRkWareNoticeTabBak.setUpdateTime(DateUtils.getNowDate());
        return tRkWareNoticeTabBakMapper.updateTRkWareNoticeTabBak(tRkWareNoticeTabBak);
    }

    /**
     * 批量删除入库通知单明细备份
     *
     * @param ids 需要删除的入库通知单明细备份主键
     * @return 结果
     */
    @Override
    public int deleteTRkWareNoticeTabBakByIds(String[] ids)
    {
        return tRkWareNoticeTabBakMapper.deleteTRkWareNoticeTabBakByIds(ids);
    }

    /**
     * 删除入库通知单明细备份信息
     *
     * @param id 入库通知单明细备份主键
     * @return 结果
     */
    @Override
    public int deleteTRkWareNoticeTabBakById(String id)
    {
        return tRkWareNoticeTabBakMapper.deleteTRkWareNoticeTabBakById(id);
    }


    /**
     * 批量禁用
     *
     * @param id 入库通知单明细备份主键
     * @return 结果
     */
    @Override
    public int disabledTRkWareNoticeTabBakByIds(String[] ids) {
        return tRkWareNoticeTabBakMapper.disabledTRkWareNoticeTabBakByIds(ids);
    }

    /**
     * 批量启用
     *
     * @param id 入库通知单明细备份主键
     * @return 结果
     */
    @Override
    public int enableTRkWareNoticeTabBakByIds(String[] ids) {
        return tRkWareNoticeTabBakMapper.enableTRkWareNoticeTabBakByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tRkWareNoticeTabBak 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TRkXnWareNoticeTabBak tRkWareNoticeTabBak) {
        return tRkWareNoticeTabBakMapper.updateTRkWareNoticeTabBak(tRkWareNoticeTabBak);
    }

    @Override
    public List<TRkXnWareNoticeTabBak> select( TRkXnWareNoticeTabBak tRkWareNoticeTabBak) {
        List<TRkXnWareNoticeTabBak> tRkWareNoticeTabBaks = tRkWareNoticeTabBakMapper.select(tRkWareNoticeTabBak);
        return tRkWareNoticeTabBaks;
    }

    @Override
    public String importData(List<TRkXnWareNoticeTabBak> appList, String s) {
        if (StringUtils.isNull(appList) || appList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (TRkXnWareNoticeTabBak basSku: appList) {
            try {

                //是否存在这个编号
                TRkXnWareNoticeTabBak tDevices = tRkWareNoticeTabBakMapper.selectById(basSku.getId());
                if (StringUtils.isNull(tDevices)){
                    tRkWareNoticeTabBakMapper.insertTRkWareNoticeTabBak(basSku);
                    successNum++;
                    successMsg.append("<br/>第" + successNum + "行、编号 " + basSku.getId() + " 导入成功");
                }else{
                    basSku.setId(tDevices.getId());
                    tRkWareNoticeTabBakMapper.updateById(basSku);
                    successNum++;
                    successMsg.append("<br/>第" + successNum + "行、编号 " + basSku.getId() + " 更新成功");
                }
            }catch (Exception e){
                failureNum++;
                String msg = "<br/>第" + failureNum + "行、编号 " + basSku.getId() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());

            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}
