<?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.GridCompanyMapper">
  <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" />
    <result column="level" jdbcType="VARCHAR" property="level" />
    <result column="is_unowned" jdbcType="TINYINT" property="isUnowned" />
  </resultMap>
  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <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, level, is_unowned
  </sql>
  <select id="selectByExample" parameterType="com.ruoyi.system.domain.grid.GridCompanyExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from grid_company
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from grid_company
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from grid_company
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.ruoyi.system.domain.grid.GridCompanyExample">
    delete from grid_company
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.ruoyi.system.domain.grid.GridCompany">
    insert into grid_company (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, level, is_unowned)
    values (#{id,jdbcType=VARCHAR}, #{companyName,jdbcType=VARCHAR}, #{qylxCode,jdbcType=VARCHAR},
      #{creditCode,jdbcType=VARCHAR}, #{lawPerson,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
      #{address,jdbcType=VARCHAR}, #{wgCode,jdbcType=VARCHAR}, #{createTime,jdbcType=VARCHAR},
      #{updateTime,jdbcType=VARCHAR}, #{createId,jdbcType=VARCHAR}, #{updateId,jdbcType=VARCHAR},
       #{isValid,jdbcType=TINYINT}, #{isClosed,jdbcType=TINYINT},#{area,jdbcType=VARCHAR},
      #{isFactory,jdbcType=VARCHAR}, #{isLive,jdbcType=VARCHAR}, #{employeeNum,jdbcType=VARCHAR},
      #{longitude,jdbcType=VARCHAR}, #{latitude,jdbcType=VARCHAR}, #{level,jdbcType=VARCHAR}, #{isUnowned,jdbcType=TINYINT})
  </insert>
  <insert id="insertSelective" parameterType="com.ruoyi.system.domain.grid.GridCompany" keyProperty="id" useGeneratedKeys="true">
    insert into grid_company
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="companyName != null">
        company_name,
      </if>
      <if test="qylxCode != null">
        qylx_code,
      </if>
      <if test="creditCode != null">
        credit_code,
      </if>
      <if test="lawPerson != null">
        law_person,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="address != null">
        address,
      </if>
      <if test="wgCode != null">
        wg_code,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="createId != null">
        create_id,
      </if>
      <if test="updateId != null">
        update_id,
      </if>
      <if test="isValid != null">
        is_valid,
      </if>
      <if test="isClosed != null">
        is_closed,
      </if>
      <if test="area != null">
        area,
      </if>
      <if test="isFactory != null">
        is_factory,
      </if>
      <if test="isLive != null">
        is_live,
      </if>
      <if test="employeeNum != null">
        employee_num,
      </if>
      <if test="longitude != null">
        longitude,
      </if>
      <if test="latitude != null">
        latitude,
      </if>
      <if test="level != null">
        level,
      </if>
      <if test="isUnowned != null">
        is_unowned,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="companyName != null">
        #{companyName,jdbcType=VARCHAR},
      </if>
      <if test="qylxCode != null">
        #{qylxCode,jdbcType=VARCHAR},
      </if>
      <if test="creditCode != null">
        #{creditCode,jdbcType=VARCHAR},
      </if>
      <if test="lawPerson != null">
        #{lawPerson,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        #{address,jdbcType=VARCHAR},
      </if>
      <if test="wgCode != null">
        #{wgCode,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=VARCHAR},
      </if>
      <if test="createId != null">
        #{createId,jdbcType=VARCHAR},
      </if>
      <if test="updateId != null">
        #{updateId,jdbcType=VARCHAR},
      </if>
      <if test="isValid != null">
        #{isValid,jdbcType=TINYINT},
      </if>
      <if test="isClosed != null">
        #{isClosed,jdbcType=TINYINT},
      </if>
      <if test="area != null">
        #{area,jdbcType=VARCHAR},
      </if>
      <if test="isFactory != null">
        #{isFactory,jdbcType=VARCHAR},
      </if>
      <if test="isLive != null">
        #{isLive,jdbcType=VARCHAR},
      </if>
      <if test="employeeNum != null">
        #{employeeNum,jdbcType=VARCHAR},
      </if>
      <if test="longitude != null">
        #{longitude,jdbcType=VARCHAR},
      </if>
      <if test="latitude != null">
        #{latitude,jdbcType=VARCHAR},
      </if>
      <if test="level != null">
        #{level,jdbcType=VARCHAR},
      </if>
      <if test="isUnowned != null">
        #{isUnowned,jdbcType=TINYINT},
      </if>
    </trim>
  </insert>

    <select id="countByExample" parameterType="com.ruoyi.system.domain.grid.GridCompanyExample" resultType="java.lang.Long">
    select count(*) from grid_company
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update grid_company
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.companyName != null">
        company_name = #{record.companyName,jdbcType=VARCHAR},
      </if>
      <if test="record.qylxCode != null">
        qylx_code = #{record.qylxCode,jdbcType=VARCHAR},
      </if>
      <if test="record.creditCode != null">
        credit_code = #{record.creditCode,jdbcType=VARCHAR},
      </if>
      <if test="record.lawPerson != null">
        law_person = #{record.lawPerson,jdbcType=VARCHAR},
      </if>
      <if test="record.phone != null">
        phone = #{record.phone,jdbcType=VARCHAR},
      </if>
      <if test="record.address != null">
        address = #{record.address,jdbcType=VARCHAR},
      </if>
      <if test="record.wgCode != null">
        wg_code = #{record.wgCode,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=VARCHAR},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=VARCHAR},
      </if>
      <if test="record.createId != null">
        create_id = #{record.createId,jdbcType=VARCHAR},
      </if>
      <if test="record.updateId != null">
        update_id = #{record.updateId,jdbcType=VARCHAR},
      </if>
      <if test="record.isValid != null">
        is_valid = #{record.isValid,jdbcType=TINYINT},
      </if>
      <if test="record.isClosed != null">
        is_closed = #{record.isClosed,jdbcType=TINYINT},
      </if>
      <if test="record.area != null">
        area = #{record.area,jdbcType=VARCHAR},
      </if>
      <if test="record.isFactory != null">
        is_factory = #{record.isFactory,jdbcType=VARCHAR},
      </if>
      <if test="record.isLive != null">
        is_live = #{record.isLive,jdbcType=VARCHAR},
      </if>
      <if test="record.employeeNum != null">
        employee_num = #{record.employeeNum,jdbcType=VARCHAR},
      </if>
      <if test="record.longitude != null">
        longitude = #{record.longitude,jdbcType=VARCHAR},
      </if>
      <if test="record.latitude != null">
        latitude = #{record.latitude,jdbcType=VARCHAR},
      </if>
      <if test="record.level != null">
        level = #{record.level,jdbcType=VARCHAR},
      </if>
      <if test="record.isUnowned != null">
        is_unowned = #{record.isUnowned,jdbcType=TINYINT},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update grid_company
    set id = #{record.id,jdbcType=VARCHAR},
      company_name = #{record.companyName,jdbcType=VARCHAR},
      qylx_code = #{record.qylxCode,jdbcType=VARCHAR},
      credit_code = #{record.creditCode,jdbcType=VARCHAR},
      law_person = #{record.lawPerson,jdbcType=VARCHAR},
      phone = #{record.phone,jdbcType=VARCHAR},
      address = #{record.address,jdbcType=VARCHAR},
      wg_code = #{record.wgCode,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=VARCHAR},
      update_time = #{record.updateTime,jdbcType=VARCHAR},
      create_id = #{record.createId,jdbcType=VARCHAR},
      update_id = #{record.updateId,jdbcType=VARCHAR},
      is_valid = #{record.isValid,jdbcType=TINYINT},
      is_closed = #{record.isClosed,jdbcType=TINYINT},
      area = #{record.area,jdbcType=VARCHAR},
      is_factory = #{record.isFactory,jdbcType=VARCHAR},
      is_live = #{record.isLive,jdbcType=VARCHAR},
      employee_num = #{record.employeeNum,jdbcType=VARCHAR},
      longitude = #{record.longitude,jdbcType=VARCHAR},
      latitude = #{record.latitude,jdbcType=VARCHAR},
    level = #{record.level,jdbcType=VARCHAR,
    is_unowned = #{record.isUnowned,jdbcType=TINYINT}}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.ruoyi.system.domain.grid.GridCompany">
    update grid_company
    <set>
      <if test="companyName != null">
        company_name = #{companyName,jdbcType=VARCHAR},
      </if>
      <if test="qylxCode != null">
        qylx_code = #{qylxCode,jdbcType=VARCHAR},
      </if>
      <if test="creditCode != null">
        credit_code = #{creditCode,jdbcType=VARCHAR},
      </if>
      <if test="lawPerson != null">
        law_person = #{lawPerson,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="address != null">
        address = #{address,jdbcType=VARCHAR},
      </if>
      <if test="wgCode != null">
        wg_code = #{wgCode,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=VARCHAR},
      </if>
      <if test="createId != null">
        create_id = #{createId,jdbcType=VARCHAR},
      </if>
      <if test="updateId != null">
        update_id = #{updateId,jdbcType=VARCHAR},
      </if>
      <if test="isValid != null">
        is_valid = #{isValid,jdbcType=TINYINT},
      </if>
      <if test="isClosed != null">
        is_closed = #{isClosed,jdbcType=TINYINT},
      </if>
      <if test="area != null">
        area = #{area,jdbcType=VARCHAR},
      </if>
      <if test="isFactory != null">
        is_factory = #{isFactory,jdbcType=VARCHAR},
      </if>
      <if test="isLive != null">
        is_live = #{isLive,jdbcType=VARCHAR},
      </if>
      <if test="employeeNum != null">
        employee_num = #{employeeNum,jdbcType=VARCHAR},
      </if>
      <if test="longitude != null">
        longitude = #{longitude,jdbcType=VARCHAR},
      </if>
      <if test="latitude != null">
        latitude = #{latitude,jdbcType=VARCHAR},
      </if>
      <if test="level != null">
        level = #{level,jdbcType=VARCHAR},
      </if>
      <if test="isUnowned != null">
        is_unowned = #{isUnowned,jdbcType=TINYINT},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.ruoyi.system.domain.grid.GridCompany">
    update grid_company
    set company_name = #{companyName,jdbcType=VARCHAR},
      qylx_code = #{qylxCode,jdbcType=VARCHAR},
      credit_code = #{creditCode,jdbcType=VARCHAR},
      law_person = #{lawPerson,jdbcType=VARCHAR},
      phone = #{phone,jdbcType=VARCHAR},
      address = #{address,jdbcType=VARCHAR},
      wg_code = #{wgCode,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=VARCHAR},
      create_id = #{createId,jdbcType=VARCHAR},
      update_id = #{updateId,jdbcType=VARCHAR},
      is_valid = #{isValid,jdbcType=TINYINT},
      is_closed = #{isClosed,jdbcType=TINYINT},
      area = #{area,jdbcType=VARCHAR},
      is_factory = #{isFactory,jdbcType=VARCHAR},
      is_live = #{isLive,jdbcType=VARCHAR},
      employee_num = #{employeeNum,jdbcType=VARCHAR},
      longitude = #{longitude,jdbcType=VARCHAR},
      latitude = #{latitude,jdbcType=VARCHAR},
    level = #{level,jdbcType=VARCHAR},
    is_unowned = #{isUnowned,jdbcType=TINYINT}
    where id = #{id,jdbcType=VARCHAR}
  </update>

  <select id="countGridCompany" parameterType="java.lang.String" resultType="map">
    select a.name, count(b.id) as number from bs_common as a LEFT JOIN grid_company as b on a.code = b.qylx_code and b.is_valid = 1
    <if test="wgCodeLikes != null and wgCodeLikes.size() > 0">
      and (
      <foreach collection="wgCodeLikes" item="wgCode" open="(" close=")" separator="or">
        b.wg_code like CONCAT(#{wgCode}, '%')
      </foreach>
      )
    </if>
    where a.parent_id = (select id from bs_common where code = 'qylx') and a.is_valid = '1' GROUP BY a.code ORDER BY number desc, a.sort
  </select>

  <select id="countGridCompanyByGrid" parameterType="java.lang.String" resultType="map">
    select a.wg_name as name, count(b.id) as number from grid_region as a LEFT JOIN grid_company as b on b.wg_code like concat(a.wg_code, '%')
    and b.is_valid = 1  where a.level = '2' and a.is_valid = '1' GROUP BY a.wg_code ORDER BY number desc, a.wg_code
  </select>


  <select id="selectByRandom" resultMap="BaseResultMap">
    SELECT * FROM grid_company AS t1
    WHERE  t1.is_valid = 1 and t1.is_closed = 1 and t1.wg_code is not null
    <if test="chooseCompanyType != null and chooseCompanyType != ''">
      and t1.qylx_code like CONCAT('%',#{chooseCompanyType},'%')
    </if>
    ORDER BY RAND() ASC LIMIT #{companyNum}
  </select>
    <select id="countByParam" resultType="java.lang.Integer" parameterType="com.ruoyi.system.domain.grid.vo.GridCompanySearchParam">
      select count(1) from grid_company where is_valid = '1'
      <if test="wgCodeLikes != null and wgCodeLikes.size() > 0">
        and (
        <foreach collection="wgCodeLikes" item="wgCode" open="(" close=")" separator="or">
          wg_code like CONCAT(#{wgCode}, '%')
        </foreach>
        )
      </if>
    </select>
    <select id="listByParam" resultType="com.ruoyi.system.domain.grid.GridCompany" parameterType="com.ruoyi.system.domain.grid.vo.GridCompanySearchParam">
      select * from grid_company where is_valid = '1'
      <if test="value != null and value != ''">
        and (company_name like CONCAT('%', #{value}, '%') or law_person like CONCAT('%', #{value}, '%') or phone like CONCAT('%', #{value}, '%'))
      </if>
      <if test="wgCodeLikes != null and wgCodeLikes.size() > 0">
        and (
        <foreach collection="wgCodeLikes" item="wgCode" open="(" close=")" separator="or">
          wg_code like CONCAT(#{wgCode}, '%')
        </foreach>
        )
      </if>
      <if test="creditCode != null and creditCode !=''">
        and credit_code like CONCAT('%', #{creditCode}, '%')
      </if>
      <if test="qylxCode != null and qylxCode !=''">
        and qylx_code = #{qylxCode}
      </if>
      <if test="isClosed != null">
        and is_closed = #{isClosed}
      </if>
      <if test="isUnowned != null">
        and is_unowned = #{isUnowned}
      </if>
      <if test="orderCondition != null and orderCondition != ''">
         order by ${orderCondition}
      </if>
    </select>
  <select id="simplePointList" resultType="com.ruoyi.system.domain.grid.vo.SimpleGridCompanyItem">
    select id, company_name as companyName, credit_code as creditCode,longitude,latitude from grid_company where is_valid = '1'
    and longitude is not null and latitude is not null and longitude != '' and latitude != ''
    <if test="name != null and name != ''">
      and company_name like CONCAT('%', #{name}, '%')
    </if>
    <if test="wgCode != null and wgCode != ''">
      and wg_code like CONCAT(#{wgCode}, '%')
    </if>
  </select>
  <select id="selectLabelById" resultType="java.lang.String">
    select label from grid_company
    where id = #{id}
  </select>

  <insert id="insertLabelById">
    update grid_company
    set label = #{label,jdbcType=VARCHAR}
    where id = #{id}
  </insert>
</mapper>
