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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.carapi.common.aop.BuildChildrenRegionWrapper;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.company.entity.NdCompanyCreditLevel;
import com.ningdatech.carapi.company.entity.dto.CompanyCreditLevelPageParam;
import com.ningdatech.carapi.company.mapper.NdCompanyCreditLevelMapper;
import com.ningdatech.carapi.company.service.INdCompanyCreditLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 信用等级 服务实现类
 * </p>
 *
 * @author liushuai
 * @since 2022-10-10
 */
@Service
@RequiredArgsConstructor
public class NdCompanyCreditLevelServiceImpl extends ServiceImpl<NdCompanyCreditLevelMapper, NdCompanyCreditLevel> implements INdCompanyCreditLevelService {

    private final NdCompanyCreditLevelMapper ndCompanyCreditLevelMapper;

    @Override
    public Page<NdCompanyCreditLevel> getCreditLevelList(CompanyCreditLevelPageParam param) {
        Page<NdCompanyCreditLevel> page = param.page();
        LambdaQueryWrapper<NdCompanyCreditLevel> wrapper = Wrappers.lambdaQuery(NdCompanyCreditLevel.class);
        NdCompanyCreditLevelServiceImpl context = (NdCompanyCreditLevelServiceImpl) AopContext.currentProxy();
        return context.searchList(page,wrapper,param);
    }

    @LambdaDataScope(wrapperIndex = 1)
    @BuildChildrenRegionWrapper(entityIndex = 2,wrapperIndex = 1)
    public Page<NdCompanyCreditLevel> searchList(Page<NdCompanyCreditLevel> page,
                                                 LambdaQueryWrapper<NdCompanyCreditLevel> wrapper, CompanyCreditLevelPageParam param) {
        wrapper.like(StringUtils.isNotEmpty(param.getCompanyName()),NdCompanyCreditLevel::getCompanyName, param.getCompanyName())
                .eq(StringUtils.isNotEmpty(param.getYear()),NdCompanyCreditLevel::getYear, param.getYear())
                .eq(StringUtils.isNotEmpty(param.getLevel()),NdCompanyCreditLevel::getLevel, param.getLevel())
                .orderBy(Boolean.TRUE,Boolean.FALSE,NdCompanyCreditLevel::getUpdateOn);
        ndCompanyCreditLevelMapper.selectPage(page, wrapper);
        CodeUtil.searchCompanyNamesAndRegionNames(page.getRecords());
        return page;
    }
}
