/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.yifu.cloud.plus.v1.yifu.archives.service.impl;

import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TElecEmployeeInfo;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TElecEmployeeInfoMapper;
import com.yifu.cloud.plus.v1.yifu.archives.service.TElecEmployeeInfoService;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;

/**
 * 电子档案信息表
 *
 * @author fxj
 * @date 2022-06-18 09:39:46
 */
@Service
public class TElecEmployeeInfoServiceImpl extends ServiceImpl<TElecEmployeeInfoMapper, TElecEmployeeInfo> implements TElecEmployeeInfoService {

	@Override
	public boolean save(TElecEmployeeInfo entity) {
		return super.save(entity);
	}

	@Override
	public IPage<TElecEmployeeInfo> getTElecEmployeeInfoPage(Page page, TElecEmployeeInfo tElecEmployeeInfo) {
		return baseMapper.getTElecEmployeeInfoPage(page,tElecEmployeeInfo);
	}

	/**
	 * @description: 解析zip文件包中的多个附件，并生成结算记录
	 * @param file zip文件包
	 * @return: com.yifu.cloud.v1.common.core.util.R<java.lang.Boolean>
	 * @author: wangweiguo
	 * @date: 2021/8/19
	 */
	@Override
	public R<Boolean> importZip(MultipartFile file) {

		File unzip = null;
		File etractFile = null;
		try {
			String fileType = FileTypeUtil.getType(file.getInputStream());
//			if (!StringUtils.equalsIgnoreCase(fileType, CommonConstants.ZIP_TYPE)) {
//				return R.failed(ErrorCodes.IMPORT_FILE_TYPE_ERROR);
//			}

			List list = new ArrayList();

			etractFile = new File(UUID.randomUUID().toString());
			unzip = ZipUtil.unzip(file.getInputStream(), etractFile, CharsetUtil.CHARSET_GBK);

			ZipFile zipFile=new ZipFile(unzip);

			for (Enumeration<? extends ZipArchiveEntry> e = zipFile.getEntries(); e.hasMoreElements();) {
				ZipArchiveEntry entry=e.nextElement();

				if(!entry.isDirectory()){
					BufferedReader br=new BufferedReader(new InputStreamReader(zipFile.getInputStream(entry)));

					String line = "";

					while((line = br.readLine()) != null){
						if(line.length() > 0){
							list.add(line);

						}
					}
					br.close();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (etractFile != null) {
				FileUtil.del(etractFile);
			}
		}
		return R.ok();
	}
}
