<?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.sckw.system.dao.KwsSpecialDao">

  <select id="findPage" resultType="com.sckw.system.model.vo.res.KwsSpecialResVo" parameterType="java.util.Map" >
    SELECT
      ks.id, ks.name, ks.website, ks.manager, ks.code, ks.ent_id entId, ks.is_main, qr_code qrCode, ks.remark, ks.status,
      ks.create_by createBy, ks.create_time createTime, ks.update_by updateBy, ks.update_time updateTime,
      ke.firm_name firmName, ku.`name` createByName, ku1.`name` managerName
    from kws_special ks
    left join kws_enterprise ke on ke.id = ks.ent_id
    left join kws_user ku on ku.id = ks.create_by
    left join kws_user ku1 on ku1.id = ks.manager
    where ks.del_flag = 0
    <if test="name != null and name != ''">
      and ks.name = #{name, jdbcType=VARCHAR}
    </if>
    <if test="code != null and code != ''">
      and ks.code = #{code, jdbcType=VARCHAR}
    </if>
    <if test="isMain != null and isMain != ''">
      and is_main = #{isMain, jdbcType=INTEGER}
    </if>
    <if test="keywords != null and keywords != ''">
      and (
        ks.name like concat('%', #{keywords}, '%')
        or ks.code like concat('%', #{keywords}, '%')
        or ke.firm_name like concat('%', #{keywords}, '%')
        or ku.`name` like concat('%', #{keywords}, '%')
      )
    </if>
    ORDER BY ks.create_time desc
  </select>

  <select id="findList" resultType="com.sckw.system.model.KwsSpecial" parameterType="java.util.Map" >
    SELECT
    id, name, website, manager, code, ent_id entId, is_main isMain, qr_code qrCode, remark, status, create_by createBy,
    create_time createTime, update_by updateBy, update_time updateTime
    from kws_special
    where del_flag = 0
    <if test="entId != null and entId != ''">
      and ent_id = #{entId, jdbcType=VARCHAR}
    </if>
    <if test="name != null and name != ''">
      and name = #{name, jdbcType=VARCHAR}
    </if>
    <if test="code != null and code != ''">
      and code = #{code, jdbcType=VARCHAR}
    </if>
    <if test="isMain != null and isMain != ''">
      and is_main = #{isMain, jdbcType=INTEGER}
    </if>
    <if test="website != null and website != ''">
      and website = #{website, jdbcType=VARCHAR}
    </if>
    <if test="websiteFuzzy != null and websiteFuzzy != ''">
      and website like concat('%', #{websiteFuzzy}, '%')
    </if>
    <if test="manager != null and manager != ''">
      and manager = #{manager, jdbcType=VARCHAR}
    </if>
    ORDER BY create_time desc
  </select>

  <select id="findSpecialPage" resultType="com.sckw.system.model.vo.res.KwsSpecialEntResVo" parameterType="java.util.Map" >
    SELECT * from (
      select
        ke.id entId, ke.firm_name firmName, 0 isMain, ks.id specialId, ks.code, kes.create_time createTime, ku.`name` createByName
      from kws_enterprise ke
      left join kws_ent_special kes on kes.ent_id = ke.id and kes.del_flag = 0
      LEFT JOIN kws_special ks on ks.id = kes.special_id
      left join kws_user ku on ku.id = kes.create_by
      where ke.del_flag = 0 and kes.del_flag = 0
      UNION
      select
        ke.id entId, ke.firm_name firmName, 1 isMain, ks.id specialId, ks.code, ks.create_time createTime, ku.`name` createByName
      from kws_enterprise ke
      LEFT JOIN kws_special ks on ks.ent_id = ke.id
      left join kws_user ku on ku.id = ks.create_by
      where ke.del_flag = 0 and ks.del_flag = 0
    ) tab where 1=1
    <if test="firmName != null and firmName != ''">
      and firmName LIKE concat('%',#{firmName},'%')
    </if>
    <if test="isMain != null and isMain != ''">
      and isMain = #{isMain, jdbcType=INTEGER}
    </if>
    <if test="specialId != null and specialId != ''">
      and specialId = #{specialId, jdbcType=BIGINT}
    </if>
    ORDER BY createTime desc
  </select>
</mapper>