<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.system.mapper.SysLicenseMapper">

    <resultMap type="SysLicense" id="SysLicenseResult">
        <result property="id" column="id"/>
        <result property="licenseKey" column="license_key"/>
        <result property="expireDate" column="expire_date"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectLicenseVo">
        select id, license_key, expire_date, create_time from sys_license
    </sql>

    <select id="selectLicense" resultMap="SysLicenseResult">
        <include refid="selectLicenseVo"/>
        order by id desc limit 1
    </select>

    <insert id="insertLicense" parameterType="SysLicense" useGeneratedKeys="true" keyProperty="id">
        insert into sys_license (
        <if test="licenseKey != null and licenseKey != ''">license_key,</if>
        <if test="expireDate != null">expire_date,</if>
        create_time
        ) values (
        <if test="licenseKey != null and licenseKey != ''">#{licenseKey},</if>
        <if test="expireDate != null">#{expireDate},</if>
        NOW()
        )
    </insert>

    <delete id="deleteLicense" flushCache="true">
        DELETE FROM sys_license
    </delete>

    <select id="checkValidLicense" parameterType="Date" resultType="int">
        SELECT COUNT(1)
        FROM sys_license
        WHERE expire_date >= #{currentDate,jdbcType=TIMESTAMP}
          AND license_key IS NOT NULL
    </select>
</mapper>
