<?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.GridCompanySafeCheckExtMapper">
  <resultMap id="BaseResultMap" type="com.ruoyi.system.domain.grid.vo.GridCompanySafeCheckExt">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="company_id" jdbcType="VARCHAR" property="companyId" />
    <result column="check_time" jdbcType="VARCHAR" property="checkTime" />
    <result column="clock_longitude" jdbcType="VARCHAR" property="clockLongitude" />
    <result column="clock_latitude" jdbcType="VARCHAR" property="clockLatitude" />
    <result column="check_user_id" jdbcType="VARCHAR" property="checkUserId" />
    <result column="check_user_name" jdbcType="VARCHAR" property="checkUserName" />
    <result column="check_status" jdbcType="VARCHAR" property="checkStatus" />
    <result column="check_status_name" jdbcType="VARCHAR" property="checkStatusName" />
    <result column="is_valid" jdbcType="VARCHAR" property="isValid" />
    <result column="create_time" jdbcType="VARCHAR" property="createTime" />
    <result column="check_type" jdbcType="VARCHAR" property="checkType" />
    <result column="company_name" jdbcType="VARCHAR" property="companyName" />
    <result column="qylx_code" jdbcType="VARCHAR" property="companyType" />
    <result column="qylx_name" jdbcType="VARCHAR" property="companyTypeName" />
    <result column="check_type_name" jdbcType="VARCHAR" property="checkTypeName" />
    <result column="wg_code" jdbcType="VARCHAR" property="wgCode" />
    <result column="wg_name" jdbcType="VARCHAR" property="wgName" />
    <result column="check_feedback" jdbcType="VARCHAR" property="checkFeedback" />
  </resultMap>
  <resultMap id="SafeCountResultMap" type="com.ruoyi.system.domain.lawcase.vo.CaseSafeCheckCountBean">
    <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="company_num" jdbcType="INTEGER" property="companyNum" />
  </resultMap>
  <sql id="Base_Column_List">
    id, company_id, check_time, clock_longitude, clock_latitude, check_user_id, check_user_name, check_status,
    is_valid, create_time
  </sql>
  <select id="selectByExample" parameterType="map" resultMap="BaseResultMap">
    select
      c1.*,
      c2.company_name,
      c2.qylx_code,
      c2.wg_code,
      bc.name as check_status_name,
      bc2.name as qylx_name,
      bc3.name as check_type_name,
      bw.wg_name as wg_name
    from grid_company_safe_check c1
    left join grid_company c2 on c1.company_id = c2.id
    left join bs_common bc on bc.code = c1.check_status
    left join bs_common bc2 on bc2.code = c2.qylx_code
    left join bs_common bc3 on bc3.code = c1.check_type
    left join grid_region bw on bw.wg_code = c2.wg_code
    where 1=1
    <if test="check.checkUserId != null and check.checkUserId != ''">
      and c1.check_user_id = #{check.checkUserId}
    </if>
    <if test="check.companyId != null and check.companyId != ''">
      and c1.company_id = #{check.companyId}
    </if>
    <if test="check.checkStatus != null and check.checkStatus != ''">
      and c1.check_status = #{check.checkStatus}
    </if>
    <if test="check.companyName != null and check.companyName != ''">
      and c2.company_name like CONCAT('%',#{check.companyName,jdbcType=VARCHAR},'%')
    </if>
    <if test="check.creditCode != null and check.creditCode != ''">
      and c2.credit_code like CONCAT('%',#{check.creditCode,jdbcType=VARCHAR},'%')
    </if>
    <if test="check.companyType != null and check.companyType != ''">
      and c2.qylx_code = #{check.companyType}
    </if>
    <if test="check.startTime != null and check.startTime !=''">
      and c1.check_time &gt;=#{check.startTime}
    </if>
    <if test="check.endTime != null and check.endTime !=''">
      and c1.check_time &lt;=#{check.endTime}
    </if>
    <if test="check.checkStatus != null and check.checkStatus != ''">
      and c1.check_status = #{check.checkStatus}
    </if>
    <if test="check.companyIds != null and check.companyIds.size &gt; 0">
      and c1.company_id in
      <foreach close=")" collection="check.companyIds" item="listItem" open="(" separator=",">
        #{listItem}
      </foreach>
    </if>
    <if test="check.isValid != null and check.isValid != ''">
      and c1.is_valid = #{check.isValid}
    </if>
    <if test="check.clockLongitude != null and check.clockLongitude != ''">
      and c1.clock_longitude = #{check.clockLongitude}
    </if>
    <if test="check.clockLatitude != null and check.clockLatitude != ''">
      and c1.clock_latitude = #{check.clockLatitude}
    </if>
    <if test="check.checkType != null and check.checkType != ''">
      and c1.check_type = #{check.checkType}
    </if>
    order by c1.check_time desc,c1.create_time desc
    <if test="check.currIndex != null and check.pageSize != null">
        limit #{check.currIndex} , #{check.pageSize}
    </if>
  </select>
  <select id="countByExample" parameterType="map" resultType="java.lang.Long">
    select
           count(*)
    from grid_company_safe_check c1
    left join grid_company c2 on c1.company_id = c2.id
    where 1=1
    <if test="check.checkUserId != null and check.checkUserId != ''">
      and c1.check_user_id = #{check.checkUserId}
    </if>
    <if test="check.companyId != null and check.companyId != ''">
      and c1.company_id = #{check.companyId}
    </if>
    <if test="check.checkStatus != null and check.checkStatus != ''">
      and c1.check_status = #{check.checkStatus}
    </if>
    <if test="check.companyName != null and check.companyName != ''">
      and c2.company_name like CONCAT('%',#{check.companyName,jdbcType=VARCHAR},'%')
    </if>
    <if test="check.creditCode != null and check.creditCode != ''">
      and c2.credit_code like CONCAT('%',#{check.creditCode,jdbcType=VARCHAR},'%')
    </if>
    <if test="check.companyType != null and check.companyType != ''">
      and c2.qylx_code = #{check.companyType}
    </if>
    <if test="check.startTime != null and check.startTime !=''">
      and c1.check_time &gt;=#{check.startTime}
    </if>
    <if test="check.endTime != null and check.endTime !=''">
      and c1.check_time &lt;=#{check.endTime}
    </if>
    <if test="check.checkStatus != null and check.checkStatus != ''">
      and c1.check_status = #{check.checkStatus}
    </if>
    <if test="check.companyIds != null and check.companyIds.size &gt; 0">
      and c1.company_id in
      <foreach close=")" collection="check.companyIds" item="listItem" open="(" separator=",">
        #{listItem}
      </foreach>
    </if>
    <if test="check.checkType != null and check.checkType != ''">
      and c1.check_type = #{check.checkType}
    </if>
    <if test="check.isValid != null and check.isValid != ''">
      and c1.is_valid = #{check.isValid}
    </if>
  </select>
  <select id="countCountryCheck" resultMap="SafeCountResultMap">
    SELECT
    ppbw.id as wg_id,
    ppbw.wg_name as wg_name,
    ppbw.wg_code as wg_code,
    count(distinct wcei.company_id) as company_num
    FROM
    grid_region_user bw
    left join grid_company wc on wc.wg_code = bw.wg_code and wc.is_valid = 1
    left join grid_company_safe_check wcei on wc.id = wcei.company_id  and wcei.is_valid = 1
    left join grid_region_user pbw on bw.wg_parent_id = pbw.wg_code
    left join grid_region_user ppbw on pbw.wg_parent_id = ppbw.wg_code
    where bw.wg_type = 1 and bw.is_valid = 1 and bw.level = 4
    <if test="level != null and level != '' and level == '2' and wgCode != null and wgCode != ''">
      and bw.wg_parent_id in (select lbw.wg_code from grid_region_user lbw where lbw.wg_parent_id = #{wgCode});
    </if>
    <if test="level != null and level != '' and level == '3' and wgCode != null and wgCode != ''">
      and bw.wg_parent_id = #{wgCode};
    </if>
    <if test="level != null and level != '' and level == '4' and wgCode != null and wgCode != ''">
      and bw.wg_code = #{wgCode};
    </if>
    group by pbw.wg_parent_id order by company_num desc
  </select>

  <select id="selectCompanyList" parameterType="map" resultMap="BaseResultMap">
    select
    c1.*,
    c2.company_name,
    c2.qylx_code,
    c2.wg_code,
    bc.name as check_status_name,
    bc2.name as qylx_name
    from grid_company_safe_check c1
    left join grid_company c2 on c1.company_id = c2.id
    <if test="check.searchName != null and check.searchName != ''">
      and (c2.company_name like CONCAT('%',#{check.searchName,jdbcType=VARCHAR},'%')
      or c2.law_person like CONCAT('%',#{check.searchName,jdbcType=VARCHAR},'%')
      or c2.phone like CONCAT('%',#{check.searchName,jdbcType=VARCHAR},'%')
      )
    </if>
    left join bs_common bc on bc.code = c1.check_status
    left join bs_common bc2 on bc2.code = c2.qylx_code
    where 1=1
    <if test="check.startTime != null and check.startTime !=''">
      and c1.check_time &gt;=#{check.startTime}
    </if>
    <if test="check.endTime != null and check.endTime !=''">
      and c1.check_time &lt;=#{check.endTime}
    </if>
    <if test="check.checkStatus != null and check.checkStatus != ''">
      and c1.check_status = #{check.checkStatus}
    </if>
    <if test="check.companyIds != null and check.companyIds.size &gt; 0">
      and c1.company_id in
      <foreach close=")" collection="check.companyIds" item="listItem" open="(" separator=",">
        #{listItem}
      </foreach>
    </if>
    <if test="check.isValid != null and check.isValid != ''">
      and c1.is_valid = #{check.isValid}
    </if>
    <if test="check.clockLongitude != null and check.clockLongitude != ''">
      and c1.clock_longitude = #{check.clockLongitude}
    </if>
    <if test="check.clockLatitude != null and check.clockLatitude != ''">
      and c1.clock_latitude = #{check.clockLatitude}
    </if>
    order by c1.check_time desc,c1.create_time desc
    <if test="check.currIndex != null and check.pageSize != null">
      limit #{check.currIndex} , #{check.pageSize}
    </if>
  </select>

  <select id="countCompanyList" parameterType="map" resultType="java.lang.Long">
    select
    count(1)
    from grid_company_safe_check c1
    left join grid_company c2 on c1.company_id = c2.id
    <if test="check.searchName != null and check.searchName != ''">
      and (c2.company_name like CONCAT('%',#{check.searchName,jdbcType=VARCHAR},'%')
      or c2.law_person like CONCAT('%',#{check.searchName,jdbcType=VARCHAR},'%')
      or c2.phone like CONCAT('%',#{check.searchName,jdbcType=VARCHAR},'%')
      )
    </if>
    where 1=1
    <if test="check.startTime != null and check.startTime !=''">
      and c1.check_time &gt;=#{check.startTime}
    </if>
    <if test="check.endTime != null and check.endTime !=''">
      and c1.check_time &lt;=#{check.endTime}
    </if>
    <if test="check.checkStatus != null and check.checkStatus != ''">
      and c1.check_status = #{check.checkStatus}
    </if>
    <if test="check.companyIds != null and check.companyIds.size &gt; 0">
      and c1.company_id in
      <foreach close=")" collection="check.companyIds" item="listItem" open="(" separator=",">
        #{listItem}
      </foreach>
    </if>
    <if test="check.isValid != null and check.isValid != ''">
      and c1.is_valid = #{check.isValid}
    </if>
    <if test="check.clockLongitude != null and check.clockLongitude != ''">
      and c1.clock_longitude = #{check.clockLongitude}
    </if>
    <if test="check.clockLatitude != null and check.clockLatitude != ''">
      and c1.clock_latitude = #{check.clockLatitude}
    </if>
  </select>
</mapper>