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.core.web.page.TableDataInfo;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.analysis.SRaWaterFlag;
import cn.cecep.talroad.feign.BaseRegionService;
import cn.cecep.talroad.mapper.analysis.SRaWaterFlagMapper;
import cn.cecep.talroad.service.analysis.ISRaWaterFlagService;
import cn.cecep.talroad.vo.analysis.SRaWaterFlagVo;
import cn.cecep.talroad.vo.query.analysis.SRaWaterFlagQuery;
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.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 关联分析系统-企业异常行为分析-企业排放分析-废水在线监测标记汇总Service业务层处理
 */
@Service
public class SRaWaterFlagServiceImpl extends ServiceImpl<SRaWaterFlagMapper, SRaWaterFlag> implements ISRaWaterFlagService {

    @Autowired
    private BaseRegionService baseRegionService;

    @Override
    public List<SRaWaterFlagVo.Row> getWaterFlagList(Page<SRaWaterFlagVo.Row> page, SRaWaterFlagQuery 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<SRaWaterFlagVo.Row> list = baseMapper.selectWaterFlagList(page, query);
        if (list.isEmpty()) {
            return list;
        }
        Map<String, Map<String, Object>> dictDataMap = new HashMap<>();
        TableDataInfo tableDataInfo = baseRegionService.getDictDataList("1", "1000", "anal_data_flag");
        if (tableDataInfo != null) {
            List<Map<String, Object>> rows = (List<Map<String, Object>>) tableDataInfo.getRows();
            dictDataMap = rows.stream().collect(Collectors.toMap(o -> (String) o.get("dictValue"), o -> o, (o1, o2) -> o1));
        }
        for (SRaWaterFlagVo.Row row : list) {
            Map<String, Object> sysDictData = dictDataMap.get(row.getDataFlag());
            if (sysDictData != null) {
                row.setDataFlagName((String) sysDictData.get("dictLabel"));
            }
        }
        return list;
    }

    @Override
    public List<SRaWaterFlagVo.DetailRow> getWaterFlagDetailList(Page<SRaWaterFlagVo.DetailRow> page, SRaWaterFlagQuery 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<SRaWaterFlagVo.DetailRow> list = baseMapper.selectWaterFlagDetailList(page, query);
        if (list.isEmpty()) {
            return list;
        }
        Map<String, Map<String, Object>> dictDataMap = new HashMap<>();
        TableDataInfo tableDataInfo = baseRegionService.getDictDataList("1", "1000", "anal_data_flag");
        if (tableDataInfo != null) {
            List<Map<String, Object>> rows = (List<Map<String, Object>>) tableDataInfo.getRows();
            dictDataMap = rows.stream().collect(Collectors.toMap(o -> (String) o.get("dictValue"), o -> o, (o1, o2) -> o1));
        }
        for (SRaWaterFlagVo.DetailRow row : list) {
            Map<String, Object> sysDictData = dictDataMap.get(row.getDataFlag());
            if (sysDictData != null) {
                row.setDataFlagName((String) sysDictData.get("dictLabel"));
            }
        }
        return list;
    }
}
