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.NdCompanyCreditReward;
import com.ningdatech.carapi.company.entity.dto.CompanyCreditRewardPageParam;
import com.ningdatech.carapi.company.mapper.NdCompanyCreditRewardMapper;
import com.ningdatech.carapi.company.service.INdCompanyCreditRewardService;
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 NdCompanyCreditRewardServiceImpl extends ServiceImpl<NdCompanyCreditRewardMapper, NdCompanyCreditReward> implements INdCompanyCreditRewardService {

    private final NdCompanyCreditRewardMapper ndCompanyCreditRewardMapper;

    @Override
    public Page<NdCompanyCreditReward> getCreditRewardList(CompanyCreditRewardPageParam param) {
        Page<NdCompanyCreditReward> page = param.page();
        LambdaQueryWrapper<NdCompanyCreditReward> wrapper = Wrappers.lambdaQuery(NdCompanyCreditReward.class);
        NdCompanyCreditRewardServiceImpl context = (NdCompanyCreditRewardServiceImpl) AopContext.currentProxy();
        return context.searchList(page,wrapper,param);
    }

    @Override
    public Boolean delete(Long id) {
        LambdaQueryWrapper<NdCompanyCreditReward> wrapper = Wrappers.lambdaQuery(NdCompanyCreditReward.class);
        NdCompanyCreditRewardServiceImpl context = (NdCompanyCreditRewardServiceImpl) AopContext.currentProxy();
        context.buildQuery(wrapper, id);
        NdCompanyCreditReward creditReward = ndCompanyCreditRewardMapper.selectOne(wrapper);
        if (Objects.isNull(creditReward)){
            return false;
        }
        ndCompanyCreditRewardMapper.deleteById(creditReward);
        return true;
    }
    @LambdaDataScope(type = LambdaDataScopeTypeEnum.ALL)
    public void buildQuery(LambdaQueryWrapper<NdCompanyCreditReward> wrapper, Long id) {
        wrapper.eq(NdCompanyCreditReward::getId,id);
    }

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