package com.zg.project.information.service.impl;

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

import com.zg.common.exception.ServiceException;
import com.zg.common.utils.DateUtils;
import com.zg.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zg.project.information.mapper.DeliveryMtdMapper;
import com.zg.project.information.domain.DeliveryMtd;
import com.zg.project.information.service.IDeliveryMtdService;

/**
 * 配送系统物料字典Service业务层处理
 * 
 * @author zg
 * @date 2025-10-24
 */
@Service
public class DeliveryMtdServiceImpl implements IDeliveryMtdService 
{
    @Autowired
    private DeliveryMtdMapper deliveryMtdMapper;

    /**
     * 查询配送系统物料字典
     * 
     * @param Id 配送系统物料字典主键
     * @return 配送系统物料字典
     */
    @Override
    public DeliveryMtd selectDeliveryMtdById(Long Id)
    {
        return deliveryMtdMapper.selectDeliveryMtdById(Id);
    }

    /**
     * 查询配送系统物料字典列表
     * 
     * @param deliveryMtd 配送系统物料字典
     * @return 配送系统物料字典
     */
    @Override
    public List<DeliveryMtd> selectDeliveryMtdList(DeliveryMtd deliveryMtd)
    {
        return deliveryMtdMapper.selectDeliveryMtdList(deliveryMtd);
    }

    /**
     * 新增配送系统物料字典
     * 
     * @param deliveryMtd 配送系统物料字典
     * @return 结果
     */
    @Override
    public int insertDeliveryMtd(DeliveryMtd deliveryMtd)
    {
        deliveryMtd.setCreateTime(DateUtils.getNowDate());
        return deliveryMtdMapper.insertDeliveryMtd(deliveryMtd);
    }

    /**
     * 修改配送系统物料字典
     * 
     * @param deliveryMtd 配送系统物料字典
     * @return 结果
     */
    @Override
    public int updateDeliveryMtd(DeliveryMtd deliveryMtd)
    {
        deliveryMtd.setUpdateTime(DateUtils.getNowDate());
        return deliveryMtdMapper.updateDeliveryMtd(deliveryMtd);
    }

    /**
     * 批量删除配送系统物料字典
     * 
     * @param Ids 需要删除的配送系统物料字典主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryMtdByIds(Long[] Ids)
    {
        return deliveryMtdMapper.deleteDeliveryMtdByIds(Ids);
    }

    /**
     * 删除配送系统物料字典信息
     * 
     * @param Id 配送系统物料字典主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryMtdById(Long Id)
    {
        return deliveryMtdMapper.deleteDeliveryMtdById(Id);
    }

    @Override
    public int importDeliveryMtd(List<DeliveryMtd> list, String operator)
    {
        if (list == null || list.isEmpty()) {
            throw new ServiceException("导入数据为空");
        }
        // 清洗与校验
        List<DeliveryMtd> clean = new ArrayList<>(list.size());
        for (DeliveryMtd m : list) {
            if (m == null) continue;
            if (StringUtils.isEmpty(m.getWlNo())) {
                // wlNo 必填，缺失则跳过
                continue;
            }
            m.setWlNo(m.getWlNo().trim());
            if (StringUtils.isEmpty(m.getIsDelete())) m.setIsDelete("0");

            // 写入操作人与时间
            m.setUpdateBy(operator);
            m.setUpdateTime(DateUtils.getNowDate());
            if (m.getCreateBy() == null) m.setCreateBy(operator);
            if (m.getCreateTime() == null) m.setCreateTime(DateUtils.getNowDate());

            clean.add(m);
        }
        if (clean.isEmpty()) {
            throw new ServiceException("有效导入数据为空（缺少 wlNo）");
        }
        // 批量 UPSERT（依赖 delivery_mtd.wl_no 唯一索引）
        return deliveryMtdMapper.batchUpsertDeliveryMtd(clean);
    }
}
