/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sys.service;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.oss.OSSClientUtil;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.isignature.service.ISignatureDocumentService;
import com.jeeplus.modules.sys.dao.WorkattachmentDao;
import com.jeeplus.modules.sys.entity.Workattachment;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.*;

/**
 * 附件信息Service
 * @author yangfan
 * @version 2017-10-11
 */
@Service
@Transactional(readOnly = true)
public class WorkattachmentService extends CrudService<WorkattachmentDao, Workattachment> {

	@Autowired
	private WorkattachmentDao workattachmentDao;

	public Workattachment get(String id) {
		return super.get(id);
	}

	public List<Workattachment> findList(Workattachment workattachment) {
		return super.findList(workattachment);
	}
	
	public Page<Workattachment> findPage(Page<Workattachment> page, Workattachment workattachment) {
		return super.findPage(page, workattachment);
	}
	
	@Transactional(readOnly = false)
	public void save(Workattachment workattachment) {
		super.save(workattachment);
	}

	@Transactional(readOnly = false)
	public void insert(Workattachment workattachment) {
		dao.insert(workattachment);
	}
	@Transactional(readOnly = false)
	public void update(Workattachment workattachment) {
		dao.update(workattachment);
	}

	@Transactional(readOnly = false)
	public void delete(Workattachment workattachment) {
		super.delete(workattachment);
	}

	@Transactional(readOnly = false)
	public void deleteByUrl(Workattachment workattachment) {
		dao.deleteByUrl(workattachment);
	}

	//用户可能在多个公司任职，只查看当前公司的评价表
	//设计是一个用户在一个公司有一个评价表，如果意外情况出现多个，取get(0)，临时策略；
	public Workattachment getByAttachmentIdAndCompanyIdAndFlag(String attachmentId,String companyId,String attachmentFlag) {
		Workattachment workattachment = new Workattachment();
		Map<String,String> map = new HashMap<>();
		map.put("attachmentId",attachmentId);
		map.put("companyId",companyId);
		map.put("attachmentFlag",attachmentFlag);
		List<Workattachment> workattachmentList = workattachmentDao.getByAttachmentIdAndCompanyIdAndFlag(map);
		if(workattachmentList!=null && workattachmentList.size()>0){
			workattachment = workattachmentList.get(0);
		}
		return workattachment;
	}

	//上传文件
	@Transactional(readOnly = false)
	public String uploadattachment(MultipartHttpServletRequest multiRequest, String id ,String srcName,String flag){
		OSSClientUtil ossUtil = new OSSClientUtil();
		Iterator<String> fileNames = multiRequest.getFileNames();
		while (fileNames.hasNext()) {
			MultipartFile file = multiRequest.getFile(fileNames.next());
			String fileName = file.getOriginalFilename();
			if(StringUtils.isNotBlank(fileName)) {
				String fileType = fileName.substring(fileName.lastIndexOf(".")+ 1);
				if (file != null && !file.isEmpty()) {
					String aliyunPath = ossUtil.uploadFile2OSS(file, srcName);
					Workattachment workattachment = new Workattachment();
					workattachment.setUrl(aliyunPath);
					workattachment.setType(fileType);
					workattachment.setAttachmentId(id);
					workattachment.setAttachmentUser(UserUtils.getUser().getId());
					workattachment.setAttachmentName(fileName);
					workattachment.setCompanyId(UserUtils.getSelectCompany().getId());
					workattachment.setAttachmentFlag(flag);
					this.save(workattachment);
					return workattachment.getId();
				}
			}
		}

		return null;
	}

	@Transactional(readOnly = false)
	public Workattachment uploadFiletoAly(MultipartHttpServletRequest multiRequest) {
		String srcName = multiRequest.getParameter("srcName");
		String id = multiRequest.getParameter("id");
		String attachmentFlag = multiRequest.getParameter("attachmentFlag");
		Iterator<String> fileNames = multiRequest.getFileNames();
		while(fileNames.hasNext()){
			MultipartFile file = multiRequest.getFile(fileNames.next());
			String fileName = file.getOriginalFilename();
			String fileType = fileName.substring(fileName.lastIndexOf(".")+1);
			if(file != null && !file.isEmpty()) {
				OSSClientUtil ossClientUtil = new OSSClientUtil();
				String url = ossClientUtil.uploadFile2OSS(file,srcName);
				Workattachment workattachment = new Workattachment();
				workattachment.setUrl(url);
				workattachment.setType(fileType);
				workattachment.setAttachmentName(fileName);
				workattachment.setAttachmentUser(UserUtils.getUser().getId());
				workattachment.setAttachmentId(id);
				workattachment.setAttachmentFlag(attachmentFlag);
				if (StringUtils.isNotBlank(id)){
					workattachment.setCompanyId(UserUtils.getSelectCompany().getId());
					this.save(workattachment);
				}
				return workattachment;
			}
		}
		return null;
	}

	//删除附件
	//阿里云文件地址http://gangwan-app.oss-cn-hangzhou.aliyuncs.com/attachment-file/workBidingDocument/3fcf03e37b3ba0fd5c7821998e392395.doc
	//其中阿里云删除接口所需的参数 key 指的是 attachment-file/workBidingDocument/3fcf03e37b3ba0fd5c7821998e392395.doc
	@Transactional(readOnly = false)
	public void deleteFileFromAliyun(Workattachment workattachment,String url) {
		if(workattachment != null){
			this.delete(workattachment);
		}
		if (StringUtils.isNotBlank(url)) {
			String aliyunUrl = Global.getAliyunUrl();
			String aliDownloadUrl = Global.getAliDownloadUrl();
			String cons = "";
			if (url.contains(aliyunUrl)){
				cons = aliyunUrl;
			}else if (url.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")){
				cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
			}else {
				cons = aliDownloadUrl;
			}
			final String[] arr = url.split(cons + "/");
			String key = arr[1];
			logger.info("key="+key);
			OSSClientUtil ossClientUtil = new OSSClientUtil();
			if (StringUtils.isNotBlank(key)) {
				ossClientUtil.deleteSingleObject(key);
			}
			Workattachment workattachment1 = new Workattachment();
			workattachment1.setUrl(url);
			deleteByUrl(workattachment1);
		}

	}

	public List<Workattachment> findAttachment(String attachmentId,String type) {
		List<Workattachment>  workattachmentList = new ArrayList<>();
		if(StringUtils.isNotBlank(attachmentId)){
			if(StringUtils.isNotBlank(attachmentId) && StringUtils.isNotBlank(type)){
				workattachmentList = workattachmentDao.getListOfAttachment(attachmentId,type);
			}
		}
		return workattachmentList ;
	}

	//查找附件
	public Workattachment getByAttachmentIdAndFlag(String attachmentId, String flag) {
		Map<String , String> map = new HashMap<String,String>();
		map.put("attachmentId",attachmentId);
		map.put("attachmentFlag",flag);
		return workattachmentDao.getByAttachmentIdAndFlag(map);
	}

	public List<Workattachment> getListByAttachmentIdAndFlag(String attachmentId,String flag){
		Map<String,String> map = new HashMap<>();
		map.put("attachmentId",attachmentId);
		map.put("attachmentFlag",flag);
		return workattachmentDao.getListByAttachmentIdAndFlag(map);
	}

}