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.SRaEmissionsDiff;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.mapper.analysis.SRaEmissionsDiffMapper;
import cn.cecep.talroad.service.analysis.ISRaEmissionsDiffService;
import cn.cecep.talroad.vo.analysis.SRaEmissionsDiffVo;
import cn.cecep.talroad.vo.query.analysis.SRaEmissionsDiffQuery;
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 SRaEmissionsDiffServiceImpl extends ServiceImpl<SRaEmissionsDiffMapper, SRaEmissionsDiff> implements ISRaEmissionsDiffService
{
    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;

    @Override
    public List<SRaEmissionsDiffVo> selectEmissionsDiffList(Page<SRaEmissionsDiffVo> page, SRaEmissionsDiffQuery 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<SRaEmissionsDiffVo> vos = baseMapper.selectEmissionsDiffList(page, query);
        page.setRecords(vos);
        return vos;
    }

}
