<?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.activity.mapper.ActivityClothItemMapper">

    <resultMap type="ActivityClothItem" id="ActivityClothItemResult">
        <result property="id" column="id"/>
        <result property="activityId" column="activity_id"/>
        <result property="clothItemId" column="cloth_item_id"/>
        <result property="price" column="price"/>
        <result property="washModeIds" column="wash_mode_ids"/>
        <result property="delFlag" column="del_flag"/>
        <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"/>
    </resultMap>

    <sql id="selectActivityClothItemVo">
        select * from activity_cloth_item
    </sql>

    <select id="selectActivityClothItemList" parameterType="ActivityClothItem" resultType="com.yiqi.activity.domain.vo.ActivityClothItemVO">
        select ac.id, c.id as clothId, ac.cloth_item_id, ac.discount, c.name as cloth_item_name, ac.price, ac.wash_mode_ids,
        ac.create_by_id, ac.create_by, ac.create_time
        , ac.update_by_id, ac.update_by, ac.update_time, ac.remark
        from activity_cloth_item as ac
        left join cloth_item as c on c.id=ac.cloth_item_id
        <where>
            and ac.del_flag='0'
            <if test="clothItemId != null ">
                and ac.cloth_item_id = #{clothItemId}
            </if>
        </where>
    </select>

    <select id="selectActivityClothItemById" parameterType="Long"
            resultMap="ActivityClothItemResult">
        <include refid="selectActivityClothItemVo"/>
        where id = #{id}
        and del_flag='0'
    </select>
    <select id="getMinPriceByActivity" resultType="com.yiqi.activity.domain.vo.ActivityClothItemMinPriceVO">

        select aci.* , min(aci.price) as minPrice
        from
            activity_cloth_item aci
                LEFT JOIN activity_info ai on aci.activity_id = ai.id
                LEFT JOIN cloth_item ci on ci.id = aci.cloth_item_id and ci.wash_id = aci.wash_mode_id
                LEFT JOIN activity_apply_org aao on aci.activity_id = aao.activity_id
        where
        aci.del_flag = '0'
          and ai.`status` = '0'
          and ai.del_flag = '0'
          and ci.`status` = '0'
          and ci.del_flag = '0'
            and aao.org_id = #{orgId}
            and aao.source_type = #{sourceType}
        <if test="nowTime != null and nowTime !=''">
            <![CDATA[and ai.start_date <= #{nowTime} and  ai.end_date >= #{nowTime}]]>
        </if>
        GROUP BY aci.cloth_item_id
    </select>
    <select id="getMinPriceWith1ClothItemByActivity"
            resultType="com.yiqi.activity.domain.vo.ActivityClothItemMinPriceVO">
        SELECT
            aci.*, min(aci.price) as minPrice
        FROM
            activity_cloth_item aci
                LEFT JOIN activity_info ai ON aci.activity_id = ai.id
                LEFT JOIN cloth_item ci ON ci.id = aci.cloth_item_id
                LEFT JOIN activity_apply_org aao on aci.activity_id = aao.activity_id
        WHERE
            aci.del_flag = '0'
          AND ai.`status` = '0'
          AND ai.del_flag = '0'
          AND ci.`status` = '0'
          AND ci.del_flag = '0'

          and aci.cloth_item_id = #{clothId}
          and aao.org_id = #{orgId}
          and aao.source_type = #{sourceType}
            <if test="nowTime != null and nowTime !=''">
                <![CDATA[and ai.start_date <= #{nowTime} and  ai.end_date >= #{nowTime}]]>
            </if>
        GROUP BY aci.wash_mode_id
    </select>

    <insert id="insertActivityClothItem" parameterType="ActivityClothItem" useGeneratedKeys="true"
            keyProperty="id">
        insert into activity_cloth_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="activityId != null">activity_id,
            </if>
            <if test="clothItemId != null">cloth_item_id,
            </if>
            <if test="price != null">price,
            </if>
            <if test="washModeId != null">wash_mode_id,
            </if>
            <if test="delFlag != null">del_flag,
            </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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="activityId != null">#{activityId},
            </if>
            <if test="clothItemId != null">#{clothItemId},
            </if>
            <if test="price != null">#{price},
            </if>
            <if test="washModeId != null">#{washModeId},
            </if>
            <if test="delFlag != null">#{delFlag},
            </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>
        </trim>
    </insert>

    <update id="updateActivityClothItem" parameterType="ActivityClothItem">
        update activity_cloth_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="activityId != null">activity_id =
                #{activityId},
            </if>
            <if test="clothItemId != null">cloth_item_id =
                #{clothItemId},
            </if>
            <if test="price != null">price =
                #{price},
            </if>
            <if test="washModeId != null">wash_mode_id =
                #{washModeId},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteActivityClothItemById" parameterType="Long">
            update activity_cloth_item set del_flag='2' where id = #{id}
        </delete>

    <delete id="deleteActivityClothItemByIds" parameterType="String">
        update activity_cloth_item set del_flag='2' where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>



</mapper>