package cn.sikey.hmd.service.qr;

import cn.sikey.framework.common.pojo.CommonResult;
import cn.sikey.hmd.dal.dataobject.DeviceQrcodeDO;
import cn.sikey.hmd.dal.mysql.DeviceQrcodeMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: qin
 * @Date: 2025/3/31
 * @Description: 二维码
 */
@Service
@Slf4j
public class QRCodeServiceImpl implements QRCodeService {

    @Resource
    private DeviceQrcodeMapper deviceQrcodeMapper;

    @Value("${qrcode.url}")
    private String url;

    /*@Resource
    private Map<String, TuLingParamConfig.TuLingConfig> tuLingConfigs;*/

    /**
     * 二维码
     *
     * @param deviceId 设备唯一标识
     * @param apiType  类型
     * @param path     路径
     * @return
     */
    @Override
    public CommonResult<String> generateQRCode(String deviceId, String apiType, String path) {
        DeviceQrcodeDO deviceQrcodeDO = new DeviceQrcodeDO();
        deviceQrcodeDO.setDeviceId(deviceId);

        // 二维码地址
        String qrcode = url + path + deviceId;
        deviceQrcodeDO.setQrcodeContent(qrcode);
        deviceQrcodeDO.setCreator(deviceId);
        deviceQrcodeDO.setUpdater(deviceId);

        LambdaQueryWrapper<DeviceQrcodeDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DeviceQrcodeDO::getDeviceId, deviceId);
        DeviceQrcodeDO existing = deviceQrcodeMapper.selectOne(wrapper);

        if (Objects.isNull(existing)) {
            deviceQrcodeDO.setApiType(apiType);
            deviceQrcodeMapper.insert(deviceQrcodeDO);
        }

        return CommonResult.success(qrcode);
    }

    /**
     * 查询剩余次数，到期时间
     *
     * @param deviceId 设备唯一标识
     * @param apiType  类型
     * @return
     */
    // @Transactional(rollbackFor = Exception.class)
    /*@Override
    public CommonResult<RemainingExpireTimeRespVO> queryRemainingTimesExpireTime(String deviceId, String apiType) {
        DeviceQrcodeDO deviceQrcodeDO = deviceQrcodeMapper.queryByDeviceId(deviceId);
        RemainingTimesExpireTimeRespVO remainingTimesExpireTimeVO = null;
        if (Objects.nonNull(deviceQrcodeDO)) {
            remainingTimesExpireTimeVO = QrCodeConvert.INSTANCE.convertRemainingTimesExpireTimeVO(deviceQrcodeDO);
            int remainingDays = 0;
            *//*if (Objects.isNull(remainingTimesExpireTimeVO.getPlansValidity()) && Objects.nonNull(remainingTimesExpireTimeVO.getRemainingDays())) {
                // 普通用户
                remainingDays = remainingTimesExpireTimeVO.getRemainingDays();
            } else {
                // 会员用户
                remainingDays = DateToolUtil.calculateRemainingDays(remainingTimesExpireTimeVO.getExpireTime());
            }

            if (Objects.nonNull(remainingTimesExpireTimeVO.getPlansValidity())) {
                // 会员用户
                remainingDays = DateTolUtil.calculateRemainingDays(remainingTimesExpireTimeVO.getExpireTime());
            }
            remainingTimesExpireTimeVO.setRemainingDays(remainingDays);
        }
        log.info("[查询每天剩余次数，到期时间]设备编码：{}，返回数据：{}", deviceId, deviceQrcodeDO);
        return CommonResult.success(remainingTimesExpireTimeVO);
    }*/

    /**
     * 更新剩余次数
     *
     * @param updateRemainingTimesReqVO 剩余次数
     * @return
     */
    /*@Override
    public CommonResult<Void> updateRemainingTimes(UpdateRemainingTimesReqVO updateRemainingTimesReqVO) {
        deviceQrcodeMapper.updateRemainingTimes(updateRemainingTimesReqVO);
        DeviceQrcodeDO deviceQrcodeDO = deviceQrcodeMapper.queryByDeviceId(updateRemainingTimesReqVO.getDeviceId());
        RemainingTimesExpireTimeRespVO remainingTimesExpireTimeVO = null;
        if (Objects.nonNull(deviceQrcodeDO)) {
            remainingTimesExpireTimeVO = QrCodeConvert.INSTANCE.convertRemainingTimesExpireTimeVO(deviceQrcodeDO);
        }
        log.info("[更新剩余次数]今日免费次数，更新参数：{}，查询设备二维码数据：{}", updateRemainingTimesReqVO, remainingTimesExpireTimeVO);
        return CommonResult.success();
    }*/

    /**
     * 更新到期时间
     *
     * @param deviceId   设备编号
     * @param expireTime 有效期
     * @return
     */
    /*@Override
    public CommonResult<Void> updateRemainingTimesExpireTime(String deviceId, String expireTime, int remainingTimes, int plansValidity) {
        deviceQrcodeMapper.updateRemainingTimesExpireTime(deviceId, expireTime, remainingTimes, plansValidity);
        return CommonResult.success();
    }*/

    /**
     * 更新设备二维码
     *
     * @param updateDeviceQrcodeReqVO 设备二维码
     * @return
     */
    /*@Override
    public void updateDeviceQrcode(UpdateDeviceQrcodeReqVO updateDeviceQrcodeReqVO) {
        deviceQrcodeMapper.updateDeviceQrcode(updateDeviceQrcodeReqVO);
    }*/

    /**
     * 查询设备二维码
     *
     * @param deviceId 设备编码
     * @return
     */
    /*@Override
    public RemainingTimesExpireTimeRespVO queryDeviceQrcodeDOByDeviceId(String deviceId) {
        DeviceQrcodeDO deviceQrcodeDO = deviceQrcodeMapper.queryByDeviceId(deviceId);
        RemainingTimesExpireTimeRespVO remainingTimesExpireTimeVO = null;
        if (Objects.nonNull(deviceQrcodeDO)) {
            remainingTimesExpireTimeVO = QrCodeConvert.INSTANCE.convertRemainingTimesExpireTimeVO(deviceQrcodeDO);
            // 获取图灵配置
            TuLingParamConfig.TuLingConfig tuLingConfig = tuLingConfigs.get(deviceQrcodeDO.getApiType());
            String apiKey;
            String apiSecret;
            if (Objects.isNull(tuLingConfig)) {
                // TODO 默认skywal，待测试完后删除
                apiKey = "f90aabf8a48d4eaba79c54d1a202534a";
                apiSecret = "7LL7f8oJa69n7mB8";
            } else {
                apiKey = tuLingConfig.getApiKey();
                apiSecret = tuLingConfig.getApiSecret();
            }
            remainingTimesExpireTimeVO.setApiKey(apiKey);
            remainingTimesExpireTimeVO.setApiSecret(apiSecret);
        }
        return remainingTimesExpireTimeVO;
    }*/

}
