# SvaAbnormalThresholdServiceImpl.java

    package cloud.wconnect.workperformance.service.sva.scheduleactual.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.workperformance.entity.sva.SvaAbnormalThreshold;
    import cloud.wconnect.workperformance.mapper.sva.SvaAbnormalThresholdMapper;
    import cloud.wconnect.workperformance.pojo.dto.sva.abnormalthreshold.SvaAbnormalThresholdDTO;
    import cloud.wconnect.workperformance.service.sva.scheduleactual.ISvaAbnormalThresholdService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.Objects;

    /**
     * <p>
     * SVA功能异常阈值表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2025-10-24
     */
    @Service
    @DS("work_performance")
    public class SvaAbnormalThresholdServiceImpl extends ServiceImpl<SvaAbnormalThresholdMapper, SvaAbnormalThreshold> implements ISvaAbnormalThresholdService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 根据资产id获取SVA功能异常阈值
         *
         * @param propertyId 资产id
         * @return cloud.wconnect.workperformance.pojo.dto.sva.abnormalthreshold.SvaAbnormalThresholdDTO
         * @author lichen
         * @date 2025/10/24
         */
        @Override
        public SvaAbnormalThresholdDTO getByPropertyId(int propertyId) {
            SvaAbnormalThreshold svaAbnormalThreshold = getOne(new LambdaUpdateWrapper<SvaAbnormalThreshold>().eq(SvaAbnormalThreshold::getPropertyId, propertyId));
            if(Objects.nonNull(svaAbnormalThreshold)) {
                return beanMapper.map(svaAbnormalThreshold, SvaAbnormalThresholdDTO.class);
            }
            return new SvaAbnormalThresholdDTO();
        }

        /**
         * 保存SVA功能异常阈值
         *
         * @param tenantId                租户id
         * @param propertyId              资产id
         * @param svaAbnormalThresholdDTO SVA功能异常阈值DTO
         * @author lichen
         * @date 2025/10/24
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(Integer tenantId, int propertyId, SvaAbnormalThresholdDTO svaAbnormalThresholdDTO) {
            SvaAbnormalThreshold svaAbnormalThreshold = getOne(new LambdaUpdateWrapper<SvaAbnormalThreshold>().eq(SvaAbnormalThreshold::getPropertyId, propertyId));
            if(Objects.isNull(svaAbnormalThreshold)){
                svaAbnormalThreshold = new SvaAbnormalThreshold();
                svaAbnormalThreshold.setTenantId(tenantId);
                svaAbnormalThreshold.setPropertyId(propertyId);
                svaAbnormalThreshold.setSalesDiffAbnormalThreshold(svaAbnormalThresholdDTO.getSalesDiffAbnormalThreshold());
                svaAbnormalThreshold.setWorkHourDiffAbnormalThreshold(svaAbnormalThresholdDTO.getWorkHourDiffAbnormalThreshold());
                //新增
                save(svaAbnormalThreshold);
            }else{
                LambdaUpdateWrapper<SvaAbnormalThreshold> updateWrapper = new LambdaUpdateWrapper<SvaAbnormalThreshold>()
                        .eq(SvaAbnormalThreshold::getId, svaAbnormalThreshold.getId())
                        .set(SvaAbnormalThreshold::getSalesDiffAbnormalThreshold, svaAbnormalThresholdDTO.getSalesDiffAbnormalThreshold())
                        .set(SvaAbnormalThreshold::getWorkHourDiffAbnormalThreshold, svaAbnormalThresholdDTO.getWorkHourDiffAbnormalThreshold());
                //修改
                update(updateWrapper);
            }
        }
    }