<?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.ruoyi.system.mapper.grid.GridCompanyEconomyInfoExtMapper">
    <resultMap id="PerMuResultMap" type="com.ruoyi.system.domain.grid.ext.GridCompanyEconomyInfoPerMuBean">
        <result column="wg_id" jdbcType="VARCHAR" property="wgId"/>
        <result column="wg_name" jdbcType="VARCHAR" property="wgName"/>
        <result column="wg_code" jdbcType="VARCHAR" property="wgCode"/>
        <result column="all_per_mu" jdbcType="DECIMAL" property="allPerMu"/>
    </resultMap>
    <resultMap id="SaleResultMap" type="com.ruoyi.system.domain.grid.ext.GridCompanyEconomyInfoSaleBean">
        <result column="wg_id" jdbcType="VARCHAR" property="wgId"/>
        <result column="wg_name" jdbcType="VARCHAR" property="wgName"/>
        <result column="wg_code" jdbcType="VARCHAR" property="wgCode"/>
        <result column="all_sale" jdbcType="DECIMAL" property="allSale"/>
        <result column="all_tax" jdbcType="DECIMAL" property="allTax"/>
    </resultMap>
    <resultMap id="EcoInfoResultMap" type="com.ruoyi.system.domain.grid.ext.GridCompanyEcoInfoBean">
        <result column="company_id" jdbcType="VARCHAR" property="companyId"/>
        <result column="company_name" jdbcType="VARCHAR" property="companyName"/>
        <result column="company_type" jdbcType="VARCHAR" property="companyType"/>
        <result column="year" jdbcType="VARCHAR" property="year"/>
        <result column="sale" jdbcType="DECIMAL" property="sale"/>
        <result column="tax" jdbcType="DECIMAL" property="tax"/>
        <result column="per_mu" jdbcType="DECIMAL" property="perMu"/>
    </resultMap>
    <resultMap id="AreaInfoResultMap" type="com.ruoyi.system.domain.grid.ext.GridCompanyEcoInfoBean">
        <result column="company_id" jdbcType="VARCHAR" property="companyId"/>
        <result column="company_name" jdbcType="VARCHAR" property="companyName"/>
        <result column="company_type" jdbcType="VARCHAR" property="companyType"/>
        <result column="all_share_area" jdbcType="DECIMAL" property="shareArea"/>
    </resultMap>
    <select id="sumSaleAndTaxAndPerMu" resultType="java.util.HashMap">
        SELECT
        sum(sale) as saleCount,
        sum(tax) as taxCount,
        sum(per_mu) as perMuCount
        FROM
        grid_company_economy_info wcei
        <where>
            and wcei.is_valid = 1
            <if test="year != null and year != ''">
                and wcei.year = #{year}
            </if>
            <if test="companyIds != null and companyIds.size()>0 ">
                and wcei.company_id in
                <foreach collection="companyIds" item="companyId" open="(" close=")" separator=",">
                    #{companyId}
                </foreach>
            </if>
        </where>
    </select>
    <select id="countPerMu" resultMap="PerMuResultMap">
        SELECT
        bw.id,
        bw.wg_name,
        bw.wg_code,
        IFNULL(sum(wcei.per_mu), 0) as all_per_mu
        FROM
        grid_region bw
        left join grid_company_land wcl on wcl.land_id = bw.id
        left join grid_company_economy_info wcei on wcei.company_id = wcl.company_id
        <if test="year != null and year != ''">
            and wcei.year = #{year}
        </if>
        where bw.wg_type = 4 and bw.is_valid = 1 and bw.level = 3 and wcl.is_valid = 1 and wcei.is_valid = 1
        <if test="landId != null and landId != ''">
            and bw.id = #{landId}
        </if>
        group by bw.id
    </select>

    <select id="countSale" resultMap="SaleResultMap">
        SELECT
        pbw.id as wg_id,
        pbw.wg_name as wg_name,
        pbw.wg_code as wg_code,
        IFNULL(sum(wcei.sale), 0) as all_sale
        FROM
        grid_region bw
        left join grid_company_land wcl on wcl.land_id = bw.id and wcl.is_valid = 1
        left join grid_company_economy_info wcei on wcl.company_id = wcei.company_id and wcei.is_valid = 1
        <if test="year != null and year != ''">
            and wcei.year = #{year}
        </if>
        left join grid_region pbw on bw.wg_parent_id = pbw.wg_code
        where bw.wg_type = 4 and bw.is_valid = 1 and bw.level = 3
        group by bw.wg_parent_id order by all_sale desc
    </select>

    <select id="countTax" resultMap="SaleResultMap">
        SELECT
        pbw.id as wg_id,
        pbw.wg_name as wg_name,
        pbw.wg_code as wg_code,
        IFNULL(sum(wcei.tax), 0) as all_tax
        FROM
        grid_region bw
        left join grid_company_land wcl on wcl.land_id = bw.id and wcl.is_valid = 1
        left join grid_company_economy_info wcei on wcl.company_id = wcei.company_id and wcei.is_valid = 1
        <if test="year != null and year != ''">
            and wcei.year = #{year}
        </if>
        left join grid_region pbw on bw.wg_parent_id = pbw.wg_code
        where bw.wg_type = 4 and bw.is_valid = 1 and bw.level = 3
        group by bw.wg_parent_id order by all_tax desc
    </select>

    <select id="selectCompanyEcoInfoList" resultMap="EcoInfoResultMap">
        select
        wc.id as company_id,
        wc.company_name,
        bc2.name as company_type,
        wcei.sale,
        wcei.tax,
        wcei.per_mu,
        wcei.year
        from
        grid_company_economy_info wcei
        left join grid_company wc on wc.id = wcei.company_id and wc.is_valid = 1
        left join bs_common bc2 on bc2.code = wc.qylx_code
        where wcei.is_valid = 1
        <if test="year != null and year != ''">
            and wcei.year = #{year}
        </if>
        <if test="searchName != null and searchName != ''">
            and (wc.company_name like CONCAT('%',#{searchName},'%')
            or wc.law_person like CONCAT('%',#{searchName},'%')
            or wc.phone like CONCAT('%',#{searchName},'%')
            )
        </if>
        <if test="type != null and type != '' and type == 'sale'">
            order by wcei.sale desc
        </if>
        <if test="type != null and type != '' and type == 'tax'">
            order by wcei.tax desc
        </if>
        <if test="type != null and type != '' and type == 'perMu'">
            order by wcei.per_mu desc
        </if>
        <if test="currIndex != null and pageSize != null">
            limit #{currIndex} , #{pageSize}
        </if>
    </select>
    <select id="countCompanyEcoInfoList" resultType="java.lang.Long">
        select
        count(1)
        from
        grid_company_economy_info wcei
        left join grid_company wc on wc.id = wcei.company_id and wc.is_valid = 1
        where wcei.is_valid = 1
        <if test="year != null and year != ''">
            and wcei.year = #{year}
        </if>
        <if test="searchName != null and searchName != ''">
            and (wc.company_name like CONCAT('%',#{searchName},'%')
            or wc.law_person like CONCAT('%',#{searchName},'%')
            or wc.phone like CONCAT('%',#{searchName},'%')
            )
        </if>
    </select>
    <select id="selectCompanyAreaInfoList" resultMap="AreaInfoResultMap">
        select
        wc.id as company_id,
        wc.company_name,
        bc2.name as company_type,
        sum(cast(wcl.share_area as DECIMAL)) as all_share_area
        from
        grid_company_land wcl
        left join grid_company wc on wc.id = wcl.company_id and wc.is_valid = 1
        left join bs_common bc2 on bc2.code = wc.qylx_code
        where wcl.is_valid = 1
        <if test="searchName != null and searchName != ''">
            and (wc.company_name like CONCAT('%',#{searchName},'%')
            or wc.law_person like CONCAT('%',#{searchName},'%')
            or wc.phone like CONCAT('%',#{searchName},'%')
            )
        </if>
        order by all_share_area desc
        <if test="currIndex != null and pageSize != null">
            limit #{currIndex} , #{pageSize}
        </if>
    </select>
    <select id="countCompanyAreaInfoList" resultType="java.lang.Long">
        select
        count(1)
        from
        grid_company_land wcl
        left join grid_company wc on wc.id = wcl.company_id and wc.is_valid = 1
        where wcl.is_valid = 1
        <if test="searchName != null and searchName != ''">
            and (wc.company_name like CONCAT('%',#{searchName},'%')
            or wc.law_person like CONCAT('%',#{searchName},'%')
            or wc.phone like CONCAT('%',#{searchName},'%')
            )
        </if>
    </select>
    <select id="pageInfoList" resultMap="EcoInfoResultMap">
        select
        wc.id as company_id,
        wc.company_name,
        bc2.name as company_type,
        wcei.sale,
        wcei.tax,
        wcei.per_mu,
        wcei.year
        from
        grid_company_economy_info wcei
        left join grid_company wc on wc.id = wcei.company_id
        left join bs_common bc2 on bc2.code = wc.qylx_code
        where wcei.is_valid = 1 and wc.is_valid = 1
        <if test="year != null and year != ''">
            and wcei.year = #{year}
        </if>
        <if test="companyKeyword != null and companyKeyword != ''">
            and (wc.company_name like CONCAT('%',#{companyKeyword},'%')
            or wc.law_person like CONCAT('%',#{companyKeyword},'%')
            )
        </if>
        <if test="qylxCode != null and qylxCode != ''">
            and wc.qylx_code = #{qylxCode}
        </if>
        <if test="orderType != null and orderType != ''">
            order by ${orderValue} ${orderType}
        </if>
        <if test="currIndex != null and pageSize != null">
            limit #{currIndex} , #{pageSize}
        </if>
    </select>
    <select id="countPageNum" resultType="java.lang.Long">
        select
        count(*)
        from
        grid_company_economy_info wcei
        left join grid_company wc on wc.id = wcei.company_id
        left join bs_common bc2 on bc2.code = wc.qylx_code
        where wcei.is_valid = 1 and wc.is_valid = 1
        <if test="year != null and year != ''">
            and wcei.year = #{year}
        </if>
        <if test="companyKeyword != null and companyKeyword != ''">
            and (wc.company_name like CONCAT('%',#{companyKeyword},'%')
            or wc.law_person like CONCAT('%',#{companyKeyword},'%')
            )
        </if>
        <if test="qylxCode != null and qylxCode != ''">
            and wc.qylx_code = #{qylxCode}
        </if>
    </select>

</mapper>