<?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.KwsEnterpriseDao">
  <resultMap id="BaseResultMap" type="com.sckw.system.model.KwsEnterprise">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="firm_name" jdbcType="VARCHAR" property="firmName" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="contacts" jdbcType="VARCHAR" property="contacts" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="legal_name" jdbcType="VARCHAR" property="legalName" />
    <result column="legal_phone" jdbcType="VARCHAR" property="legalPhone" />
    <result column="head" jdbcType="VARCHAR" property="head" />
    <result column="integral" jdbcType="INTEGER" property="integral" />
    <result column="balance" jdbcType="DECIMAL" property="balance" />
    <result column="experience" jdbcType="INTEGER" property="experience" />
    <result column="member_level" jdbcType="INTEGER" property="memberLevel" />
    <result column="reg_time" jdbcType="TIMESTAMP" property="regTime" />
    <result column="reg_source" jdbcType="VARCHAR" property="regSource" />
    <result column="org_code" jdbcType="VARCHAR" property="orgCode" />
    <result column="city_code" jdbcType="INTEGER" property="cityCode" />
    <result column="detail_address" jdbcType="VARCHAR" property="detailAddress" />
    <result column="city_name" jdbcType="VARCHAR" property="cityName" />
    <result column="lat" jdbcType="VARCHAR" property="lat" />
    <result column="lng" jdbcType="VARCHAR" property="lng" />
    <result column="approval" jdbcType="INTEGER" property="approval" />
    <result column="approval_time" jdbcType="TIMESTAMP" property="approvalTime" />
    <result column="manager" jdbcType="BIGINT" property="manager" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="BIGINT" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
    <result column="website" jdbcType="VARCHAR" property="website" />
    <result column="business" jdbcType="VARCHAR" property="business" />
  </resultMap>

  <sql id="Base_Column_List">
    id, firm_name, code, contacts, phone, legal_name, legal_phone, head, integral,
    balance, experience, member_level, reg_time, reg_source, org_code, city_code, detail_address, 
    lat, lng, approval, approval_time, manager, remark, status, create_by, create_time, 
    update_by, update_time, del_flag,city_name, website, business
  </sql>

  <select id="selectByKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from kws_enterprise
    where id = #{id,jdbcType=BIGINT}
  </select>


  <select id="findPojo" resultType="com.sckw.system.model.pojo.FindEntListPojo" parameterType="com.sckw.system.model.pojo.FindPojoParam">
    select tab.*,
           case when tab.id in (select ent_id from kws_ent_dep) then '子单位' else '主单位' end entDept,
           case when tab.status = 0 then '正常' else '冻结' end statusName
    from
    (
          select a.id,
          min(a.firm_name) firm_name,
          min(a.code) code,
          min(a.contacts) contacts,
          min(a.phone) phone,
          min(a.legal_name) legal_name,
          min(a.legal_phone) legal_phone,
          min(a.head) head,
          min(a.integral) integral,
          min(a.balance) balance,
          min(a.experience) experience,
          min(a.member_level) member_level,
          min(a.reg_time) reg_time,
          min(a.reg_source) reg_source,
          min(a.org_code) org_code,
          min(a.city_code) city_code,
          min(a.detail_address) detail_address,
          min(a.lat) lat,
          min(a.lng) lng,
          min(a.approval) approval,
          min(a.approval_time) approval_time,
          min(a.manager) manager,
          min(a.remark) remark,
          min(a.status) status,
          min(a.create_by) create_by,
          min(a.create_time) create_time,
          min(a.update_by) update_by,
          min(a.update_time) update_time,
          min(a.del_flag) del_flag,
          ifnull(GROUP_CONCAT(distinct b.type), '') type,
          min(k.system_type) system_type,
          min(k.name) createByName,
          min(ku.name) updateByName,
          min(d.id) pid,
          min(d.firm_name) pEntName
    from kws_enterprise a
    left join kws_user k on a.create_by = k.id
    left join kws_user ku on a.update_by = ku.id
    left join kws_ent_type b on a.id = b.ent_id and b.del_flag = 0
        <if test="dto.approval == null or (dto.approval != null and dto.approval != 2)">
            and b.status != 1
        </if>
    left join kws_ent_dep c on a.id = c.ent_id
    left join kws_enterprise d on c.ent_pid = d.id
    where a.del_flag = 0
      <if test="dto.typeList != null and dto.typeList.size() > 0">
        and exists (select 1
                      from kws_ent_type kt
                     where a.id = kt.ent_id
                       and kt.del_flag = 0
                       and kt.type in
                      <foreach collection="dto.typeList" item="item" open="(" close=")" separator=",">
                          #{item}
                      </foreach>
                  )
      </if>
      <choose>
        <when test="dto.approval != null and dto.approval == 3">
          and a.approval in (3,4)
        </when>
        <when test="dto.approval != null and dto.approval != 3">
          and a.approval = #{dto.approval}
        </when>
      </choose>
      <if test="dto.customer != null and dto.customer == 1">
        and a.approval in (1, 4)
      </if>
    <if test="dto.keywords != null">
      and (a.contacts like concat('%', #{dto.keywords}, '%') or a.phone like concat('%', #{dto.keywords}, '%') or a.firm_name like concat('%', #{dto.keywords}, '%'))
    </if>
    <if test="dto.startTime != null">
      and a.reg_time >= #{dto.startTime}
    </if>
    <if test="dto.endTime != null">
      and a.reg_time &lt;= #{dto.endTime}
    </if>
    <if test="dto.approvalStartTime != null">
      and a.approval_time >= #{dto.approvalStartTime}
    </if>
    <if test="dto.approvalEndTime != null">
      and a.approval_time &lt; date_add(#{dto.approvalEndTime}, INTERVAL 1 DAY)
    </if>
    <if test="dto.status != null and dto.status != ''">
      and a.status = #{dto.status}
    </if>
    <if test="dto.entryType != null and dto.entryType == 1">
      and k.system_type = 2
    </if>
    <if test="dto.entryType != null and dto.entryType == 2">
      and k.system_type = 1
    </if>
      <if test="dto.idList != null and dto.idList.size() > 0">
        and a.id in
        <foreach collection="dto.idList" item="item" separator="," open="(" close=")">
          #{item}
        </foreach>
      </if>
    <choose>
      <when test="dto.entDept != null and dto.entDept == 0">
        and not exists (
            select 1
              from kws_ent_dep dep
              where a.id = dep.ent_id
              <if test="dto.entId != null">
                and dep.ent_pid = #{dto.entId}
              </if>
            )
      </when>
     <when test="dto.entDept != null and dto.entDept == 1">
       and exists (
           select 1 from kws_ent_dep dep
            where a.id = dep.ent_id
       <if test="dto.entId != null">
         and dep.ent_pid = #{dto.entId}
       </if>)
     </when>
    </choose>
    group by a.id
    ) tab
     where tab.id is not null
    order by tab.update_time desc
  </select>

  <select id="findList" resultType="com.sckw.system.model.vo.res.FindEntListResVo" parameterType="com.sckw.system.model.vo.req.FindListReqVo">
    select a.*,
           d.id userId,
           d.account,
           d.name
      from kws_enterprise a
    left join kws_dept b on a.id = b.ent_id and b.del_flag = 0
    left join kws_user_dept c on b.id = c.dept_id
    left join kws_user d on c.user_id = d.id and d.del_flag = 0 and d.status = 0
    where a.del_flag = 0
    <if test="approval != null">
      and a.approval = #{approval}
    </if>
      <if test="entName != null and entName != ''">
        and a.firm_name like concat('%', #{entName}, '%')
      </if>
    <if test="entType != null">
      and exists (select 1 from kws_ent_type e where a.id = e.ent_id and e.type = #{entType})
    </if>
    order by a.create_time
  </select>

    <select id="findByUserId" resultType="com.sckw.system.model.KwsEnterprise">
      select distinct a.*
        from kws_enterprise a
       inner join kws_dept b on a.id = b.ent_id
       inner join kws_user_dept c on b.id = c.dept_id
       inner join kws_user d on c.user_id = d.id
       where d.id = #{id}
         and a.del_flag = 0
         and b.del_flag = 0
         and c.del_flag = 0
         and d.del_flag = 0
         and a.status = 0
         and b.status = 0
         and c.status = 0
         and d.status = 0
    </select>

  <select id="selectByKeys" resultType="com.sckw.system.model.KwsEnterprise">
    select a.*
    from kws_enterprise a
    where del_flag = 0
      and exists( select 1 from kws_ent_check_track b where a.id = b.ent_id and b.status = 0)
    <if test="list != null and list.size() > 0">
      and id in
      <foreach collection="list" item="item" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
  </select>

  <select id="findUserByEntIds" resultType="com.sckw.system.model.pojo.FindEntUserPojo">
    select distinct a.id entId,
           a.firm_name entName,
           d.id userId,
           d.name,
           d.account,
           d.phone,
           f.name roleName
      from kws_enterprise a
      left join kws_dept b on a.id = b.ent_id
      left join kws_user_dept c on b.id = c.dept_id
      left join kws_user d on c.user_id = d.id
      left join kws_user_role e on d.id = e.user_id
      left join kws_role f on e.role_id = f.id
     where a.del_flag = 0
       and b.del_flag = 0
       and c.del_flag = 0
       and d.del_flag = 0
       and f.del_flag = 0
       and a.id in
        <foreach collection="list" separator="," open="(" close=")" item="item">
          #{item}
        </foreach>
  </select>

  <select id="findManageInfoByEntId" resultType="com.sckw.system.model.pojo.FindManagePojo">
    select distinct d.id userId,
           b.id deptId,
           f.id roleId,
           a.id entId
      from kws_enterprise a
      left join kws_dept b on a.id = b.ent_id
      left join kws_user_dept c on b.id = c.dept_id
      left join kws_user d on c.user_id = d.id
      left join kws_user_role e on d.id = e.user_id
      left join kws_role f on e.role_id = f.id
     where a.del_flag = 0
       and b.del_flag = 0
       and c.del_flag = 0
       and d.del_flag = 0
       and e.del_flag = 0
       and f.del_flag = 0
       and d.is_main = 1
       and a.id = #{entId}
  </select>

  <select id="findManageInfoByEntIdBeforeApproval" resultType="com.sckw.system.model.pojo.FindManagePojo">
    select distinct d.id userId,
                    f.id roleId
    from kws_enterprise a
           left join kws_dept b on a.id = b.ent_id
           LEFT JOIN kws_user d ON a.phone = d.account
           left join kws_role f on b.id = f.dept_id
    where a.del_flag = 0
      and b.del_flag = 0
      and d.del_flag = 0
      and f.del_flag = 0
      and d.is_main = 1
      and d.system_type = 2
      and a.id = #{entId}
  </select>

  <select id="findEnts" resultType="com.sckw.system.model.KwsEnterprise" parameterType="java.util.Map" >
    select * from kws_enterprise ke
    where ke.del_flag = 0
    <if test="firmName != null and firmName != ''">
      and ke.firm_name like concat('%', #{firmName}, '%')
    </if>
    <choose>
      <when test="entTypes != null and entTypes != '' and entTypes.size() > 0">
        and exists (select 1 from kws_ent_type ket where ket.del_flag = 0 and ke.id = ket.ent_id and ket.type in
        <foreach collection="entTypes" item="type" open="(" close=")" separator=",">
          #{type,jdbcType=BIGINT}
        </foreach>
        )
      </when>
    </choose>
  </select>

  <select id="checkEntRepeat" resultType="com.sckw.system.model.KwsEnterprise">
    select * from kws_enterprise where  (
          firm_name = #{entName}
        <if test="phone != null and phone != ''">
          or phone = #{phone}
        </if>
         )
        and del_flag = 0 and approval in (1,3,4)
  </select>

  <select id="selectAllByKeys" resultType="com.sckw.system.model.KwsEnterprise">
    select
    <include refid="Base_Column_List" />
    from kws_enterprise
    where del_flag = 0
    <if test="list != null and list.size() > 0">
      and id in
      <foreach collection="list" item="item" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
  </select>

    <select id="queryEntInfoByCityCodeAndEntTypesWithPage" resultType="com.sckw.system.model.KwsEnterprise">
      SELECT a.*
        FROM kws_enterprise a
        LEFT JOIN sys_area b ON a.city_code = b.CODE
      where 1 = 1
        <if test="cityCode != null">
          and (b.code = #{cityCode} or b.province_code = #{cityCode} or b.pcode = #{cityCode})
        </if>
        <if test="entIdList != null and entIdList.size() > 0">
          and a.id in
          <foreach collection="entIdList" item="item" open="(" close=")" separator=",">
            #{item}
          </foreach>
        </if>
        <if test="entName != null and entName != ''">
          and a.firm_name like concat('%', #{entName}, '%')
        </if>
        and exists (select 1
                      from kws_ent_type k
                     where a.id = k.ent_id
                       and k.status = 0
                       and k.del_flag = 0
                       and k.type in
                        <foreach collection="entTypeList" open="(" close=")" separator="," item="item">
                            #{item}
                         </foreach>
                    )
        and exists (
                    select 1
                      from kws_ent_check_track e
                     where a.id = e.ent_id
                       and a.status = 0
                )
    </select>

  <select id="findManageInfoByEntIds" resultType="com.sckw.system.model.pojo.FindManagePojo">
    select distinct d.id userId,
                    b.id deptId,
                    f.id roleId,
                    a.id entId
      from kws_enterprise a
             left join kws_dept b on a.id = b.ent_id
             left join kws_user_dept c on b.id = c.dept_id
             left join kws_user d on c.user_id = d.id
             left join kws_user_role e on d.id = e.user_id
             left join kws_role f on e.role_id = f.id
      where a.del_flag = 0
        and b.del_flag = 0
        and c.del_flag = 0
        and d.del_flag = 0
        and e.del_flag = 0
        and f.del_flag = 0
        and d.is_main = 1
        and d.system_type = 2
        and a.id in
      <foreach collection="list" separator="," open="(" close=")" item="item">
        #{item}
      </foreach>
  </select>

  <insert id="insert" parameterType="com.sckw.system.model.KwsEnterprise">
    insert into kws_enterprise
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="firmName != null">
        firm_name,
      </if>
      <if test="code != null">
        code,
      </if>
      <if test="contacts != null">
        contacts,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="legalName != null">
        legal_name,
      </if>
      <if test="legalPhone != null">
        legal_phone,
      </if>
      <if test="head != null">
        head,
      </if>
      <if test="integral != null">
        integral,
      </if>
      <if test="balance != null">
        balance,
      </if>
      <if test="experience != null">
        experience,
      </if>
      <if test="memberLevel != null">
        member_level,
      </if>
      <if test="regTime != null">
        reg_time,
      </if>
      <if test="regSource != null">
        reg_source,
      </if>
      <if test="orgCode != null">
        org_code,
      </if>
      <if test="cityCode != null">
        city_code,
      </if>
      <if test="detailAddress != null">
        detail_address,
      </if>
      <if test="lat != null">
        lat,
      </if>
      <if test="lng != null">
        lng,
      </if>
      <if test="approval != null">
        approval,
      </if>
      <if test="approvalTime != null">
        approval_time,
      </if>
      <if test="manager != null">
        manager,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="delFlag != null">
        del_flag,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="firmName != null">
        #{firmName,jdbcType=VARCHAR},
      </if>
      <if test="code != null">
        #{code,jdbcType=VARCHAR},
      </if>
      <if test="contacts != null">
        #{contacts,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="legalName != null">
        #{legalName,jdbcType=VARCHAR},
      </if>
      <if test="legalPhone != null">
        #{legalPhone,jdbcType=VARCHAR},
      </if>
      <if test="head != null">
        #{head,jdbcType=VARCHAR},
      </if>
      <if test="integral != null">
        #{integral,jdbcType=INTEGER},
      </if>
      <if test="balance != null">
        #{balance,jdbcType=DECIMAL},
      </if>
      <if test="experience != null">
        #{experience,jdbcType=INTEGER},
      </if>
      <if test="memberLevel != null">
        #{memberLevel,jdbcType=INTEGER},
      </if>
      <if test="regTime != null">
        #{regTime,jdbcType=TIMESTAMP},
      </if>
      <if test="regSource != null">
        #{regSource,jdbcType=VARCHAR},
      </if>
      <if test="orgCode != null">
        #{orgCode,jdbcType=VARCHAR},
      </if>
      <if test="cityCode != null">
        #{cityCode,jdbcType=INTEGER},
      </if>
      <if test="detailAddress != null">
        #{detailAddress,jdbcType=VARCHAR},
      </if>
      <if test="lat != null">
        #{lat,jdbcType=VARCHAR},
      </if>
      <if test="lng != null">
        #{lng,jdbcType=VARCHAR},
      </if>
      <if test="approval != null">
        #{approval,jdbcType=INTEGER},
      </if>
      <if test="approvalTime != null">
        #{approvalTime,jdbcType=TIMESTAMP},
      </if>
      <if test="manager != null">
        #{manager,jdbcType=BIGINT},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        #{delFlag,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>

  <update id="update" parameterType="com.sckw.system.model.KwsEnterprise">
    update kws_enterprise
    <set>
      <if test="firmName != null">
        firm_name = #{firmName,jdbcType=VARCHAR},
      </if>
      <if test="code != null">
        code = #{code,jdbcType=VARCHAR},
      </if>
      <if test="contacts != null">
        contacts = #{contacts,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="legalName != null">
        legal_name = #{legalName,jdbcType=VARCHAR},
      </if>
      <if test="legalPhone != null">
        legal_phone = #{legalPhone,jdbcType=VARCHAR},
      </if>
      <if test="head != null">
        head = #{head,jdbcType=VARCHAR},
      </if>
      <if test="integral != null">
        integral = #{integral,jdbcType=INTEGER},
      </if>
      <if test="balance != null">
        balance = #{balance,jdbcType=DECIMAL},
      </if>
      <if test="experience != null">
        experience = #{experience,jdbcType=INTEGER},
      </if>
      <if test="memberLevel != null">
        member_level = #{memberLevel,jdbcType=INTEGER},
      </if>
      <if test="regTime != null">
        reg_time = #{regTime,jdbcType=TIMESTAMP},
      </if>
      <if test="regSource != null">
        reg_source = #{regSource,jdbcType=VARCHAR},
      </if>
      <if test="orgCode != null">
        org_code = #{orgCode,jdbcType=VARCHAR},
      </if>
      <if test="cityCode != null">
        city_code = #{cityCode,jdbcType=INTEGER},
      </if>
      <if test="cityName != null">
        city_name = #{cityName,jdbcType=VARCHAR},
      </if>
      <if test="detailAddress != null">
        detail_address = #{detailAddress,jdbcType=VARCHAR},
      </if>
      <if test="lat != null">
        lat = #{lat,jdbcType=VARCHAR},
      </if>
      <if test="lng != null">
        lng = #{lng,jdbcType=VARCHAR},
      </if>
      <if test="approval != null">
        approval = #{approval,jdbcType=INTEGER},
      </if>
      <if test="approvalTime != null">
        approval_time = #{approvalTime,jdbcType=TIMESTAMP},
      </if>
      <if test="manager != null">
        manager = #{manager,jdbcType=BIGINT},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        create_by = #{createBy,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        del_flag = #{delFlag,jdbcType=INTEGER},
      </if>
      <if test="website != null">
        website = #{website,jdbcType=VARCHAR},
      </if>
      <if test="business != null">
        business = #{business,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <update id="approval">
    update kws_enterprise
       set approval = #{approval},
           approval_time = now(),
           remark = #{remark}
     where id = #{id,jdbcType=BIGINT}
       and del_flag = 0
  </update>

</mapper>