package com.ho.user.controller;

import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.sun.jna.Library;
import com.sun.jna.Native;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(ContextConstant.SYS + "checkLicence")
@Slf4j
public class CheckLicenceController {

    public interface NativeLibrary extends Library {
        int CheckLicence();
        NativeLibrary INSTANCE = (NativeLibrary) Native.loadLibrary("/usr/lib/libauth_lib.so", NativeLibrary.class);
    }

    @PostMapping("check")
    @ApiOperation(value = "检查licence")
    @TokenIgnore
    public DataResult check() {
        log.info("begin to checkLicence");
        // -1 失败  0 成功
        int result = NativeLibrary.INSTANCE.CheckLicence();
        log.info("check result {}",result);
        if(CommonConstant.ZERO != result){
            throw new BusinessException(BaseResponseCode.LOSE_LICENCE);
        }
        log.info("end to checkLicence");
        return DataResult.success();
    }


}
