<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.caliverse.admin.domain.dao.admin.CaliumMapper">
    <resultMap id="CaliumRequestResultMap" type="com.caliverse.admin.domain.entity.Calium">
        <id property="id" column="id"/>
        <result property="rowNum" column="row_num"/>
        <result property="dept" column="dept"/>
        <result property="count" column="count"/>
        <result property="status" column="status"/>
        <result property="content" column="content"/>
        <result property="requestId" column="request_id"/>
        <result property="createTime" column="create_time"/>
        <result property="stateMessage" column="state_message"/>
        <result property="stateTime" column="state_time"/>
        <result property="createBy" column="create_by"/>
        <result property="createDt" column="create_dt"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateDt" column="update_dt"/>
    </resultMap>

    <!--칼리움 요청 리스트 조회-->
    <select id="getCaliumRequestList" resultMap="CaliumRequestResultMap" parameterType="map">
        SELECT (@row_number:=@row_number + 1) AS row_num , c.* FROM
        (
            SELECT
                a.id
                , a.dept
                , a.count AS count
                , a.status
                , a.content
                , a.create_time
                , a.state_time
                , a.state_message
                , a.create_dt
                , (SELECT name FROM admin WHERE id = a.create_by ) AS create_by
            FROM calium_request a
            WHERE 1 = 1

            <if test="content != null and content != ''">
                <foreach item="content" index="index" collection="content.split(' ')" open="AND (" separator="OR" close=")">
                    a.content LIKE CONCAT('%', #{content}, '%')
                </foreach>
            </if>

            <if test="status != null and status != ''">
                <choose>
                    <when test="status != 'ALL' ">
                        AND a.status = #{status}
                    </when>
                </choose>
            </if>

            <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
                AND a.create_dt &gt;= #{start_dt, jdbcType=TIMESTAMP}
                AND a.create_dt &lt;= #{end_dt, jdbcType=TIMESTAMP}
            </if>
            ORDER BY a.create_dt
        )c
        , (SELECT @row_number:=0) AS t
        <if test="orderby != null and orderby != ''">
            ORDER BY row_num ${orderby}
        </if>
        <if test="pageSize != null and pageSize != ''">
            LIMIT ${pageSize} OFFSET ${offset}
        </if>
    </select>

    <select id="getCaliumTotal" resultType="java.lang.Double">
        select ROUND(COALESCE(SUM(count), 0), 2) from calium_request WHERE deleted = 0 AND status = 'FINISH'
    </select>

    <select id="getTotal" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM calium_request WHERE deleted = 0
    </select>

    <select id="getAllCnt" resultType="java.lang.Integer" parameterType="map">
        SELECT count(*) FROM calium_request a
        WHERE a.deleted = 0
        <if test="content != null and content != ''">
            <foreach item="content" index="index" collection="content.split(' ')" open="AND (" separator="OR" close=")">
                a.content LIKE CONCAT('%', #{content}, '%')
            </foreach>
        </if>

        <if test="status != null and status != ''">
            <choose>
                <when test="status != 'ALL' ">
                    AND a.status = #{status}
                </when>
            </choose>
        </if>

        <if test="start_dt != null and start_dt != '' and end_dt !=null and end_dt!= ''">
            AND a.create_dt &gt;= #{start_dt, jdbcType=TIMESTAMP}
            AND a.create_dt &lt;= #{end_dt, jdbcType=TIMESTAMP}
        </if>
    </select>

    <select id="getCaliumRequestDetail" parameterType="java.lang.Long" resultMap="CaliumRequestResultMap" >
        SELECT
            a.id
             , a.dept
             , a.count
             , a.status
             , a.content
             , (SELECT email FROM admin WHERE id = a.update_by ) AS update_by
             , a.update_dt
             , a.create_by
        FROM calium_request a
        WHERE a.id = #{id}
    </select>

    <insert id="postCaliumRequest" parameterType="com.caliverse.admin.domain.request.CaliumRequest" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO calium_request (dept, count, content, request_id, create_time, create_by, update_by)
                  VALUES (#{dept}, #{count}, #{content}, #{requestId}, #{createTime}, #{createBy}, #{createBy})
    </insert>

    <update id="updateStatusCaliumRequest" parameterType="map">
        UPDATE calium_request
        SET status = #{status}
            <if test="stateTime != null">
                ,state_time = #{stateTime}
            </if>
            <if test="updateBy != null">
                , update_by = #{updateBy}
                , update_dt = NOW()
            </if>
        where id = #{id}
    </update>

    <select id="getScheduleCaliumRequest" resultMap="CaliumRequestResultMap">
        SELECT id,
               request_id
        FROM calium_request
        WHERE status = 'WAIT'
          AND deleted = 0
    </select>

</mapper>
