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.TfYwpnHydrantService;
import com.tofly.xrtymis.custom.service.export.ExportHydrantService;
import com.tofly.xrtymis.entity.TfYwpnHydrant;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@AllArgsConstructor
@Service("exportHydrantService")
public class ExportHydrantServiceImpl implements ExportHydrantService {

    private final TfYwpnHydrantService hydrantService;
    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<TfYwpnHydrant> result = new ArrayList<>();
        if (list.size() > 0 ) {
            for (String id : list) {
                TfYwpnHydrant hydrant = hydrantService.getById(id);
                if (hydrant != null ) {
                    TfYwpnHydrant ywpnHydrant = tfYwpnHydrantCode2Name(hydrant);
                    result.add(ywpnHydrant);
                }
            }
        }
        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 TfYwpnHydrant tfYwpnHydrantCode2Name(TfYwpnHydrant hydrant) {
        if(StringUtils.isNotEmpty(hydrant.getMainvalvetype())) {
            String mainvalveType = codeClient.getname(Long.parseLong(hydrant.getMainvalvetype()));
            if (StringUtils.isNotBlank(mainvalveType)) {
                hydrant.setMainvalvetype(mainvalveType);
            }
        }
        if(StringUtils.isNotEmpty(hydrant.getMaterial())) {
            String material = codeClient.getname(Long.parseLong(hydrant.getMaterial()));
            if (StringUtils.isNotBlank(material)) {
                hydrant.setMaterial(material);
            }
        }
        return hydrant;
    }

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



}
