<?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.hz.pm.api.projectdeclared.mapper.DeclaredStatisticsMapper">

    <select id="getOrgStatistics" resultType="com.hz.pm.api.projectdeclared.model.po.DeclaredProjectStatisticsPO">
        SELECT
            count(0) totalNum,
            count(CASE WHEN p.project_type = 1 THEN 1 end) buildNum,
            count(CASE WHEN p.project_type = 2 THEN 1 end) operationNum,
            count(CASE WHEN p.stage = 10000 THEN 1 end) notApprovedNum,
            count(CASE WHEN p.stage = 20000 THEN 1 end) approvedNum,
            count(CASE WHEN p.stage = 20000 AND p.status = 20002 THEN 1 end) constructionNum,
            count(CASE WHEN p.stage = 20000 AND p.status = 20003 THEN 1 end) tobeInspectedNum,
            count(CASE WHEN p.stage = 30000 THEN 1 end) archivedNum,
            sum(p.declare_amount) declaredAmount,
            sum(p.approval_amount) approvalAmount
        FROM
            nd_project p
        WHERE build_org_code = #{orgCode} and project_year = #{year} and newest = 1
    </select>

    <select id="getRegionStatistics" resultType="com.hz.pm.api.projectdeclared.model.po.DeclaredProjectStatisticsPO">
        SELECT
            count(0) totalNum,
            count(CASE WHEN p.project_type = 1 THEN 1 end) buildNum,
            count(CASE WHEN p.project_type = 2 THEN 1 end) operationNum,
            count(CASE WHEN p.stage = 10000 THEN 1 end) notApprovedNum,
            count(CASE WHEN p.stage = 20000 THEN 1 end) approvedNum,
            count(CASE WHEN p.stage = 20000 AND p.status = 20002 THEN 1 end) constructionNum,
            count(CASE WHEN p.stage = 20000 AND p.status = 20003 THEN 1 end) tobeInspectedNum,
            count(CASE WHEN p.stage = 30000 THEN 1 end) archivedNum,
            sum(p.declare_amount) declaredAmount,
            sum(p.approval_amount) approvalAmount
        FROM
            nd_project p
        WHERE project_year = #{year} and newest = 1
    </select>
</mapper>
