/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2022/8/24
 */
package org.springblade.modules.yjxt.business.warn.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.modules.resource.builder.oss.OssBuilder;
import org.springblade.modules.yjxt.business.warn.dto.OriginalWarningInfoDTO;
import org.springblade.modules.yjxt.business.warn.entity.OriginalWarningInfoEntity;
import org.springblade.modules.yjxt.business.warn.mapper.OriginalWarningMapper;
import org.springblade.modules.yjxt.business.warn.service.IOriginalWarningService;
import org.springblade.utils.QRCodeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/***
 * Date:2022/8/24
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Service
@Slf4j
public class OriginalWarningServiceImpl extends BaseServiceImpl<OriginalWarningMapper, OriginalWarningInfoEntity> implements IOriginalWarningService {

    @Resource
    private OssBuilder ossBuilder;

    @Value("${spring.yjxt.qrdir}")
    private String qrDir;

    @Value("${spring.yjxt.transfer-reg-qr}")
    private String transferRegQr;


    @Override
    public IPage<OriginalWarningInfoEntity> selectPage(IPage<OriginalWarningInfoEntity> page, OriginalWarningInfoEntity entity) {
        return page.setRecords(baseMapper.selectPage(page, entity));
    }

    @Override
    public String createQr(String warnId) {
        try {
            String qrFileName = "warn-qr-" + warnId + ".jpg";
            File file = new File(qrDir + File.separator + qrFileName);
            if (file.exists()) {
                if (!file.delete()) {
                    return null;
                }
            }
            String content = transferRegQr + "?warnId=" + warnId;
            QRCodeUtils.encode2(content, "warn-qr-" + warnId, qrDir);
            FileInputStream fileInputStream = new FileInputStream(new File(qrDir + File.separator + "warn-qr-" + warnId + ".jpg"));
            BladeFile bladeFile = ossBuilder.template().putFile(qrFileName, fileInputStream);
            return bladeFile.getName();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return null;
    }

    @Override
    public List<OriginalWarningInfoEntity> selectDangerAreaList(OriginalWarningInfoDTO dto) {
        return baseMapper.selectDangerAreaList(dto);
    }
}
