package com.ruoyi.web.modules.industrypolicy.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.constant.Constants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.modules.industrypolicy.domain.BizSubsidyGrantRecord;
import com.ruoyi.web.modules.industrypolicy.mapper.BizSubsidyGrantRecordMapper;
import com.ruoyi.web.modules.industrypolicy.service.IBizSubsidyGrantRecordService;
import com.ruoyi.web.modules.industrypolicy.support.SubsidyGrantRecordMasking;
import com.ruoyi.web.modules.industrypolicy.support.SubsidyGrantRecordValidation;

/**
 * 补贴发放记录服务实现。
 */
@Service
public class BizSubsidyGrantRecordServiceImpl implements IBizSubsidyGrantRecordService
{
    private static final String PERM_VIEW_FULL = "industryPolicy:subsidyRecord:viewFull";

    private static final String PERM_EDIT = "industryPolicy:subsidyRecord:edit";

    @Autowired
    private BizSubsidyGrantRecordMapper bizSubsidyGrantRecordMapper;

    @Override
    public BizSubsidyGrantRecord selectBizSubsidyGrantRecordById(Long id)
    {
        BizSubsidyGrantRecord row = bizSubsidyGrantRecordMapper.selectBizSubsidyGrantRecordById(id);
        return maskIfNeeded(row);
    }

    @Override
    public List<BizSubsidyGrantRecord> selectBizSubsidyGrantRecordList(BizSubsidyGrantRecord query)
    {
        SubsidyGrantRecordValidation.validateDateRange(query.getStartDate(), query.getEndDate());
        List<BizSubsidyGrantRecord> list = bizSubsidyGrantRecordMapper.selectBizSubsidyGrantRecordList(query);
        boolean showFull = canViewSensitivePlaintext();
        for (BizSubsidyGrantRecord row : list)
        {
            SubsidyGrantRecordMasking.applySensitiveMask(row, showFull);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizSubsidyGrantRecord(BizSubsidyGrantRecord row)
    {
        SubsidyGrantRecordValidation.validateRowForSave(row);
        return bizSubsidyGrantRecordMapper.insertBizSubsidyGrantRecord(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizSubsidyGrantRecord(BizSubsidyGrantRecord row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        BizSubsidyGrantRecord db = requireRow(row.getId());
        SubsidyGrantRecordValidation.mergeSensitiveFieldsForUpdate(row, db);
        SubsidyGrantRecordValidation.validateRowForSave(row);
        return bizSubsidyGrantRecordMapper.updateBizSubsidyGrantRecord(row);
    }

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

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

    private BizSubsidyGrantRecord maskIfNeeded(BizSubsidyGrantRecord row)
    {
        return SubsidyGrantRecordMasking.applySensitiveMask(row, canViewSensitivePlaintext());
    }

    /** 具备查看明文或编辑权限时，详情/编辑接口返回未脱敏敏感字段 */
    private boolean canViewSensitivePlaintext()
    {
        try
        {
            return SecurityUtils.hasPermi(PERM_VIEW_FULL)
                    || SecurityUtils.hasPermi(PERM_EDIT)
                    || SecurityUtils.hasPermi(Constants.ALL_PERMISSION);
        }
        catch (Exception e)
        {
            return false;
        }
    }
}
