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.BizGrassland;
import com.ruoyi.web.modules.industryservice.domain.BizGrasslandUsageRecord;
import com.ruoyi.web.modules.industryservice.domain.BizPasture;
import com.ruoyi.web.modules.industryservice.mapper.BizGrasslandMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizGrasslandUsageRecordMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizPastureMapper;
import com.ruoyi.web.modules.industryservice.service.IBizGrasslandUsageRecordService;
import com.ruoyi.web.modules.industryservice.support.GrasslandRules;
import com.ruoyi.web.modules.industryservice.support.GrasslandUsageRules;
import com.ruoyi.web.modules.industryservice.support.GrasslandUsageValidation;
import com.ruoyi.web.modules.industrypolicy.support.PastureRules;

/**
 * 草场使用记录服务实现。
 */
@Service
public class BizGrasslandUsageRecordServiceImpl implements IBizGrasslandUsageRecordService
{
    @Autowired
    private BizGrasslandUsageRecordMapper bizGrasslandUsageRecordMapper;

    @Autowired
    private BizGrasslandMapper bizGrasslandMapper;

    @Autowired
    private BizPastureMapper bizPastureMapper;

    @Override
    public BizGrasslandUsageRecord selectBizGrasslandUsageRecordById(Long id)
    {
        return bizGrasslandUsageRecordMapper.selectBizGrasslandUsageRecordById(id);
    }

    @Override
    public List<BizGrasslandUsageRecord> selectBizGrasslandUsageRecordList(BizGrasslandUsageRecord query)
    {
        GrasslandUsageValidation.validateDateRangeForList(query.getStartDate(), query.getEndDate());
        return bizGrasslandUsageRecordMapper.selectBizGrasslandUsageRecordList(query);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBizGrasslandUsageRecord(BizGrasslandUsageRecord row)
    {
        BizGrassland grassland = requireActiveGrassland(row.getGrasslandId());
        requireActivePasture(row.getPastureId());
        GrasslandUsageValidation.validateRowForSave(row, grassland.getAreaMu());
        row.setDelFlag(GrasslandUsageRules.DEL_FLAG_NORMAL);
        return bizGrasslandUsageRecordMapper.insertBizGrasslandUsageRecord(row);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBizGrasslandUsageRecord(BizGrasslandUsageRecord row)
    {
        if (row.getId() == null)
        {
            throw new ServiceException("主键不能为空");
        }
        requireActiveUsageRecord(row.getId());
        BizGrassland grassland = requireActiveGrassland(row.getGrasslandId());
        requireActivePasture(row.getPastureId());
        GrasslandUsageValidation.validateRowForSave(row, grassland.getAreaMu());
        return bizGrasslandUsageRecordMapper.updateBizGrasslandUsageRecord(row);
    }

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

    private BizGrasslandUsageRecord requireActiveUsageRecord(Long id)
    {
        BizGrasslandUsageRecord row = bizGrasslandUsageRecordMapper.selectBizGrasslandUsageRecordById(id);
        if (row == null)
        {
            throw new ServiceException("记录不存在或已删除");
        }
        return row;
    }

    private BizGrassland requireActiveGrassland(Long grasslandId)
    {
        GrasslandUsageValidation.validateGrasslandId(grasslandId);
        BizGrassland grassland = bizGrasslandMapper.selectBizGrasslandById(grasslandId);
        if (grassland == null || GrasslandRules.DEL_FLAG_DELETED.equals(grassland.getDelFlag()))
        {
            throw new ServiceException("草场不存在或已失效");
        }
        return grassland;
    }

    private BizPasture requireActivePasture(Long pastureId)
    {
        GrasslandUsageValidation.validatePastureId(pastureId);
        BizPasture pasture = bizPastureMapper.selectBizPastureById(pastureId);
        if (pasture == null || PastureRules.DEL_FLAG_DELETED.equals(pasture.getDelFlag()))
        {
            throw new ServiceException("养殖主体不存在或已失效");
        }
        return pasture;
    }
}
