<?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="org.springblade.modules.investment.mapper.IbaProjectMapper">

    <resultMap id="BaseResultMap" type="org.springblade.modules.investment.pojo.entity.IbaProject">
            <id property="id" column="id" />
            <result property="projectName" column="project_name" />
            <result property="type" column="type" />
            <result property="routeType" column="route_type" />
            <result property="stage" column="stage" />
            <result property="constructStartTime" column="construct_start_time" />
            <result property="constructEndTime" column="construct_end_time" />
            <result property="operationStartTime" column="operation_start_time" />
            <result property="operationEndTime" column="operation_end_time" />
            <result property="operationCompany" column="operation_company" />
            <result property="actualOperatingMileage" column="actual_operating_mileage" />
            <result property="lane" column="lane" />
            <result property="isDeleted" column="is_deleted" />
            <result property="createTime" column="create_time" />
            <result property="createUser" column="create_user" />
            <result property="updateTime" column="update_time" />
            <result property="updateUser" column="update_user" />
    </resultMap>

    <sql id="Base_Column_List">
        id,project_name,type,route_type,stage,construct_start_time,
        construct_end_time,operation_start_time,operation_end_time,operation_company,actual_operating_mileage,
        lane,is_deleted,
        create_time,create_user,update_time,update_user
    </sql>
    <update id="updateIsDeleted">
        UPDATE iba_project SET is_deleted = #{isDeleted} WHERE id IN
        <foreach item="id" collection="ids" separator="," open="(" close=")" index="">
            #{id}
        </foreach>
    </update>


    <select id="companyTotal" resultType="java.lang.Long">
        select count(0) from (select DISTINCT operation_company from iba_project where is_deleted = 0 ) a
    </select>


    <resultMap id="IbaProjectTypeTotalVo" type="org.springblade.modules.investment.pojo.vo.IbaProjectTypeTotalVo">
        <result property="type" column="type"/>
        <collection property="stageTotalVos" javaType="java.util.List"
                    ofType="org.springblade.modules.investment.pojo.vo.IbaProjectStageTotalVo"
                    select="stageTotalByType"
                    column="{type=type}"/>
    </resultMap>

    <select id="typeTotal" resultMap="IbaProjectTypeTotalVo">
        SELECT
            a.type,
            b.dict_value typeName,
            count( 0 ) count
        FROM
            iba_project a
            INNER JOIN blade_dict_biz b ON a.type = b.id
        WHERE
            a.is_deleted = 0 and b.is_deleted = 0
        GROUP BY
            a.type
    </select>

    <select id="stageTotalByType" resultType="org.springblade.modules.investment.pojo.vo.IbaProjectStageTotalVo">
        SELECT
            a.stage,
            b.dict_value stageName,
            count( 0 ) count
        FROM
            iba_project a
            INNER JOIN blade_dict_biz b ON b.CODE = 'ibaProjectStage'
            AND a.stage = b.dict_key
        WHERE
            a.is_deleted = 0
          AND b.is_deleted = 0
          AND a.type = #{type}
        GROUP BY
            a.stage
    </select>
    <!--运营公司分组统计-->
    <resultMap id="IbaProjectCompanyTotalVo" type="org.springblade.modules.investment.pojo.vo.IbaProjectCompanyTotalVo">
        <result property="operationCompany" column="operation_company"/>
        <collection property="stageTotalVos" javaType="java.util.List"
                    ofType="org.springblade.modules.investment.pojo.vo.IbaProjectStageTotalVo"
                    select="stageTotalByCompany"
                    column="{operationCompany=operation_company}"/>
    </resultMap>
    <select id="companyGroupTotal"
            resultMap="IbaProjectCompanyTotalVo">
        SELECT
            a.operation_company,
            count( 0 ) count
        FROM
            iba_project a
        WHERE
            a.is_deleted = 0
        GROUP BY
            a.operation_company
    </select>
    <select id="stageTotalByCompany" resultType="org.springblade.modules.investment.pojo.vo.IbaProjectStageTotalVo">
        SELECT
            a.stage,
            b.dict_value stageName,
            count( 0 ) count
        FROM
            iba_project a
            INNER JOIN blade_dict_biz b ON b.CODE = 'ibaProjectStage'
            AND a.stage = b.dict_key
        WHERE
            a.is_deleted = 0
          AND b.is_deleted = 0
          AND a.operation_company = #{operationCompany}
        GROUP BY
            a.stage
    </select>




    <select id="incomeTotalList" resultType="org.springblade.modules.investment.pojo.vo.incomeTotalVo">
        SELECT
            *
        FROM
            (
                SELECT
                    a.id,
                    a.project_name,
                    a.operation_company ,
                    IFNULL(( b.toll_inflow + b.other_inflow ), 0 ) inflow
                FROM
                    iba_project a
                        LEFT JOIN iba_project_fund b ON a.id = b.project_id
                <where>
                    <if test="year != null and year != ''">
                        b.year = #{year}
                    </if>
                </where>
                GROUP BY
                    <choose>
                        <when test="type == 1">
                            a.id
                        </when>
                        <otherwise>
                            a.operation_company
                        </otherwise>
                    </choose>

            ) a
        ORDER BY
            inflow DESC
            LIMIT #{size}
    </select>
    <select id="companyList" resultType="org.springblade.modules.investment.pojo.entity.IbaProjectVolume">
        SELECT
            sum( b.number ) number,
            b.*
        FROM
            iba_project a
                LEFT JOIN iba_project_volume b ON a.id = b.project_id
        <where>
            <choose>
                <when test="type == 1">
                    a.id = #{id}
                </when>
                <otherwise>
                    a.operationCompany = #{operationCompany}
                </otherwise>
            </choose>
            <if test="year != null and year != ''">
                AND b.year = #{year}
            </if>
        </where>

        GROUP BY
            <choose>
                <when test="type == 1">
                    a.id,
                </when>
                <otherwise>
                    a.operationCompany,
                </otherwise>
            </choose>
            b.`month`
        order by b.month
    </select>
    <select id="selectMyPage" resultType="org.springblade.modules.investment.pojo.entity.IbaProject">
        SELECT
            a.*,
            b.dict_value typeName,
            c.dict_value routeTypeName,
            d.dict_value stageName
        FROM
            iba_project a
            LEFT JOIN blade_dict_biz b ON a.type = b.id
            LEFT JOIN blade_dict_biz c ON a.route_type = c.id
            LEFT JOIN blade_dict_biz d ON d.code = 'ibaProjectStage' and  a.stage = d.dict_key
        <where>
            a.is_deleted = 0
            <if test="query.projectName != null and query.projectName != ''">
                AND a.project_name LIKE CONCAT('%',#{query.projectName},'%')
            </if>
            <if test="query.type != null and query.type != ''">
                AND a.type = #{query.type}
            </if>
            <if test="query.routeType != null and query.routeType != ''">
                AND a.route_type = #{query.routeType}
            </if>
            <if test="query.stage != null">
                AND a.stage = #{query.stage}
            </if>
            <if test="query.operationCompany != null and query.operationCompany != ''">
                AND a.operation_company = #{query.operationCompany}
            </if>
        </where>
    </select>
</mapper>
