<?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">
<!-- UIWS system 이식: 거래처(tb_uiws_company). -->
<mapper namespace="com.zioinfo.signage.uiws.system.mapper.CompanyMapper">

    <sql id="kw">
        <if test="keyword != null and keyword != ''">
            AND (company_id ILIKE '%' || #{keyword} || '%' OR company_nm ILIKE '%' || #{keyword} || '%')
        </if>
    </sql>

    <select id="search" resultType="com.zioinfo.signage.uiws.system.model.SysCompany">
        SELECT * FROM tb_uiws_company WHERE 1=1 <include refid="kw"/>
        ORDER BY company_nm ASC, company_id ASC
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countSearch" resultType="long">
        SELECT COUNT(*) FROM tb_uiws_company WHERE 1=1 <include refid="kw"/>
    </select>

    <select id="searchActive" resultType="com.zioinfo.signage.uiws.system.model.SysCompany">
        SELECT * FROM tb_uiws_company WHERE use_yn = 'Y' <include refid="kw"/>
        ORDER BY company_nm ASC
    </select>

    <select id="findActiveOrdered" resultType="com.zioinfo.signage.uiws.system.model.SysCompany">
        SELECT * FROM tb_uiws_company WHERE use_yn = 'Y' ORDER BY company_nm ASC
    </select>

    <select id="findAll" resultType="com.zioinfo.signage.uiws.system.model.SysCompany">
        SELECT * FROM tb_uiws_company ORDER BY company_nm ASC
    </select>

    <select id="findById" resultType="com.zioinfo.signage.uiws.system.model.SysCompany">
        SELECT * FROM tb_uiws_company WHERE company_id = #{companyId}
    </select>

    <select id="existsById" resultType="boolean">
        SELECT EXISTS(SELECT 1 FROM tb_uiws_company WHERE company_id = #{companyId})
    </select>

    <insert id="insert" parameterType="com.zioinfo.signage.uiws.system.model.SysCompany">
        INSERT INTO tb_uiws_company (company_id, company_nm, biz_no, use_yn, created_by, created_at)
        VALUES (#{companyId}, #{companyNm}, #{bizNo}, #{useYn}, #{createdBy}, now())
    </insert>

    <update id="update" parameterType="com.zioinfo.signage.uiws.system.model.SysCompany">
        UPDATE tb_uiws_company SET
            company_nm = #{companyNm}, biz_no = #{bizNo}, use_yn = #{useYn},
            updated_by = #{updatedBy}, updated_at = now()
        WHERE company_id = #{companyId}
    </update>

    <delete id="deleteById">
        DELETE FROM tb_uiws_company WHERE company_id = #{companyId}
    </delete>
</mapper>
