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

import java.util.List;

import com.zg.common.exception.ServiceException;
import com.zg.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zg.project.wisdom.mapper.GysJhMapper;
import com.zg.project.wisdom.domain.GysJh;
import com.zg.project.wisdom.service.IGysJhService;

/**
 * 供应计划Service业务层处理
 * 
 * @author zg
 * @date 2025-05-28
 */
@Service
public class GysJhServiceImpl implements IGysJhService 
{
    @Autowired
    private GysJhMapper gysJhMapper;

    /**
     * 查询供应计划
     * 
     * @param id 供应计划主键
     * @return 供应计划
     */
    @Override
    public GysJh selectGysJhById(Long id)
    {
        return gysJhMapper.selectGysJhById(id);
    }

    /**
     * 查询供应计划列表
     * 
     * @param gysJh 供应计划
     * @return 供应计划
     */
    @Override
    public List<GysJh> selectGysJhList(GysJh gysJh)
    {
        return gysJhMapper.selectGysJhList(gysJh);
    }

    /**
     * 新增供应计划
     * 
     * @param gysJh 供应计划
     * @return 结果
     */
    @Override
    public int insertGysJh(GysJh gysJh)
    {
        gysJh.setCreateTime(DateUtils.getNowDate());
        return gysJhMapper.insertGysJh(gysJh);
    }

    /**
     * 修改供应计划
     * 
     * @param gysJh 供应计划
     * @return 结果
     */
    @Override
    public int updateGysJh(GysJh gysJh)
    {
        gysJh.setUpdateTime(DateUtils.getNowDate());
        return gysJhMapper.updateGysJh(gysJh);
    }

    /**
     * 批量删除供应计划
     * 
     * @param ids 需要删除的供应计划主键
     * @return 结果
     */
    @Override
    public int deleteGysJhByIds(Long[] ids)
    {
        return gysJhMapper.deleteGysJhByIds(ids);
    }

    /**
     * 删除供应计划信息
     * 
     * @param id 供应计划主键
     * @return 结果
     */
    @Override
    public int deleteGysJhById(Long id)
    {
        return gysJhMapper.deleteGysJhById(id);
    }

    /**
     * 供应计划导入
     * @param jhList
     * @param operName
     * @return
     */
    @Override
    public String importGysJhList(List<GysJh> jhList, String operName) {
        if (jhList == null || jhList.isEmpty()) {
            throw new ServiceException("导入数据不能为空！");
        }

        int successNum = 0;
        int failureNum = 0;
        StringBuilder failureMsg = new StringBuilder();

        for (GysJh jh : jhList) {
            try {
                jh.setCreateBy(operName);
                jh.setStatus("0");
                jh.setIsDelete("0");
                insertGysJh(jh);
                successNum++;
            } catch (Exception e) {
                failureNum++;
                failureMsg.append("<br/>项目号：").append(jh.getXmNo())
                        .append("，物料号：").append(jh.getWlNo())
                        .append(" 导入失败：").append(e.getMessage());
            }
        }

        if (failureNum > 0) {
            failureMsg.insert(0, "导入完成，但有 " + failureNum + " 条记录失败：");
            throw new ServiceException(failureMsg.toString());
        }

        return "导入成功，共 " + successNum + " 条数据";
    }

    /**
     * 根据SAP编号查询
     * @param sapNo
     * @return
     */
    @Override
    public List<GysJh> getBySapNo(String sapNo) {
        return gysJhMapper.getBySapNo(sapNo);
    }
}
