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

    <resultMap type="com.ym.mec.biz.dal.entity.SubjectGoodsMapper" id="SubjectGoodsMapper">
        <result column="id_" property="id"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="type_" property="type" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="goods_category_id_" property="goodsCategoryId"/>
        <result column="goods_id_list_" property="goodsIdList"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="tenant_id_" property="tenantId"/>
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="SubjectGoodsMapper">
		SELECT * FROM subject_goods_mapper where tenant_id_ = #{tenantId} ORDER BY id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SubjectGoodsMapper" useGeneratedKeys="true"
            keyColumn="id" keyProperty="id">
        INSERT INTO subject_goods_mapper (subject_id_,goods_category_id_,create_time_,update_time_,type_,goods_id_list_,tenant_id_)
        VALUES(#{subjectId},#{goodsCategoryId},now(),now(),#{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{goodsIdList},#{tenantId})
    </insert>
    
    <insert id="batchAdd">
        INSERT INTO subject_goods_mapper (subject_id_,goods_category_id_,create_time_,update_time_,tenant_id_)
        VALUES
        <foreach collection="subjectIds" item="item" index="index" separator=",">
            (#{item},#{goodsCategoryId},now(),now(),#{tenantId})
        </foreach>
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.SubjectGoodsMapper">
        UPDATE subject_goods_mapper
        <set>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="goodsCategoryId != null">
                goods_category_id_ = #{goodsCategoryId},
            </if>
            <if test="type != null">
                type_ = #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="goodsIdList != null">
                goods_id_list_ = #{goodsIdList},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
		DELETE FROM subject_goods_mapper WHERE id_ = #{id}
	</delete>

    <delete id="delByGoodsCategoryId">
        DELETE FROM subject_goods_mapper WHERE goods_category_id_ = #{goodsCategoryId}
    </delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SubjectGoodsMapper" parameterType="map">
        SELECT * FROM subject_goods_mapper where tenant_id_ = #{tenantId} ORDER BY id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM subject_goods_mapper where tenant_id_ = #{tenantId}
	</select>
    <select id="countSubjectGoods" resultType="java.lang.Integer">
        SELECT COUNT(sgm.id_) FROM subject_goods_mapper sgm  where sgm.tenant_id_ = #{tenantId}
    </select>
    <select id="querySubjectGoods" resultMap="querySubjectGoodsMap">
        SELECT sgc.*,g.type_ goods_type_,g.name_ goods_name_,g.id_ goods_id_ FROM
        (SELECT s.name_ subject_name_,s.id_ subject_id_,gc.id_ goods_category_id_,gc.name_ goods_category_name_,
        sgm.goods_id_list_,sgm.id_ subject_goods_mapper_id_
        FROM subject_goods_mapper sgm
        LEFT JOIN `subject` s ON s.id_ = sgm.subject_id_
        LEFT JOIN goods_category gc ON gc.id_ = sgm.goods_category_id_ <include refid="global.limit"/>) sgc
        LEFT JOIN goods g ON FIND_IN_SET(g.id_,sgc.goods_id_list_)
        where g.tenant_id_ = #{tenantId}
    </select>
    <resultMap id="querySubjectGoodsMap" type="com.ym.mec.biz.dal.dto.SubjectGoodsDto">
        <result property="subjectName" column="subject_name_"/>
        <result property="subjectGoodsMapperId" column="subject_goods_mapper_id_"/>
        <result property="subjectId" column="subject_id_"/>
        <result property="goodsCategoryId" column="goods_category_id_"/>
        <result property="goodsCategoryName" column="goods_category_name_"/>
        <collection property="goodsList" ofType="com.ym.mec.biz.dal.entity.Goods">
            <result property="id" column="goods_id_"/>
            <result property="name" column="goods_name_"/>
            <result property="type" column="goods_type_"/>
        </collection>
    </resultMap>
</mapper>
