<?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.ho.business.mapper.IncomeMappingMapper">

    <resultMap type="com.ho.business.entity.RateTemplate" id="rateTemplateResultMap">
        <id property="id" column="id"/>
        <result property="templateType" column="template_type"/>
        <result property="templateTypeName" column="template_type_name"/>
        <result property="templateName" column="template_name"/>
        <result property="rateNum" column="rate_num"/>
        <result property="context" column="context"/>
        <result property="operatorName" column="operator_name"/>
        <result property="createTime" column="create_time"/>
        <result property="modifyName" column="modify_name"/>
        <result property="modifyTime" column="modify_time"/>
        <result property="stationId" column="station_id"/>
        <result property="open" column="open"/>
        <collection  property="rateMappings" ofType="com.ho.business.entity.IncomeRateMapping">
            <id property="rateId" column="rate_id"/>
            <result property="rateName" column="rate_name"/>
            <result property="price" column="price"/>
            <result property="templateId" column="template_id"/>
            <result property="startTime" column="start_time"/>
            <result property="endTime" column="end_time"/>
            <result property="discount" column="discount"/>
        </collection >
    </resultMap>

    <select id="selectRateTemplates" resultMap="rateTemplateResultMap">
        select t1.id,t1.template_type,t3.type_name template_type_name,t1.template_name,t1.rate_num,t1.context,t1.operator_id,date_format(t1.create_time,'%Y-%m-%d') create_time,t1.modify_id,
        date_format(t1.modify_time,'%Y-%m-%d') modify_time,t1.station_id, t2.rate_id,t2.rate_name,t2.price,t2.template_id,t2.discount,t2.start_time,t2.end_time,t1.open
        from
            income_rate_template t1
            left join income_rate_mapping t2 on t1.id = t2.template_id
            left join income_template_type t3 on t1.template_type = t3.type
        where t1.station_id = #{stationId}
        <if test=" templateName != null and templateName != '' ">
          and t1.template_name like concat('%',#{templateName},'%')
        </if>
        order by t1.id,t2.rate_id
    </select>

    <insert id="insertRateTemplate" parameterType="com.ho.business.entity.RateTemplate">
        insert into income_rate_template ( id,template_type,template_name,rate_num,context,operator_id,create_time,station_id,open )
                values ( #{id},#{templateType},#{templateName},#{rateNum},#{context},#{operatorId},#{createTime},#{stationId},0 )
    </insert>

    <update id="modifyRateTemplate" parameterType="com.ho.business.entity.IncomeRateMapping">
      update income_rate_template set template_type = #{templateType},template_name = #{templateName},rate_num = #{rateNum},context =#{context},modify_id = #{modifyId},modify_time = #{createTime}
        where id = #{id}
    </update>

    <delete id="deleteRateTemplate">
        delete from income_rate_template where id = #{id}
    </delete>

    <resultMap id="templateTypeMap" type="com.ho.business.entity.IncomeTemplateType">
        <id property="id" column="id"/>
        <result property="type" column="type"/>
        <result property="typeName" column="type_name"/>
    </resultMap>

    <select id="getIncomeTemplateTypes" resultMap="templateTypeMap" >
        select id ,type ,type_name from income_template_type
    </select>

    <insert id="insertRateMapping" parameterType="com.ho.business.entity.IncomeRateMapping">
      insert into income_rate_mapping ( rate_id,start_time,end_time,rate_name,price,template_id,discount )
              values
        <foreach item="data" collection="incomeRateMappings" separator=",">
        (
            #{data.rateId},#{data.startTime},#{data.endTime},#{data.rateName},#{data.price},#{templateId},#{data.discount}
        )
        </foreach>
    </insert>

    <delete id="deleteRateMapping">
        delete from income_rate_mapping where template_id = #{templateId}
    </delete>

    <select id="checkTemplateName" resultType="java.lang.Integer">
        select count(1) from income_rate_template where template_name = #{templateName} and station_id = #{stationId}
    </select>

    <select id="checkTemplateOpen" resultType="java.lang.Integer" parameterType="com.ho.business.entity.RateTemplate">
        select count(1) from income_rate_template where station_id = #{stationId} and template_type = #{templateType} and open = 1
    </select>

    <update id="changeSwitch" parameterType="com.ho.business.entity.RateTemplate">
        update income_rate_template set open = #{open} where id = #{id}
    </update>

    <select id="checkTemplateRate" resultType="java.lang.Integer">
        select count(1) from income_rate_template t1
            join income_rate t2 on t1.id = t2.template_id where t1.id = #{id}
    </select>

    <select id="checkTemplateQRate" resultType="java.lang.Integer">
        select count(1) from income_rate_template t1
            join income_q_rate t2 on t1.id = t2.template_id where t1.id = #{id}
    </select>

    <select id="checkTotalTemplate" resultType="java.lang.Integer">
        select count(1) from income_rate_template t1
            join income_total t2 on (t1.id = t2.template_id or t1.id = t2.surf_template_id)
        where t1.id = #{id} and t2.type = '1' and t2.flag = '0'
    </select>

</mapper>