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.SRaEmissionsOver;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.mapper.analysis.SRaEmissionsOverMapper;
import cn.cecep.talroad.service.analysis.ISRaEmissionsOverService;
import cn.cecep.talroad.vo.analysis.SRaEmissionsOverVo;
import cn.cecep.talroad.vo.query.analysis.SRaEmissionsOverQuery;
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业务层处理
 */
@Service
public class SRaEmissionsOverServiceImpl extends ServiceImpl<SRaEmissionsOverMapper, SRaEmissionsOver> implements ISRaEmissionsOverService
{
    @Autowired
    @Qualifier("baseRegionService")
    private BaseRegionService baseRegionService;

    @Override
    public List<SRaEmissionsOverVo.RegionVo> getRegionEmissions(Page<SRaEmissionsOverVo.RegionVo> page, SRaEmissionsOverQuery query) {
        // 查询当前用户区域下的数据
        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<SRaEmissionsOverVo.RegionVo> list = baseMapper.selectRegionEmissions(page, query);
        page.setRecords(list);
        return list;
    }

    @Override
    public List<SRaEmissionsOverVo.IndustryVo> getIndustryEmissions(Page<SRaEmissionsOverVo.IndustryVo> page, SRaEmissionsOverQuery query) {
        // 若未指定区域，根据用户所属区域级别判断，若是唐山市级别，展示所有行业的汇总记录，否则返回用户所属区县的记录
        if (StringUtils.isEmpty(query.getRegionCode())) {
            String userRegionCode = SecurityUtils.getLoginUser().getSysUser().getRegionCode();
            if (StringUtils.isEmpty(userRegionCode)) {
                return new ArrayList<>();
            }

            if ("130200".equals(userRegionCode)) {
                // 唐山市，不需要设置regionCode，不设置则查询所有区域的汇总记录
            } else {
                query.setRegionCode(userRegionCode);
            }
        }
        List<SRaEmissionsOverVo.IndustryVo> list = baseMapper.selectIndustryEmissions(page, query);
        page.setRecords(list);
        return list;
    }

}
