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.industrypolicy.support.PastureRules;
import com.ruoyi.web.modules.industryservice.domain.BizYakEarTagRecord;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenEarTagDto;
import com.ruoyi.web.modules.industryservice.mapper.BizYakAssetMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizYakEarTagRecordMapper;
import com.ruoyi.web.modules.industryservice.support.FarmingSyncRules;
import com.ruoyi.web.modules.industryservice.support.FarmingSyncSupport;

/**
 * 单条耳标温度步数数据同步事务服务。
 */
@Service
public class YakEarTagSyncTxService
{
    @Autowired
    private BizYakEarTagRecordMapper earTagRecordMapper;

    @Autowired
    private BizYakAssetMapper yakAssetMapper;

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public boolean persistEarTag(OpenEarTagDto dto, Date syncTime)
    {
        if (dto == null || dto.getId() == null)
        {
            throw new ServiceException(FarmingSyncRules.MSG_EXTERNAL_ID_REQUIRED);
        }
        Date now = syncTime != null ? syncTime : new Date();
        String earTagNumber = StringUtils.isNotEmpty(dto.getEarTagNumber()) ? dto.getEarTagNumber().trim() : null;
        String yakNo = FarmingSyncSupport.firstNonBlank(dto.getEarTagNumber(), dto.getCattleNo());

        BizYakEarTagRecord row = new BizYakEarTagRecord();
        row.setExternalId(dto.getId());
        row.setEarTagNumber(earTagNumber);
        row.setYakNo(yakNo);
        row.setYakAssetId(FarmingSyncSupport.resolveYakAssetId(yakAssetMapper, null,
                dto.getEarTagNumber(), dto.getCattleNo()));
        row.setActualTemperature(FarmingSyncSupport.parseDecimal(dto.getActualTemperature()));
        row.setTemperature(FarmingSyncSupport.parseDecimal(dto.getTemperature()));
        row.setSteps(parseSteps(dto.getSteps()));
        row.setReportTime(FarmingSyncSupport.parseThirdPartyTime(dto.getReportTime(), now));
        row.setDataSource(FarmingSyncSupport.DATA_SOURCE_THIRD_PARTY);
        row.setLastSyncTime(syncTime);
        row.setDelFlag(PastureRules.DEL_FLAG_NORMAL);
        row.setUpdateBy(FarmingSyncSupport.SYNC_BY);

        BizYakEarTagRecord existing = earTagRecordMapper.selectByExternalId(dto.getId());
        if (existing == null)
        {
            row.setCreateBy(FarmingSyncSupport.SYNC_BY);
            row.setCreateTime(now);
            earTagRecordMapper.insertBizYakEarTagRecord(row);
            return true;
        }
        row.setId(existing.getId());
        row.setCreateTime(existing.getCreateTime());
        row.setUpdateTime(now);
        earTagRecordMapper.updateBizYakEarTagRecordFromSync(row);
        return false;
    }

    private static Integer parseSteps(String value)
    {
        if (StringUtils.isEmpty(value))
        {
            return null;
        }
        try
        {
            return (int) Math.round(Double.parseDouble(value.trim()));
        }
        catch (NumberFormatException ex)
        {
            return null;
        }
    }
}
