<?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.casic.missiles.mapper.business.BusinessOriginalRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.model.business.BusinessOriginalRecord">
        <id column="id" property="id"/>
        <result column="original_record_code" property="originalRecordCode"/>
        <result column="original_record_name" property="originalRecordName"/>
        <result column="measure_category" property="measureCategory"/>
        <result column="calibration_time" property="calibrationTime"/>
        <result column="environment_id" property="environmentId"/>
        <result column="temperature" property="temperature"/>
        <result column="humidity" property="humidity"/>
        <result column="calibration_place" property="calibrationPlace"/>
        <result column="order_id" property="orderId"/>
        <result column="sample_id" property="sampleId"/>
        <result column="template_id" property="templateId"/>
        <result column="original_record_file" property="originalRecordFile"/>
        <result column="remark" property="remark"/>
        <result column="is_del" property="isDel"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        <!-- id, original_record_code, original_record_name, measure_category, calibration_time, environment_id, calibration_place, order_id, sample_id, template_id, original_record_file, remark, is_del, create_user, create_time, update_time-->
        bor.*
    </sql>

    <select id="originListPage" resultType="com.casic.missiles.model.business.BusinessOriginalRecord">
        SELECT
            <include refid="Base_Column_List"/>,
            su.name  AS createUserName
        FROM business_original_record bor
            LEFT JOIN customer_sample_info cs ON bor.sample_id = cs.id
            LEFT JOIN sys_user su ON su.id = bor.create_user
        WHERE bor.is_del =0
        <if test="request.originalRecordCode != null and request.originalRecordCode !='' ">
            and bor.original_record_code like concat('%', #{request.originalRecordCode}, '%')
        </if>
        <if test="request.sampleNo != null and request.sampleNo !=''">
            and cs.sample_no like concat('%', #{request.sampleNo}, '%')
        </if>
        <if test="request.sampleName != null and request.sampleName !=''">
            and cs.sample_name like concat('%', #{request.sampleName}, '%')
        </if>
        <if test="request.sampleModel != null and request.sampleModel !=''">
            and cs.sample_model like concat('%', #{request.sampleModel}, '%')
        </if>
        <if test="request.manufacturingNo != null and request.manufacturingNo !=''">
            and cs.manufacturing_no like concat('%', #{request.manufacturingNo}, '%')
        </if>
        <if test="request.measureCategory != null and request.measureCategory !=''">
            and bor.measure_category = #{request.measureCategory}
        </if>
        <if test="request.createUser != null and request.createUser !=''">
            and  su.name = #{request.createUser}
        </if>
        ORDER BY bor.create_time DESC
    </select>


    <select id="originExportList" resultType="com.casic.missiles.dto.business.originRecord.OriginRecordResponse">
        SELECT
            <include refid="Base_Column_List"/>,
            cs.sample_no AS sampleNo,
            cs.sample_name AS sampleName,
            cs.sample_model AS sampleModel,
            cs.manufacturing_no AS manufacturingNo,
            su.name  AS createUserName
        FROM business_original_record bor
            LEFT JOIN customer_sample_info cs ON bor.sample_id = cs.id
            LEFT JOIN sys_user su ON su.id = bor.create_user
        WHERE bor.is_del =0
        <if test="request.originalRecordCode != null and request.originalRecordCode !='' ">
            and bor.original_record_code like concat('%', #{request.originalRecordCode}, '%')
        </if>
        <if test="request.sampleNo != null and request.sampleNo !=''">
            and cs.sample_no like concat('%', #{request.sampleN}, '%')
        </if>
        <if test="request.sampleName != null and request.sampleName !=''">
            and cs.sample_name like concat('%', #{request.sampleName}, '%')
        </if>
        <if test="request.sampleModel != null and request.sampleModel !=''">
            and cs.sample_model like concat('%', #{request.sampleModel}, '%')
        </if>
        <if test="request.manufacturingNo != null and request.manufacturingNo !=''">
            and cs.manufacturing_no like concat('%', #{request.manufacturingNo}, '%')
        </if>
        <if test="request.measureCategory != null and request.measureCategory !=''">
            and bor.measure_category = #{request.measureCategory}
        </if>
        <if test="request.createUser != null and request.createUser !=''">
            and  su.name = #{request.createUser}
        </if>

        <if test="request.ids != null and request.ids.size()>0">
            AND bor.id IN
            <foreach collection="request.ids" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ORDER BY bor.create_time DESC
    </select>

</mapper>
