<?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.material.main.mapper.MainMapper">


    <select id="getPublishList" resultType="com.bonus.common.biz.domain.lease.LeasePublishInfo">
        SELECT
        IFNULL( sum( lad.pre_num ), 0 ) AS preCountNum
        FROM
        lease_apply_info lai
        LEFT JOIN tm_task tt ON lai.task_id = tt.task_id
        LEFT JOIN lease_apply_details lad ON lai.id = lad.parent_id
        LEFT JOIN tm_task_agreement tta on tta.task_id = tt.task_id
        LEFT JOIN bm_agreement_info bagi on bagi.agreement_id = tta.agreement_id
        AND bagi.`status` = '1'
        LEFT JOIN bm_project bp on bp.pro_id = bagi.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu on bu.unit_id = bagi.unit_id
        AND bu.del_flag = '0'
        WHERE
        tt.task_status IN (3, 4)
        AND tt.task_type = 2
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="unitId != null ">
            AND bu.unit_id = #{unitId}
        </if>
        <if test="projectId != null ">
            AND bp.pro_id = #{projectId}
        </if>
    </select>

    <select id="getPublishListNew" resultType="java.math.BigDecimal">
        select
        IFNULL(sum(lpd.num),0) as preCountNum
        from
        lease_publish_details lpd
        where 1=1
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lpd.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="unitId != null ">
            AND lpd.unit_id = #{unitId}
        </if>
        <if test="projectId != null ">
            AND lpd.project_id = #{projectId}
        </if>
    </select>

    <select id="getSuppliedQuantityNum" resultType="java.math.BigDecimal">
        SELECT
        IFNULL(sum(lod.out_num),0) as preCountNum
        FROM
        lease_apply_info lai
        LEFT JOIN tm_task tt ON lai.task_id = tt.task_id
        LEFT JOIN lease_out_details lod ON lai.id = lod.parent_id
        LEFT JOIN tm_task_agreement tta on tta.task_id = tt.task_id
        LEFT JOIN bm_agreement_info bagi on bagi.agreement_id = tta.agreement_id
        AND bagi.`status` = '1'
        LEFT JOIN bm_project bp on bp.pro_id = bagi.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu on bu.unit_id = bagi.unit_id
        AND bu.del_flag = '0'
        WHERE
        tt.task_status IN (3, 4)
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lod.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="unitId != null ">
            AND bu.unit_id = #{unitId}
        </if>
        <if test="projectId != null ">
            AND bp.pro_id = #{projectId}
        </if>
    </select>

    <select id="getProjectNum" resultType="java.lang.Integer">
        SELECT
            count( DISTINCT bp.pro_id ) AS projectNum
        FROM
            bm_project bp
                LEFT JOIN bm_agreement_info ba ON bp.pro_id = ba.project_id
                LEFT JOIN ( SELECT agreement_id, is_slt FROM slt_agreement_info GROUP BY agreement_id ) a
                    ON a.agreement_id = ba.agreement_id
        WHERE
            bp.del_flag = '0'
          AND a.is_slt = '0'
    </select>

    <select id="getSuppliedQuantityPrice" resultType="com.bonus.material.main.domain.DemandAndSupplyVo">
        SELECT
            IFNULL( sum( lod.out_num ), 0 ) AS suppliedQuantityNum,
            IFNULL(mt.buy_price, 0) AS buyPrice
        FROM
            lease_out_details lod
                LEFT JOIN ma_type mt ON lod.type_id = mt.type_id
        GROUP BY
            lod.type_id
    </select>

    <select id="getDemandList" resultType="com.bonus.material.main.domain.DemandAndSupplyVo">
        SELECT
            lad.type_id AS typeId,
            mt.type_name AS typeName,
            mt1.type_name AS typeModelName,
            IFNULL( sum( lad.pre_num ), 0 ) AS demandNum
        FROM
            lease_apply_info lai
                LEFT JOIN tm_task tt ON lai.task_id = tt.task_id
                LEFT JOIN lease_apply_details lad ON lai.id = lad.parent_id
                LEFT JOIN tm_task_agreement tta ON tta.task_id = tt.task_id
                LEFT JOIN bm_agreement_info bagi ON bagi.agreement_id = tta.agreement_id
                AND bagi.`status` = '1'
                LEFT JOIN bm_project bp ON bp.pro_id = bagi.project_id
                AND bp.del_flag = '0'
                LEFT JOIN bm_unit bu ON bu.unit_id = bagi.unit_id
                AND bu.del_flag = '0'
                LEFT JOIN ma_type mt1 ON lad.type_id = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_type mt ON mt1.parent_id = mt.type_id
                AND mt.del_flag = '0'
        WHERE
            tt.task_status IN ( 3, 4 )
          AND tt.task_type = 2
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lad.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="unitId != null ">
            AND bu.unit_id = #{unitId}
        </if>
        <if test="projectId != null ">
            AND bp.pro_id = #{projectId}
        </if>
        GROUP BY
            lad.type_id
    </select>

    <select id="getUseList" resultType="com.bonus.material.main.domain.DemandAndSupplyVo">
        SELECT
            lpd.new_type AS typeId,
            IFNULL( sum( lpd.num ), 0 ) AS demandNum,
            mt.type_name AS typeName,
            mt1.type_name AS typeModelName
        FROM
            lease_publish_details lpd
                LEFT JOIN ma_type mt1 ON lpd.new_type = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_type mt ON mt1.parent_id = mt.type_id
                AND mt.del_flag = '0'
        WHERE 1 = 1
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lpd.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="unitId != null ">
            AND lpd.unit_id = #{unitId}
        </if>
        <if test="projectId != null ">
            AND lpd.project_id = #{projectId}
        </if>
        GROUP BY
            lpd.new_type
    </select>
    <select id="getSuppliedQuantityList" resultType="com.bonus.material.main.domain.DemandAndSupplyVo">
        SELECT
            IFNULL( sum( lod.out_num ), 0 ) AS suppliedQuantityNum,
            lod.type_id AS typeId,
            mt.type_name AS typeName,
            mt1.type_name AS typeModelName
        FROM
            lease_apply_info lai
                LEFT JOIN tm_task tt ON lai.task_id = tt.task_id
                LEFT JOIN lease_out_details lod ON lai.id = lod.parent_id
                LEFT JOIN tm_task_agreement tta ON tta.task_id = tt.task_id
                LEFT JOIN bm_agreement_info bagi ON bagi.agreement_id = tta.agreement_id
                AND bagi.`status` = '1'
                LEFT JOIN bm_project bp ON bp.pro_id = bagi.project_id
                AND bp.del_flag = '0'
                LEFT JOIN bm_unit bu ON bu.unit_id = bagi.unit_id
                AND bu.del_flag = '0'
                LEFT JOIN ma_type mt1 ON lod.type_id = mt1.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_type mt ON mt1.parent_id = mt.type_id
                AND mt.del_flag = '0'
        WHERE
            tt.task_status IN ( 3, 4 )
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lod.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="unitId != null ">
            AND bu.unit_id = #{unitId}
        </if>
        <if test="projectId != null ">
            AND bp.pro_id = #{projectId}
        </if>
        GROUP BY
            lod.type_id
    </select>

    <select id="getInStockNum" resultType="com.bonus.material.main.domain.MaTypeHomeVo">
        SELECT
            IFNULL( SUM( storage_num ), 0 ) AS inStockNum,
            IFNULL( SUM( inuse_num ), 0 ) AS inUseNum,
            IFNULL( SUM( repair_num ), 0 ) AS inRepairNum,
            IFNULL( SUM( new_num ), 0 ) AS newPurchaseNum,
            IFNULL( SUM( repaired_num ), 0 ) AS newRepairNum,
            IFNULL( SUM( scrap_num ), 0 ) AS newScrapNum
        FROM
            ma_status_backup
        WHERE 1 = 1
        <if test="endTime != null and endTime != ''">
            AND DATE_FORMAT( time, '%Y-%m-%d' ) = #{endTime}
        </if>
    </select>

</mapper>
