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

import java.util.Objects;

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.constant.UserRoleTypeEnum;
import com.ningdatech.carapi.common.util.CodeUtil;
import com.ningdatech.carapi.company.entity.NdCompanyQualityManage;
import com.ningdatech.carapi.company.entity.dto.ResQualityManageDTO;
import com.ningdatech.carapi.company.entity.po.CompanyQualityPO;
import com.ningdatech.carapi.company.mapper.NdCompanyQualityManageMapper;
import com.ningdatech.carapi.company.service.INdCompanyQualityManageService;
import com.ningdatech.carapi.datascope.aop.LambdaDataScope;
import com.ningdatech.carapi.datascope.aop.XmlDataScope;
import com.ningdatech.carapi.datascope.contants.LambdaDataScopeTypeEnum;
import com.ningdatech.carapi.user.security.auth.model.UserInfoDetails;
import com.ningdatech.carapi.user.util.LoginUserUtil;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 质量通报 服务实现类
 * </p>
 *
 * @author liushuai
 * @since 2022-10-10
 */
@Service
@RequiredArgsConstructor
public class NdCompanyQualityManageServiceImpl extends ServiceImpl<NdCompanyQualityManageMapper, NdCompanyQualityManage> implements INdCompanyQualityManageService {

    private final NdCompanyQualityManageMapper ndCompanyQualityManageMapper;

    @Override
    public Page<ResQualityManageDTO> qualityManageList(Long regionId, String year, int pageNum, int pageSize) {
        NdCompanyQualityManageServiceImpl context = (NdCompanyQualityManageServiceImpl)AopContext.currentProxy();
        UserInfoDetails userInfoDetails = LoginUserUtil.loginUserDetail();
        Long roleId = userInfoDetails.getRoleId();
        // 非省系统管理员
        if (!UserRoleTypeEnum.SYSTEM_ADMIN_ROLE_ID.getId().equals(roleId)) {
            return context.searchList(new Page<>(pageNum, pageSize),
                    new CompanyQualityPO(regionId, null, year, null));
        }else {
            return context.searchAllList(new Page<>(pageNum, pageSize),
                    new CompanyQualityPO(regionId, null, year, null));
        }
    }

    @XmlDataScope(entityIndex = 1)
    @BuildChildrenRegionEntity(entityIndex = 1)
    public Page<ResQualityManageDTO> searchList(Page<ResQualityManageDTO> page, CompanyQualityPO po) {
        ndCompanyQualityManageMapper.qualityManageList(page, po);
        CodeUtil.searchRegionNames(page.getRecords());
        return page;
    }

    @XmlDataScope(entityIndex = 1)
    @BuildChildrenRegionEntity(entityIndex = 1)
    public Page<ResQualityManageDTO> searchAllList(Page<ResQualityManageDTO> page, CompanyQualityPO po) {
        ndCompanyQualityManageMapper.qualityAllManageList(page, po);
        CodeUtil.searchRegionNames(page.getRecords());
        return page;
    }

    @Override
    public boolean incrementDownCount(Long id) {
       return ndCompanyQualityManageMapper.incrementDownCount(id) > 0;
    }

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

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