<?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.SecondaryWarehouseMapper">
    <insert id="addOperate">
        insert into team_lease_info (agreement_id,type_id,team_name,lease_man,phone,num,create_by,create_time,type)
        values (#{agreementId},#{typeId},#{teamName},#{leaseMan},#{phone},#{num},#{createBy},now(),#{type});
    </insert>

    <select id="getList" resultType="com.bonus.sgzb.material.domain.SecondaryWarehouse">
        SELECT
        bui.unit_id AS unitId,
        lod.id as id,
        tta.agreement_id AS agreementId,
        bui.unit_name AS unitName,
        bp.lot_name AS proName,
        mt1.type_name AS typeName,
        mt.type_name AS modelName,
        mt.unit_name AS nuitName,
        lod.type_id AS typeId,
        IFNULL(SUM( lod.out_num ),0) AS Num

        from lease_out_details lod
        left join lease_apply_info lai on lod.parent_id=lai.id
        left join tm_task_agreement tta on lai.task_id=tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project_lot bp ON bp.lot_id = bai.project_id
        LEFT JOIN bm_unit_info bui ON bui.unit_id = bai.unit_id
        LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        WHERE 1=1
        <if test="unitId != null  and unitId != ''">
            and bui.unit_id = #{unitId}
        </if>
        <if test="proId != null  and proId != ''">
            and bp.lot_id = #{proId}
        </if>
        <if test="typeId != null  and typeId != ''">
            and mt1.type_id = #{typeId}
        </if>
        <if test="modelId != null  and modelId != ''">
            and mt.type_id = #{modelId}
        </if>
        <if test="leaseType != null">
            and lai.lease_type = #{leaseType}
        </if>
        <if test="keyword != null  and keyword != ''">
            and (bui.unit_name like concat('%', #{keyword}, '%') or
            bp.lot_name like concat('%', #{keyword}, '%') or
            mt1.type_name like concat('%', #{keyword}, '%') or
            mt.type_name like concat('%', #{keyword}, '%')
            )
        </if>
        GROUP BY
        tta.agreement_id,
        lod.type_id
    </select>
    <select id="getList1" resultType="com.bonus.sgzb.material.domain.SecondaryWarehouse">
        SELECT
        bui.unit_id AS unitId,
        lod.id as id,
        tta.agreement_id AS agreementId,
        bui.unit_name AS unitName,
        bp.lot_name AS proName,
        mt1.type_name AS typeName,
        mt.type_name AS modelName,
        mt.unit_name AS nuitName,
        lod.type_id AS typeId,
        IFNULL(SUM( lod.out_num ),0) AS receiveNum

        from lease_out_details lod
        left join lease_apply_info lai on lod.parent_id=lai.id
        left join tm_task_agreement tta on lai.task_id=tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project_lot bp ON bp.lot_id = bai.project_id
        LEFT JOIN bm_unit_info bui ON bui.unit_id = bai.unit_id
        LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        WHERE 1=1
        <if test="unitId != null  and unitId != ''">
            and bui.unit_id = #{unitId}
        </if>
        <if test="proId != null  and proId != ''">
            and bp.lot_id = #{proId}
        </if>
        <if test="typeId != null  and typeId != ''">
            and mt1.type_id = #{typeId}
        </if>
        <if test="modelId != null  and modelId != ''">
            and mt.type_id = #{modelId}
        </if>
        <if test="leaseType != null  and leaseType != ''">
            and lai.lease_type = #{leaseType}
        </if>
        <if test="keyword != null  and keyword != ''">
            and (bui.unit_name like concat('%', #{keyword}, '%') or
            bp.lot_name like concat('%', #{keyword}, '%') or
            mt1.type_name like concat('%', #{keyword}, '%') or
            mt.type_name like concat('%', #{keyword}, '%')
            )
        </if>
        GROUP BY
        tta.agreement_id,
        lod.type_id
    </select>
    <select id="getOperateList" resultType="com.bonus.sgzb.material.domain.TeamLeaseInfo">
        SELECT
        bui.unit_name AS unitName,
        bp.lot_name AS proName,
        mt1.type_name AS typeName,
        mt.type_name AS modelName,
        tli.num,
        tli.lease_man AS leaseMan,
        su.nick_name AS createBy,
        tli.create_time AS createTime
        FROM
        team_lease_info tli
        LEFT JOIN bm_agreement_info bai ON tli.agreement_id = bai.agreement_id
        LEFT JOIN bm_project_lot bp ON bp.lot_id = bai.project_id
        LEFT JOIN bm_unit_info bui ON bui.unit_id = bai.unit_id
        LEFT JOIN ma_type mt ON tli.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        LEFT JOIN sys_user su ON su.user_id = tli.create_by
        where tli.type = #{type}
        <if test="unitId != null  and unitId != ''">
            and bui.unit_id = #{unitId}
        </if>
        <if test="proId != null  and proId != ''">
            and bp.lot_id = #{proId}
        </if>
        <if test="typeId != null  and typeId != ''">
            and mt1.type_id = #{typeId}
        </if>
        <if test="modelId != null  and modelId != ''">
            and mt.type_id = #{modelId}
        </if>
        <if test="keyword != null  and keyword != ''">
            and (bui.unit_name like concat('%', #{keyword}, '%') or
            bp.lot_name like concat('%', #{keyword}, '%') or
            mt1.type_name like concat('%', #{keyword}, '%') or
            mt.type_name like concat('%', #{keyword}, '%') or
            tli.lease_man like concat('%', #{keyword}, '%') or
            su.nick_name like concat('%', #{keyword}, '%')
            )
        </if>
    </select>
    <select id="getList2" resultType="com.bonus.sgzb.material.domain.SecondaryWarehouseVo">
        SELECT
        bui.unit_id AS unitId,
        lod.id as id,
        tta.agreement_id AS agreementId,
        bui.unit_name AS unitName,
        bp.lot_name AS proName,
        mt1.type_name AS typeName,
        mt.type_name AS modelName,
        mt.unit_name AS nuitName,
        lod.type_id AS typeId,
        IFNULL(SUM( lod.out_num ),0) AS Num

        from lease_out_details lod
        left join lease_apply_info lai on lod.parent_id=lai.id
        left join tm_task_agreement tta on lai.task_id=tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project_lot bp ON bp.lot_id = bai.project_id
        LEFT JOIN bm_unit_info bui ON bui.unit_id = bai.unit_id
        LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        WHERE 1=1
        <if test="unitId != null  and unitId != ''">
            and bui.unit_id = #{unitId}
        </if>
        <if test="proId != null  and proId != ''">
            and bp.lot_id = #{proId}
        </if>
        <if test="typeId != null  and typeId != ''">
            and mt1.type_id = #{typeId}
        </if>
        <if test="modelId != null  and modelId != ''">
            and mt.type_id = #{modelId}
        </if>
        <if test="keyword != null  and keyword != ''">
            and (bui.unit_name like concat('%', #{keyword}, '%') or
            bp.lot_name like concat('%', #{keyword}, '%') or
            mt1.type_name like concat('%', #{keyword}, '%') or
            mt.type_name like concat('%', #{keyword}, '%')
            )
        </if>
        GROUP BY
        tta.agreement_id,
        lod.type_id
    </select>
    <select id="getRecordsOne" resultType="com.bonus.sgzb.material.domain.TeamLeaseInfo">
        SELECT
        mt1.type_name AS typeName,
        mt.type_name AS modelName,
        mt.unit_name as nuitName,
        sai.num AS num,
        sai.start_time as startTime
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        where mt.type_id = #{typeId} and sai.agreement_id=#{agreementId}
        <if test="startTime != null  and startTime != ''">
            and DATE(sai.start_time) = #{startTime}
        </if>
        order by sai.start_time desc
    </select>
    <select id="getRecordsTwo" resultType="com.bonus.sgzb.material.domain.TeamLeaseInfo">
        SELECT
        mt1.type_name AS typeName,
        mt.type_name AS modelName,
        mt.unit_name as nuitName,
        sai.num AS num,
        sai.end_time as startTime
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        where sai.agreement_id=#{agreementId} and mt.type_id = #{typeId} and sai.status = '1'
        <if test="startTime != null  and startTime != ''">
            and DATE(sai.end_time) = #{startTime}
        </if>
        order by sai.end_time desc
    </select>
    <select id="getRecordsThree" resultType="com.bonus.sgzb.material.domain.TeamLeaseInfo">
        SELECT
        mt1.type_name AS typeName,
        mt.type_name AS modelName,
        mt.unit_name as nuitName,
        sai.num AS num,
        sai.start_time as startTime
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_type mt ON sai.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        where sai.agreement_id=#{agreementId} and mt.type_id = #{typeId} and sai.status = '0'
        <if test="startTime != null  and startTime != ''">
            and DATE(sai.start_time) = #{startTime}
        </if>
        order by sai.start_time desc
    </select>
    <select id="getRecordsFour" resultType="com.bonus.sgzb.material.domain.TeamLeaseInfo">
        select tli.team_name as teamName,
               mt1.type_name AS typeName,
               mt.type_name AS modelName,
               tli.num,
               su.nick_name as createBy,
               tli.create_time as startTime
        from team_lease_info tli
                 LEFT JOIN ma_type mt ON tli.type_id = mt.type_id
                 LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                 left join sys_user su on su.user_id = tli.create_by
        where tli.agreement_id=#{agreementId} and mt.type_id = #{typeId} and tli.type = '1'
        <if test="startTime != null  and startTime != ''">
            and DATE(tli.create_time) = #{startTime}
        </if>
    </select>
    <select id="getRecordsFive" resultType="com.bonus.sgzb.material.domain.TeamLeaseInfo">
        select tli.team_name as teamName,
        mt1.type_name AS typeName,
        mt.type_name AS modelName,
        tli.num,
        su.nick_name as createBy,
        tli.create_time as startTime
        from team_lease_info tli
        LEFT JOIN ma_type mt ON tli.type_id = mt.type_id
        LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
        left join sys_user su on su.user_id = tli.create_by
        where tli.agreement_id=#{agreementId} and mt.type_id = #{typeId} and tli.type = '2'
        <if test="startTime != null  and startTime != ''">
            and DATE(tli.create_time) = #{startTime}
        </if>
    </select>
    <select id="selectStockNum" resultType="java.lang.Integer">
        SELECT
            (
                SUM(
                        IF
                        ( rd.type = 1, rd.out_num, 0 )) - SUM(
                        IF
                        ( rd.type = 2, rd.out_num, 0 ))) AS receiveNum
        FROM
            receive_detail rd
        WHERE
            parent_id = #{id}
    </select>
    <select id="getNewRecords" resultType="com.bonus.sgzb.material.domain.TeamLeaseInfo">
        select mt.type_name modelName,
               mt2.type_name typeName,
               su.nick_name nickName,
               rd.out_num outNum,
               rd.pick_name pickName,
               rd.out_time outTime
        from receive_detail rd
        left join  ma_type mt on rd.type_id = mt.type_id
        left join  ma_type mt2 on mt.parent_id = mt2.type_id
        left join sys_user su on rd.out_name = su.user_id
        where rd.type = 1
        <if test="typeId != null and typeId != ''">
           and mt2.type_id = #{typeId}
        </if>
        <if test="modelId != null and modelId != ''">
           and mt.type_id = #{modelId}
        </if>
    </select>
    <select id="selectStockNum1" resultType="java.lang.Integer">
        SELECT
            (
                SUM(
                        IF
                        ( rd.type = 1, rd.out_num, 0 )) - SUM(
                        IF
                        ( rd.type = 2, rd.out_num, 0 ))) AS receiveNum
        FROM
            receive_detail rd
        WHERE
            parent_id = #{id}
    </select>


    <insert id="addNewOperate">
        insert into receive_detail (parent_id,type_id,out_num,out_time,type,out_name,pick_name)
        values (#{id},#{typeId},#{outNum},now(),#{type},#{createBy},#{leaseMan});
    </insert>
</mapper>
