<?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.ga.core.mapper.dict.DataDomainMapper">

    <resultMap id="DataDomainRespMap" type="com.ga.core.vo.dict.DataDomainResp"/>
    <resultMap id="DataDomainOptionMap" type="com.ga.core.vo.dict.DataDomainOptionResp"/>

    <sql id="domainCols">
        domain_code, domain_name, domain_category, data_type,
        length, precision, scale, format_pattern, validation_regex,
        example_value, masking_type, encrypted, description, is_active,
        created_at, created_by, updated_at, updated_by
    </sql>

    <select id="selectList" resultMap="DataDomainRespMap">
        SELECT <include refid="domainCols"/>
        FROM data_domain
        <where>
            <if test="domainCategory != null and domainCategory != ''">
                AND domain_category = #{domainCategory}
            </if>
            <if test="isActive != null">
                AND is_active = #{isActive}
            </if>
            <if test="searchKeyword != null and searchKeyword != ''">
                AND (domain_code LIKE CONCAT('%', #{searchKeyword}, '%')
                  OR domain_name LIKE CONCAT('%', #{searchKeyword}, '%'))
            </if>
        </where>
        ORDER BY domain_code ASC
    </select>

    <select id="selectByCode" resultMap="DataDomainRespMap">
        SELECT <include refid="domainCols"/>
        FROM data_domain
        WHERE domain_code = #{domainCode}
    </select>

    <select id="selectAll" resultMap="DataDomainOptionMap">
        SELECT domain_code, domain_name, domain_category
        FROM data_domain
        WHERE is_active = 'Y'
        ORDER BY domain_category, domain_name
    </select>

    <insert id="insert" parameterType="com.ga.core.vo.dict.DataDomainVO">
        INSERT INTO data_domain (
            domain_code, domain_name, domain_category, data_type,
            length, precision, scale, format_pattern, validation_regex,
            example_value, masking_type, encrypted, description, is_active,
            created_at, created_by
        ) VALUES (
            #{domainCode}, #{domainName}, #{domainCategory}, #{dataType},
            #{length}, #{precision}, #{scale}, #{formatPattern}, #{validationRegex},
            #{exampleValue}, #{maskingType}, #{encrypted}, #{description}, #{isActive},
            NOW(), #{createdBy}
        )
    </insert>

    <update id="update" parameterType="com.ga.core.vo.dict.DataDomainVO">
        UPDATE data_domain SET
            domain_name      = #{domainName},
            domain_category  = #{domainCategory},
            data_type        = #{dataType},
            length           = #{length},
            precision        = #{precision},
            scale            = #{scale},
            format_pattern   = #{formatPattern},
            validation_regex = #{validationRegex},
            example_value    = #{exampleValue},
            masking_type     = #{maskingType},
            encrypted        = #{encrypted},
            description      = #{description},
            is_active        = #{isActive},
            updated_at       = NOW(),
            updated_by       = #{updatedBy}
        WHERE domain_code = #{domainCode}
    </update>

    <delete id="delete">
        DELETE FROM data_domain WHERE domain_code = #{domainCode}
    </delete>

    <select id="countUsageByCode" resultType="int">
        SELECT COUNT(*) FROM data_dictionary WHERE domain_code = #{domainCode}
    </select>

</mapper>
