package com.tofly.xrtymis.custom.service.export.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tofly.api.CodeClient;
import com.tofly.xrtymis.custom.commons.Constate;
import com.tofly.xrtymis.custom.service.TfYwpnManholeService;
import com.tofly.xrtymis.custom.service.export.ExportManholeService;
import com.tofly.xrtymis.entity.TfYwpnHydrant;
import com.tofly.xrtymis.entity.TfYwpnManhole;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@AllArgsConstructor
@Service("exportManholeService")
public class ExportManholeServiceImpl implements ExportManholeService {

    private final TfYwpnManholeService manholeService;
    private final CodeClient codeClient;

    @Override
    public List<Map<String, Object>> getExportData(String dto, String templateFlag) {
        List<String> list = Arrays.asList(dto.split(Constate.CSV));
        ArrayList<TfYwpnManhole> result = new ArrayList<>();
        if (list.size() > 0 ) {
            for (String id : list) {
                TfYwpnManhole manhole = manholeService.getById(id);
                if (manhole != null) {
                    TfYwpnManhole ywpnManhole = tfYwpnManholeCode2Name(manhole);
                    result.add(ywpnManhole);
                }
            }
        }
        List<Map<String, Object>> returnMap = new ArrayList<>();
        Map<String, Object> parentMap = new HashMap<>();
        String resultListToString = JSONArray.toJSONStringWithDateFormat(result,"yyyy-MM-dd");
        List<Map> result1 = JSONObject.parseArray(resultListToString , Map.class);
        parentMap.put("list", result1);
        returnMap.add(parentMap);
        return returnMap;
    }

    private TfYwpnManhole tfYwpnManholeCode2Name(TfYwpnManhole manhole) {
        if(StringUtils.isNotEmpty(manhole.getMonitorwelltype())) {
            String monitorwelltype = codeClient.getname(Long.parseLong(manhole.getMonitorwelltype()));
            if (StringUtils.isNotBlank(monitorwelltype)) {
                manhole.setMonitorwelltype(monitorwelltype);
            }
        }
        if(StringUtils.isNotEmpty(manhole.getManholetype())) {
            String manholetype = codeClient.getname(Long.parseLong(manhole.getManholetype()));
            if (StringUtils.isNotBlank(manholetype)) {
                manhole.setManholetype(manholetype);
            }
        }
        if(StringUtils.isNotEmpty(manhole.getManholematerial())) {
            String manholematerial = codeClient.getname(Long.parseLong(manhole.getManholematerial()));
            if (StringUtils.isNotBlank(manholematerial)) {
                manhole.setManholematerial(manholematerial);
            }
        }
        if(StringUtils.isNotEmpty(manhole.getWelltype())) {
            String welltype = codeClient.getname(Long.parseLong(manhole.getWelltype()));
            if (StringUtils.isNotBlank(welltype)) {
                manhole.setWelltype(welltype);
            }
        }
        if(StringUtils.isNotEmpty(manhole.getWellmateial())) {
            String wellmateial = codeClient.getname(Long.parseLong(manhole.getWellmateial()));
            if (StringUtils.isNotBlank(wellmateial)) {
                manhole.setWellmateial(wellmateial);
            }
        }
        return manhole;
    }

    @Override
    public Boolean excelDataStorage(List<Object> dataList) {
        return null;
    }
}

