package com.ruoyi.web.modules.prosperity.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.prosperity.domain.BizCommonProsperityAchievement;
import com.ruoyi.web.modules.prosperity.mapper.BizCommonProsperityAchievementMapper;
import com.ruoyi.web.modules.prosperity.service.IBizCommonProsperityAchievementService;
import com.ruoyi.web.modules.prosperity.support.CommonProsperityAchievementValidation;

/**
 * 共同富裕成果服务实现。
 */
@Service
public class BizCommonProsperityAchievementServiceImpl implements IBizCommonProsperityAchievementService
{
    @Autowired
    private BizCommonProsperityAchievementMapper achievementMapper;

    @Override
    public BizCommonProsperityAchievement selectBizCommonProsperityAchievementById(Long id)
    {
        return achievementMapper.selectBizCommonProsperityAchievementById(id);
    }

    @Override
    public List<BizCommonProsperityAchievement> selectBizCommonProsperityAchievementList(
            BizCommonProsperityAchievement query)
    {
        return achievementMapper.selectBizCommonProsperityAchievementList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizCommonProsperityAchievement(BizCommonProsperityAchievement row)
    {
        CommonProsperityAchievementValidation.validateRowForSave(row);
        assertStatPeriodUnique(row.getStatPeriod(), null);
        return achievementMapper.insertBizCommonProsperityAchievement(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizCommonProsperityAchievement(BizCommonProsperityAchievement row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        requireRow(row.getId());
        CommonProsperityAchievementValidation.validateRowForSave(row);
        assertStatPeriodUnique(row.getStatPeriod(), row.getId());
        return achievementMapper.updateBizCommonProsperityAchievement(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizCommonProsperityAchievementByIds(Long[] ids)
    {
        if (ids == null || ids.length == 0)
        {
            return 0;
        }
        List<Long> cleaned = new ArrayList<>();
        for (Long id : ids)
        {
            if (id != null)
            {
                cleaned.add(id);
            }
        }
        if (cleaned.isEmpty())
        {
            return 0;
        }
        return achievementMapper.deleteBizCommonProsperityAchievementByIds(cleaned.toArray(new Long[0]));
    }

    private void assertStatPeriodUnique(String statPeriod, Long excludeId)
    {
        if (achievementMapper.countByStatPeriod(statPeriod, excludeId) > 0)
        {
            throw new ServiceException("该月份成果已存在");
        }
    }

    private BizCommonProsperityAchievement requireRow(Long id)
    {
        BizCommonProsperityAchievement row = achievementMapper.selectBizCommonProsperityAchievementById(id);
        if (row == null)
        {
            throw new ServiceException("记录不存在");
        }
        return row;
    }
}
