<?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.project.zcustom.mapper.logistics.PlatHousingLandMapper">
    <resultMap type="com.project.zcustom.domain.addional.LargeHousingLand" id="LargeHousingLandResult">
        <result property="id"    column="id"    />
        <result property="housing"    column="housing"    />
        <result property="investmentPlanFlag"    column="investment_plan_flag"    />
        <result property="ownershipFlag"    column="ownership_flag"    />
        <result property="constructionTime"    column="construction_time"    />
    </resultMap>

    <select id="getStockOfEstateResources" parameterType="String" resultMap="LargeHousingLandResult">
        SELECT
        count( 1 ) AS num,
        sum( acreage ) AS acreage,
        type
        FROM
        large_plat_housing_land
        WHERE del_flag = '0'
        <if test="appOrg != '0000'"> AND app_org = #{appOrg} </if>
        GROUP BY
        type
    </select>

    <select id="getStockOfLandResources" parameterType="String" resultType="map">
        SELECT
        count( 1 ) AS num,
        COUNT( CASE WHEN YEAR ( construction_time ) >= YEAR ( CURDATE()) THEN 1 END ) AS new,
        COUNT( CASE WHEN ownership_flag = 1 THEN 1 END ) AS own
        FROM
        large_plat_housing_land
        WHERE del_flag = '0'
        <if test="appOrg != '0000'"> AND app_org = #{appOrg} </if>
    </select>

    <select id="getInvestFinishState" parameterType="String" resultType="map">
        SELECT
        COUNT( CASE WHEN YEAR ( construction_time ) >= YEAR ( CURDATE()) THEN 1 END ) AS new,
        COUNT( CASE WHEN ( investment_plan_flag = 1 AND YEAR ( construction_time ) >= YEAR ( CURDATE())) THEN 1 END ) AS invest
        FROM
        large_plat_housing_land
        WHERE del_flag = '0'
        <if test="appOrg != '0000'"> AND app_org = #{appOrg} </if>
    </select>

    <insert id="upload" parameterType="com.project.zcustom.domain.addional.LargeHousingLand">
        insert into large_plat_housing_land (housing,
                                          acreage,
                                          type,
                                          ownership_flag,
                                          investment_plan_flag,
                                          construction_time,
                                          del_flag,
                                          create_time)
        values
        <foreach collection="list" item="item" separator="," close=";">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.housing},
                #{item.acreage},
                #{item.type},
                #{item.ownershipFlag},
                #{item.investmentPlanFlag},
                #{item.constructionTime},
            '0', sysdate()
            </trim>
        </foreach>
    </insert>
</mapper>
