package com.yifu.cloud.plus.v1.yifu.insurances.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.FileVo;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.insurances.entity.TInsuranceEnclosure;
import com.yifu.cloud.plus.v1.yifu.insurances.mapper.TInsuranceEnclosureMapper;
import com.yifu.cloud.plus.v1.yifu.insurances.service.TInsuranceEnclosureService;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.InsuranceEnclosureParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;

/**
* @author Administrator
* @description 针对表【t_insurance_enclosure】的数据库操作Service实现
* @createDate 2022-08-22 16:00:46
*/
@Service
@Slf4j
public class TInsuranceEnclosureServiceImpl extends ServiceImpl<TInsuranceEnclosureMapper, TInsuranceEnclosure> implements TInsuranceEnclosureService {


	private final OSSUtil ossUtil;

	public TInsuranceEnclosureServiceImpl(OSSUtil ossUtil) {
		this.ossUtil = ossUtil;
	}

	/**
	 * 上传商险附件
	 *
	 * @author zhaji
	 * @param file 文件
	 * @param filePath 文件路径
	 * @param remark 备注
	 * @return {@link R}
	 * @throws IOException
	 */
	@Override
	public R uploadInsuranceEnclosure(MultipartFile file, String filePath, String remark) throws IOException {
		String fileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
		String enclosureName = fileName.substring(0, fileName.lastIndexOf("."));
		//filePath不传默认存储空间的根目录
		//jpg,jpeg,png,bmp
		String key = "";
		if (!Common.isNotNull(filePath)) {
			key = fileName;
		} else {
			key = filePath + "/" + fileName;
		}
		boolean flag = ossUtil.uploadFileByStream(file.getInputStream(), key, null);
		FileVo fileVo;
		TInsuranceEnclosure insuranceEnclosure = new TInsuranceEnclosure();
		URL url = null;
		if (flag) {
			log.info("文件：" + fileName + "上传至存储空间" + ossUtil.getBucketName() + "成功！");
			insuranceEnclosure.setDeleteFlag(CommonConstants.ZERO_INT);
			insuranceEnclosure.setRemark(remark);
			insuranceEnclosure.setEnclosureName(enclosureName);
			insuranceEnclosure.setEnclosureAddress(key);
			try {
				this.save(insuranceEnclosure);
			} catch (Exception e) {
				log.error("OSS文件上传接口异常：" + e.getMessage());
				ossUtil.deleteObject(null, key);
				return R.failed("failed:" + e.getMessage());
			}
			url = ossUtil.getObjectUrl(null, insuranceEnclosure.getEnclosureAddress());
			fileVo = new FileVo(insuranceEnclosure.getId().toString(), insuranceEnclosure.getEnclosureName(), url.toString());
			return R.ok(fileVo);
		} else {
			return R.failed("failed:上传至存储空间失败");
		}
	}

	/**
	 * 获取附件列表
	 *
	 * @author zhaji
	 * @param page 分页参数
	 * @param param 查询参数
	 * @return {@link R}
	 */
	@Override
	public R getInsuranceEnclosurePage(Page<InsuranceEnclosureParam> page, InsuranceEnclosureParam param) {
		return R.ok(this.baseMapper.getInsuranceEnclosurePage(page,param));
	}

	/**
	 * 获取商险附件地址
	 *
	 * @author zhaji
	 * @param id 附件id
	 * @return {@link R}
	 */
	@Override
	public R getInsuranceEnclosureUrl(String id) {
		if(Common.isEmpty(id)){
			return R.failed("附件id为空");
		}
		TInsuranceEnclosure insuranceEnclosure = getById(id);
		if (null == insuranceEnclosure) {
			return R.failed("无对应附件信息");
		}
		String enclosureAddress = insuranceEnclosure.getEnclosureAddress();
		log.info("key:"+enclosureAddress);
		URL url = ossUtil.getObjectUrl(null, insuranceEnclosure.getEnclosureAddress());
		return new R<>(url.toString());
	}

	/**
	 * 删除商险附件
	 *
	 * @author zhaji
	 * @param id 附件id
	 * @return {@link R}
	 */
	@Override
	public R deleteInsuranceEnclosure(String id) {
		if(Common.isEmpty(id)){
			return R.failed("附件id为空");
		}
		TInsuranceEnclosure insuranceEnclosure = getById(id);
		if (null == insuranceEnclosure) {
			return R.failed("无对应附件信息");
		}
		try {
			if (removeById(id)) {
				ossUtil.deleteObject(null, insuranceEnclosure.getEnclosureAddress());
				return R.ok("删除成功！");
			}
		} catch (Exception e) {
			log.error("附件删除异常：" + e.getMessage());
			return R.failed("附件删除异常！");
		}
		return R.ok("删除失败！");
	}
}
