<?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.technologicalProcessDetailDrawing.mapper.BizTechnologicalProcessDetailDrawingMapper">

    <resultMap type="cn.ezhizao.project.business.technologicalProcessDetailDrawing.domain.BizTechnologicalProcessDetailDrawing" id="BizTechnologicalProcessDetailDrawingResult">
        <id column="id" property="id"/>
        <result column="process_code" property="processCode"/>
        <association property="product" column="product_id"  javaType="cn.ezhizao.project.business.product.domain.BizProduct" select="getProduct"/>
        <association property="processAlias" column="process_code" javaType="java.lang.String" select="getProcessAlias"/>
        <association property="recordsType" column="id" javaType="java.lang.Integer" select="getRecordsType"/>
        <association property="recordsAbandoned" column="id" javaType="java.lang.Integer" select="getRecordsAbandoned"/>
    </resultMap>
    <resultMap id="BizProductResult" type="cn.ezhizao.project.business.product.domain.BizProduct">
        <id column="id" property="id"/>
    </resultMap>
    <select id="getProcessAlias" resultType="java.lang.String">
        select process_alias from biz_process where deleted = 0 and process_code = #{processCode}
    </select>
    <select id="getProcuctDrawing" resultType="integer">
        select count(1) from biz_product where deleted = 0 and type = 9 and production_type_code = 'N' and id in(
            select product_id from biz_technological_process_detail_drawing where deleted = 0 AND MONTH(create_time) = MONTH(CURRENT_DATE)
        )
    </select>

    <select id="getRecordsType" resultType="java.lang.Integer">
        select type from biz_technological_process_detail_drawing_user where deleted = 0 and drawing_id = #{id} order by create_time desc limit 1
    </select>

    <select id="getRecordsAbandoned" resultType="java.lang.Integer">
        select abandoned from biz_technological_process_detail_drawing_user where deleted = 0 and drawing_id = #{id} order by create_time desc limit 1
    </select>
    <select id="getProduct" resultMap="BizProductResult">
        select * from biz_product where deleted = 0 and id = #{product_id}
    </select>

    <select id="getList" parameterType="BizTechnologicalProcessDetailDrawing" resultMap="BizTechnologicalProcessDetailDrawingResult">
        SELECT * FROM biz_technological_process_detail_drawing
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0 and abandoned = 0 and status &lt;3
            <if test="url != null  and url != ''"> AND url = #{url}</if>
            <if test="productId != null">and product_id = #{productId} </if>
            <if test="productVersion != null and productVersion !=''">AND product_version = #{productVersion}</if>
            <if test="processCode != null and processCode != ''">AND process_code = #{processCode}</if>
            <if test="drawingName != null  and drawingName != ''"> AND drawing_name = #{drawingName}</if>
        </trim>
        order by create_time desc
    </select>


    <select id="getDrawingList" parameterType="BizTechnologicalProcessDetailDrawing" resultMap="BizTechnologicalProcessDetailDrawingResult">
        SELECT * FROM biz_technological_process_detail_drawing
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="abandoned != null  "> AND abandoned = #{abandoned}</if>
            <if test="tenantId != null and tenantId != 0">AND tenant_id = #{tenantId}</if>
            <if test="url != null  and url != ''"> AND url = #{url}</if>
            <if test="status != null ">AND status = #{status}</if>
            <if test="productId != null">and product_id = #{productId} </if>
            <if test="productVersion != null and productVersion !=''">AND product_version = #{productVersion}</if>
            <if test="technologyVersion != null and technologyVersion !=''">AND product_version = #{technologyVersion}</if>
            <if test="processCode != null and processCode != 'undefined' and processCode != ''">AND process_code = #{processCode}</if>
            <if test="drawingName != null  and drawingName != ''"> AND drawing_name = #{drawingName}</if>
            <if test="drawingNumber != null and drawingNumber != ''">AND  product_id IN (select id from biz_product where deleted = 0 and drawing_number  like concat('%', #{drawingNumber}, '%')) </if>
            <if test="companyAlias != null and companyAlias != ''">AND  product_id IN (select id from biz_product where deleted = 0 and company_alias  like concat('%', #{companyAlias}, '%')) </if>
            <if test="specification != null and specification != ''">AND  product_id IN (select id from biz_product where deleted = 0 and specification  like concat('%', #{specification}, '%')) </if>
            <if test="description != null and description != ''">AND product_id IN (select id from biz_product where deleted = 0 and description like concat('%', #{description}, '%')) </if>
            <if test="processAlias !=null and processAlias !=''">AND process_code IN(select process_code from biz_process where deleted = 0 and  process_alias  like concat('%', #{processAlias}, '%')) </if>
            <if test="pattern != null and pattern != ''">
                <if test="pattern == '作废'">
                    AND abandoned = 1
                </if>
                <if test="pattern != '作废'">
                    AND pattern LIKE CONCAT('%', #{pattern}, '%')
                </if>
            </if>
        </trim>
        order by create_time desc
    </select>


    <select id="getDrawingListByBasics" parameterType="BizTechnologicalProcessDetailDrawing" resultMap="BizTechnologicalProcessDetailDrawingResult">
        SELECT * FROM biz_technological_process_detail_drawing
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0 and issuer_id = 0 and reviewer_id = 0 and producer_id = 0
            <if test="abandoned != null  "> AND abandoned = #{abandoned}</if>
            <if test="url != null  and url != ''"> AND url = #{url}</if>
            <if test="status != null ">AND status = #{status}</if>
            <if test="productId != null">and product_id = #{productId} </if>
            <if test="productVersion != null and productVersion !=''">AND product_version = #{productVersion}</if>
            <if test="technologyVersion != null and technologyVersion !=''">AND product_version = #{technologyVersion}</if>
            <if test="processCode != null and processCode != 'undefined' and processCode != ''">AND process_code = #{processCode}</if>
            <if test="drawingName != null  and drawingName != ''"> AND drawing_name = #{drawingName}</if>
            <if test="drawingNumber != null and drawingNumber != ''">AND  product_id IN (select id from biz_product where deleted = 0 and drawing_number  like concat('%', #{drawingNumber}, '%')) </if>
            <if test="companyAlias != null and companyAlias != ''">AND  product_id IN (select id from biz_product where deleted = 0 and company_alias  like concat('%', #{companyAlias}, '%')) </if>
            <if test="specification != null and specification != ''">AND  product_id IN (select id from biz_product where deleted = 0 and specification  like concat('%', #{specification}, '%')) </if>
            <if test="description != null and description != ''">AND product_id IN (select id from biz_product where deleted = 0 and description like concat('%', #{description}, '%')) </if>
            <if test="processAlias !=null and processAlias !=''">AND process_code IN(select process_code from biz_process where deleted = 0 and  process_alias  like concat('%', #{processAlias}, '%')) </if>
            <if test="pattern != null and pattern != ''">AND pattern like concat('%', #{pattern}, '%')</if>
        </trim>
        order by create_time desc
    </select>
    <delete id="physicalDelete">
        DELETE FROM biz_technological_process_detail_drawing
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
