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.BizYakCollarLocation;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenCollarLocationDto;
import com.ruoyi.web.modules.industryservice.mapper.BizYakAssetMapper;
import com.ruoyi.web.modules.industryservice.mapper.BizYakCollarLocationMapper;
import com.ruoyi.web.modules.industryservice.support.FarmingSyncSupport;

/**
 * 单条牦牛定位项圈数据同步事务服务。
 * <p>
 * 第三方无独立主键，按 {@code (yak_no, report_time)} 唯一 upsert 到 {@code biz_yak_collar_location}。
 * </p>
 */
@Service
public class YakCollarLocationSyncTxService
{
    @Autowired
    private BizYakCollarLocationMapper collarLocationMapper;

    @Autowired
    private BizYakAssetMapper yakAssetMapper;

    /**
     * 持久化一条第三方定位项圈记录。
     *
     * @return {@code true}=新增，{@code false}=更新
     */
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public boolean persistCollarLocation(OpenCollarLocationDto dto, Date syncTime)
    {
        if (dto == null || StringUtils.isEmpty(dto.getCattleNo()))
        {
            throw new ServiceException("牦牛编号不能为空");
        }
        Date now = syncTime != null ? syncTime : new Date();
        Date reportTime = FarmingSyncSupport.parseThirdPartyTime(dto.getReportTime(), null);
        if (reportTime == null)
        {
            throw new ServiceException("上报时间不能为空");
        }
        String yakNo = dto.getCattleNo().trim();

        BizYakCollarLocation row = new BizYakCollarLocation();
        row.setYakNo(yakNo);
        row.setYakAssetId(FarmingSyncSupport.resolveYakAssetId(yakAssetMapper, null, null, yakNo));
        row.setLongitude(dto.getLongitude());
        row.setLatitude(dto.getLatitude());
        row.setReportTime(reportTime);
        row.setDataSource(FarmingSyncSupport.DATA_SOURCE_THIRD_PARTY);
        row.setLastSyncTime(syncTime);
        row.setDelFlag(PastureRules.DEL_FLAG_NORMAL);
        row.setUpdateBy(FarmingSyncSupport.SYNC_BY);

        BizYakCollarLocation existing = collarLocationMapper.selectByYakNoAndReportTime(yakNo, reportTime);
        if (existing == null)
        {
            row.setCreateBy(FarmingSyncSupport.SYNC_BY);
            row.setCreateTime(now);
            collarLocationMapper.insertBizYakCollarLocation(row);
            return true;
        }
        row.setId(existing.getId());
        row.setCreateTime(existing.getCreateTime());
        row.setUpdateTime(now);
        collarLocationMapper.updateBizYakCollarLocationFromSync(row);
        return false;
    }
}
