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.web.modules.industryservice.domain.BizYakHealthSourceRecord;
import com.ruoyi.web.modules.industryservice.mapper.BizYakHealthSourceRecordMapper;

@Service
public class YakHealthSourceRecordSyncTxService
{
    @Autowired
    private BizYakHealthSourceRecordMapper healthSourceRecordMapper;

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public boolean persistSourceRecord(BizYakHealthSourceRecord incoming, Date syncTime)
    {
        incoming.setLastSyncTime(syncTime);
        BizYakHealthSourceRecord existing = healthSourceRecordMapper.selectByExternalId(incoming.getExternalId());
        if (existing == null)
        {
            healthSourceRecordMapper.insertBizYakHealthSourceRecord(incoming);
            return true;
        }
        incoming.setId(existing.getId());
        incoming.setAnalyzedFlag(existing.getAnalyzedFlag());
        healthSourceRecordMapper.updateBizYakHealthSourceRecord(incoming);
        return false;
    }
}
