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

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhyc.module.dairyProducts.mapper.XzDryMatterCorrectionMapper;
import com.zhyc.module.dairyProducts.domain.XzDryMatterCorrection;
import com.zhyc.module.dairyProducts.service.IXzDryMatterCorrectionService;
import com.zhyc.common.annotation.DataScope; // 引入数据权限注解

/**
 * 干物质校正Service业务层处理
 * * @author ruoyi
 * @date 2025-07-12
 */
@Service
public class XzDryMatterCorrectionServiceImpl implements IXzDryMatterCorrectionService
{
    @Autowired
    private XzDryMatterCorrectionMapper xzDryMatterCorrectionMapper;

    /**
     * 查询干物质校正
     * * @param id 干物质校正主键
     * @return 干物质校正
     */
    @Override
    public XzDryMatterCorrection selectXzDryMatterCorrectionById(Long id)
    {
        return xzDryMatterCorrectionMapper.selectXzDryMatterCorrectionById(id);
    }

    /**
     * 查询干物质校正列表
     * * @param xzDryMatterCorrection 干物质校正
     * @return 干物质校正
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "d") // 添加数据权限注解
    public List<XzDryMatterCorrection> selectXzDryMatterCorrectionList(XzDryMatterCorrection xzDryMatterCorrection)
    {
        return xzDryMatterCorrectionMapper.selectXzDryMatterCorrectionList(xzDryMatterCorrection);
    }

    /**
     * 新增干物质校正 - 添加默认值和重复校验
     */
    @Override
    public int insertXzDryMatterCorrection(XzDryMatterCorrection xzDryMatterCorrection)
    {
        // 设置干物质标准默认值为18（如果未提供）
        if (xzDryMatterCorrection.getStandard() == null) {
            xzDryMatterCorrection.setStandard(18.0);
        }

        // 检查同年月同厂区是否已存在
        if (isDuplicateRecord(xzDryMatterCorrection)) {
            throw new RuntimeException("该厂区在同一年月已存在记录，不能重复添加");
        }

        return xzDryMatterCorrectionMapper.insertXzDryMatterCorrection(xzDryMatterCorrection);
    }

    /**
     * 修改干物质校正 - 添加重复校验
     */
    @Override
    public int updateXzDryMatterCorrection(XzDryMatterCorrection xzDryMatterCorrection)
    {
        // 检查同年月同厂区是否已存在（排除当前记录）
        if (isDuplicateRecord(xzDryMatterCorrection)) {
            throw new RuntimeException("该厂区在同一年月已存在记录，不能重复添加");
        }

        return xzDryMatterCorrectionMapper.updateXzDryMatterCorrection(xzDryMatterCorrection);
    }


    /**
     * 检查是否存在重复记录（同年月同厂区）
     */
    private boolean isDuplicateRecord(XzDryMatterCorrection xzDryMatterCorrection) {
        // 查询相同年月和厂区的记录
        XzDryMatterCorrection query = new XzDryMatterCorrection();
        query.setDatetime(xzDryMatterCorrection.getDatetime());
        query.setFactory(xzDryMatterCorrection.getFactory());

        List<XzDryMatterCorrection> existingRecords = xzDryMatterCorrectionMapper.selectXzDryMatterCorrectionList(query);

        // 如果是更新操作，需要排除当前记录
        if (xzDryMatterCorrection.getId() != null) {
            return existingRecords.stream()
                    .anyMatch(record ->
                            !record.getId().equals(xzDryMatterCorrection.getId())
                    );
        }

        // 如果是新增操作，只要存在记录就返回true
        return !existingRecords.isEmpty();
    }

    /**
     * 批量删除干物质校正
     * * @param ids 需要删除的干物质校正主键
     * @return 结果
     */
    @Override
    public int deleteXzDryMatterCorrectionByIds(Long[] ids)
    {
        return xzDryMatterCorrectionMapper.deleteXzDryMatterCorrectionByIds(ids);
    }

    /**
     * 删除干物质校正信息
     * * @param id 干物质校正主键
     * @return 结果
     */
    @Override
    public int deleteXzDryMatterCorrectionById(Long id)
    {
        return xzDryMatterCorrectionMapper.deleteXzDryMatterCorrectionById(id);
    }
}