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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.BizThreeAssetsProblemReport;
import com.ruoyi.web.modules.industryservice.mapper.BizThreeAssetsProblemReportMapper;
import com.ruoyi.web.modules.industryservice.service.IBizThreeAssetsProblemReportService;
import com.ruoyi.web.modules.industryservice.support.ReportDeptDataScopeSupport;
import com.ruoyi.web.modules.industryservice.support.ThreeAssetsProblemReportValidation;
import com.ruoyi.web.modules.industryservice.support.TownDeptSupport;

/**
 * 三资具体问题上报服务实现。
 */
@Service
public class BizThreeAssetsProblemReportServiceImpl implements IBizThreeAssetsProblemReportService
{
    @Autowired
    private BizThreeAssetsProblemReportMapper mapper;

    @Autowired
    private TownDeptSupport townDeptSupport;

    @Override
    public BizThreeAssetsProblemReport selectBizThreeAssetsProblemReportById(Long id)
    {
        return mapper.selectBizThreeAssetsProblemReportById(id);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizThreeAssetsProblemReport(BizThreeAssetsProblemReport row)
    {
        ThreeAssetsProblemReportValidation.validateRowForSave(row, townDeptSupport);
        return mapper.insertBizThreeAssetsProblemReport(row);
    }

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteBizThreeAssetsProblemReportByIds(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.deleteBizThreeAssetsProblemReportByIds(cleaned.toArray(new Long[0]));
    }

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

    @Override
    public List<TreeSelect> listVillageTree()
    {
        return townDeptSupport.buildVillageTree();
    }

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