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

import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.carapi.common.aop.BuildChildrenRegionWrapper;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.company.entity.NdCompanyCreditDeduct;
import com.ningdatech.carapi.company.entity.dto.CompanyCreditDeductPageParam;
import com.ningdatech.carapi.company.mapper.NdCompanyCreditDeductMapper;
import com.ningdatech.carapi.company.service.INdCompanyCreditDeductService;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.datascope.contants.LambdaDataScopeTypeEnum;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 平安办扣分 服务实现类
 * </p>
 *
 * @author liushuai
 * @since 2022-10-10
 */
@Service
@RequiredArgsConstructor
public class NdCompanyCreditDeductServiceImpl extends ServiceImpl<NdCompanyCreditDeductMapper, NdCompanyCreditDeduct> implements INdCompanyCreditDeductService {

    private final NdCompanyCreditDeductMapper ndCompanyCreditDeductMapper;

    @Override
    public Page<NdCompanyCreditDeduct> getCreditDeductList(CompanyCreditDeductPageParam param) {
        Page<NdCompanyCreditDeduct> page = param.page();
        LambdaQueryWrapper<NdCompanyCreditDeduct> wrapper = Wrappers.lambdaQuery(NdCompanyCreditDeduct.class);
        NdCompanyCreditDeductServiceImpl context = (NdCompanyCreditDeductServiceImpl) AopContext.currentProxy();
        Page<NdCompanyCreditDeduct> ndCompanyCreditDeductPage = context.searchList(page, wrapper, param);
        CodeUtil.searchCompanyNamesAndRegionNames(page.getRecords());
        return ndCompanyCreditDeductPage;
    }

    @Override
    public Boolean delete(Long id) {
        LambdaQueryWrapper<NdCompanyCreditDeduct> wrapper = Wrappers.lambdaQuery(NdCompanyCreditDeduct.class);
        NdCompanyCreditDeductServiceImpl context = (NdCompanyCreditDeductServiceImpl) AopContext.currentProxy();
        context.buildQuery(wrapper, id);
        NdCompanyCreditDeduct creditDeduct = ndCompanyCreditDeductMapper.selectOne(wrapper);
        if (Objects.isNull(creditDeduct)){
            return false;
        }
        ndCompanyCreditDeductMapper.deleteById(creditDeduct);
        return true;
    }

    @LambdaDataScope(type = LambdaDataScopeTypeEnum.ALL)
    public void buildQuery(LambdaQueryWrapper<NdCompanyCreditDeduct> wrapper, Long id) {
        wrapper.eq(NdCompanyCreditDeduct::getId,id);
    }

    @LambdaDataScope(wrapperIndex = 1)
    @BuildChildrenRegionWrapper(entityIndex = 2,wrapperIndex = 1)
    public Page<NdCompanyCreditDeduct> searchList(Page<NdCompanyCreditDeduct> page, LambdaQueryWrapper<NdCompanyCreditDeduct> wrapper
            , CompanyCreditDeductPageParam param) {
        wrapper.like(StringUtils.isNotEmpty(param.getCompanyName()),NdCompanyCreditDeduct::getCompanyName, param.getCompanyName())
                .ge(StringUtils.isNotEmpty(param.getStartTime()),NdCompanyCreditDeduct::getCreateOn, param.getStartTime())
                .le(StringUtils.isNotEmpty(param.getEndTime()),NdCompanyCreditDeduct::getCreateOn, param.getEndTime())
                .orderBy(Boolean.TRUE,Boolean.FALSE,NdCompanyCreditDeduct::getUpdateOn);
        return ndCompanyCreditDeductMapper.selectPage(page, wrapper);
    }
}
