package cn.cecep.talroad.service.impl;

import cn.cecep.talroad.common.core.utils.StringUtils;
import cn.cecep.talroad.domain.AppVersionAddressManagement;
import cn.cecep.talroad.mapper.AppVersionAddressManagementMapper;
import cn.cecep.talroad.service.IAppVersionAddressManagementService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * @Description: app版本管理表
 * @Author: code-generate
 * @Date:   2023-07-06
 * @Version: V1.0
 */
@Service
public class AppVersionAddressManagementServiceImpl extends ServiceImpl<AppVersionAddressManagementMapper, AppVersionAddressManagement> implements IAppVersionAddressManagementService {


    /**
     * 展示 base64格式的图片 所需前缀
     */
    private String BASE64_CODE_PREFIX = "data:image/jpg;base64,";


    /**
     * 是否生产 0: 测试 1: 生产  默认为生产
     */
    private Integer isDebugThis = 1;

    @Override
    public AppVersionAddressManagement findUpToDateVersionAddress(Integer isDebug) {
        return baseMapper.findUpToDateVersionAddress(isDebug);
    }

    @Override
    public Object generateAppAddressQrCode(String url, String logUrl, Integer isDebug, String ipOrigin) throws Exception {
        if (isDebug != null) {
            isDebugThis = isDebug;
        }
        //查询最新apk地址
        AppVersionAddressManagement versionAddress = baseMapper.findUpToDateVersionAddress(isDebugThis);
        if (ObjectUtil.isNull(versionAddress)) {
            return "没有生产的任何版本的apk";
        }

        //是否指定链接  默认为apk的链接
        if (StringUtils.isEmpty(url) && StringUtils.isEmpty(ipOrigin) ) {
            url = versionAddress.getDownloadUrl();
        }

        // TODO 暂无 内嵌log图片  没有访问的图片 默认不加
        if (StringUtils.isEmpty(logUrl)) {
            logUrl = versionAddress.getLogImgUrl();
        }
        return null;
//        String encode = QrCodeUtils.createQrCodeBase64Encode(url, StrUtil.isNotEmpty(logUrl)? FileUtil.file(logUrl): null,  true);
//        return BASE64_CODE_PREFIX + encode;
    }
}
