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

package com.yami.shop.platform.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.AttachFile;
import com.yami.shop.common.bean.Qiniu;
import com.yami.shop.common.config.Constant;
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.platform.util.SecurityUtils;
import com.yami.shop.service.AttachFileService;
import org.apache.ibatis.annotations.Delete;
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;
import com.yami.shop.common.util.R;
/**
 * 文件上传管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-11-26
 */
@RestController
@RequestMapping("/admin/file")
public class FileController {

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

	@PostMapping("/upload/element")
	public R<String> uploadElementFile(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
            return R.FAIL("文件不能为空");
        }
		String fileName = attachFileService.uploadFile(file.getBytes(),file.getOriginalFilename());
        return R.SUCCESS(fileName);
	}

	@PostMapping("/upload/img")
	public R<String> uploadImg(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
			return R.FAIL("文件不能为空");
		}
		AttachFile attachFile = new AttachFile();
		attachFile.setFileType(FileUtil.extName(file.getOriginalFilename()));
		attachFile.setFileName(FileUtil.mainName(file.getOriginalFilename()));
		attachFile.setType(1);
		attachFile.setShopId(Constant.PLATFORM_SHOP_ID);
		attachFile.setUploadTime(new Date());
		String fileName = attachFileService.uploadImg(file.getBytes(),attachFile);
		return R.SUCCESS(fileName);
	}

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

	/**
	 * 分页获取历史图片
	 */
	@GetMapping("/attachFilePage")
	public R<IPage<AttachFile>> attachFilePage(PageParam<AttachFile> page, AttachFile attachFile) {
		attachFile.setShopId(Constant.PLATFORM_SHOP_ID);
		IPage<AttachFile> attachFilePage = attachFileService.getPage(page,attachFile);
//		IPage<AttachFile> attachFilePage = attachFileService.page(page,new LambdaQueryWrapper<AttachFile>()
//				.eq(AttachFile::getShopId,Constant.PLATFORM_SHOP_ID)
//				.eq(AttachFile::getType,1)
//				.like(StrUtil.isNotBlank(attachFile.getFileName()),AttachFile::getFileName,attachFile.getFileName())
//				.orderByDesc(AttachFile::getUploadTime));
		return R.SUCCESS(attachFilePage);
	}
	/**
	 * 删除图片
	 */
	@DeleteMapping("/deleteFile/{fileId}")
	public R<Void> deleteFile(@PathVariable("fileId") Long fileId){
		AttachFile attachFile = attachFileService.getById(fileId);
		attachFileService.deleteFile(attachFile.getFilePath());
		return R.SUCCESS();
	}


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