<?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.gxwebsoft.common.system.mapper.CompanyMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,b.tenant_id,b.tenant_name,b.tenant_code,c.title as categoryName
        FROM sys_company a
        LEFT JOIN sys_tenant b ON a.tenant_id = b.tenant_id
        LEFT JOIN cms_navigation c ON a.category_id = c.navigation_id
        <where>
            <if test="param.companyId != null">
                AND a.company_id = #{param.companyId}
            </if>
            <if test="param.type != null">
              AND a.type = #{param.type}
            </if>
            <if test="param.official != null">
              AND a.official = #{param.official}
            </if>
            <if test="param.categoryId != null">
              AND a.category_id = #{param.categoryId}
            </if>
            <if test="param.shortName != null">
                AND a.short_name LIKE CONCAT('%', #{param.shortName}, '%')
            </if>
            <if test="param.companyName != null">
                AND a.company_name LIKE CONCAT('%', #{param.companyName}, '%')
            </if>
            <if test="param.companyType != null">
                AND a.company_type = #{param.companyType}
            </if>
            <if test="param.companyLogo != null">
                AND a.company_logo LIKE CONCAT('%', #{param.companyLogo}, '%')
            </if>
            <if test="param.domain != null">
                AND a.domain LIKE CONCAT('%', #{param.domain}, '%')
            </if>
            <if test="param.phone != null">
                AND a.phone = #{param.phone}
            </if>
            <if test="param.invoiceHeader != null">
                AND a.Invoice_header LIKE CONCAT('%', #{param.invoiceHeader}, '%')
            </if>
            <if test="param.startTime != null">
                AND a.start_time LIKE CONCAT('%', #{param.startTime}, '%')
            </if>
            <if test="param.expirationTime != null">
                AND a.expiration_time LIKE CONCAT('%', #{param.expirationTime}, '%')
            </if>
            <if test="param.version != null">
                AND a.version = #{param.version}
            </if>
            <if test="param.members != null">
                AND a.members = #{param.members}
            </if>
            <if test="param.industryParent != null">
                AND a.industry_parent LIKE CONCAT('%', #{param.industryParent}, '%')
            </if>
            <if test="param.industryChild != null">
                AND a.industry_child LIKE CONCAT('%', #{param.industryChild}, '%')
            </if>
            <if test="param.departments != null">
                AND a.departments = #{param.departments}
            </if>
            <if test="param.country != null">
                AND a.country LIKE CONCAT('%', #{param.country}, '%')
            </if>
            <if test="param.province != null">
                AND a.province LIKE CONCAT('%', #{param.province}, '%')
            </if>
            <if test="param.city != null">
                AND a.city LIKE CONCAT('%', #{param.city}, '%')
            </if>
            <if test="param.region != null">
                AND a.region LIKE CONCAT('%', #{param.region}, '%')
            </if>
            <if test="param.address != null">
                AND a.address LIKE CONCAT('%', #{param.address}, '%')
            </if>
            <if test="param.longitude != null">
                AND a.longitude LIKE CONCAT('%', #{param.longitude}, '%')
            </if>
            <if test="param.latitude != null">
                AND a.latitude LIKE CONCAT('%', #{param.latitude}, '%')
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.authentication != null">
                AND a.authentication = #{param.authentication}
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.appType != null">
              AND a.app_type = #{param.appType}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.authoritative != null">
              AND a.authoritative = #{param.authoritative}
            </if>
            <if test="param.recommend != null">
              AND a.recommend = 1
            </if>
            <if test="param.appName != null">
              AND a.short_name = #{param.appName}
            </if>
            <if test="param.email != null">
              AND a.email = #{param.email}
            </if>
            <if test="param.merchantId != null">
              AND a.merchant_id = #{param.merchantId}
            </if>
            <if test="param.companyIds != null">
              AND a.company_id IN
              <foreach collection="param.companyIds" item="item" separator="," open="(" close=")">
                #{item}
              </foreach>
            </if>
            <if test="param.keywords != null">
              AND (a.company_name LIKE CONCAT('%', #{param.keywords}, '%')
              OR a.short_name LIKE CONCAT('%', #{param.keywords}, '%')
              OR a.tenant_id = #{param.keywords}
              OR a.phone = #{param.keywords}
              OR a.domain LIKE CONCAT('%', #{param.keywords}, '%')
              )
            </if>
        </where>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.common.system.entity.Company">
        <include refid="selectSql"></include>
    </select>

    <!-- 分页查询不限租户 -->
    <select id="selectPageRelAll" resultType="com.gxwebsoft.common.system.entity.Company">
      <include refid="selectSql"></include>
    </select>

    <!-- 企业查询不限租户 -->
    <select id="getCompanyAll" resultType="com.gxwebsoft.common.system.entity.Company">
      SELECT a.*,b.tenant_id,b.tenant_name,b.tenant_code
      FROM sys_company a
      LEFT JOIN sys_tenant b ON a.tenant_id = b.tenant_id
      <where>
        <if test="companyId != null">
          AND a.company_id = #{companyId}
        </if>
      </where>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.common.system.entity.Company">
        <include refid="selectSql"></include>
    </select>

    <!-- 检查是否存在 -->
    <select id="getCount" resultType="com.gxwebsoft.common.system.entity.Company">
      <include refid="selectSql"></include>
    </select>

  <!-- 更新企业信息-->
  <update id="updateByCompanyId">
    UPDATE sys_company SET storage = #{param.storage} WHERE company_id = #{param.companyId}
  </update>

  <!-- 删除企业信息-->
  <update id="removeCompanyAll">
    UPDATE sys_company SET deleted = 1 WHERE company_id = #{param.companyId}
  </update>

  <update id="undeleteAll">
    UPDATE sys_company SET deleted = 0 WHERE company_id = #{param.companyId}
  </update>

  <!-- 按租户ID检查企业 -->
  <select id="getByTenantId" resultType="com.gxwebsoft.common.system.entity.Company">
    SELECT a.*
    FROM sys_company a WHERE a.tenant_id = #{tenantId} and a.deleted = 0
  </select>

</mapper>
