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

import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.modules.industryservice.domain.BizPasture;
import com.ruoyi.web.modules.industryservice.domain.BizYakDiseaseWarning;
import com.ruoyi.web.modules.industryservice.domain.dto.YakDiseaseWarningDto;
import com.ruoyi.web.modules.industryservice.mapper.BizPastureMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizYakDiseaseWarningMapper;
import com.ruoyi.web.modules.industryservice.support.YakDiseaseWarningRules;
import com.ruoyi.web.modules.industryservice.support.YakDiseaseWarningValidation;

@Service
public class YakDiseaseWarningSyncTxService
{
    @Autowired
    private BizYakDiseaseWarningMapper warningMapper;

    @Autowired
    private BizPastureMapper bizPastureMapper;

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public boolean persistAlert(YakDiseaseWarningDto dto, Date syncTime)
    {
        if (dto == null)
        {
            throw new ServiceException("预警数据无效");
        }
        if (dto.getAlertTime() == null)
        {
            throw new ServiceException("预警时间不能为空");
        }
        if (dto.getWarningType() != null && !YakDiseaseWarningRules.isValidWarningType(dto.getWarningType()))
        {
            throw new ServiceException("预警类型无效");
        }
        BizYakDiseaseWarning row = new BizYakDiseaseWarning();
        row.setAlertTime(dto.getAlertTime());
        row.setWarningType(dto.getWarningType());
        row.setMeasuredValue(StringUtils.trim(dto.getMeasuredValue()));
        row.setReferenceRange(StringUtils.trim(dto.getReferenceRange()));
        row.setDeviceNo(StringUtils.trim(dto.getDeviceNo()));
        row.setCollectTime(dto.getCollectTime());
        row.setYakAvatarUrl(StringUtils.trim(dto.getYakAvatarUrl()));
        row.setAlertMessage(YakDiseaseWarningValidation.buildAlertMessage(dto.getWarningType(), row.getMeasuredValue(),
                row.getReferenceRange(), dto.getAlertMessage()));
        row.setDataSource(YakDiseaseWarningRules.DATA_SOURCE_LOCAL);
        row.setLastSyncTime(syncTime);
        resolvePasture(row, dto.getPastureName());

        Date now = syncTime != null ? syncTime : new Date();
        row.setCreateTime(now);
        row.setUpdateTime(now);
        warningMapper.insertBizYakDiseaseWarning(row);
        return true;
    }

    private void resolvePasture(BizYakDiseaseWarning row, String pastureName)
    {
        String name = StringUtils.trim(pastureName);
        if (StringUtils.isEmpty(name))
        {
            row.setPastureName("未关联");
            return;
        }
        BizPasture pasture = bizPastureMapper.selectBizPastureByExactName(name);
        if (pasture != null)
        {
            row.setPastureId(pasture.getId());
            row.setPastureName(pasture.getPastureName());
        }
        else
        {
            row.setPastureId(null);
            row.setPastureName(name);
        }
    }
}
