<?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.EquipmentApplyMapper">

    <select id="selectMaxNo" resultType="java.lang.Long">
        SELECT IFNULL(max(RIGHT(apply_no, 12)), 0) from equipment_apply
    </select>
    <sql id="sql">
        SELECT
            msa.id,
            msa.apply_no,
            msa.apply_type,
            msa.approval_status,
            msa.apply_name,
            sd.SIMPLE_NAME as apply_unit_name,
            su.`NAME` as apply_person_name,
            DATE_FORMAT( msa.time, '%Y-%m-%d %H:%i' ) as time,
            msa.overhaul_person,
            msa.apply_desc,
            msa.process_id,
            msa.process_result,
            msa.remark,
            msa.back_time
        FROM
            equipment_apply msa
            LEFT JOIN sys_user su ON su.id = msa.apply_person
            LEFT JOIN sys_dept sd ON sd.id = msa.apply_unit
        where msa.is_del = 0
    </sql>
    <select id="equipmentApplyList" resultType="com.casic.missiles.model.equipment.EquipmentApply">
        <include refid="sql"/>
        <if test="request.applyNo != null and request.applyNo !=''">
            and msa.apply_no like concat('%', #{request.applyNo}, '%')
        </if>
        <if test="request.applyName != null and request.applyName !=''">
            and msa.apply_name like concat('%', #{request.applyName}, '%')
        </if>
        <if test="request.applyUnit != null and request.applyUnit !=''">
            and msa.apply_unit = #{request.applyUnit}
        </if>
        <if test="request.applyPerson != null and request.applyPerson !=''">
            and msa.apply_person = #{request.applyPerson}
        </if>
        <if test="request.approvalStatus != null and request.approvalStatus !='' and request.approvalStatus != 0">
            and msa.approval_status = #{request.approvalStatus}
        </if>
        <if test="request.startTime != null and request.startTime !=''">
            and DATE_FORMAT( msa.time, '%Y-%m-%d' ) &gt;= #{request.startTime}
        </if>
        <if test="request.endTime != null and request.endTime !=''">
            and DATE_FORMAT( msa.time, '%Y-%m-%d' ) &lt;= #{request.endTime}
        </if>
        <if test="request.processResult != null and request.processResult !=''">
            and msa.process_result = #{request.processResult}
        </if>
        <if test="request.createUser != null and request.createUser !=''">
            and msa.create_user = #{request.createUser}
        </if>
        <if test="request.applyType != null and request.applyType !=''">
            and msa.apply_type = #{request.applyType}
        </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 msa.create_time DESC
    </select>
    <select id="equipmentApplyInfo" resultType="com.casic.missiles.model.equipment.EquipmentApply">
        SELECT
            msa.id,
            msa.apply_no,
            msa.apply_type,
            msa.approval_status,
            msa.apply_name,
            msa.apply_unit,
            sd.SIMPLE_NAME AS apply_unit_name,
            msa.apply_person,
            su.`NAME` AS apply_person_name,
            DATE_FORMAT( msa.time, '%Y-%m-%d %H:%i' ) as time,
            msa.overhaul_person,
            msa.apply_desc,
            msa.process_id,
            msa.process_result,
            msa.remark,
            msa.back_time
        FROM
            equipment_apply msa
            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
        and msa.id = #{id}
    </select>


</mapper>
