<?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.bonus.sgzb.material.mapper.ReturnOfMaterialsInfoMapper">

    <select id="getReturnOfMaterialsInfoAll" resultType="com.bonus.sgzb.material.domain.ReturnOfMaterialsInfo">
        SELECT
            tt.`code` ,bui.unit_name as unitName ,bpl.lot_name as lotName,
            mt3.type_name as typeName,mt2.type_name as kindName,mt.type_name as modelName,
            iad.input_num as inputNum,mm.ma_code as maCode,
            tt.create_time as returnTime,
            iad.create_time as submitStorageTime,
            us.user_name as submitToStoragePersonnel,iad.remark as remark
        FROM input_apply_details iad
         LEFT JOIN tm_task tt ON iad.task_id = tt.task_id
         LEFT JOIN tm_task_agreement tta on tta.task_id = tt.task_id
         LEFT JOIN bm_agreement_info bai ON bai.agreement_id = tta.agreement_id
         LEFT JOIN bm_project_lot bpl ON bpl.lot_id = bai.project_id
         LEFT JOIN bm_unit_info bui on bui.unit_id = bai.unit_id
         LEFT JOIN ma_type mt on mt.type_id = iad.type_id
         LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id AND mt2.`level` = '3'
         LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id AND mt3.`level` = '2'
         LEFT JOIN sys_user us on us.user_id=iad.create_by
         LEFT JOIN ma_machine mm ON mm.ma_id = iad.ma_id
        WHERE tt.task_status = '40' and mt.`level` = '4' and input_type ='2'
        <if test="keyWord != null  and keyWord != ''">
            and tt.`code` like concat('%', #{keyWord}, '%')
        </if>

        <if test="submitStorageTime != null  and submitStorageTime != ''">
            and DATE_FORMAT(iad.create_time, '%Y-%m-%d') = #{submitStorageTime}
        </if>
        <if test="typeId != null  and typeId != ''">
            and mt2.type_id = #{typeId}
        </if>
        <if test="modelId != null  and modelId != ''">
            and mt.type_id = #{modelId}
        </if>
        ORDER BY iad.id DESC
    </select>

    <select id="getTypeList" resultType="com.bonus.sgzb.material.domain.ReturnOfMaterialsInfo">
        SELECT type_id as typeId,type_name as typeName FROM `ma_type`
        WHERE `level` = #{level}
    </select>

    <select id="getInfoListByType" resultType="com.bonus.sgzb.material.domain.ReturnOfMaterialsInfo">
        SELECT type_id as typeId,code,model_code as modelCode,type_name as typeName FROM `ma_type`
        WHERE `level` = #{level} and parent_id = #{parentId}
    </select>
    <select id="getMaTypeList" resultType="com.bonus.sgzb.material.domain.ReturnOfMaterialsInfo">
        SELECT mt.type_id as typeId,mt.type_name as typeName ,mt.manage_type FROM `ma_type` mt
        LEFT JOIN ma_type mt2 on mt2.parent_id = mt.type_id
        WHERE mt.`level` = #{level} and mt2.manage_type = 0
        GROUP BY mt.type_id
    </select>

</mapper>