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

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

    <sql id="selectSql">
        SELECT
            msa.id,
            meae.id AS apply_equipment_id,
            mfa.id AS equipment_id,
            msa.apply_no,
            msa.apply_name,
            msa.apply_type,
            mfa.equipment_no,
            mfa.equipment_name,
            mfa.model_no,
            mfa.manager_state,
            msa.approval_status,
            msa.apply_unit,
            sd.SIMPLE_NAME as apply_unit_name,
            su.`NAME` as apply_person_name,
            msa.apply_person,
            DATE_FORMAT( msa.time, '%Y-%m-%d %H:%i' ) as time,
            mfa.equipment_specifications,
            mfa.manufacturing_no,
            mfa.manufacturer,
            meae.financial_value,
            meae.asset_origin_value,
            msa.process_id,
            msa.process_result,
            msa.remark
        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
            LEFT JOIN equipment_apply_equipment meae on msa.id = meae.apply_id
            LEFT JOIN equipment_fixed_assets mfa on meae.equipment_id = mfa.id
        where msa.is_del = 0
    </sql>

    <select id="stateManageList" parameterType="com.casic.missiles.dto.equipment.StateManageRequest" resultType="com.casic.missiles.dto.equipment.StateManageResponse">
            <include refid="selectSql"/>
            <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 mfa.equipment_no like concat('%', #{request.equipmentNo}, '%')
            </if>
            <if test="request.equipmentName != null and request.equipmentName !=''">
                and mfa.equipment_name like concat('%', #{request.equipmentName}, '%')
            </if>
            <if test="request.applyUnit != null and request.applyUnit !=''">
                and msa.apply_unit = #{request.applyUnit}
            </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.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.ids != null and request.ids.size() > 0">
                and msa.id in
                <foreach collection='request.ids' item='id' open='(' separator=',' close=')'>
                    #{id}
                </foreach>
            </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>
        ORDER BY msa.create_time DESC
    </select>
    <select id="selectById" resultType="com.casic.missiles.dto.equipment.StateManageResponse">
        <include refid="selectSql"/>
        and msa.id = #{id}
    </select>
</mapper>
