package com.goomood.phm.service.impl;

import java.io.IOException;
import java.util.List;

import com.goomood.common.constant.Constants;
import com.goomood.common.utils.DateUtils;
import com.goomood.common.utils.StringUtils;
import com.goomood.common.utils.file.FileUtils;
import com.goomood.es.service.FileService;
import com.goomood.framework.config.ServerConfig;
import com.goomood.phm.utils.PhmConstants;
import com.goomood.web.controller.common.CommonController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import com.goomood.phm.mapper.TechDataMapper;
import com.goomood.phm.domain.TechData;
import com.goomood.phm.service.ITechDataService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import static com.goomood.framework.datasource.DynamicDataSourceContextHolder.log;

/**
 * 技术资料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);
    }
    @Resource
    private ServerConfig serverConfig;
    @Resource
    private CommonController commonService;
    @Override
    public int downloadFile(Long oosId, HttpServletResponse response) {
        TechData techData = this.selectTechDataById(oosId);
//        String  filePath = serverConfig.getUrl() + techData.getFilePath();
        commonService.fileDownload(techData.getFilePath(),false,response);
        System.out.println(techData.getFilePath());
        resourceDownload(techData.getFilePath(),response);
        return 0;
    }
    public void resourceDownload(String resource, HttpServletResponse response) {
        try {
            if (!FileUtils.checkAllowDownload(resource)) {
                throw new Exception(StringUtils.format("资源文件({})非法，不允许下载。 ", resource));
            }
//            // 本地资源路径
//            String localPath = EcoConfig.getProfile();
//            // 数据库资源地址
//            String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX);
            // 下载名称
            String downloadName = StringUtils.substringAfterLast(resource, "/");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, downloadName);
            FileUtils.writeBytes(resource, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载文件失败", e);
        }
    }
}
