package com.skr.mdm.api.equipment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.skr.mdm.api.equipment.service.DebugLogService;
import com.skr.mdm.bean.qo.controller.ClientDebugLogQO;
import com.skr.mdm.bean.vo.equipment.ClientDebugLogVO;
import com.skr.mdm.dao.ClientDebugLogDao;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.entity.DeviceEquipmentDO;
import com.skr.mdm.enums.LogAdminTypeEnum;
import com.skr.mdm.handle.LogAdminHandler;
import com.skr.mdm.manager.oss.MinIoManager;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.util.FileDownloadUtil;
import com.skr.mdm.util.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;

/**
 * @Author tty
 * @Date 2022/3/21 14:20
 * @Description
 */
@Service
@Slf4j
public class DebugLogServiceImpl implements DebugLogService {

    @Autowired
    private ClientDebugLogDao clientDebugLogDao;
    @Autowired
    private LogAdminHandler logAdminHandler;
    @Autowired
    private DeviceEquipmentDao deviceEquipmentDao;
    @Autowired
    private MinIoManager minIoManager;

    @Override
    public ResponseResult queryDebugLogList(LoginAdminDTO admin, ClientDebugLogQO clientDebugLogQO) {
        Integer pageNumber = clientDebugLogQO.getPageNumber();
        Integer pageSize = clientDebugLogQO.getPageSize();
        clientDebugLogQO.setCompanyId(admin.getCompanyId());
        PageHelper.startPage(pageNumber, pageSize);
        List<ClientDebugLogVO> clientDebugLogVOList = clientDebugLogDao.queryDebugLogList(clientDebugLogQO);
        PageInfo<ClientDebugLogVO> pageInfo = new PageInfo<>(clientDebugLogVOList);
        return ResponseResult.success(PageUtil.getPageResult(pageInfo));
    }

    @Override
    public void debugLogDownload(LoginAdminDTO loginAdminDTO, HttpServletResponse response, Integer equipmentId, String source) throws Exception {
        String fileSource = StringUtils.substringAfter(source, "/");
        String name = StringUtils.substringBeforeLast(source, ".");
        String type = StringUtils.substringAfterLast(source, ".");
        File temp = File.createTempFile(name, ".".concat(type));
        log.info("临时文件路径 :  {}", temp.getAbsolutePath());
        minIoManager.downLoadMinIoObject(minIoManager.getCompanyName(loginAdminDTO.getCompanyId()) + "/" + source, temp.getAbsolutePath());
        FileDownloadUtil.download(response, temp, fileSource);
        boolean delete = temp.delete();
        if (!delete) {
            log.warn("临时文件删除失败, 退出时删除!");
            temp.deleteOnExit();
        }
        String module = "调试日志-下载";
        DeviceEquipmentDO deviceEquipmentDO = deviceEquipmentDao.selectById(equipmentId);
        logAdminHandler.log(loginAdminDTO, deviceEquipmentDO.getUserName() + "_" + deviceEquipmentDO.getEquipmentName(),
                LogAdminTypeEnum.DEVICE_TYPE.getLogType(), fileSource, module);
    }
}