package org.springblade.finance.controller;


import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.finance.dto.DateRangeFileDTO;
import org.springblade.finance.entity.YksytAccountFile;
import org.springblade.finance.service.IYksytAccountFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zbc
 * @since 2022-04-14
 */
@RestController
@RequestMapping("finance/yksytAccountFile")
@Api(value = "优酷收银台结算表文件上传", tags = "优酷收银台结算表文件上传接口")
@Slf4j
public class YksytAccountFileController {

	@Autowired
	private IYksytAccountFileService yksytAccountFileService;

	@Value("${blade.file.uploadPath}")
	private String picDir;

	@PostMapping("/uploadFile")
	@ApiOperation("结算表文件上传")
	public R uploadFile(DateRangeFileDTO d){
		if (null == d || d.getFile() == null || d.getSid() == null) {
			return R.fail("参数异常");
		}
		//图片上传
		String oFileName = d.getFile().getOriginalFilename();
		String fileSuffix = oFileName.substring(oFileName.lastIndexOf("."));

		if (!".jpg".equalsIgnoreCase(fileSuffix) &&
			!".jpeg".equalsIgnoreCase(fileSuffix) &&
			!".gif".equalsIgnoreCase(fileSuffix) &&
			!".png".equalsIgnoreCase(fileSuffix) &&
			!".pdf".equalsIgnoreCase(fileSuffix) &&
			!".doc".equalsIgnoreCase(fileSuffix) &&
			!".docx".equalsIgnoreCase(fileSuffix) &&
			!".xls".equalsIgnoreCase(fileSuffix) &&
			!".xlsx".equalsIgnoreCase(fileSuffix)
		) {
			return R.fail("只能上传jpg/jpeg/gif/png/pdf/doc/xls类型的文件");
		}
		String tempFileName = UUID.randomUUID().toString() + fileSuffix;
		File f = new File(picDir +"yksytAccount/"+ tempFileName);
		try {
			FileUtils.copyInputStreamToFile(d.getFile().getInputStream(), f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		String path = "uploads/yksytAccount/" + tempFileName;

		BladeUser bladeUser = AuthUtil.getUser();
		YksytAccountFile accountFile = new YksytAccountFile();
		accountFile.setSid(d.getSid());
		accountFile.setFileName(d.getFile().getOriginalFilename());
		accountFile.setFilePath(path);
		accountFile.setUploadUser(bladeUser.getUserName());
		accountFile.setUploadTime(new Date());
		accountFile.setFileCode(RandomUtil.randomString(20));
		return R.data(yksytAccountFileService.save(accountFile));
	}


	@PostMapping("/removeFile")
	@ApiOperation("删除结算表上传文件")
	public R removeFile(String fileCode){
		if(StringUtils.isBlank(fileCode)){
			return R.fail("文件编码不能为空");
		}
		return R.data(yksytAccountFileService.remove(new LambdaQueryWrapper<YksytAccountFile>()
			.eq(YksytAccountFile::getFileCode,fileCode))
		);
	}

}

