package com.ruoyi.web.modules.industryservice.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.industryservice.domain.BizRuralThreeAssets;
import com.ruoyi.web.modules.industryservice.mapper.BizRuralThreeAssetsMapper;
import com.ruoyi.web.modules.industryservice.service.IBizRuralThreeAssetsService;
import com.ruoyi.web.modules.industryservice.support.ReportDeptDataScopeSupport;
import com.ruoyi.web.modules.industryservice.support.RuralThreeAssetsValidation;
import com.ruoyi.web.modules.industryservice.support.TownDeptSupport;

/**
 * 农村三资数据填报服务实现。
 * <p>唯一键：村；固定资产保存时派生。</p>
 */
@Service
public class BizRuralThreeAssetsServiceImpl implements IBizRuralThreeAssetsService
{
    @Autowired
    private BizRuralThreeAssetsMapper mapper;

    @Autowired
    private TownDeptSupport townDeptSupport;

    @Override
    public BizRuralThreeAssets selectBizRuralThreeAssetsById(Long id)
    {
        return mapper.selectBizRuralThreeAssetsById(id);
    }

    @Override
    public List<BizRuralThreeAssets> selectBizRuralThreeAssetsList(BizRuralThreeAssets query)
    {
        ReportDeptDataScopeSupport.apply(query, "r");
        return mapper.selectBizRuralThreeAssetsList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizRuralThreeAssets(BizRuralThreeAssets row)
    {
        RuralThreeAssetsValidation.validateRowForSave(row, townDeptSupport);
        assertUniqueVillage(row.getVillageDeptId(), null);
        return mapper.insertBizRuralThreeAssets(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizRuralThreeAssets(BizRuralThreeAssets row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        RuralThreeAssetsValidation.validateRowForSave(row, townDeptSupport);
        requireExistingRow(row.getId());
        assertUniqueVillage(row.getVillageDeptId(), row.getId());
        return mapper.updateBizRuralThreeAssets(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizRuralThreeAssetsByIds(Long[] ids, String updateBy)
    {
        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 mapper.deleteBizRuralThreeAssetsByIds(cleaned.toArray(new Long[0]));
    }

    @Override
    public List<java.util.Map<String, Object>> listTownOptions()
    {
        return townDeptSupport.toOptionList();
    }

    @Override
    public List<com.ruoyi.common.core.domain.TreeSelect> listVillageTree()
    {
        return townDeptSupport.buildVillageTree();
    }

    private void assertUniqueVillage(Long villageDeptId, Long excludeId)
    {
        if (mapper.countByVillageExcludeId(villageDeptId, excludeId) > 0)
        {
            throw new ServiceException("该村已填报");
        }
    }

    private BizRuralThreeAssets requireExistingRow(Long id)
    {
        BizRuralThreeAssets row = mapper.selectBizRuralThreeAssetsById(id);
        if (row == null)
        {
            throw new ServiceException("记录不存在");
        }
        return row;
    }
}
