package com.goomood.phm.service.impl;

import java.io.IOException;
import java.util.List;
import com.goomood.common.utils.DateUtils;
import com.goomood.common.utils.StringUtils;
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.TechDataMapper;
import com.goomood.phm.domain.TechData;
import com.goomood.phm.service.ITechDataService;

/**
 * 技术资料Service业务层处理
 * 
 * @author goomood
 * @date 2024-11-16
 */
@Service
public class TechDataServiceImpl implements ITechDataService 
{
    @Autowired
    private TechDataMapper techDataMapper;

    @Autowired
    private FileService fileServicel;

    /**
     * 查询技术资料
     * 
     * @param id 技术资料主键
     * @return 技术资料
     */
    @Override
    public TechData selectTechDataById(Long id)
    {
        TechData techData = techDataMapper.selectTechDataById(id);
        return techData;
    }

    /**
     * 查询技术资料列表
     * 
     * @param techData 技术资料
     * @return 技术资料
     */
    @Override
    public List<TechData> selectTechDataList(TechData techData)
    {
        return techDataMapper.selectTechDataList(techData);
    }

    /**
     * 新增技术资料
     * 
     * @param techData 技术资料
     * @return 结果
     */
    @Override
    public int insertTechData(TechData techData)
    {
        techData.setCreateTime(DateUtils.getNowDate());
        int res = techDataMapper.insertTechData(techData);
        fileServicel.fileToEs(techData.getFilePath(), PhmConstants.ES_INDEX_BELIONG_TYPE_TECH,techData.getId(),techData.getDataTitle());
        return res;
    }

    /**
     * 修改技术资料
     * 
     * @param techData 技术资料
     * @return 结果
     */
    @Override
    public int updateTechData(TechData techData)
    {
        techData.setUpdateTime(DateUtils.getNowDate());
        int res = techDataMapper.updateTechData(techData);
        if(res != 0){
            TechData entity =  this.selectTechDataById(techData.getId());
            fileServicel.fileToEs(entity.getFilePath(), PhmConstants.ES_INDEX_BELIONG_TYPE_TECH,entity.getId(),entity.getDataTitle());
        }
        return res;
    }

    /**
     * 批量删除技术资料
     * 
     * @param ids 需要删除的技术资料主键
     * @return 结果
     */
    @Override
    public int deleteTechDataByIds(Long[] ids)
    {
        try {
            for(Long id: ids){
                TechData entity = this.selectTechDataById(id);
                fileServicel.deleteDocumentByBelong(entity.getFilePath(),PhmConstants.ES_INDEX_BELIONG_TYPE_TECH,entity.getId());
            }
            return techDataMapper.deleteTechDataByIds(ids);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除技术资料信息
     * 
     * @param id 技术资料主键
     * @return 结果
     */
    @Override
    public int deleteTechDataById(Long id)
    {
        TechData entity = this.selectTechDataById(id);
        try {
            fileServicel.deleteDocumentByBelong(entity.getFilePath(),PhmConstants.ES_INDEX_BELIONG_TYPE_TECH,entity.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return techDataMapper.deleteTechDataById(id);
    }
}
