<?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.ym.mec.biz.dal.dao.ChargeTypeDao">

    <resultMap type="com.ym.mec.biz.dal.entity.ChargeType" id="ChargeType">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="del_flag_" property="delFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="description_" property="description"/>
        <result column="course_form_" property="courseForm"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <collection property="subjects" ofType="com.ym.mec.biz.dal.entity.Subject">
            <result property="id" column="subject_id_"/>
            <result property="name" column="subject_name_"/>
        </collection>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="ChargeType">
		SELECT * FROM charge_type WHERE id_ = #{id}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="ChargeType">
		SELECT * FROM charge_type WHERE del_flag_ = 0 ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ChargeType" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO charge_type (id_,name_,description_,course_form_,create_time_,update_time_)
        VALUES(#{id},#{name},#{description},#{courseForm},now(),now())
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ChargeType">
        UPDATE charge_type
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="description != null">
                description_ = #{description},
            </if>
            <if test="courseForm != null">
                course_form_ = #{courseForm},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		UPDATE charge_type SET del_flag_ = 1 WHERE id_ = #{id}
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ChargeType" parameterType="map">
        SELECT ct.*,s.id_ subject_id_,s.name_ subject_name_
        FROM (SELECT * FROM charge_type WHERE del_flag_ = 0 <include refid="global.limit"/>) ct
        LEFT JOIN charge_type_subject_mapper ctsm ON ct.id_ = ctsm.charge_type_id_
        LEFT JOIN `subject` s ON ctsm.subject_id_ = s.id_
        ORDER BY ct.update_time_
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM charge_type WHERE del_flag_ = 0
	</select>
    <select id="queryNameByIds" resultType="java.util.Map" parameterType="list">
        SELECT id_ `key`,name_ `value` FROM charge_type WHERE FIND_IN_SET(id_,#{chargeTypeIds})
    </select>
</mapper>
