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.NdCompanyCreditInside;
import com.ningdatech.carapi.company.entity.dto.CompanyCreditInsidePageParam;
import com.ningdatech.carapi.company.mapper.NdCompanyCreditInsideMapper;
import com.ningdatech.carapi.company.service.INdCompanyCreditInsideService;
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 NdCompanyCreditInsideServiceImpl extends ServiceImpl<NdCompanyCreditInsideMapper, NdCompanyCreditInside> implements INdCompanyCreditInsideService {

    private final NdCompanyCreditInsideMapper ndCompanyCreditInsideMapper;

    @Override
    public Page<NdCompanyCreditInside> getList(CompanyCreditInsidePageParam param) {
        Page<NdCompanyCreditInside> page = param.page();
        LambdaQueryWrapper<NdCompanyCreditInside> wrapper = Wrappers.lambdaQuery(NdCompanyCreditInside.class);
        NdCompanyCreditInsideServiceImpl context = (NdCompanyCreditInsideServiceImpl)AopContext.currentProxy();
        return context.searchList(page,wrapper,param);
    }

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

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

    @LambdaDataScope(wrapperIndex = 1)
    @BuildChildrenRegionWrapper(entityIndex = 2,wrapperIndex = 1)
    public Page<NdCompanyCreditInside> searchList(Page<NdCompanyCreditInside> page,LambdaQueryWrapper<NdCompanyCreditInside> wrapper
            ,CompanyCreditInsidePageParam param) {
        wrapper.eq(NdCompanyCreditInside::getCheckType, param.getCheckType())
                .like(StringUtils.isNotEmpty(param.getCompanyName()),NdCompanyCreditInside::getCompanyName, param.getCompanyName())
                .eq(StringUtils.isNotEmpty(param.getType()),NdCompanyCreditInside::getType, param.getType())
                .ge(StringUtils.isNotEmpty(param.getStartTime()),NdCompanyCreditInside::getCreateOn, param.getStartTime())
                .le(StringUtils.isNotEmpty(param.getEndTime()),NdCompanyCreditInside::getCreateOn, param.getEndTime())
                .orderBy(Boolean.TRUE,Boolean.FALSE,NdCompanyCreditInside::getUpdateOn);
        Page<NdCompanyCreditInside> res = ndCompanyCreditInsideMapper.selectPage(page, wrapper);
        CodeUtil.searchCompanyNamesAndRegionNames(res.getRecords());
        return res;
    }
}
