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

    <resultMap type="com.yiqi.core.domain.vo.ClothFlawVo" id="ClothFlawResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="flawTypeId" column="flaw_type_id"/>
        <result property="sort" column="sort"/>
        <result property="status" column="status"/>
        <result property="flawTypeName" column="flaw_type_name"/>
        <result property="createTime" column="create_time"/>
    </resultMap>

    <sql id="selectClothFlawVo">
        select flaw.*,flawType.name as flaw_type_name
        from cloth_flaw as flaw left join cloth_flaw_type as flawType on flaw.flaw_type_id = flawType.id
    </sql>

    <select id="selectClothFlawList" parameterType="com.yiqi.core.domain.ClothFlaw" resultMap="ClothFlawResult">
        <include refid="selectClothFlawVo"/>
        <where>
            and flaw.del_flag = 0
                        <if test="name != null  and name != ''">
                            and flaw.name like concat('%', #{name}, '%')
                        </if>
                        <if test="flawTypeId != null  and flawTypeId != ''">
                            and flaw.flaw_type_id = #{flawTypeId}
                        </if>
                        <if test="sort != null ">
                            and flaw.sort = #{sort}
                        </if>
                        <if test="status != null  and status != ''">
                            and flaw.status = #{status}
                        </if>
                        <if test="createById != null ">
                            and flaw.create_by_id = #{createById}
                        </if>
                        <if test="updateById != null ">
                            and flaw.update_by_id = #{updateById}
                        </if>
        </where>
        order by flaw.sort
    </select>

    <select id="selectClothFlawById" parameterType="Long"
            resultMap="ClothFlawResult">
            <include refid="selectClothFlawVo"/>
            where flaw.id = #{id}
    </select>
    <select id="allClothFlaw" resultType="com.yiqi.core.domain.ClothFlaw">
        select f.*
        from cloth_flaw as f
                left join cloth_flaw_type as ftype on ftype.id=f.flaw_type_id
                 left join cloth_flaw_top as ft on f.id = ft.flaw_id and target_id = #{orgId} and source_type = #{sourceType}
        where ftype.status='0' and ftype.del_flag='0' and f.status = '0'
        and f.del_flag = '0' order by ft.source_type desc
    </select>

    <insert id="insertClothFlaw" parameterType="com.yiqi.core.domain.ClothFlaw" useGeneratedKeys="true"
            keyProperty="id">
        insert into cloth_flaw
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="name != null and name != ''">name,
                    </if>
                    <if test="flawTypeId != null">flaw_type_id,
                    </if>
                    <if test="sort != null">sort,
                    </if>
                    <if test="status != null">status,
                    </if>
                    <if test="delFlag != null">del_flag,
                    </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="name != null and name != ''">#{name},
                    </if>
                    <if test="flawTypeId != null">#{flawTypeId},
                    </if>
                    <if test="sort != null">#{sort},
                    </if>
                    <if test="status != null">#{status},
                    </if>
                    <if test="delFlag != null">#{delFlag},
                    </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="updateClothFlaw" parameterType="com.yiqi.core.domain.ClothFlaw">
        update cloth_flaw
        <trim prefix="SET" suffixOverrides=",">
                    <if test="name != null and name != ''">name =
                        #{name},
                    </if>
                    <if test="flawTypeId != null">flaw_type_id =
                        #{flawTypeId},
                    </if>
                    <if test="sort != null">sort =
                        #{sort},
                    </if>
                    <if test="status != null">status =
                        #{status},
                    </if>
                    <if test="delFlag != null">del_flag =
                        #{delFlag},
                    </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>

    <update id="deleteClothFlawById" parameterType="Long">
        update cloth_flaw
        <trim prefix="SET" suffixOverrides=",">
            del_flag = 2 ,
            update_time = now()
        </trim>
        where id = #{id}
    </update>

    <update id="deleteClothFlawByIds" parameterType="String">
        update cloth_flaw
        <trim prefix="SET" suffixOverrides=",">
            del_flag = 2 ,
            update_time = now()
        </trim>
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>