<?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="program">

<resultMap id="programRow" type="cokr.xit.base.menu.Program">
	<result property="filename" column="PGRM_FILE_NM"/>
	<result property="location" column="PGRM_FILE_PATH"/>
	<result property="name" column="PGRM_NM"/>
	<result property="description" column="DSCRP"/>
	<result property="url" column="URL"/>
</resultMap>

<resultMap id="reqRow" type="cokr.xit.base.menu.ChangeRequest">
	<result property="filename" column="PGRM_FILE_NM"/>
	<result property="id" column="REQ_ID"/>
	<result property="subject" column="SUBJECT"/>
	<result property="requestorID" column="REQ_USER"/>
	<result property="requestDate" column="REQ_DT"/>
	<result property="requestDetail" column="REQ_CNTNT"/>
	<result property="processorID" column="PRSC_USER"/>
	<result property="processDate" column="PRCS_DT"/>
	<result property="processDetail" column="PRCS_CNTNT"/>
	<result property="status" column="PRCS_STATUS"/>
</resultMap>

<sql id="selectPrograms">
<include refid="utility.paging-prefix"/>
SELECT A.*
  FROM TB_PROGRAM A
<where>
<if test="by != null and term != null">${by} LIKE CONCAT('%', #{term}, '%')</if>
<if test='filenames != null'>PGRM_FILE_NM IN (<foreach collection="filenames" item="filename" separator=",">#{filename}</foreach>)</if>
</where>
 ORDER BY<if test='by != null'>${by}</if><if test='by == null'>PGRM_FILE_NM</if>
<include refid="utility.paging-suffix"/></sql>

<select id="getProgramList" parameterType="map" resultType="dataobject">/* 프로그램 목록 조회(program.getProgramList) */
<include refid="selectPrograms"/></select>

<select id="getPrograms" parameterType="map" resultMap="programRow">/* 프로그램 가져오기(program.getPrograms) */
<include refid="selectPrograms"/></select>

<insert id="insertProgram" parameterType="cokr.xit.base.menu.Program">/* 프로그램 등록(program.insertProgram) */
INSERT INTO TB_PROGRAM (
       PGRM_FILE_NM
     , PGRM_FILE_PATH
     , PGRM_NM
     , DSCRP
     , URL
) VALUES (
       #{filename}
     , #{location}
     , #{name}
     , #{description}
     , #{url}
)</insert>

<update id="updateProgram" parameterType="cokr.xit.base.menu.Program">/* 프로그램 수정(program.updateProgram) */
UPDATE TB_PROGRAM SET
       PGRM_FILE_PATH = #{location}
     , PGRM_NM = #{name}
     , DSCRP = #{description}
     , URL = #{url}
WHERE PGRM_FILE_NM = #{filename}</update>

<delete id="removePrograms" parameterType="map">/* 프로그램 삭제(program.removePrograms) */
DELETE FROM TB_PROGRAM
 WHERE PGRM_FILE_NM IN (<foreach collection="filenames" item="filename" separator=",">#{filename}</foreach>)</delete>

<delete id="clearPrograms" parameterType="map">/* 프로그램 비우기(program.clearPrograms) */
DELETE FROM TB_PROGRAM
 WHERE PGRM_FILE_NM NOT IN (SELECT PGRM_FILE_NM FROM TB_MENU)</delete>

<sql id="selectRequests">
SELECT A.*
  FROM TB_PGRM_CHNG_REQ A
<where>
<if test='fromReqDate != null'>REQ_DT >= #{fromReqDate}</if>
<if test='toReqDate != null'>REQ_DT &lt;= #{toReqDate}</if>
<if test="by != null and term != null">${by} LIKE CONCAT('%', #{term}, '%')</if>
<if test='filenames != null'>PGRM_FILE_NAME IN (<foreach collection="filenames" item="filename" separator=",">#{filename}</foreach>)</if>
<if test='reqIDs != null'>REQ_ID IN (<foreach collection="reqIDs" item="reqID" separator=",">#{reqID}</foreach>)</if>
</where>
 ORDER BY PGRM_FILE_NM,<if test='by != null'>${by}</if><if test='by == null'>REQ_ID DESC</if></sql>

<select id="getRequestList" parameterType="map" resultType="dataobject">/* 변경요청 목록 조회(program.getRequestList) */
<include refid="selectRequests" /></select>

<select id="getRequests" parameterType="map" resultType="dataobject">/* 프로그램별 변경요청 목록 조회(program.getRequests) */
<include refid="selectRequests" /></select>

<insert id="insertRequest" parameterType="cokr.xit.base.menu.ChangeRequest">/* 프로그램 변경요청 등록(program.insertRequest) */
INSERT INTO TB_PGRM_CHNG_REQ (
       PGRM_FILE_NM
     , REQ_ID
     , SUBJECT
     , REQ_USER
     , REQ_DT
     , REQ_CNTNT
     , PRSC_USER
     , PRCS_DT
     , PRCS_CNTNT
     , PRCS_STATUS
) VALUES (
       #{filename}
     , #{id}
     , #{subject}
     , #{requestorID}
     , #{requestDate}
     , #{requestDetail}
     , #{processorID}
     , #{processDate}
     , #{processDetail}
     , #{status}
)</insert>

<update id="updateRequest" parameterType="cokr.xit.base.menu.ChangeRequest">/* 프로그램 변경요청 수정(program.updateRequest) */
UPDATE TB_PGRM_CHNG_REQ SET
       SUBJECT = #{subject}
     , REQ_USER = #{requestorID}
     , REQ_DT = #{requestDate}
     , REQ_CNTNT = #{requestDetail}
     , PRSC_USER = #{processorID}
     , PRCS_DT = #{processDate}
     , PRCS_CNTNT = #{processDetail}
WHERE PGRM_FILE_NM = #{filename}
AND REQ_ID = #{id}</update>

<update id="setRequestStatus" parameterType="map">/* 프로그램 변경요청 상태 변경(program.setRequestStatus) */
UPDATE TB_PGRM_CHNG_REQ SET
       PRCS_STATUS = #{status}
 WHERE PGRM_FILE_NM = #{filename}
   AND REQ_ID = #{id}</update>

</mapper>