package com.goomood.phm.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.goomood.common.utils.DateUtils;
import com.goomood.es.service.FileService;
import com.goomood.phm.utils.PhmConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goomood.phm.mapper.FaultRecordMapper;
import com.goomood.phm.domain.FaultRecord;
import com.goomood.phm.service.IFaultRecordService;

/**
 * 故障记录Service业务层处理
 * 
 * @author goomood
 * @date 2024-11-17
 */
@Service
public class FaultRecordServiceImpl implements IFaultRecordService 
{
    @Autowired
    private FaultRecordMapper faultRecordMapper;


    @Autowired
    private FileService fileServicel;

    /**
     * 查询故障记录
     * 
     * @param id 故障记录主键
     * @return 故障记录
     */
    @Override
    public FaultRecord selectFaultRecordById(Long id)
    {
        return faultRecordMapper.selectFaultRecordById(id);
    }

    /**
     * 查询故障记录列表
     * 
     * @param faultRecord 故障记录
     * @return 故障记录
     */
    @Override
    public List<FaultRecord> selectFaultRecordList(FaultRecord faultRecord)
    {
        return faultRecordMapper.selectFaultRecordList(faultRecord);
    }

    /**
     * 新增故障记录
     * 
     * @param faultRecord 故障记录
     * @return 结果
     */
    @Override
    public int insertFaultRecord(FaultRecord faultRecord)
    {
        faultRecord.setCreateTime(DateUtils.getNowDate());
        int res = faultRecordMapper.insertFaultRecord(faultRecord);
        fileServicel.fileToEs(faultRecord.getFilePath(), PhmConstants.ES_INDEX_BELIONG_TYPE_FAULT_RECORD,faultRecord.getId(),faultRecord.getFaultyPartName());
        return res;
    }

    /**
     * 修改故障记录
     * 
     * @param faultRecord 故障记录
     * @return 结果
     */
    @Override
    public int updateFaultRecord(FaultRecord faultRecord)
    {
        faultRecord.setUpdateTime(DateUtils.getNowDate());
        int res =  faultRecordMapper.updateFaultRecord(faultRecord);
        if(res != 0){
            FaultRecord entity = this.selectFaultRecordById(faultRecord.getId());
            fileServicel.fileToEs(entity.getFilePath(), PhmConstants.ES_INDEX_BELIONG_TYPE_FAULT_RECORD,entity.getId(),entity.getFaultyPartName());
        }
        return res;
    }

    /**
     * 批量删除故障记录
     * 
     * @param ids 需要删除的故障记录主键
     * @return 结果
     */
    @Override
    public int deleteFaultRecordByIds(Long[] ids)
    {
        try {
            for(Long id: ids){
                FaultRecord entity = this.selectFaultRecordById(id);
                fileServicel.deleteDocumentByBelong(entity.getFilePath(),PhmConstants.ES_INDEX_BELIONG_TYPE_FAULT_RECORD,entity.getId());
            }
            return faultRecordMapper.deleteFaultRecordByIds(ids);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除故障记录信息
     * 
     * @param id 故障记录主键
     * @return 结果
     */
    @Override
    public int deleteFaultRecordById(Long id)
    {
        FaultRecord entity = this.selectFaultRecordById(id);
        try {
            fileServicel.deleteDocumentByBelong(entity.getFilePath(),PhmConstants.ES_INDEX_BELIONG_TYPE_FAULT_RECORD,entity.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return faultRecordMapper.deleteFaultRecordById(id);
    }




    @Override
    public List<String> getAircraftTypes() {
        return faultRecordMapper.getAircraftTypes();
    }

    @Override
    public List<String> getAircraftNos() {
        return faultRecordMapper.getAircraftNos();
    }

    @Override
    public List<String> getSpecialties() {
        return faultRecordMapper.getSpecialties();
    }


    @Override
    public Map<String, List<Object>> getSystemFaultStatsByStandAlone(String aircraftType, String aircraftNo, String startDate, String endDate) {
        List<Map<String, Object>> rawData = faultRecordMapper.getSystemFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate);
        return convertToEChartsFormat(rawData);
    }

    @Override
    public Map<String, List<Object>> getSpecialtyFaultStatsByStandAlone(String aircraftType, String aircraftNo, String startDate, String endDate) {
        List<Map<String, Object>> rawData = faultRecordMapper.getSpecialtyFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate);
        return convertToEChartsFormat(rawData);
    }

    @Override
    public Map<String, List<Object>> getPartFaultStatsByStandAlone(String aircraftType, String aircraftNo, String startDate, String endDate) {
        List<Map<String, Object>> rawData = faultRecordMapper.getPartFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate);
        return convertToEChartsFormat(rawData);
    }

    @Override
    public Map<String, List<Object>> getLocationFaultStatsByStandAlone(String aircraftType, String aircraftNo, String startDate, String endDate) {
        List<Map<String, Object>> rawData = faultRecordMapper.getLocationFaultStatsByStandAlone(aircraftType, aircraftNo, startDate, endDate);
        return convertToEChartsFormat(rawData);
    }

    private Map<String, List<Object>> convertToEChartsFormat(List<Map<String, Object>> rawData) {
        List<Object> xAxisData = new ArrayList<>();
        List<Object> yAxisData = new ArrayList<>();

        for (Map<String, Object> entry : rawData) {
            xAxisData.add(entry.get("name"));
            yAxisData.add(entry.get("value"));
        }

        Map<String, List<Object>> result = new HashMap<>();
        result.put("xAxis", xAxisData);
        result.put("yAxis", yAxisData);
        return result;
    }



    @Override
    public Map<String, List<Object>> getAircraftFaultStatsBySpecialty(String aircraftType, String specialty, String startDate, String endDate) {
        List<Map<String, Object>> rawData = faultRecordMapper.getAircraftFaultStatsBySpecialty(aircraftType, specialty, startDate, endDate);
        return convertToEChartsFormat(rawData);
    }

    @Override
    public Map<String, List<Object>> getPartFaultStatsBySpecialty(String aircraftType, String specialty, String startDate, String endDate) {
        List<Map<String, Object>> rawData = faultRecordMapper.getPartFaultStatsBySpecialty(aircraftType, specialty, startDate, endDate);
        return convertToEChartsFormat(rawData);
    }


    @Override
    public Map<String, List<Object>> getSpecialtyFaultStatsBySpecialty(String aircraftType,String specialty, String startDate, String endDate) {
        List<Map<String, Object>> rawData = faultRecordMapper.getSpecialtyFaultStatsBySpecialty(aircraftType,specialty, startDate, endDate);
        return convertToEChartsFormat(rawData);
    }


    @Override
    public Map<String, List<Object>> getSystemFaultStatsBySpecialty(String aircraftType, String specialty, String startDate, String endDate) {
        List<Map<String, Object>> rawData = faultRecordMapper.getSystemFaultStatsBySpecialty(aircraftType, specialty, startDate, endDate);
        return convertToEChartsFormat(rawData);
    }
}
