package com.ningdatech.carapi.safe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.carapi.common.aop.BuildChildrenRegionWrapper;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.safe.entity.NdSecurityAdvisory;
import com.ningdatech.carapi.safe.mapper.NdSecurityAdvisoryMapper;
import com.ningdatech.carapi.safe.model.dto.SecurityAdvisoryPageQuery;
import com.ningdatech.carapi.safe.service.INdSecurityAdvisoryService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-10-12
 */
@Service
public class NdSecurityAdvisoryServiceImpl extends ServiceImpl<NdSecurityAdvisoryMapper, NdSecurityAdvisory>
        implements INdSecurityAdvisoryService {

    @Override
    public boolean incrementDownCount(Long id) {
        return baseMapper.incrementDownCount(id) > 0;
    }

    @Override
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    @LambdaDataScope
    @BuildChildrenRegionWrapper
    public List<NdSecurityAdvisory> dataScopeList(LambdaQueryWrapper<NdSecurityAdvisory> wrapper, SecurityAdvisoryPageQuery securityAdvisoryPageQuery) {
        wrapper.eq(Objects.nonNull(securityAdvisoryPageQuery.getAdvisoryDate()),NdSecurityAdvisory::getAdvisoryDate,securityAdvisoryPageQuery.getAdvisoryDate());
        wrapper.eq(Objects.nonNull(securityAdvisoryPageQuery.getRegionId()),NdSecurityAdvisory::getRegionId,securityAdvisoryPageQuery.getRegionId());
        wrapper.orderBy(Boolean.TRUE,Boolean.FALSE,NdSecurityAdvisory::getUpdateOn);
        return baseMapper.selectList(wrapper);
    }
}
