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

    <resultMap type="ClothHangerDetail" id="ClothHangerDetailResult">
        <result property="id" column="id"/>
        <result property="hangerId" column="hanger_id"/>
        <result property="orgId" column="org_id"/>
        <result property="sourceType" column="source_type"/>
        <result property="code" column="code"/>
        <result property="isHanger" column="is_hanger"/>
        <result property="createById" column="create_by_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateById" column="update_by_id"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="hangerCode" column="hanger_code"/>
        <result property="washCode" column="wash_code"/>
    </resultMap>

    <sql id="selectClothHangerDetailVo">
        select id, hanger_id, org_id, source_type, code, is_hanger, create_by_id, create_by, create_time, update_by_id, update_by, update_time, remark, hanger_code,wash_code
        from cloth_hanger_detail
    </sql>

    <select id="selectClothHangerDetailList" parameterType="ClothHangerDetail" resultMap="ClothHangerDetailResult">
        <include refid="selectClothHangerDetailVo"/>
        <where>
            hanger_id = #{hangerId}
        </where>
    </select>

    <select id="selectClothHangerDetailById" parameterType="Long"
            resultMap="ClothHangerDetailResult">
        <include refid="selectClothHangerDetailVo"/>
        where id = #{id}
    </select>
    <select id="getClothOrderInfoByHangerCodeAndId" resultType="com.yiqi.core.domain.vo.ClothHangerOrderRelationVO">
        select d.order_no as orderNo, f.name as clothName, g.cloth_color_name as colorName, b.cloth_hanger_code as hangerCode ,
               d.take_cloth_time as takeClothTime, c.wash_code as washCode,
                d.order_status as status, e.real_name  as userName, e.phone_number  as phoneNumber
            FROM order_cloth_item c
            LEFT JOIN cloth_hanger_detail a ON a.hanger_id = c.cloth_hanger_id  AND a.CODE = c.cloth_hanger_code
            LEFT JOIN cloth_hanger_order_relation b ON a.hanger_id = b.cloth_hanger_id  AND a.CODE = b.cloth_hanger_code  AND b.order_cloth_item_id = c.id
            LEFT JOIN order_cloth_color g ON g.order_cloth_id = c.id
            LEFT JOIN order_cloth d ON c.order_id = d.id
            LEFT JOIN app_user e ON d.app_user_id = e.id
            LEFT JOIN cloth_item f ON f.id = c.cloth_item_id
        where a.hanger_id = #{hangerId} and a.code = #{hangerCode}
    </select>
    <select id="getIsHangerList" resultType="com.yiqi.core.domain.vo.ClothHangerDetailVO">
        select a.id as id, a.code as code,a.hanger_id as hangerId, a.is_hanger as isHanger,  count(b.id) as isHangerCount
        from cloth_hanger_detail a
            left join order_cloth_item b
            on a.hanger_id = b.cloth_hanger_id and a.code = b.cloth_hanger_code
                 where a.hanger_id = #{hangerId} group by a.id
    </select>

    <insert id="insertClothHangerDetail" parameterType="ClothHangerDetail" useGeneratedKeys="true"
            keyProperty="id">
        insert into cloth_hanger_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="hangerId != null">hanger_id,
            </if>
            <if test="orgId != null">org_id,
            </if>
            <if test="sourceType != null">source_type,
            </if>
            <if test="code != null">code,
            </if>
            <if test="isHanger != null">is_hanger,
            </if>
            <if test="createById != null">create_by_id,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateById != null">update_by_id,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
            <if test="remark != null">remark,
            </if>
            <if test="hangerCode != null">hanger_code,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="hangerId != null">#{hangerId},
            </if>
            <if test="orgId != null">#{orgId},
            </if>
            <if test="sourceType != null">#{sourceType},
            </if>
            <if test="code != null">#{code},
            </if>
            <if test="isHanger != null">#{isHanger},
            </if>
            <if test="createById != null">#{createById},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateById != null">#{updateById},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
            <if test="remark != null">#{remark},
            </if>
            <if test="hangerCode != null">#{hangerCode},
            </if>
        </trim>
    </insert>

    <update id="updateClothHangerDetail" parameterType="ClothHangerDetail">
        update cloth_hanger_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="hangerId != null">hanger_id =
                #{hangerId},
            </if>
            <if test="orgId != null">org_id =
                #{orgId},
            </if>
            <if test="sourceType != null">source_type =
                #{sourceType},
            </if>
            <if test="code != null">code =
                #{code},
            </if>
            <if test="isHanger != null">is_hanger =
                #{isHanger},
            </if>
            <if test="createById != null">create_by_id =
                #{createById},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateById != null">update_by_id =
                #{updateById},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
            <if test="hangerCode != null">hanger_code =
                #{hangerCode},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteClothHangerDetailById" parameterType="Long">
        delete from cloth_hanger_detail where id = #{id}
    </delete>

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



</mapper>