<?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.zfire.mall.manager.commonmapper.manage.material.ProductComposeReserveMapper">

    <resultMap id="listDetailResultMap" type="com.zfire.mall.manager.bean.manage.material.ProductComposeBean">
        <result column="id" jdbcType="VARCHAR" property="id" />
        <result column="admin_company_id" jdbcType="VARCHAR" property="adminCompanyId" />
        <result column="material_id" jdbcType="VARCHAR" property="materialId" />
        <result column="product_model" jdbcType="VARCHAR" property="productModel" />
        <result column="product_number" jdbcType="VARCHAR" property="productNumber" />
        <result column="create_by" jdbcType="VARCHAR" property="createBy" />
        <result column="create_name" jdbcType="VARCHAR" property="createName" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
        <result column="update_name" jdbcType="VARCHAR" property="updateName" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <collection property="items" ofType="com.zfire.mall.manager.plus.entity.ProductComposeRela">
            <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
            <result column="machine_configure_id" jdbcType="VARCHAR" property="machineConfigureId" />
            <result column="type" jdbcType="VARCHAR" property="type" />
        </collection>
    </resultMap>

    <select id="listPage" resultType="com.zfire.mall.manager.bean.manage.material.ProductComposeListBean">
        SELECT
            a.*,
            m.name AS materialName,
            m.specification AS product_model,
            m.number AS product_number,
            c.type,
            c.name,
            c.model,
            c.number,
            c.volume
        FROM
            product_compose a
                LEFT JOIN king_dee_material m ON a.material_id = m.id
                LEFT JOIN product_compose_rela b ON a.id = b.parent_id
                LEFT JOIN machine_configure_record c ON b.machine_configure_id = c.id
        <where>
            <if test="adminCompanyId != null and adminCompanyId != ''">
                AND a.admin_company_id = #{adminCompanyId}
            </if>
            <if test="name != null and name != ''">
                AND m.`name` like concat('%',#{name},'%')
            </if>
            <if test="type != null and type != ''">
                AND m.specification like concat('%',#{type},'%')
            </if>
            <if test="number != null and number != ''">
                AND m.number like concat('%',#{number},'%')
            </if>
            <if test="volume != null and volume != ''">
                AND c.volume = #{volume}
            </if>
            <if test="machineType != null and machineType != ''">
                AND c.type = #{machineType}
            </if>
            <if test="machineName != null and machineName != ''">
                AND c.name like concat('%',#{machineName},'%')
            </if>
            <if test="machineModel != null and machineModel != ''">
                AND c.model like concat('%',#{machineModel},'%')
            </if>
            <if test="machineNumber != null and machineNumber != ''">
                AND c.number like concat('%',#{machineName},'%')
            </if>
            AND a.del = 0
        </where>
        ORDER BY a.create_time desc, a.id DESC
    </select>
    <select id="detail" resultMap="listDetailResultMap">
        SELECT
            a.*,
            b.parent_id,
            b.machine_configure_id,
            b.type
        FROM
            product_compose a
            LEFT JOIN product_compose_rela b ON a.id = b.parent_id
        <where>
            <if test="adminCompanyIds != null and adminCompanyIds.size > 0">
                AND a.admin_company_id in
                <foreach item="item" index="index" collection="adminCompanyIds" open="(" separator=","
                         close=")">
                    #{item}
                </foreach>
            </if>
            AND a.id = #{id}
            AND a.del = 0
        </where>
    </select>
    <select id="getMaterialDetail" resultType="com.zfire.mall.manager.plus.entity.MachineConfigureRecord">
        SELECT
            c.*
        FROM
            product_compose a
                LEFT JOIN product_compose_rela b ON a.id = b.parent_id
                LEFT JOIN machine_configure_record c ON b.machine_configure_id = c.id
        WHERE
            a.material_id = #{id}
    </select>

    <select id="queryOne" resultType="com.zfire.mall.manager.plus.entity.ProductCategory">
        select b.* from product_category_rela a
            join product_category b on a.product_category_id=b.product_category_id
            where a.king_dee_category_id = #{kingDeeCategoryId}
    </select>
    <select id="v2ListPage" resultType="com.zfire.mall.manager.bean.manage.v2.material.ProductComposeVO">
        SELECT
            a.*,
            m.name AS materialName,
            m.specification AS product_model,
            m.number AS product_number,
            c.type,
            c.name,
            c.model,
            c.number,
            c.volume
        FROM
            product_compose a
                LEFT JOIN king_dee_material m ON a.material_id = m.id
                LEFT JOIN product_compose_rela b ON a.id = b.parent_id
                LEFT JOIN machine_configure_record c ON b.machine_configure_id = c.id
        ${ex.query}
        AND a.del = 0
        <if test="ex.orderBy == null or ex.orderBy ==''">
            ORDER BY a.create_time desc, a.id DESC
        </if>
        ${ex.orderBy}
    </select>

</mapper>