<?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.GridCompanyExtMapper">
  <resultMap id="BaseResultMap" type="com.ruoyi.system.domain.grid.GridCompany">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="company_name" jdbcType="VARCHAR" property="companyName" />
    <result column="qylx_code" jdbcType="VARCHAR" property="qylxCode" />
    <result column="credit_code" jdbcType="VARCHAR" property="creditCode" />
    <result column="law_person" jdbcType="VARCHAR" property="lawPerson" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="address" jdbcType="VARCHAR" property="address" />
    <result column="wg_code" jdbcType="VARCHAR" property="wgCode" />
    <result column="create_time" jdbcType="VARCHAR" property="createTime" />
    <result column="update_time" jdbcType="VARCHAR" property="updateTime" />
    <result column="create_id" jdbcType="VARCHAR" property="createId" />
    <result column="update_id" jdbcType="VARCHAR" property="updateId" />
    <result column="is_valid" jdbcType="TINYINT" property="isValid" />
    <result column="is_closed" jdbcType="TINYINT" property="isClosed" />
    <result column="area" jdbcType="VARCHAR" property="area" />
    <result column="is_factory" jdbcType="VARCHAR" property="isFactory" />
    <result column="is_live" jdbcType="VARCHAR" property="isLive" />
    <result column="employee_num" jdbcType="VARCHAR" property="employeeNum" />
    <result column="longitude" jdbcType="VARCHAR" property="longitude" />
    <result column="latitude" jdbcType="VARCHAR" property="latitude" />
  </resultMap>
  <sql id="Base_Column_List">
    id, company_name, qylx_code, credit_code, law_person, phone, address, wg_code, create_time,
    update_time, create_id, update_id, is_valid, is_closed, area, is_factory, is_live,
    employee_num, longitude, latitude
  </sql>
  <select id="selectCompanyBySecondLevel" resultMap="BaseResultMap">
    select
        wc.*
    from grid_company wc
    where 1=1
    <if test="company.isValid != null">
      and wc.is_valid = #{company.isValid}
    </if>
    <if test="company.wgCode != null and company.wgCode != '' and company.wgValue != null and company.wgValue != ''">
      and wc.wg_code in
          (select bs1.wg_code from grid_region bs1 where bs1.wg_parent_id in
                    (select bs2.wg_code from grid_region bs2 where bs2.wg_parent_id = #{company.wgCode} and bs2.wg_type = #{company.wgValue})
      and bs1.wg_type = #{company.wgValue})
    </if>
  </select>
  <select id="selectCompanyByThirdLevel" resultMap="BaseResultMap">
    select
    wc.*
    from grid_company wc
    where 1=1
    <if test="company.isValid != null">
      and wc.is_valid = #{company.isValid}
    </if>
    <if test="company.wgCode != null and company.wgCode != '' and company.wgValue != null and company.wgValue != ''">
      and wc.wg_code in
      (select bs1.wg_code from grid_region bs1 where bs1.wg_parent_id = #{company.wgCode} and bs1.wg_type = #{company.wgValue})
    </if>
  </select>
  <select id="selectCompanyByForthLevel" resultMap="BaseResultMap">
    select
    wc.*
    from grid_company wc
    where 1=1
    <if test="company.isValid != null">
      and wc.is_valid = #{company.isValid}
    </if>
    <if test="company.wgCode != null and company.wgCode != ''">
      and wc.wg_code = #{company.wgCode}
    </if>
  </select>
</mapper>
