package com.zhyc.module.dairyProducts.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.zhyc.common.exception.ServiceException;
import com.zhyc.module.dairyProducts.domain.XzWegihCorrection;
import com.zhyc.module.dairyProducts.mapper.XzWegihCorrectionMapper;
import com.zhyc.module.dairyProducts.service.IXzWegihCorrectionService;
import com.zhyc.common.annotation.DataScope; // 引入数据权限注解
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 称重校正Service业务层处理
 * * @author ruoyi
 * @date 2025-07-12
 */
@Service
public class XzWegihCorrectionServiceImpl implements IXzWegihCorrectionService
{
    @Autowired
    private XzWegihCorrectionMapper xzWegihCorrectionMapper;

    /**
     * 查询称重校正
     * * @param id 称重校正主键
     * @return 称重校正
     */
    @Override
    public XzWegihCorrection selectXzWegihCorrectionById(Long id)
    {
        return xzWegihCorrectionMapper.selectXzWegihCorrectionById(id);
    }

    /**
     * 查询称重校正列表
     * * @param xzWegihCorrection 称重校正
     * @return 称重校正
     */
    @Override
    @DataScope(deptAlias = "w", userAlias = "w") // 添加数据权限注解
    public List<XzWegihCorrection> selectXzWegihCorrectionList(XzWegihCorrection xzWegihCorrection)
    {
        return xzWegihCorrectionMapper.selectXzWegihCorrectionList(xzWegihCorrection);
    }

    /**
     * 检查是否已存在相同日期和厂区的记录
     * @param datetime 日期
     * @param factory 厂区
     * @param excludeId 需要排除的ID（用于更新操作时排除自身）
     * @return 如果存在返回true，否则返回false
     */
    private boolean existsSameDateAndFactory(Date datetime, String factory, Long excludeId) {
        // 创建一个查询条件对象
        XzWegihCorrection query = new XzWegihCorrection();
        query.setDatetime(datetime);
        query.setFactory(factory);

        // 查询符合条件的记录
        List<XzWegihCorrection> existingRecords = xzWegihCorrectionMapper.selectXzWegihCorrectionList(query);

        // 如果有需要排除的ID（更新操作），则过滤掉自身
        if (excludeId != null) {
            existingRecords = existingRecords.stream()
                    .filter(record -> !record.getId().equals(excludeId))
                    .collect(Collectors.toList());
        }

        // 如果找到记录，返回true
        return !existingRecords.isEmpty();
    }

    /**
     * 新增称重校正
     */
    @Override
    public int insertXzWegihCorrection(XzWegihCorrection xzWegihCorrection)
    {
        // 新增前检查是否已存在相同日期和厂区的记录
        if (existsSameDateAndFactory(xzWegihCorrection.getDatetime(), xzWegihCorrection.getFactory(), null)) {
            // 抛出异常，提示用户已存在相同记录
            throw new ServiceException("已存在相同日期和厂区的记录，请勿重复添加");
        }

        return xzWegihCorrectionMapper.insertXzWegihCorrection(xzWegihCorrection);
    }

    /**
     * 修改称重校正
     */
    @Override
    public int updateXzWegihCorrection(XzWegihCorrection xzWegihCorrection)
    {
        // 更新前检查是否已存在相同日期和厂区的记录（排除自身）
        if (existsSameDateAndFactory(xzWegihCorrection.getDatetime(), xzWegihCorrection.getFactory(), xzWegihCorrection.getId())) {
            // 抛出异常，提示用户已存在相同记录
            throw new ServiceException("已存在相同日期和厂区的记录，无法修改");
        }

        return xzWegihCorrectionMapper.updateXzWegihCorrection(xzWegihCorrection);
    }

    /**
     * 批量删除称重校正
     * * @param ids 需要删除的称重校正主键
     * @return 结果
     */
    @Override
    public int deleteXzWegihCorrectionByIds(Long[] ids)
    {
        return xzWegihCorrectionMapper.deleteXzWegihCorrectionByIds(ids);
    }

    /**
     * 删除称重校正信息
     * * @param id 称重校正主键
     * @return 结果
     */
    @Override
    public int deleteXzWegihCorrectionById(Long id)
    {
        return xzWegihCorrectionMapper.deleteXzWegihCorrectionById(id);
    }
}