<?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.dora.mapper.PointsConfigMapper">

    <select id="findAll" resultType="com.dora.entity.PointsConfig">
        SELECT * FROM points_config WHERE is_deleted = 0 ORDER BY id ASC
    </select>

    <select id="findById" parameterType="long" resultType="com.dora.entity.PointsConfig">
        SELECT * FROM points_config WHERE id = #{id} AND is_deleted = 0
    </select>

    <select id="findByModelName" parameterType="string" resultType="com.dora.entity.PointsConfig">
        SELECT * FROM points_config WHERE model_name = #{modelName} AND is_deleted = 0
    </select>

    <insert id="insert" parameterType="com.dora.entity.PointsConfig" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO points_config (model_name, points_cost, description, is_enabled)
        VALUES (#{modelName}, #{pointsCost}, #{description}, #{isEnabled})
    </insert>

    <update id="update" parameterType="com.dora.entity.PointsConfig">
        UPDATE points_config
        <set>
            <if test="modelName != null">model_name = #{modelName},</if>
            <if test="pointsCost != null">points_cost = #{pointsCost},</if>
            <if test="description != null">description = #{description},</if>
            <if test="isEnabled != null">is_enabled = #{isEnabled},</if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="delete" parameterType="long">
        UPDATE points_config SET is_deleted = 1 WHERE id = #{id}
    </update>

    <select id="findAllEnabled" resultType="com.dora.entity.PointsConfig">
        SELECT * FROM points_config 
        WHERE is_deleted = 0 AND is_enabled = 1 
        ORDER BY id ASC
    </select>

    <select id="findByProviderType" resultType="com.dora.entity.PointsConfig">
        SELECT * FROM points_config 
        WHERE is_deleted = 0
        <if test="providerType != null and providerType != ''">
            AND provider_type = #{providerType}
        </if>
        <if test="enabledOnly != null and enabledOnly == true">
            AND is_enabled = 1
        </if>
        ORDER BY id ASC
    </select>

    <select id="findByTaskType" resultType="com.dora.entity.PointsConfig">
        SELECT * FROM points_config 
        WHERE is_deleted = 0
        <if test="taskType != null and taskType != ''">
            AND task_type = #{taskType}
        </if>
        <if test="enabledOnly != null and enabledOnly == true">
            AND is_enabled = 1
        </if>
        ORDER BY id ASC
    </select>

    <select id="findByProviderAndTaskType" resultType="com.dora.entity.PointsConfig">
        SELECT * FROM points_config 
        WHERE is_deleted = 0
        <if test="providerType != null and providerType != ''">
            AND provider_type = #{providerType}
        </if>
        <if test="taskType != null and taskType != ''">
            AND task_type = #{taskType}
        </if>
        <if test="enabledOnly != null and enabledOnly == true">
            AND is_enabled = 1
        </if>
        ORDER BY id ASC
    </select>

</mapper>
