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

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

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.BuildChildrenRegionEntity;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.company.entity.NdCompanyBlackList;
import com.ningdatech.carapi.company.entity.dto.CompanyCreditBlackListPageParam;
import com.ningdatech.carapi.company.entity.dto.ResCompanyBlackDetailListDTO;
import com.ningdatech.carapi.company.entity.dto.ResCompanyBlackListDTO;
import com.ningdatech.carapi.company.mapper.NdCompanyBlackListMapper;
import com.ningdatech.carapi.company.service.INdCompanyBlackListService;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.datascope.aop.XmlDataScope;
import com.ningdatech.carapi.datascope.contants.LambdaDataScopeTypeEnum;

import cn.hutool.core.collection.CollUtil;
import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 企业失信名单 服务实现类
 * </p>
 *
 * @author liushuai
 * @since 2022-10-10
 */
@Service
@RequiredArgsConstructor
public class NdCompanyBlackListServiceImpl extends ServiceImpl<NdCompanyBlackListMapper, NdCompanyBlackList> implements INdCompanyBlackListService {

    private final NdCompanyBlackListMapper ndCompanyBlackListMapper;

    @Override
    public Page<ResCompanyBlackListDTO> companyBlackList(CompanyCreditBlackListPageParam param) {
        Page<ResCompanyBlackListDTO> page = param.page();
        NdCompanyBlackListServiceImpl context = (NdCompanyBlackListServiceImpl) AopContext.currentProxy();
        return context.searchList(page,param);
    }

    @XmlDataScope(entityIndex = 1)
    @BuildChildrenRegionEntity(entityIndex = 1)
    public Page<ResCompanyBlackListDTO> searchList(Page<ResCompanyBlackListDTO> page, CompanyCreditBlackListPageParam param) {
        ndCompanyBlackListMapper.companyBlackList(page, param);
        CodeUtil.searchCompanyNames(page.getRecords());
        return page;
    }

    @Override
    public List<ResCompanyBlackDetailListDTO> companyBlackInfoList(Long companyId) {

        LambdaQueryWrapper<NdCompanyBlackList> wrapper = Wrappers.lambdaQuery(NdCompanyBlackList.class);
        NdCompanyBlackListServiceImpl context = (NdCompanyBlackListServiceImpl) AopContext.currentProxy();
        context.buildQuery(wrapper, companyId);
        List<NdCompanyBlackList> companyBlackLists = ndCompanyBlackListMapper.selectList(wrapper);
        if (CollUtil.isEmpty(companyBlackLists)){
            return Collections.emptyList();
        }
        return companyBlackLists.stream().map(c -> {
            ResCompanyBlackDetailListDTO dto = new ResCompanyBlackDetailListDTO();
            dto.setType(c.getType());
            dto.setBlackTime(c.getBlackTime());
            dto.setContent(c.getContent());
            dto.setSite(c.getSite());
            return dto;
        }).collect(Collectors.toList());
    }

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