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

import java.util.*;

@AllArgsConstructor
@Service("exportNoValveService")
public class ExportNoValveServiceImpl implements ExportNoValveService {

    private final TfYwpnNonvalveService nonvalveService;
    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<TfYwpnNonvalve> result = new ArrayList<>();
        if (list.size() > 0 ) {
            for (String id : list) {
                TfYwpnNonvalve nonvalve = nonvalveService.getById(id);
                if (nonvalve != null ) {
                    TfYwpnNonvalve ywpnNonvalve = tfYwpnNonvalveCode2Name(nonvalve);
                    result.add(ywpnNonvalve);
                }
            }
        }
        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 TfYwpnNonvalve tfYwpnNonvalveCode2Name(TfYwpnNonvalve nonvalve) {
        if(StringUtils.isNotEmpty(nonvalve.getValvetype())) {
            String valvetype = codeClient.getname(Long.parseLong(nonvalve.getValvetype()));
            if (StringUtils.isNotBlank(valvetype)) {
                nonvalve.setValvetype(valvetype);
            }
        }
        if(StringUtils.isNotEmpty(nonvalve.getValveclass())) {
            String valveclass = codeClient.getname(Long.parseLong(nonvalve.getValveclass()));
            if (StringUtils.isNotBlank(valveclass)) {
                nonvalve.setValveclass(valveclass);
            }
        }
        if(StringUtils.isNotEmpty(nonvalve.getMaterial())) {
            String material = codeClient.getname(Long.parseLong(nonvalve.getMaterial()));
            if (StringUtils.isNotBlank(material)) {
                nonvalve.setMaterial(material);
            }
        }
        if(StringUtils.isNotEmpty(nonvalve.getIslock())) {
            String islock = codeClient.getname(Long.parseLong(nonvalve.getIslock()));
            if (StringUtils.isNotBlank(islock)) {
                nonvalve.setIslock(islock);
            }
        }
        if(StringUtils.isNotEmpty(nonvalve.getManholetype())) {
            String manholetype = codeClient.getname(Long.parseLong(nonvalve.getManholetype()));
            if (StringUtils.isNotBlank(manholetype)) {
                nonvalve.setManholetype(manholetype);
            }
        }
        if(StringUtils.isNotEmpty(nonvalve.getManholematerial())) {
            String manholematerial = codeClient.getname(Long.parseLong(nonvalve.getManholematerial()));
            if (StringUtils.isNotBlank(manholematerial)) {
                nonvalve.setManholematerial(manholematerial);
            }
        }
        if(StringUtils.isNotEmpty(nonvalve.getWelltype())) {
            String welltype = codeClient.getname(Long.parseLong(nonvalve.getWelltype()));
            if (StringUtils.isNotBlank(welltype)) {
                nonvalve.setWelltype(welltype);
            }
        }
        if(StringUtils.isNotEmpty(nonvalve.getWellmateial())) {
            String wellmateial = codeClient.getname(Long.parseLong(nonvalve.getWellmateial()));
            if (StringUtils.isNotBlank(wellmateial)) {
                nonvalve.setWellmateial(wellmateial);
            }
        }
        if(StringUtils.isNotEmpty(nonvalve.getConnecttype())) {
            String connecttype = codeClient.getname(Long.parseLong(nonvalve.getConnecttype()));
            if (StringUtils.isNotBlank(connecttype)) {
                nonvalve.setConnecttype(connecttype);
            }
        }
        return nonvalve;
    }

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