package org.springblade.modules.csjcInfo.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.csjcInfo.entity.CsjcInfoEntity;
import org.springblade.modules.csjcInfo.excel.CsjcInfoExcel;
import org.springblade.modules.csjcInfo.mapper.CsjcInfoMapper;
import org.springblade.modules.csjcInfo.service.ICsjcInfoService;
import org.springblade.modules.csjcInfo.vo.CsjcInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 场所检查 服务实现类
 *
 * @author BladeX
 * @since 2023-07-21
 */
@Service
public class CsjcInfoServiceImpl extends BaseServiceImpl<CsjcInfoMapper, CsjcInfoEntity> implements ICsjcInfoService {

	@Autowired
	private CsjcInfoJlServiceImpl csjcInfoJlService;

	@Override
	public IPage<CsjcInfoVO> selectInfoPage(IPage<CsjcInfoVO> page, Map<String, Object> params) {
		String str = "";
		String strEnd = "";
		if (params.get("datetime") != null) {
			String time = params.get("datetime").toString();
			String[] datetime = time.split(",");
			str = datetime[0];
			strEnd = datetime[1];
		}
		return page.setRecords(baseMapper.selectInfoPage(page, params, str, strEnd));
	}


	@Override
	public List<CsjcInfoExcel> exportInfo(Map<String, Object> params) {
		List<CsjcInfoExcel> infoList = baseMapper.exportInfo(params);
		return infoList;
	}

	@Override
	public IPage<CsjcInfoVO> getPageByCsId(IPage<CsjcInfoVO> page, Map<String, Object> params) {
		return page.setRecords(baseMapper.getPageByCsId(page, params));
	}

	@Override
	public CsjcInfoVO getDetail(Long id) {
		String deptId = AuthUtil.getDeptId();
		return baseMapper.getDetail(id,deptId);
	}

	@Override
	public IPage<CsjcInfoVO> getQsfkList(IPage<Object> page, Map<String, Object> params) {
		return baseMapper.getQsfkList(page, params);
	}

	@Override
	public IPage<CsjcInfoVO> getZgList(IPage<Object> page, Map<String, Object> params) {
		return baseMapper.getZgList(page, params);
	}

	@Override
	public Long getSqId(String id) {
		return baseMapper.getSqId(id);
	}


	/**
	 * 百分号编码工具方法
	 *
	 * @param s 需要百分号编码的字符串
	 * @return 百分号编码后的字符串
	 */
	public static String percentEncode(String s) throws UnsupportedEncodingException {
		String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
		return encode.replaceAll("\\+", "%20");
	}
	public void down(HttpServletResponse response, String filePath, String realFileName) {
		String percentEncodedFileName = null;
		try {
			percentEncodedFileName = percentEncode(realFileName);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		StringBuilder contentDispositionValue = new StringBuilder();
		contentDispositionValue.append("attachment; filename=").append(percentEncodedFileName).append(";").append("filename*=").append("utf-8''").append(percentEncodedFileName);

		response.addHeader("Access-Control-Allow-Origin", "*");
		response.addHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename");
		response.setHeader("Content-disposition", contentDispositionValue.toString());
		response.setHeader("download-filename", percentEncodedFileName);
		try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath));
			 // 输出流
			 BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());) {
			byte[] buff = new byte[1024];
			int len = 0;
			while ((len = bis.read(buff)) > 0) {
				bos.write(buff, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public CsjcInfoEntity getPublicCsjcInfo(Long id) {
		return baseMapper.getPublicCsjcInfo(id);
	}

	@Override
	public List<CsjcInfoEntity> getPublicCsjcList(Long csId) {
		return baseMapper.getPublicCsjcList(csId);
	}

	@Override
	public List<String> getJidu() {
		return baseMapper.getJidu();
	}

	@Override
	public void updateCreateTime(Long id, Date day) {
		baseMapper.updateCreateTime(id, day);
	}

	@DS("jl")
	@Override
	public void saveToJl(CsjcInfoEntity info) {
		csjcInfoJlService.save(info);
	}
}
