<?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.GridCompanyLandExtMapper">
    <resultMap id="BaseResultMap" type="com.ruoyi.system.domain.grid.ext.GridCompanyLandExt">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="company_id" jdbcType="VARCHAR" property="companyId"/>
        <result column="land_id" jdbcType="VARCHAR" property="landId"/>
        <result column="share_area" jdbcType="VARCHAR" property="shareArea"/>
        <result column="land_purpose" jdbcType="VARCHAR" property="landPurpose" />
        <result column="land_purpose_name" jdbcType="VARCHAR" property="landPurposeName" />
        <result column="lease_relation" jdbcType="VARCHAR" property="leaseRelation"/>
        <result column="is_valid" jdbcType="VARCHAR" property="isValid"/>
        <result column="create_time" jdbcType="VARCHAR" property="createTime"/>
        <result column="comanyName" jdbcType="VARCHAR" property="comanyName"/>
        <result column="landName" jdbcType="VARCHAR" property="landName"/>
        <result column="zlgxName" jdbcType="VARCHAR" property="zlgxName"/>
    </resultMap>

    <select id="getList" resultMap="BaseResultMap">
        SELECT
        wcl.*,wc.company_name as comanyName,bc.name as zlgxName,bw.wg_name as landName,
               bc1.name as land_purpose_name
        FROM
        grid_company_land wcl
        LEFT JOIN
        grid_company wc on wc.id = wcl.company_id
        LEFT JOIN
        grid_region bw on bw.id = wcl.land_id
        LEFT JOIN
        bs_common bc on bc.code = wcl.lease_relation
        left join
        bs_common bc1 on bc1.code = wcl.land_purpose
        <where>
            and wcl.is_valid = '1'
            <if test="companyId != null and companyId != ''">
                and wcl.company_id = #{companyId}
            </if>
            <if test="landId != null and landId != ''">
                and wcl.land_id = #{landId}
            </if>
            <if test="landPurpose != null and landPurpose != ''">
                and wc.land_purpose = #{landPurpose}
            </if>
        </where>
        order by wcl.create_time desc
    </select>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        wcl.*,wc.company_name as comanyName,bc.name as zlgxName,bw.wg_name as landName, bc1.name as land_purpose_name
        FROM
        grid_company_land wcl
        LEFT JOIN
        grid_company wc on wc.id = wcl.company_id
        LEFT JOIN
        grid_region bw on bw.id = wcl.land_id
        LEFT JOIN
        bs_common bc on bc.code = wcl.lease_relation
        LEFT JOIN
        bs_common bc1 on bc1.code = wcl.land_purpose
        where wcl.is_valid = '1'
        and wcl.id = #{id}
    </select>

    <select id="countByZlgx" resultType="java.lang.Integer">
        SELECT count(DISTINCT wcl.land_id)
        FROM
        grid_company_land wcl
        LEFT JOIN
        grid_company wc on wc.id = wcl.company_id
        LEFT JOIN
        grid_region bw on bw.id = wcl.land_id
        LEFT JOIN
        bs_common bc on bc.code = wcl.lease_relation
        <where>
            and wcl.is_valid = '1'
            <if test="zlgx != null and zlgx != ''">
                and wcl.lease_relation = #{zlgx}
            </if>
        </where>
    </select>

    <select id="getPermuSortList" resultType="com.ruoyi.system.domain.grid.vo.GridEconomyPerMuItem">
        SELECT bw.id as id, bw.wg_name as wgName,
        IFNULL(bw.wg_area,0) as landArea,
        IFNULL(sum(wcei.per_mu),0) as perMuSum
        from  grid_company_land wcl
        LEFT JOIN
        grid_region bw
        on bw.id = wcl.land_id
        LEFT JOIN
        grid_company_economy_info wcei
        on wcl.company_id = wcei.company_id
        <where>
            and wcl.is_valid = '1'
            and wcei.is_valid = '1'
            and bw.wg_type =4
            and bw.level = 3
            <if test="year != null and year != ''">
                and wcei.year = #{year}
            </if>
        </where>
        group by bw.id
        order by perMuSum desc
    </select>

</mapper>