<?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.foreverwin.mesnac.common.mapper.CustomReportConfigMapper">

    <resultMap type="com.foreverwin.mesnac.common.model.CustomReportMainmodel" id="CustomReportMainmodelResult">
        <result property="handle"    column="HANDLE"    />
        <result property="site"    column="SITE"    />
        <result property="reportId"    column="REPORT_ID"    />
        <result property="reportTitle"    column="REPORT_TITLE"    />
        <result property="reportDesc"    column="REPORT_DESC"    />
        <result property="conditionSqlConfig"    column="CONDITION_SQL_CONFIG"    />
        <result property="displayType"    column="DISPLAY_TYPE"    />
        <result property="timeRefresh"    column="TIME_REFRESH"    />
        <result property="backgroundColor"    column="BACKGROUND_COLOR"    />
        <result property="fontColor"    column="FONT_COLOR"    />
        <result property="pageSize"    column="PAGE_SIZE"    />
        <result property="modifyDateTime"    column="MODIFY_DATE_TIME"    />
        <result property="modifyUser"    column="MODIFY_USER"    />
    </resultMap>

    <resultMap type="com.foreverwin.mesnac.common.model.CustomReportSearchConditionConfig" id="CustomReportSearchConditionConfigResult">
        <result property="handle"    column="HANDLE"    />
        <result property="site"    column="SITE"    />
        <result property="customReportMainModelBo"    column="CUSTOM_REPORT_MAIN_MODEL_BO"    />
        <result property="seq"    column="SEQ"    />
        <result property="conditionName"    column="CONDITION_NAME"    />
        <result property="conditionField"    column="CONDITION_FIELD"    />
        <result property="fieldType"    column="FIELD_TYPE"    />
        <result property="dailogCheckboxSQL"    column="DIALOG_CHKBOX_SQL"    />
        <result property="requiredType"    column="REQUIRED_TYPE"    />
        <result property="rowID"    column="ROW_ID"    />
        <result property="columnID"    column="COLUMN_ID"    />
        <result property="columnWidthPercent"    column="COLUMN_WIDTH_PERCENT"    />
        <result property="modifyDateTime"    column="MODIFY_DATE_TIME"    />
        <result property="modifyUser"    column="MODIFY_USER"    />
    </resultMap>

    <resultMap type="com.foreverwin.mesnac.common.model.CustomReportSearchConditionDynSQLConfig" id="CustomReportSearchConditionDynSQLConfigResult">
        <result property="handle"    column="HANDLE"    />
        <result property="site"    column="SITE"    />
        <result property="customReportMainModelBo"    column="CUSTOM_REPORT_MAIN_MODEL_BO"    />
        <result property="seq"    column="SEQ"    />
        <result property="dynConditionReplaceField"    column="DYN_CONDITION_REPLACE_FIELD"    />
        <result property="dynamicConditionSQL"    column="DYNAMIC_CONDITION_SQL"    />
        <result property="conditionField"    column="CONDITION_FIELD"    />
        <result property="modifyDateTime"    column="MODIFY_DATE_TIME"    />
        <result property="modifyUser"    column="MODIFY_USER"    />
    </resultMap>

    <resultMap type="com.foreverwin.mesnac.common.model.CustomReportSearchResultColumnConfig" id="CustomReportSearchResultColumnConfigResult">
        <result property="handle"    column="HANDLE"    />
        <result property="site"    column="SITE"    />
        <result property="customReportMainModelBo"    column="CUSTOM_REPORT_MAIN_MODEL_BO"    />
        <result property="seq"    column="SEQ"    />
        <result property="columnField" column="COLUMN_FIELD"  />
        <result property="columnName"    column="COLUMN_NAME"    />
        <result property="columnWidthPercent"    column="COLUMN_WIDTH_PERCENT"    />
        <result property="modifyDateTime"    column="MODIFY_DATE_TIME"    />
        <result property="modifyUser"    column="MODIFY_USER"    />
    </resultMap>

    <select id="selectReportIdDialog" resultMap="CustomReportMainmodelResult">
        SELECT REPORT_ID,REPORT_TITLE
        FROM Z_CUSTOM_REPORT_MAINMODEL
        WHERE SITE=#{SITE}
    </select>

    <select id="selectMain" resultMap="CustomReportMainmodelResult">
        SELECT HANDLE, SITE, REPORT_ID, REPORT_TITLE, REPORT_DESC, CONDITION_SQL_CONFIG, DISPLAY_TYPE, TIME_REFRESH,
        BACKGROUND_COLOR, FONT_COLOR, PAGE_SIZE, MODIFY_DATE_TIME, MODIFY_USER
        FROM Z_CUSTOM_REPORT_MAINMODEL
        WHERE SITE='*' AND REPORT_ID=#{reportId}
    </select>

    <select id="selectSearchConditionConfig" resultMap="CustomReportSearchConditionConfigResult">
        SELECT SEQ, CONDITION_NAME, CONDITION_FIELD, FIELD_TYPE, DIALOG_CHKBOX_SQL, REQUIRED_TYPE,
        ROW_ID, COLUMN_ID, COLUMN_WIDTH_PERCENT
        FROM Z_CUSTOM_REPORT_SEARCHCONDITION_CONFIG
        WHERE CUSTOM_REPORT_MAIN_MODEL_BO=#{mainhandle}
    </select>

    <select id="selectDynSqlConfig" resultMap="CustomReportSearchConditionDynSQLConfigResult">
        SELECT SEQ, DYN_CONDITION_REPLACE_FIELD, DYNAMIC_CONDITION_SQL, CONDITION_FIELD
        FROM Z_CUSTOM_REPORT_SEARCHCONDITION_DYNSQL_CONFIG
        WHERE CUSTOM_REPORT_MAIN_MODEL_BO=#{mainhandle}
    </select>

    <select id="selectResultColumnConfig" resultMap="CustomReportSearchResultColumnConfigResult">
        SELECT SEQ, COLUMN_FIELD, COLUMN_NAME, COLUMN_WIDTH_PERCENT
        FROM Z_CUSTOM_REPORT_SEARCHRESULT_COLUMN_CONFIG
        WHERE CUSTOM_REPORT_MAIN_MODEL_BO=#{mainhandle}
        ORDER BY TO_NUMBER(SEQ)
    </select>

    <delete id="deleteDynSqlConfig" parameterType="String">
        DELETE FROM Z_CUSTOM_REPORT_SEARCHCONDITION_DYNSQL_CONFIG
        WHERE CUSTOM_REPORT_MAIN_MODEL_BO=#{mainhandle}
    </delete>

    <delete id="deleteResultColumnConfig" parameterType="String">
        DELETE FROM Z_CUSTOM_REPORT_SEARCHRESULT_COLUMN_CONFIG
        WHERE CUSTOM_REPORT_MAIN_MODEL_BO=#{mainhandle}
    </delete>

    <delete id="deleteSearchConditionConfig" parameterType="String">
        DELETE FROM Z_CUSTOM_REPORT_SEARCHCONDITION_CONFIG
        WHERE CUSTOM_REPORT_MAIN_MODEL_BO=#{mainhandle}
    </delete>

    <delete id="deleteMainmodel" parameterType="String">
        DELETE FROM Z_CUSTOM_REPORT_MAINMODEL
        WHERE HANDLE=#{mainhandle}
    </delete>

    <insert id="insertDynSqlConfig" parameterType="com.foreverwin.mesnac.common.model.CustomReportSearchConditionDynSQLConfig">
        INSERT INTO Z_CUSTOM_REPORT_SEARCHCONDITION_DYNSQL_CONFIG (HANDLE, CUSTOM_REPORT_MAIN_MODEL_BO, SEQ,
        DYN_CONDITION_REPLACE_FIELD, DYNAMIC_CONDITION_SQL, CONDITION_FIELD, MODIFY_DATE_TIME, MODIFY_USER)
        VALUES(#{handle}, #{customReportMainModelBo}, #{seq}, #{dynConditionReplaceField}, #{dynamicConditionSQL},
        #{conditionField}, #{modifyDateTime}, #{modifyUser})
    </insert>

    <!--<insert id="insertResultColumnConfig" parameterType="CustomReportSearchResultColumnConfig">
        INSERT INTO Z_CUSTOM_REPORT_SEARCHRESULT_COLUMN_CONFIG (HANDLE, CUSTOM_REPORT_MAIN_MODEL_BO, SEQ,
        COLUMN_NAME, COLUMN_WIDTH_PERCENT, MODIFY_DATE_TIME, MODIFY_USER)
        VALUES(#{handle}, #{customReportMainModelBo}, #{seq}, #{columnName}, #{columnWidthPercent}, #{modifyDateTime}, #{modifyUser})
    </insert>-->
    <insert id="insertResultColumnConfig" parameterType="com.foreverwin.mesnac.common.model.CustomReportSearchResultColumnConfig">
        insert into Z_CUSTOM_REPORT_SEARCHRESULT_COLUMN_CONFIG
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="handle != null  ">HANDLE,</if>
            <if test="customReportMainModelBo != null  and customReportMainModelBo != ''  ">CUSTOM_REPORT_MAIN_MODEL_BO,</if>
            <if test="seq != null  and seq != ''  ">SEQ,</if>
            <if test="columnField != null  and columnField != ''  ">COLUMN_FIELD,</if>
            <if test="columnName != null  and columnName != ''  ">COLUMN_NAME,</if>
            <if test="columnWidthPercent != null  and columnWidthPercent != ''  ">COLUMN_WIDTH_PERCENT,</if>
            <if test="modifyDateTime != null  ">MODIFY_DATE_TIME,</if>
            <if test="modifyUser != null  ">MODIFY_USER,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="handle != null  ">#{handle},</if>
            <if test="customReportMainModelBo != null  and customReportMainModelBo != ''  ">#{customReportMainModelBo},</if>
            <if test="seq != null  and seq != ''  ">#{seq},</if>
            <if test="columnField != null  and columnField != ''  ">#{columnField},</if>
            <if test="columnName != null  and columnName != ''  ">#{columnName},</if>
            <if test="columnWidthPercent != null  and columnWidthPercent != ''  ">#{columnWidthPercent},</if>
            <if test="modifyDateTime != null  ">#{modifyDateTime},</if>
            <if test="modifyUser != null  ">#{modifyUser},</if>
        </trim>
    </insert>

    <!--<insert id="insertSearchConditionConfig" parameterType="CustomReportSearchConditionConfig">
        INSERT INTO Z_CUSTOM_REPORT_SEARCHCONDITION_CONFIG
        (HANDLE, SITE, CUSTOM_REPORT_MAIN_MODEL_BO, SEQ, CONDITION_NAME, CONDITION_FIELD, FIELD_TYPE, DIALOG_CHKBOX_SQL, REQUIRED_TYPE, ROW_ID, COLUMN_ID, COLUMN_WIDTH_PERCENT, MODIFY_DATE_TIME, MODIFY_USER)
        VALUES(#{handle}, #{site}, #{customReportMainModelBo}, #{seq}, #{conditionName}, #{conditionField}, #{fieldType}, #{dailogCheckboxSQL}, #{requiredType}, #{rowID}, #{columnID}, #{columnWidthPercent}, #{modifyDateTime}, #{modifyUser})
    </insert>-->
   <!-- <insert id="insertSearchConditionConfig" >
        insert into Z_CUSTOM_REPORT_MAINMODEL
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="_parameter != null  ">HANDLE,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="_parameter != null  ">#{handle},</if>
        </trim>
    </insert>-->

    <insert id="insertSearchConditionConfig" parameterType="com.foreverwin.mesnac.common.model.CustomReportSearchConditionConfig">
        insert into Z_CUSTOM_REPORT_SEARCHCONDITION_CONFIG
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="handle != null  ">HANDLE,</if>
            <if test="site != null  and site != ''  ">SITE,</if>
            <if test="customReportMainModelBo != null  and customReportMainModelBo != ''  ">CUSTOM_REPORT_MAIN_MODEL_BO,</if>
            <if test="seq != null">SEQ,</if>
            <if test="conditionName != null  and conditionName != ''  ">CONDITION_NAME,</if>
            <if test="conditionField != null  and conditionField != ''  ">CONDITION_FIELD,</if>
            <if test="fieldType != null  and fieldType != ''  ">FIELD_TYPE,</if>
            <if test="dailogCheckboxSQL != null  and dailogCheckboxSQL != ''  ">DIALOG_CHKBOX_SQL,</if>
            <if test="requiredType != null  and requiredType != ''  ">REQUIRED_TYPE,</if>
            <if test="rowID != null">ROW_ID,</if>
            <if test="columnID != null ">COLUMN_ID,</if>
            <if test="columnWidthPercent != null  ">COLUMN_WIDTH_PERCENT,</if>
            <if test="modifyDateTime != null  ">MODIFY_DATE_TIME,</if>
            <if test="modifyUser != null  ">MODIFY_USER,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="handle != null  ">#{handle},</if>
            <if test="site != null  and site != ''  ">#{site},</if>
            <if test="customReportMainModelBo != null  and customReportMainModelBo != ''  ">#{customReportMainModelBo},</if>
            <if test="seq != null  ">#{seq},</if>
            <if test="conditionName != null  and conditionName != ''  ">#{conditionName},</if>
            <if test="conditionField != null  and conditionField != ''  ">#{conditionField},</if>
            <if test="fieldType != null  and fieldType != ''  ">#{fieldType},</if>
            <if test="dailogCheckboxSQL != null  and dailogCheckboxSQL != ''  ">#{dailogCheckboxSQL},</if>
            <if test="requiredType != null  and requiredType != ''  ">#{requiredType},</if>
            <if test="rowID != null ">#{rowID},</if>
            <if test="columnID != null  ">#{columnID},</if>
            <if test="columnWidthPercent != null  ">#{columnWidthPercent},</if>
            <if test="modifyDateTime != null  ">#{modifyDateTime},</if>
            <if test="modifyUser != null  ">#{modifyUser},</if>
        </trim>
    </insert>

    <insert id="insertInsertMainmodel" parameterType="com.foreverwin.mesnac.common.model.CustomReportMainmodel">
        INSERT INTO Z_CUSTOM_REPORT_MAINMODEL
        (HANDLE, SITE, REPORT_ID, REPORT_TITLE, REPORT_DESC, CONDITION_SQL_CONFIG, DISPLAY_TYPE, TIME_REFRESH, BACKGROUND_COLOR, FONT_COLOR, PAGE_SIZE, MODIFY_DATE_TIME, MODIFY_USER)
        VALUES(#{handle}, #{site}, #{reportId}, #{reportTitle}, #{reportDesc}, #{conditionSqlConfig}, #{displayType}, #{timeRefresh}, #{backgroundColor}, #{fontColor}, #{pageSize}, #{modifyDateTime}, #{modifyUser})
    </insert>

</mapper>