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

    <resultMap type="ClothItemVo" id="ClothItemResult">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="typeId" column="type_id"/>
            <result property="typeName" column="type_name"/>
            <result property="unit" column="unit"/>
            <result property="isChangePrice" column="is_change_price"/>
            <result property="isCalArea" column="is_cal_area"/>
            <result property="isSpecial" column="is_special"/>
            <result property="imgUrl" column="img_url"/>
            <result property="content" column="content"/>
            <result property="sort" column="sort"/>
            <result property="status" column="status"/>
            <result property="delFlag" column="del_flag"/>
            <result property="createBy" column="create_by"/>
            <result property="createById" column="create_by_id"/>
            <result property="createTime" column="create_time"/>
            <result property="updateBy" column="update_by"/>
            <result property="updateById" column="update_by_id"/>
            <result property="updateTime" column="update_time"/>
            <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectClothItemVo">
        select item.*,type.name as type_name,wm.name as wash_name
        from cloth_item as item
        left join cloth_type as type on item.type_id = type.id
        left join cloth_wash_mode as wm on wm.id=item.wash_id
    </sql>

    <select id="selectClothItemPriceList" parameterType="Long" resultType="com.yiqi.core.domain.vo.ClothItemPriceVO">
        select c.*, cp.price
            from cloth_item as c
             left join cloth_type as ct on ct.id = c.type_id
             left join sys_store as s on s.id = #{storeId}
             left join cloth_price as cp
                       on cp.price_type = '0' and cp.store_area_id = s.bind_area_id and cp.cloth_id = c.id and
                          c.wash_id = cp.wash_mode_id
            where ct.del_flag='0' and ct.status='0'
    </select>

    <select id="selectClothItemList" parameterType="ClothItem" resultMap="ClothItemResult">
        <include refid="selectClothItemVo"/>
        <where>
            item.del_flag = 0
                        <if test="name != null  and name != ''">
                            and item.name like concat('%', #{name}, '%')
                        </if>
                        <if test="typeId != null ">
                            and item.type_id = #{typeId}
                        </if>
                        <if test="washId != null ">
                            and item.wash_id = #{washId}
                        </if>
                        <if test="unit != null  and unit != ''">
                            and item.unit = #{unit}
                        </if>
                        <if test="isChangePrice != null  and isChangePrice != ''">
                            and item.is_change_price = #{isChangePrice}
                        </if>
                        <if test="isCalArea != null  and isCalArea != ''">
                            and item.is_cal_area = #{isCalArea}
                        </if>
                        <if test="isSpecial != null  and isSpecial != ''">
                            and item.is_special = #{isSpecial}
                        </if>
                        <if test="imgUrl != null  and imgUrl != ''">
                            and item.img_url = #{imgUrl}
                        </if>
                        <if test="content != null  and content != ''">
                            and item.content = #{content}
                        </if>
                        <if test="sort != null ">
                            and item.sort = #{sort}
                        </if>
                        <if test="status != null  and status != ''">
                            and item.status = #{status}
                        </if>
        </where>
        order by item.sort
    </select>

    <select id="selectClothItemById" parameterType="Long"
            resultMap="ClothItemResult">
            <include refid="selectClothItemVo"/>
            where item.id = #{id}
    </select>

    <insert id="insertClothItem" parameterType="ClothItem" useGeneratedKeys="true"
            keyProperty="id">
        insert into cloth_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="name != null and name != ''">name,
                    </if>
                    <if test="typeId != null">type_id,
                    </if>
                    <if test="washId != null">wash_id,
                    </if>
                    <if test="unit != null and unit != ''">unit,
                    </if>
                    <if test="isChangePrice != null">is_change_price,
                    </if>
                    <if test="isCalArea != null">is_cal_area,
                    </if>
                    <if test="isSpecial != null">is_special,
                    </if>
                    <if test="imgUrl != null">img_url,
                    </if>
                    <if test="content != null">content,
                    </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="typeId != null">#{typeId},
                    </if>
                    <if test="washId != null">#{washId},
                    </if>
                    <if test="unit != null and unit != ''">#{unit},
                    </if>
                    <if test="isChangePrice != null">#{isChangePrice},
                    </if>
                    <if test="isCalArea != null">#{isCalArea},
                    </if>
                    <if test="isSpecial != null">#{isSpecial},
                    </if>
                    <if test="imgUrl != null">#{imgUrl},
                    </if>
                    <if test="content != null">#{content},
                    </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="updateClothItem" parameterType="ClothItem">
        update cloth_item
        <trim prefix="SET" suffixOverrides=",">
                    <if test="name != null and name != ''">name =
                        #{name},
                    </if>
                    <if test="typeId != null">type_id =
                        #{typeId},
                    </if>
                    <if test="washId != null">wash_id =
                        #{washId},
                    </if>
                    <if test="unit != null and unit != ''">unit =
                        #{unit},
                    </if>
                    <if test="isChangePrice != null">is_change_price =
                        #{isChangePrice},
                    </if>
                    <if test="isCalArea != null">is_cal_area =
                        #{isCalArea},
                    </if>
                    <if test="isSpecial != null">is_special =
                        #{isSpecial},
                    </if>
                    <if test="imgUrl != null">img_url =
                        #{imgUrl},
                    </if>
                    <if test="content != null">content =
                        #{content},
                    </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="deleteClothItemById">
        update cloth_item
        <trim prefix="SET" suffixOverrides=",">
            del_flag = 2 ,
            update_time = now()
        </trim>
        where id = #{id}
    </update>

    <update id="deleteClothItemByIds">
        update cloth_item
        <trim prefix="SET" suffixOverrides=",">
            del_flag = 2 ,
            update_time = now()
        </trim>
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="allWithPrice" resultType="com.yiqi.core.domain.vo.ClothItemWithPriceQueryVO">
        select c.id,c.name,c.type_id,c.wash_id,wm.name as wash_name,ifnull(p.price,0) as default_price,c.unit
            ,c.is_change_price,c.is_cal_area,c.is_special,ifnull(plusCp.price,0) as plusPrice
            ,ifnull(silkCp.price,0) as silkPrice, ifnull(sheepCp.price,0) as sheepPrice, ifnull(childCp.price,0) as childPrice
        from cloth_item as c
            left join cloth_wash_mode as wm on wm.id=c.wash_id
            left join cloth_price as p
                on p.price_type = '0' and p.wash_mode_id = c.wash_id and p.store_area_id = #{bindAreaId} and p.cloth_id = c.id
            left join cloth_price as plusCp on plusCp.price_type='1' and plusCp.cloth_id=c.id and plusCp.cloth_type='1' and plusCp.store_area_id = #{bindAreaId}
            left join cloth_price as silkCp on silkCp.price_type='1' and silkCp.cloth_id=c.id and silkCp.cloth_type='2' and silkCp.store_area_id = #{bindAreaId}
            left join cloth_price as sheepCp on sheepCp.price_type='1' and sheepCp.cloth_id=c.id and sheepCp.cloth_type='3' and sheepCp.store_area_id = #{bindAreaId}
            left join cloth_price as childCp on childCp.price_type='1' and childCp.cloth_id=c.id and childCp.cloth_type='4' and childCp.store_area_id = #{bindAreaId}
        <where>
            <if test="clothTypeId != null">
                and c.type_id=#{clothTypeId}
            </if>
        </where>
    </select>
</mapper>