<?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.lframework.xingyun.basedata.mappers.ProductPropertyMapper">

    <resultMap id="ProductProperty" type="com.lframework.xingyun.basedata.entity.ProductProperty">
        <id column="id" property="id"/>
        <result column="code" property="code"/>
        <result column="name" property="name"/>
        <result column="is_required" property="isRequired"/>
        <result column="column_type" property="columnType"/>
        <result column="column_data_type" property="columnDataType"/>
        <result column="property_type" property="propertyType"/>
        <result column="available" property="available"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="create_time" property="createTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <resultMap id="ProductPropertyModelorDto"
               type="com.lframework.xingyun.basedata.dto.product.property.ProductPropertyModelorDto">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="is_required" property="isRequired"/>
        <result column="column_type" property="columnType"/>
        <result column="column_data_type" property="columnDataType"/>
        <result column="property_type" property="propertyType"/>
        <collection property="items"
                    ofType="com.lframework.xingyun.basedata.dto.product.property.ProductPropertyModelorDto$ProductPropertyItemModelorDto"
                    javaType="java.util.ArrayList">
            <id column="item_id" property="id"/>
            <result column="item_name" property="name"/>
        </collection>
    </resultMap>

    <sql id="ProductPropertyDto_sql">
        SELECT
            p.id,
            p.code,
            p.name,
            p.is_required,
            p.column_type,
            p.column_data_type,
            p.property_type,
            p.available,
            p.description,
            p.create_by,
            p.create_time,
            p.update_by,
            p.update_time
        FROM base_data_product_property AS p
    </sql>

    <select id="query" resultMap="ProductProperty">
        <include refid="ProductPropertyDto_sql"/>
        <where>
            <if test="vo != null">
                <if test="vo.code != null and vo.code != ''">
                    AND p.code = #{vo.code}
                </if>
                <if test="vo.name != null and vo.name != ''">
                    AND p.name LIKE CONCAT('%', #{vo.name}, '%')
                </if>
                <if test="vo.available != null">
                    AND p.available = #{vo.available}
                </if>
            </if>
        </where>
        ORDER BY p.code
    </select>
    <select id="getModelorByCategoryId" resultMap="ProductPropertyModelorDto">
        SELECT
        p.id, p.name, p.is_required, p.column_type, p.column_data_type, p.property_type, i.id AS item_id, i.name AS
        item_name
        FROM base_data_product_property AS p
        LEFT JOIN base_data_product_property_item AS i ON i.property_id = p.id AND i.available = TRUE
        WHERE (p.property_type = 1 OR (p.property_type = 2 AND p.id IN (SELECT property_id FROM base_data_product_category_property WHERE category_id IN <foreach collection="categoryIds" open="(" separator="," close=")" item="item">#{item}</foreach>)))
        AND ((p.column_type IN (1, 2) AND i.id IS NOT NULL) OR p.column_type = 3)
        AND p.available = TRUE
        ORDER BY p.code, i.code
    </select>
</mapper>
