package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.domain.R;
import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.analysis.SRaEmissionsOverFactory;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.mapper.analysis.SRaEmissionsOverFactoryMapper;
import cn.cecep.talroad.service.analysis.ISRaEmissionsOverFactoryService;
import cn.cecep.talroad.vo.analysis.SRaEmissionsOverFactoryVo;
import cn.cecep.talroad.vo.query.analysis.SRaEmissionsOverFactoryQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;




/**
 * 企业排放量超标分析Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-24*/
@Service
public class SRaEmissionsOverFactoryServiceImpl extends ServiceImpl<SRaEmissionsOverFactoryMapper, SRaEmissionsOverFactory> implements ISRaEmissionsOverFactoryService
{
    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;

    @Override
    public List<SRaEmissionsOverFactoryVo> selectEmissionsOverFactory(Page<SRaEmissionsOverFactoryVo> page, SRaEmissionsOverFactoryQuery query) {
        // 若未指定，查询当前用户区域下的数据
        if (StringUtils.isEmpty(query.getRegionCode())) {
            String userRegionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();

            if (StringUtils.isEmpty(userRegionCode)) {
                return new ArrayList<>();
            }
            R<List<String>> r = baseRegionService.getRegionCodeListChild(userRegionCode);
            if (r.getCode() == R.SUCCESS) {
                query.setRegionCodeList(r.getData());
            } else {
                return new ArrayList<>();
            }
        }
        List<SRaEmissionsOverFactoryVo> vos = baseMapper.selectEmissionsOverFactory(page, query);
        page.setRecords(vos);
        return vos;
    }

    @Override
    public List<String> getStatScopeList() {
        return baseMapper.selectStatScopeList();
    }

}
