<?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.yiqi.core.mapper.ClothFlawTopMapper">

    <resultMap type="com.yiqi.core.domain.vo.ClothFlawTopVo" id="ClothFlawTopResult">
            <result property="id" column="id"/>
            <result property="flawId" column="flaw_id"/>
            <result property="flawName" column="flaw_name"/>
            <result property="flawTypeName" column="flaw_type_name"/>
            <result property="isCheck" column="is_check"/>
    </resultMap>

    <sql id="selectClothFlawTopVo">
        select top.id, flaw.id as flaw_id, flaw.name as flaw_name, flawType.name as flaw_type_name,
        case when (top.source_type = #{sourceType} and target_id = #{targetId} ) then 1 else 0 end as is_check
        from cloth_flaw as flaw
        left join cloth_flaw_top as top on top.flaw_id = flaw.id
        left join cloth_flaw_type as flawType on flaw.flaw_type_id = flawType.id
    </sql>

    <select id="selectClothFlawTopList" parameterType="ClothFlawTop" resultMap="ClothFlawTopResult">
        <include refid="selectClothFlawTopVo"/>
        <where>
            flaw.del_flag = '0' and flaw.status = '0'
                        <if test="flawId != null ">
                            and flaw.id = #{flawId}
                        </if>
        </where>
        order by flaw.sort
    </select>

    <select id="selectClothFlawTopById" parameterType="Long"
            resultMap="ClothFlawTopResult">
            <include refid="selectClothFlawTopVo"/>
            where id = #{id}
    </select>

    <insert id="insertClothFlawTop" parameterType="ClothFlawTop" useGeneratedKeys="true"
            keyProperty="id">
        insert into cloth_flaw_top
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="flawId != null">flaw_id,
                    </if>
                    <if test="targetId != null">target_id,
                    </if>
                    <if test="sourceType != null">source_type,
                    </if>
                    <if test="createBy != null">create_by,
                    </if>
                    <if test="createById != null">create_by_id,
                    </if>
                    <if test="createTime != null">create_time,
                    </if>
                    <if test="updateBy != null">update_by,
                    </if>
                    <if test="updateById != null">update_by_id,
                    </if>
                    <if test="updateTime != null">update_time,
                    </if>
                    <if test="remark != null">remark,
                    </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                    <if test="flawId != null">#{flawId},
                    </if>
                    <if test="targetId != null">#{targetId},
                    </if>
                    <if test="sourceType != null">#{sourceType},
                    </if>
                    <if test="createBy != null">#{createBy},
                    </if>
                    <if test="createById != null">#{createById},
                    </if>
                    <if test="createTime != null">#{createTime},
                    </if>
                    <if test="updateBy != null">#{updateBy},
                    </if>
                    <if test="updateById != null">#{updateById},
                    </if>
                    <if test="updateTime != null">#{updateTime},
                    </if>
                    <if test="remark != null">#{remark},
                    </if>
        </trim>
    </insert>

    <update id="updateClothFlawTop" parameterType="ClothFlawTop">
        update cloth_flaw_top
        <trim prefix="SET" suffixOverrides=",">
                    <if test="flawId != null">flaw_id =
                        #{flawId},
                    </if>
                    <if test="targetId != null">target_id =
                        #{targetId},
                    </if>
                    <if test="sourceType != null">type =
                        #{sourceType},
                    </if>
                    <if test="createBy != null">create_by =
                        #{createBy},
                    </if>
                    <if test="createById != null">create_by_id =
                        #{createById},
                    </if>
                    <if test="createTime != null">create_time =
                        #{createTime},
                    </if>
                    <if test="updateBy != null">update_by =
                        #{updateBy},
                    </if>
                    <if test="updateById != null">update_by_id =
                        #{updateById},
                    </if>
                    <if test="updateTime != null">update_time =
                        #{updateTime},
                    </if>
                    <if test="remark != null">remark =
                        #{remark},
                    </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteClothFlawTop" parameterType="ClothFlawTopDTO">
        delete from cloth_flaw_top where flaw_id=#{flawId} and source_type=#{sourceType} and target_id=#{targetId}
    </delete>

    <delete id="deleteClothFlawTopByIds" parameterType="String">
        delete from cloth_flaw_top where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>