/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.multishop.controller;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.AttachFile;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.AttachFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.Objects;

/**
 * 文件上传 controller
 * @author lgh
 *
 */
@RestController
@RequestMapping("/admin/file")
public class FileController {

	@Autowired
	private AttachFileService attachFileService;
	@Autowired
	private ShopConfig shopConfig;

	@PostMapping("/upload/element")
	public ResponseEntity<String> uploadElementFile(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
            return ResponseEntity.noContent().build();
        }
		String fileName = attachFileService.uploadFile(file.getBytes(),file.getOriginalFilename());
        return ResponseEntity.ok(fileName);
	}

	@PostMapping("/upload/img")
	public ResponseEntity<String> uploadImg(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
			return ResponseEntity.noContent().build();
		}
		Long shopId = SecurityUtils.getShopUser().getShopId();
		AttachFile attachFile = new AttachFile();
		attachFile.setFileType(FileUtil.extName(file.getOriginalFilename()));
		attachFile.setFileName(FileUtil.mainName(file.getOriginalFilename()));
		attachFile.setType(1);
		attachFile.setShopId(shopId);
		attachFile.setUploadTime(new Date());
		String fileName = attachFileService.uploadImg(file.getBytes(),attachFile);
		return ResponseEntity.ok(fileName);
	}

	@PostMapping("/upload/tinymceEditor")
	public ResponseEntity<String> uploadTinymceEditorImages(@RequestParam("editorFile") MultipartFile editorFile) throws IOException{
		String fileName =  attachFileService.uploadFile(editorFile.getBytes(),editorFile.getOriginalFilename());
        return ResponseEntity.ok(shopConfig.getDomain().getResourcesDomainName() + "/" + fileName);
	}

	/**
	 * 分页获取历史图片
	 */
	@GetMapping("/attachFilePage")
	public ResponseEntity<IPage<AttachFile>> attachFilePage(PageParam<AttachFile> page, AttachFile attachFile) {
		attachFile.setShopId(SecurityUtils.getShopUser().getShopId());
		IPage<AttachFile> attachFilePage = attachFileService.getPage(page,attachFile);
		return ResponseEntity.ok(attachFilePage);
	}

	/**
	 * 删除图片
	 */
	@DeleteMapping("/deleteFile/{fileId}")
	public ResponseEntity<Void> deleteFile(@PathVariable("fileId") Long fileId){
		AttachFile attachFile = attachFileService.getById(fileId);
		attachFileService.deleteFile(attachFile.getFilePath());
		return ResponseEntity.ok().build();
	}

	/**
	 * 更改图片名称
	 */
	@PutMapping("/updateFileName")
	public ResponseEntity<Boolean> updateFileName(@RequestBody AttachFile attachFile) {
		if (Objects.isNull(attachFile.getFileName())){
			throw new GlobalException("图片名称不能为空");
		}
		return ResponseEntity.ok(attachFileService.updateFileName(attachFile));
	}

}
