/**
 * Copyright (c) 2022 雷掣 All rights reserved.
 *
 * https://www.lc_crm.com
 *
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import io.renren.annotation.Login;
import io.renren.cloud.OSSFactory;
import io.renren.common.exception.ErrorCode;
import io.renren.common.utils.Result;
import io.renren.entity.SysOssEntity;
import io.renren.service.SysOssService;
import io.renren.util.IdCardUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文件上传
 * 
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("api/oss")
@Api(tags="文件上传")
public class SysOssController extends AbstractController{
	@Autowired
	private SysOssService sysOssService;

	@ApiOperation(value="查询当前登录用户的文件",httpMethod="POST",notes="")
	@RequestMapping("/getFileByUser")
    @Login
	public Result<List<SysOssEntity>> queryFileByType(){
    	return new Result<List<SysOssEntity>>().ok(sysOssService.getFileByUser(getUserId()));
	}

	@PostMapping("upload")
	@ApiOperation(value = "上传文件")
	@Login
	public Result<Map<String, Object>> upload(@RequestParam("front") MultipartFile front,@RequestParam("negative") MultipartFile negative) throws Exception {
		if (front.isEmpty() || negative.isEmpty()) {
			return new Result<Map<String, Object>>().error(ErrorCode.UPLOAD_FILE_EMPTY);
		}

		Map<String, String> bodys = new HashMap<String, String>();
		bodys.put("front", Base64.encodeBase64String(front.getBytes()));
		bodys.put("negative", Base64.encodeBase64String(negative.getBytes()));
		String idCard = IdCardUtils.doPost(bodys);
		if(null == idCard) {
			return new Result<Map<String, Object>>().error("身份证识别失败");
		}
		
		//上传文件
		String frontExtension = FilenameUtils.getExtension(front.getOriginalFilename());
		String frontUrl = OSSFactory.build().uploadSuffix(front.getBytes(), frontExtension);
		//保存文件信息
		SysOssEntity frontEntity = new SysOssEntity();
		frontEntity.setUserId(getUserId());
		frontEntity.setUrl(frontUrl);
		frontEntity.setCreateDate(new Date());
		frontEntity.setType("02");
		sysOssService.insert(frontEntity);
		
		//上传文件
		String negativeExtension = FilenameUtils.getExtension(negative.getOriginalFilename());
		String negativeUrl = OSSFactory.build().uploadSuffix(negative.getBytes(), negativeExtension);
		//保存文件信息
		SysOssEntity negativeEntity = new SysOssEntity();
		negativeEntity.setUserId(getUserId());
		negativeEntity.setUrl(negativeUrl);
		negativeEntity.setCreateDate(new Date());
		negativeEntity.setType("01");
		sysOssService.insert(negativeEntity);

		Map<String, Object> data = new HashMap<>(1);
		data.put("idCard", idCard);
		data.put("frontUrl", frontUrl);
		data.put("negativeUrl", negativeUrl);

		return new Result<Map<String, Object>>().ok(data);
	}

	@DeleteMapping
	@ApiOperation(value = "删除")
	@Login
	public Result<Boolean> delete(@RequestBody Long[] ids){
		sysOssService.deleteBatchIds(Arrays.asList(ids));
		return new Result<Boolean>();
	}

}