<?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.ghy.worker.mapper.WorkerGoodsCategoryMapper">

    <resultMap id="WorkerGoodsCategoryResult"       type="com.ghy.worker.domain.WorkerGoodsCategory">
        <result property="workerGoodsCategoryId"    column="worker_goods_category_id"/>
        <result property="workerId"                 column="worker_id"/>
        <result property="goodsCategoryId"          column="goods_category_id"/>
        <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="remark"                   column="remark"/>
        <result property="goodsCategoryName"        column="goods_category_name"/>
    </resultMap>

    <sql id="selectWorkerGoodsCategory">
        SELECT wgc.worker_goods_category_id, wgc.worker_id, wgc.goods_category_id, wgc.create_by, wgc.create_time, wgc.update_by,
        wgc.update_time, wgc.remark, gc.goods_category_name
        FROM worker_goods_category wgc
        LEFT JOIN goods_category gc on gc.goods_category_id = wgc.goods_category_id
    </sql>

    <insert id="insert" parameterType="com.ghy.worker.domain.WorkerGoodsCategory" useGeneratedKeys="true" keyProperty="workerGoodsCategoryId">
        INSERT INTO worker_goods_category(
        <if test="workerId != null and workerId > 0">worker_id,</if>
        <if test="goodsCategoryId != null and goodsCategoryId > 0">goods_category_id,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        <if test="remark != null and remark != ''">remark,</if>
        create_time
        )VALUES(
        <if test="workerId != null and workerId > 0">#{workerId},</if>
        <if test="goodsCategoryId != null and goodsCategoryId > 0">#{goodsCategoryId},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        sysdate()
        )
    </insert>

    <insert id="batchInsert" parameterType="list" >
        INSERT INTO worker_goods_category ( worker_id, goods_category_id, create_by, create_time)
        VALUES
        <foreach collection="workerCategories" separator=","  item="item">
            (#{item.workerId}, #{item.goodsCategoryId}, #{item.workerId}, sysdate())
        </foreach>
    </insert>

    <select id="getByWorker" parameterType="Long" resultMap="WorkerGoodsCategoryResult">
        <include refid="selectWorkerGoodsCategory"></include>
        WHERE wgc.worker_id = #{workerId}
    </select>

    <select id="getWorkerGoodsCategory" parameterType="com.ghy.worker.domain.WorkerGoodsCategory" resultMap="WorkerGoodsCategoryResult">
        <include refid="selectWorkerGoodsCategory"></include>
        <where>
            <if test="goodsCategoryId != null">
                AND wgc.goods_category_id = #{goodsCategoryId}
            </if>
            <if test="goodsCategoryIds != null">
                AND wgc.goods_category_id in
                <foreach collection="goodsCategoryIds" item="goodsCategoryId" open="(" separator="," close=")">
                    #{goodsCategoryId}
                </foreach>
            </if>
        </where>
    </select>

    <delete id="deleteByWorker" parameterType="Long">
        DELETE FROM worker_goods_category WHERE worker_id = #{workerId}
    </delete>

    <delete id="delete" parameterType="Long">
        DELETE FROM worker_goods_category WHERE worker_goods_category_id IN
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>
