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.TfYwpnStationService;
import com.tofly.xrtymis.custom.service.export.ExportStationService;
import com.tofly.xrtymis.entity.TfYwpnHydrant;
import com.tofly.xrtymis.entity.TfYwpnStation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@AllArgsConstructor
@Service("exportStationService")
public class ExportStationServiceImpl implements ExportStationService {

    private final TfYwpnStationService stationService;
    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<TfYwpnStation> result = new ArrayList<>();
        if (list.size() > 0 ) {
            for (String id : list) {
                TfYwpnStation station = stationService.getById(id);
                if (station != null) {
                    TfYwpnStation ywpnStation = tfYwpnStationCode2Name(station);
                    result.add(ywpnStation);
                }
            }
        }
        List<Map<String, Object>> returnMap = new ArrayList<>();
        String resultListToString = JSONArray.toJSONStringWithDateFormat(result,"yyyy-MM-dd");
        List<Map> result1 = JSONObject.parseArray(resultListToString , Map.class);
        Map<String, Object> parentMap = new HashMap<>();
        parentMap.put("list", result1);
        returnMap.add(parentMap);
        return returnMap;
    }

    private TfYwpnStation tfYwpnStationCode2Name(TfYwpnStation station) {
        if(StringUtils.isNotEmpty(station.getMonitortype())) {
            String monitortype = codeClient.getname(Long.parseLong(station.getMonitortype()));
            if (StringUtils.isNotBlank(monitortype)) {
                station.setMonitortype(monitortype);
            }
        }
        return station;
    }

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