<?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.GoodsCategoryDao">

    <resultMap type="com.ym.mec.biz.dal.entity.GoodsCategory" id="GoodsCategory">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="img_" property="img"/>
        <result column="parent_id_" property="parentId"/>
        <result column="order_no_" property="orderNo"/>
        <result column="del_flag_" property="delFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="desc_" property="desc"/>
        <result column="update_time_" property="updateTime"/>
        <result column="create_time_" property="createTime"/>
        <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="GoodsCategory">
		SELECT * FROM goods_category WHERE id_ = #{id} 
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="GoodsCategory">
		SELECT * FROM goods_category ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.GoodsCategory" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO goods_category (id_,name_,img_,parent_id_,order_no_,desc_,update_time_,create_time_)
        VALUES(#{id},#{name},#{img},#{parentId},#{orderNo},#{desc},now(),now())
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.GoodsCategory">
        UPDATE goods_category
        <set>
            <if test="parentId != null">
                parent_id_ = #{parentId},
            </if>
            <if test="delFlag != null">
                del_flag_ = #{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="orderNo != null">
                order_no_ = #{orderNo},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
            <if test="img != null">
                img_ = #{img},
            </if>
            <if test="desc != null">
                desc_ = #{desc},
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

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

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="GoodsCategory" parameterType="map">
        SELECT gc.*,s.id_ subject_id_,s.name_ subject_name_
        FROM (
        SELECT * FROM goods_category
        <include refid="queryPageSql"/>
        <include refid="global.limit"/>
        ) gc
        LEFT JOIN subject_goods_mapper sgm ON sgm.goods_category_id_ = gc.id_
        LEFT JOIN `subject` s ON s.id_ = sgm.subject_id_
    </select>

    <sql id="queryPageSql">
        <where>
            <if test="parentId != null">
                parent_id_ = #{parentId}
            </if>
            <if test="delFlag != null">
                AND del_flag_ = #{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(id_) FROM goods_category
        <include refid="queryPageSql"/>
	</select>

    <select id="findByParentId" resultMap="GoodsCategory">
        SELECT * FROM goods_category WHERE parent_id_ = #{parentId} AND del_flag_ = #{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </select>
</mapper>
