/*
 *    Copyright (c) 2018-2025, hnqz All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: hnqz
 */
package com.qunzhixinxi.hnqz.daemon.quartz.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.common.oss.OssProperties;
import com.qunzhixinxi.hnqz.common.oss.service.OssTemplate;
import com.qunzhixinxi.hnqz.daemon.quartz.entity.WmWkArticle;
import com.qunzhixinxi.hnqz.daemon.quartz.mapper.WmWkArticleMapper;
import com.qunzhixinxi.hnqz.daemon.quartz.service.WmWkArticleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 文库-文章
 *
 * @author pigx code generator
 * @date 2020-08-03 19:27:31
 */
@Service
@AllArgsConstructor
public class WmWkArticleServiceImpl extends ServiceImpl<WmWkArticleMapper, WmWkArticle> implements WmWkArticleService {
	private final OssTemplate minioTemplate;
	private final OssProperties ossProperties;
	private final WmWkArticleMapper wmWkArticleMapper;
	@Override
	public String uploadImg(MultipartFile multipartFile) {
		String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(multipartFile.getOriginalFilename());
		Map<String, String> resultMap = new HashMap<>(4);
		resultMap.put("bucketName", ossProperties.getBucketName());
		resultMap.put("fileName", fileName);
		resultMap.put("url", String.format("/admin/sys-file/%s/%s", ossProperties.getBucketName(), fileName));

		try {
			minioTemplate.putObject(ossProperties.getBucketName(), fileName, multipartFile.getInputStream());
			// 文件管理数据记录,收集管理追踪文件
			//fileLog(file, fileName);
		}
		catch (Exception e) {
			log.error("上传失败", e);
		}
		return String.format("/admin/sys-file/%s/%s", ossProperties.getBucketName(), fileName);
	}


	@Override
	public String uploadImgByByte(InputStream inputStream) {
		String fileName = IdUtil.simpleUUID() + StrUtil.DOT + ".jpg";
		Map<String, String> resultMap = new HashMap<>(4);
		resultMap.put("bucketName", ossProperties.getBucketName());
		resultMap.put("fileName", fileName);
		resultMap.put("url", String.format("/admin/sys-file/%s/%s", ossProperties.getBucketName(), fileName));

		try {
			minioTemplate.putObject(ossProperties.getBucketName(), fileName, inputStream);
			// 文件管理数据记录,收集管理追踪文件
			//fileLog(file, fileName);
		}
		catch (Exception e) {
			log.error("上传失败", e);
		}
		return String.format("/admin/sys-file/%s/%s", ossProperties.getBucketName(), fileName);
	}

	@Override
	public Boolean updateNewFlag() {
		return wmWkArticleMapper.updateNewFlag();
	}

	@Override
	public Boolean setHotArtileTask(String para) {

		return wmWkArticleMapper.setHotArtileTask();
	}
}
