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.TckOrderdetailBak;
import com.zbf.system.domain.TckXnOrderdetailBak;
import com.zbf.system.mapper.TckOrderdetailBakMapper;
import com.zbf.system.mapper.TckXnOrderdetailBakMapper;
import com.zbf.system.service.ITckOrderdetailBakService;
import com.zbf.system.service.ITckXnOrderdetailBakService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 出库物料明细Service业务层处理
 *
 * @author tony
 * @date 2024-09-09
 */
@Service
public class TckXnOrderdetailBakServiceImpl extends ServiceImpl<TckXnOrderdetailBakMapper, TckXnOrderdetailBak> implements ITckXnOrderdetailBakService //修改
{
    @Autowired
    private TckXnOrderdetailBakMapper tckOrderdetailBakMapper;

    /**
     * 查询出库物料明细
     *
     * @param id 出库物料明细主键
     * @return 出库物料明细
     */
    @Override
    public TckXnOrderdetailBak selectTckOrderdetailBakById(String id)
    {
        return tckOrderdetailBakMapper.selectTckOrderdetailBakById(id);
    }

    /**
     * 查询出库物料明细列表
     *
     * @param tckOrderdetailBak 出库物料明细
     * @return 出库物料明细
     */
    @Override
    public List<TckXnOrderdetailBak> selectTckOrderdetailBakList(TckXnOrderdetailBak tckOrderdetailBak)
    {
        return tckOrderdetailBakMapper.selectTckOrderdetailBakList(tckOrderdetailBak);
    }

    /**
     * 新增出库物料明细
     *
     * @param tckOrderdetailBak 出库物料明细
     * @return 结果
     */
    @Override
    public int insertTckOrderdetailBak(TckXnOrderdetailBak tckOrderdetailBak)
    {
        tckOrderdetailBak.setCreateTime(DateUtils.getNowDate());
        return tckOrderdetailBakMapper.insertTckOrderdetailBak(tckOrderdetailBak);
    }

    /**
     * 修改出库物料明细
     *
     * @param tckOrderdetailBak 出库物料明细
     * @return 结果
     */
    @Override
    public int updateTckOrderdetailBak(TckXnOrderdetailBak tckOrderdetailBak)
    {
                tckOrderdetailBak.setUpdateTime(DateUtils.getNowDate());
        return tckOrderdetailBakMapper.updateTckOrderdetailBak(tckOrderdetailBak);
    }

    /**
     * 批量删除出库物料明细
     *
     * @param ids 需要删除的出库物料明细主键
     * @return 结果
     */
    @Override
    public int deleteTckOrderdetailBakByIds(String[] ids)
    {
        return tckOrderdetailBakMapper.deleteTckOrderdetailBakByIds(ids);
    }

    /**
     * 删除出库物料明细信息
     *
     * @param id 出库物料明细主键
     * @return 结果
     */
    @Override
    public int deleteTckOrderdetailBakById(String id)
    {
        return tckOrderdetailBakMapper.deleteTckOrderdetailBakById(id);
    }


    /**
     * 批量禁用
     *
     * @param id 出库物料明细主键
     * @return 结果
     */
    @Override
    public int disabledTckOrderdetailBakByIds(String[] ids) {
        return tckOrderdetailBakMapper.disabledTckOrderdetailBakByIds(ids);
    }

    /**
     * 批量启用
     *
     * @param id 出库物料明细主键
     * @return 结果
     */
    @Override
    public int enableTckOrderdetailBakByIds(String[] ids) {
        return tckOrderdetailBakMapper.enableTckOrderdetailBakByIds(ids);
    }

    /**
     * 修改状态
     *
     * @param tckOrderdetailBak 工序管理主键
     * @return 结果
     */
    @Override
    public int updateStatus(TckXnOrderdetailBak tckOrderdetailBak) {
        return tckOrderdetailBakMapper.updateTckOrderdetailBak(tckOrderdetailBak);
    }

    @Override
    public List<TckXnOrderdetailBak> select(TckXnOrderdetailBak tckOrderdetailBak) {
        List<TckXnOrderdetailBak> tckOrderdetailBaks = tckOrderdetailBakMapper.selectTMiStockList(tckOrderdetailBak);
        return tckOrderdetailBaks;
    }

    @Override
    public String importData(List<TckXnOrderdetailBak> 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 (TckXnOrderdetailBak basSku: appList) {
            try {

                //是否存在这个编号
                TckXnOrderdetailBak tDevices = tckOrderdetailBakMapper.selectTckOrderdetailBakById(basSku.getId());
                if (StringUtils.isNull(tDevices)){
                    tckOrderdetailBakMapper.insertTckOrderdetailBak(basSku);
                    successNum++;
                    successMsg.append("<br/>第" + successNum + "行、编号 " + basSku.getId() + " 导入成功");
                }else{
                    basSku.setId(tDevices.getId());
                    tckOrderdetailBakMapper.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();
    }
}
