package com.goomood.phm.service.impl;

import java.io.IOException;
import java.util.List;
import com.goomood.common.utils.DateUtils;
import com.goomood.es.service.FileService;
import com.goomood.phm.domain.FaultRecord;
import com.goomood.phm.utils.PhmConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.goomood.phm.mapper.FaultCaseMapper;
import com.goomood.phm.domain.FaultCase;
import com.goomood.phm.service.IFaultCaseService;

/**
 * 故障案例Service业务层处理
 * 
 * @author goomood
 * @date 2024-11-17
 */
@Service
public class FaultCaseServiceImpl implements IFaultCaseService 
{
    @Autowired
    private FaultCaseMapper faultCaseMapper;


    @Autowired
    private FileService fileServicel;

    /**
     * 查询故障案例
     * 
     * @param id 故障案例主键
     * @return 故障案例
     */
    @Override
    public FaultCase selectFaultCaseById(Long id)
    {
        return faultCaseMapper.selectFaultCaseById(id);
    }

    /**
     * 查询故障案例列表
     * 
     * @param faultCase 故障案例
     * @return 故障案例
     */
    @Override
    public List<FaultCase> selectFaultCaseList(FaultCase faultCase)
    {
        return faultCaseMapper.selectFaultCaseList(faultCase);
    }

    /**
     * 新增故障案例
     * 
     * @param faultCase 故障案例
     * @return 结果
     */
    @Override
    public int insertFaultCase(FaultCase faultCase)
    {
        faultCase.setCreateTime(DateUtils.getNowDate());
        int res = faultCaseMapper.insertFaultCase(faultCase);
        fileServicel.fileToEs(faultCase.getFilePath(), PhmConstants.ES_INDEX_BELIONG_TYPE_FAULT_CASE,faultCase.getId(),faultCase.getFaultName());
        return res;
    }

    /**
     * 修改故障案例
     * 
     * @param faultCase 故障案例
     * @return 结果
     */
    @Override
    public int updateFaultCase(FaultCase faultCase)
    {
        faultCase.setUpdateTime(DateUtils.getNowDate());
        int res =  faultCaseMapper.updateFaultCase(faultCase);
        if(res != 0){
            FaultCase entity = this.selectFaultCaseById(faultCase.getId());
            fileServicel.fileToEs(entity.getFilePath(), PhmConstants.ES_INDEX_BELIONG_TYPE_FAULT_CASE,entity.getId(),entity.getFaultName());
        }
        return res;
    }

    /**
     * 批量删除故障案例
     * 
     * @param ids 需要删除的故障案例主键
     * @return 结果
     */
    @Override
    public int deleteFaultCaseByIds(Long[] ids)
    {
        try{
            for (Long id: ids){
                FaultCase entity = this.selectFaultCaseById(id);
                fileServicel.deleteDocumentByBelong(entity.getFilePath(),PhmConstants.ES_INDEX_BELIONG_TYPE_FAULT_CASE,entity.getId());
            }
            return faultCaseMapper.deleteFaultCaseByIds(ids);
        }catch (IOException e){
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除故障案例信息
     * 
     * @param id 故障案例主键
     * @return 结果
     */
    @Override
    public int deleteFaultCaseById(Long id)
    {
        FaultCase entity = this.selectFaultCaseById(id);
        try {
            fileServicel.deleteDocumentByBelong(entity.getFilePath(),PhmConstants.ES_INDEX_BELIONG_TYPE_FAULT_CASE,entity.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return faultCaseMapper.deleteFaultCaseById(id);
    }
}
