<?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.mmc.pms.dao.WebDeviceDao">
    <resultMap id="secondDistrict"
               type="com.mmc.pms.entity.DistrictDO">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="level" column="level"/>
        <result property="pid" column="pid"/>
        <collection property="childInfo" javaType="java.util.List"
                    ofType="com.mmc.pms.entity.DistrictDO">
            <id property="id" column="childId"/>
            <result property="name" column="childName"/>
            <result property="level" column="childLevel"/>
            <result property="pid" column="childPid"/>
        </collection>
    </resultMap>

    <resultMap type="com.mmc.pms.entity.WareInfoDO"
               id="wareInfoResultMap">
        <id property="id" column="id"/>
        <result property="wareNo" column="ware_no"/>
        <result property="wareTitle" column="ware_title"/>
        <result property="wareTypeId" column="ware_type_id"/>
        <result property="wareStatus" column="ware_status"/>
        <result property="payStatus" column="pay_status"/>
        <result property="minDeposit" column="min_deposit"/>
        <result property="maxDeposit" column="max_deposit"/>
        <result property="minRent" column="min_rent"/>
        <result property="maxRent" column="max_rent"/>
        <result property="totalStock" column="total_stock"/>
        <result property="totalSale" column="total_sale"/>
        <result property="skuNum" column="sku_num"/>
        <result property="tags" column="tags"/>
        <result property="deleted" column="is_deleted"/>
        <result property="updateTime" column="update_time"/>
        <result property="createTime" column="create_time"/>

        <association property="warePropDO"
                     javaType="com.mmc.pms.entity.WarePropDO">
            <id property="id" column="ware_prop_id"/>
            <result property="wareInfoId" column="ware_info_id"/>
            <result property="propInfoId" column="prop_info_id"/>
            <result property="propPoster" column="prop_poster"/>
        </association>

        <collection property="wareImgs" javaType="java.util.List"
                    ofType="com.mmc.pms.entity.WareImgDO">
            <id property="id" column="ware_img_id"/>
            <result property="wareInfoId" column="ware_info_id"/>
            <result property="imgUrl" column="img_url"/>
            <result property="imgType" column="img_type"/>
        </collection>
    </resultMap>
    <select id="listSecondDistrict" resultMap="secondDistrict">
        select p1.id,
               p1.`name`,
               p1.`level`,
               p1.pid,
               p2.id      as childId,
               p2.`name`  as
                             childName,
               p2.`level` as childLevel,
               p2.pid     as childPid
        from sys_district p1,
             sys_district p2
        where p2.level!=3 and p1.id=p2.pid
    </select>

    <select id="category" resultType="com.mmc.pms.entity.DeviceCategory">
        select id, `name`
        from device_category;
    </select>

    <select id="brand" resultType="com.mmc.pms.entity.Brand">
        select id, `name`
        from brand;
    </select>

    <select id="model" resultType="com.mmc.pms.entity.Model">
        select id, `name`
        from model;
    </select>

    <select id="deviceList" resultType="com.mmc.pms.entity.DeviceListDO">
        SELECT d.id,
        d.`name` AS deviceName,
        d.image_url AS imageUrl,
        d.price,
        b.`name` AS brandName,
        sd.`name` AS sysDistrictName,
        m.`name` AS modelName,
        dc.`name` AS deviceCategoryName,
        i.residue_count AS residueCount,
        i.id as inventoryId,
        d.appraise
        FROM device d
        LEFT JOIN brand b ON d.brand_id = b.id
        LEFT JOIN sys_district sd ON d.sys_district_id = sd.id
        LEFT JOIN model m ON m.id = d.model_id
        LEFT JOIN device_category dc ON dc.id = d.device_category_id
        LEFT JOIN inventory i ON i.id = d.inventory_id
        WHERE 1=1
        <if test='districtId!=null and districtId!=" " '>
            and sd.id = #{districtId}
        </if>
        <if test='brandId!=null and brandId!=" " '>
            and b.id = #{brandId}
        </if>
        <if test='modelId!=null and modelId!=" " '>
            and m.id = #{modelId}
        </if>
        <if test='categoryId!=null and categoryId!=" " '>
            and dc.id = #{categoryId}
        </if>
    </select>

    <update id="update">
        update inventory
        set residue_count=#{inventoryUsage}
        where id = #{inventoryId}
    </update>
    <select id="findInventory" resultType="com.mmc.pms.entity.InventoryDO">
        select id, all_count AS allCount, residue_count AS residueCount
        from inventory
    </select>

    <select id="detail" resultMap="wareInfoResultMap">
        select
        wi.id,wi.ware_no,wi.ware_type_id,wi.ware_title,wi.ware_status,wi.pay_status,wi.min_deposit,wi.max_deposit,wi.min_rent,
        wi.max_rent,wi.total_stock,wi.total_sale,wi.sku_num,wi.tags,wi.district_id,
        wi.brand_id,
        wi.model_id,wi.create_time,wi.update_time,img.id as
        ware_img_id,img.ware_info_id,img.img_url,img.img_type,wp.prop_info_id
        from ware_info wi INNER JOIN ware_img img ON wi.id=img.ware_info_id LEFT JOIN ware_prop wp on
        wi.id=wp.ware_info_id
        where wi.is_deleted = 0 and img.img_type = 0
        <if test="id != null ">
            and wi.id = #{id}
        </if>
        order by wi.create_time DESC
    </select>

    <select id="countListWareInfoPage" parameterType="com.mmc.pms.model.qo.WareInfoQO" resultType="int">
        select count(*)
        from ware_info wi INNER JOIN ware_img img ON wi.id=img.ware_info_id LEFT JOIN ware_prop wp on
        wi.id=wp.ware_info_id
        where wi.is_deleted = 0 and img.img_type = 0
        <if test="districtId != null ">
            and wi.district_id = #{districtId}
        </if>
        <if test="categoryId != null ">
            and wi.ware_type_id = #{categoryId}
        </if>
        <if test="brandId != null ">
            and wi.brand_id = #{brandId}
        </if>
        <if test="modelId != null and modelId != '' ">
            and wi.model_id = #{modelId}
        </if>
    </select>

    <select id="listWareInfoPage" parameterType="com.mmc.pms.model.qo.WareInfoQO" resultMap="wareInfoResultMap">
        select
        wi.id,wi.ware_no,wi.ware_type_id,wi.ware_title,wi.ware_status,wi.pay_status,wi.min_deposit,wi.max_deposit,wi.min_rent,
        wi.max_rent,wi.total_stock,wi.total_sale,wi.sku_num,wi.tags,wi.district_id,
        wi.brand_id,
        wi.model_id,wi.create_time,wi.update_time,img.id as
        ware_img_id,img.ware_info_id,img.img_url,img.img_type,wp.prop_info_id
        from ware_info wi INNER JOIN ware_img img ON wi.id=img.ware_info_id LEFT JOIN ware_prop wp on
        wi.id=wp.ware_info_id
        where wi.is_deleted = 0 and img.img_type = 0
        <if test="id != null ">
            and wi.id = #{id}
        </if>
        <if test="districtId != null ">
            and wi.district_id = #{districtId}
        </if>
        <if test="categoryId != null ">
            and wi.ware_type_id = #{categoryId}
        </if>
        <if test="brandId != null ">
            and wi.brand_id = #{brandId}
        </if>
        <if test="modelId != null and modelId != '' ">
            and wi.model_id = #{modelId}
        </if>
        order by wi.create_time DESC
        limit #{pageNo},#{pageSize}
    </select>
    <select id="getWareInfoById" resultMap="wareInfoResultMap" parameterType="java.lang.Integer">
        select wi.id,
               wi.ware_no,
               wi.ware_type_id,
               wi.ware_title,
               wi.ware_status,
               wi.pay_status,
               wi.min_deposit,
               wi.max_deposit,
               wi.is_deleted,
               wi.min_rent,
               wi.max_rent,
               wi.total_stock,
               wi.total_sale,
               wi.sku_num,
               wi.tags,
               wi.create_time,
               wi.update_time,
               wp.id  as ware_prop_id,
               wp.ware_info_id,
               wp.prop_info_id,
               wp.prop_poster,
               img.id as ware_img_id,
               img.img_url,
               img.img_type
        from ware_info wi
                 INNER JOIN ware_img img ON wi.id = img.ware_info_id
                 LEFT JOIN ware_prop wp ON wi.id = wp.ware_info_id
        where wi.is_deleted = 0
          and wi.id = #{id}
    </select>
    <resultMap id="wareDetailResultMap" type="com.mmc.pms.entity.WareDetailDO">
        <id property="id" column="id"/>
        <result property="wareInfoId" column="ware_info_id"/>
        <result property="content" column="content"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <select id="getWareDetailById" resultMap="wareDetailResultMap" parameterType="java.lang.Integer">
        select id, ware_info_id, content, create_time, update_time
        from ware_detail
        where ware_info_id = #{wareInfoId}
    </select>
</mapper>
