/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 运维系统
 * 创建日期:2023/9/13
 */
package org.springblade.modules.yjxt.base.map.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.base.map.dto.DangerAreaDTO;
import org.springblade.modules.yjxt.base.map.entity.DangerAreaEntity;
import org.springblade.modules.yjxt.base.map.mapper.DangerAreaMapper;
import org.springblade.modules.yjxt.base.map.service.IDangerAreaService;
import org.springblade.modules.yjxt.base.map.vo.DangerAreaVO;
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:2023/9/13
 * Title:文件所属模块（必须填写）
 * Description：对本文件的详细描述，原则上不能少于30字
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Service
@Slf4j
public class DangerAreaServiceImpl extends BaseServiceImpl<DangerAreaMapper, DangerAreaEntity> implements IDangerAreaService {
    @Resource
    private OssBuilder ossBuilder;

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

    @Value("${spring.yjxt.dangerarea-resident-reg-qr}")
    private String dangerAreaResidentRegQr;


    @Override
    public IPage<DangerAreaVO> selectPage(IPage<DangerAreaVO> page, DangerAreaDTO dto) {
        return page.setRecords(baseMapper.selectPage(page, dto));
    }

    @Override
    public List<DangerAreaVO> selectAdDangerArea(DangerAreaDTO dto) {
        return baseMapper.selectAdDangerArea(dto);
    }

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

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