<?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="cn.ezhizao.project.business.mapper.BizProductionPlanDetailSubDetailMapper">

    <resultMap type="cn.ezhizao.project.business.domain.BizProductionPlanDetailSubDetail" id="BizProductionPlanDetailSubDetailResult">
        <id column="id" property="id"/>
        <result column="production_plan_detail_id" property="productionPlanDetailId"/>
        <association property="productionPlanNo" javaType="java.lang.String" column="production_plan_detail_id" select="getProductionPlanNo"/>
        <association property="productDescription" javaType="java.lang.String" column="product_id" select="getProductDescription"/>
    </resultMap>
    <select id="getProductDescription" resultType="java.lang.String">
        select description as productDescription from biz_product where deleted = 0 and id = #{product_id}
    </select>

    <select id="getProductionPlanNo" resultType="java.lang.String">
        select production_plan_no from biz_production_plan_detail where id = #{production_plan_detail_id}
    </select>
    <select id="getList" parameterType="BizProductionPlanDetailSubDetail" resultMap="BizProductionPlanDetailSubDetailResult">
        SELECT * FROM biz_production_plan_detail_sub_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="lotNumber != null "> AND lot_number = #{lotNumber}</if>
        </trim>
        ORDER BY create_time DESC
    </select>

    <!-- 根据关键字获取列表 -->
    <select id="getListByKeywords" parameterType="String" resultMap="BizProductionPlanDetailSubDetailResult">
        SELECT
            t1.*,
            t2.specification AS product_specification,
            t2.drawing_number,
            t2.company_alias,
            t2.description AS product_description
        FROM biz_production_plan_detail_sub_detail t1
        INNER JOIN biz_product t2 ON t1.product_id=t2.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="keywords != null and keywords != ''"> AND t2.description LIKE CONCAT('%', #{keywords}, '%')</if>
        </trim>
        ORDER BY create_time DESC
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_production_plan_detail_sub_detail
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
