<?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.equipment.EquipmentStandardApplyMapper">

    <select id="selectMaxNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(apply_no, 12)), 0) from equipment_standard_apply
    </select>

    <select id="selectApplyInfo" resultType="com.casic.missiles.model.equipment.EquipmentStandardApply">
        <include refid="equipmentStandardApply"/>
        and msa.id = #{id}
    </select>
    <sql id="equipmentStandardApply">
        SELECT
            msa.id,
            msa.apply_no,
            msa.apply_type,
            msa.standard_equipment_id,
            mse.standard_no,
            mse.standard_name,
            mo.organize_name AS construction_standard_unit_name,
            mf.NAME AS standard_owner,
            DATE_FORMAT( msa.apply_time, '%Y-%m-%d %H:%i:%s' ) AS apply_time,
            msa.approval_status,
            msa.apply_person,
            su.`NAME` AS apply_person_name,
            msa.apply_unit,
            sd.SIMPLE_NAME AS apply_unit_name,
            msa.apply_name,
            msa.process_id
        FROM
            equipment_standard_apply msa
                LEFT JOIN equipment_standard_equipment mse ON msa.standard_equipment_id = mse.id
                LEFT JOIN meter_staff mf ON mf.id = mse.standard_owner
                LEFT JOIN meter_organize mo ON mo.dept_id = mse.standard_laboratory
                LEFT JOIN sys_user su ON msa.apply_person = su.id
                LEFT JOIN sys_dept sd ON msa.apply_unit = sd.id
        WHERE
            msa.is_del = 0
    </sql>

    <select id="standardEquipmentList" resultType="com.casic.missiles.model.equipment.EquipmentStandardApply">
        <include refid="equipmentStandardApply"/>
        <if test="request.applyNo != null and request.applyNo !=''">
            and msa.apply_no like concat('%', #{request.applyNo}, '%')
        </if>
        <if test="request.equipmentNo != null and request.equipmentNo !=''">
            and msa.standard_equipment_id like concat('%', #{request.equipmentNo}, '%')
        </if>
        <if test="request.standardNo != null and request.standardNo !=''">
            and  mse.standard_no like concat('%', #{request.standardNo}, '%')
        </if>
        <if test="request.standardName != null and request.standardName !=''">
            and mse.standard_name like concat('%', #{request.standardName}, '%')
        </if>
        <if test="request.applyUnit != null and request.applyUnit !=''">
            and msa.apply_unit = #{request.applyUnit}
        </if>
        <if test="request.applyType != null and request.applyType !=''">
            and msa.apply_type = #{request.applyType}
        </if>
        <if test="request.createUser != null and request.createUser !=''">
            and msa.create_user = #{request.createUser}
        </if>
        <if test="request.applyStatus != null and request.applyStatus !=''">
            and msa.approval_status = #{request.applyStatus}
        </if>
        <if test="request.approvalStatus != null and request.approvalStatus !='' and request.approvalStatus != 0">
            and msa.approval_status = #{request.approvalStatus}
        </if>
        <if test="request.businessKeys != null and request.businessKeys.size() > 0">
            and msa.id in
            <foreach collection='request.businessKeys' item='id' open='(' separator=',' close=')'>
                #{id}
            </foreach>
        </if>
        <if test="request.ids != null and request.ids.size() > 0">
            and msa.id in
            <foreach collection='request.ids' item='id' open='(' separator=',' close=')'>
                #{id}
            </foreach>
        </if>
        ORDER BY mse.create_time DESC
    </select>


</mapper>
