<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.HolidayInfoDAO">
    <resultMap id="HolidayInfoMap" type="com.jpsoft.shinestar.modules.base.entity.HolidayInfo">
        <id property="id" column="id_"/>
        <result property="holidayName" column="holiday_name"/>
        <result property="beginDate" column="begin_date"/>
        <result property="endDate" column="end_date"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="delFlag" column="del_flag"/>
        <result property="working" column="working_"/>
        <result property="refWeekday" column="ref_weekday"/>
        <result property="companyId" column="company_id"/>
        <result property="companyName" column="company_name"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.HolidayInfo">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_holiday_info
	    (id_,holiday_name,begin_date,end_date,create_by,create_time,update_by,update_time,
	    del_flag,working_,ref_weekday,company_id)
		values
		(
            #{id,jdbcType=VARCHAR}
            ,#{holidayName,jdbcType=VARCHAR}
            ,#{beginDate,jdbcType= TIMESTAMP }
            ,#{endDate,jdbcType= TIMESTAMP }
            ,#{createBy,jdbcType=VARCHAR}
            ,#{createTime,jdbcType= TIMESTAMP }
            ,#{updateBy,jdbcType=VARCHAR}
            ,#{updateTime,jdbcType= TIMESTAMP }
            ,#{delFlag,jdbcType= NUMERIC }
            ,#{working,jdbcType= NUMERIC }
            ,#{refWeekday,jdbcType= NUMERIC }
            ,#{companyId,jdbcType=VARCHAR}
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_holiday_info where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.HolidayInfo">
        update base_holiday_info
        <set>
            <if test="holidayName!=null">
                holiday_name=#{holidayName,jdbcType=VARCHAR},
            </if>
            <if test="beginDate!=null">
                begin_date=#{beginDate,jdbcType= TIMESTAMP },
            </if>
            <if test="endDate!=null">
                end_date=#{endDate,jdbcType= TIMESTAMP },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="working!=null">
                working_=#{working,jdbcType= NUMERIC },
            </if>
            <if test="refWeekday!=null">
                ref_weekday=#{refWeekday,jdbcType= NUMERIC },
            </if>
            <if test="companyId!=null">
                company_id=#{companyId,jdbcType=VARCHAR},
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="HolidayInfoMap">
        select *
        from base_holiday_info
        where id_=#{0}
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_holiday_info where id_=#{0}
    </select>
    <select id="list" resultMap="HolidayInfoMap">
        select * from base_holiday_info
    </select>
    <select id="search" parameterType="hashmap" resultMap="HolidayInfoMap">
        <![CDATA[
			select a.*,b.name_ as company_name from base_holiday_info a
			left join base_company_info b on a.company_id = b.id_
		]]>
        <where>
            a.del_flag=0
            <if test="searchParams.companyCode != null">
                and b.code_ like #{searchParams.companyCode}
            </if>
            <if test="searchParams.holidayName != null">
                and a.holiday_name like #{searchParams.holidayName}
            </if>
            <if test="searchParams.startDate != null">
                <![CDATA[
                    and a.begin_date>=#{searchParams.startDate}
                ]]>
            </if>
            <if test="searchParams.endDate != null">
                <![CDATA[
                    and a.begin_date<=#{searchParams.endDate}
                ]]>
            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>
    <!--
        查询本单位或上级单位是否设置了假日
        取时间交集
    -->
    <select id="findByCompanyCodeAndTimeRange" resultMap="HolidayInfoMap">
        <![CDATA[
            select a.*,b.name_ as company_name
            from base_holiday_info a
			left join base_company_info b on a.company_id = b.id_
            where instr(#{childrenCode},b.code_)=1
            and a.begin_date<=#{endDate}
            and a.end_date>=#{startDate}
            and a.del_flag=0
            order by a.begin_date asc
        ]]>
    </select>
    <select id="findByDate" resultMap="HolidayInfoMap">
        <![CDATA[
            select a.*
            from base_holiday_info a
            where a.del_flag=0
            and a.begin_date<=#{date}
            and a.end_date>=#{date}
            order by a.begin_date asc
        ]]>
    </select>
    <select id="existByDateAndCompanyId" resultMap="HolidayInfoMap">
        <![CDATA[
            select * from base_holiday_info
            where
            begin_date>=#{attendanceDate}
            and
            end_date<=#{attendanceDate}
            and
            company_id=#{companyId}
            and
            del_flag=0
            and
            working_=1
            order by begin_date asc
        ]]>
    </select>
</mapper>