<?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.zcxk.rmcp.core.dao.CommunityMapper">

  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.Community">
    <id column="id" property="id" />
    <result column="tenant_id" property="tenantId" />
    <result column="company_org_id" property="companyOrgId" />
    <result column="dept_org_id" property="deptOrgId" />
    <result column="name" property="name" />
    <result column="code" property="code" />
    <result column="province" property="province" />
    <result column="city" property="city" />
    <result column="region" property="region" />
    <result column="address" property="address" />
    <result column="lng" property="lng" />
    <result column="lat" property="lat" />
    <result column="remark" property="remark" />
    <result column="status" property="status" />
    <result column="create_date" property="createDate" />
    <result column="update_date" property="updateDate" />
    <result column="create_by" property="createBy" />
    <result column="update_by" property="updateBy" />
  </resultMap>

  <sql id="Base_Column_List">
    id, tenant_id, company_org_id, dept_org_id, `name`, code, province, city, region,
    address, lng, lat, remark, `status`, create_date, update_date, create_by, update_by
  </sql>

  <sql id="permissionCondition">
    <if test="condition.userType!=null and condition.userType!=-999 and condition.userType!=-9999 and  condition.programItems != null and condition.programItems.size() > 0">
      <if test="condition.permissionType == 5 or condition.permissionType == 2">
        and ( sc.dept_org_id in
        <foreach collection="condition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        or
        sc.company_org_id in
        <foreach collection="condition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        )
      </if>
      <if test="condition.permissionType == 4 or condition.permissionType == 3">
        and sc.dept_org_id in
        <foreach collection="condition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
      </if>
      <if test="condition.permissionType == 1">
        and sc.company_org_id in
        <foreach collection="condition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        and (sc.dept_org_id is null or sc.dept_org_id =0)
      </if>
    </if>
  </sql>
  <insert id="insertCommunity" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.Community" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_community
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        tenant_id,
      </if>
      <if test="companyOrgId != null">
        company_org_id,
      </if>
      <if test="deptOrgId != null">
        dept_org_id,
      </if>
      <if test="name != null">
        `name`,
      </if>
      <if test="code != null">
        code,
      </if>
      <if test="province != null">
        province,
      </if>
      <if test="city != null">
        city,
      </if>
      <if test="region != null">
        region,
      </if>
      <if test="address != null">
        address,
      </if>
      <if test="lng != null">
        lng,
      </if>
      <if test="lat != null">
        lat,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="updateDate != null">
        update_date,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="tenantId != null">
        #{tenantId},
      </if>
      <if test="companyOrgId != null">
        #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        #{deptOrgId},
      </if>
      <if test="name != null">
        #{name},
      </if>
      <if test="code != null">
        #{code},
      </if>
      <if test="province != null">
        #{province},
      </if>
      <if test="city != null">
        #{city},
      </if>
      <if test="region != null">
        #{region},
      </if>
      <if test="address != null">
        #{address},
      </if>
      <if test="lng != null">
        #{lng},
      </if>
      <if test="lat != null">
        #{lat},
      </if>
      <if test="remark != null">
        #{remark},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="updateDate != null">
        #{createDate},
      </if>
      <if test="updateDate != null">
        #{updateDate},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.rmcp.core.entity.Community">
    update rmcp_community
    <set>
      <if test="tenantId != null">
        tenant_id = #{tenantId},
      </if>
      <if test="companyOrgId != null">
        company_org_id = #{companyOrgId},
      </if>
      <if test="deptOrgId != null">
        dept_org_id = #{deptOrgId},
      </if>
      <if test="name != null">
        `name` = #{name},
      </if>
      <if test="code != null">
        code = #{code},
      </if>
      <if test="province != null">
        province = #{province},
      </if>
      <if test="city != null">
        city = #{city},
      </if>
      <if test="region != null">
        region = #{region},
      </if>
      <if test="address != null">
        address = #{address},
      </if>
      <if test="lng != null">
        lng = #{lng},
      </if>
      <if test="lat != null">
        lat = #{lat},
      </if>
      <if test="remark != null">
        remark = #{remark},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="createDate != null">
        create_date = #{createDate},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
      <if test="createBy != null">
        create_by = #{createBy},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
    </set>
    where id = #{id}
  </update>

  <select id="findByNameUnique" resultType="java.lang.Integer">
    select count(1) from rmcp_community where status = 1 and province = #{province} and city = #{city} and region = #{region} and name = #{name}
    <if test="id != null"> and id != #{id}</if>
  </select>

  <select id="getMaxCodeBySiteId" resultType="java.lang.String">
        select  MAX(ifnull(code,0))+1 code
        from rmcp_community
        where tenant_id = #{tenantId}
  </select>
  
  <select id="findCommunity" resultType="com.zcxk.rmcp.core.entity.Community">
        select <include refid="Base_Column_List"></include>
        from rmcp_community
        where status = 1 and id = #{id}
  </select>

  <select id="listCommunity" resultType="com.zcxk.rmcp.api.vo.CommunityVo">
    select sc.id, sc.tenant_id, sc.company_org_id, sc.dept_org_id, sc.`name`, sc.code, sc.province, sc.city, sc.region,
    sc.address, sc.lng, sc.lat, sc.remark,
    sa1.name as provinceName,
    sa2.name as cityName,
    sa3.name as regionName,
    org1.org_name as companyOrgName,
    org2.org_name as deptOrgName
    from rmcp_community sc
    left join rmcp_area sa1 on sa1.id = sc.province
    left join rmcp_area sa2 on sa2.id = sc.city
    left join rmcp_area sa3 on sa3.id = sc.region
    left join rmcp_org org1 on org1.id = sc.company_org_id
    left join rmcp_org org2 on org2.id = sc.dept_org_id
    where
    sc.status = 1
    <if test="dto.tenantId != null"> and sc.tenant_id = #{dto.tenantId} </if>
    <if test="dto.name != null and dto.name != ''"> and sc.name like concat('%',#{dto.name} ,'%')</if>
    <if test="dto.companyOrgId != null"> and sc.company_org_id = #{dto.companyOrgId}</if>
    <if test="dto.deptOrgId != null"> and sc.dept_org_id = #{dto.deptOrgId}</if>
    <include refid="permissionCondition"/>
    order by sc.create_date desc
  </select>

  <select id="findByName" resultMap="BaseResultMap">
    select id,name from rmcp_community where status = 1 and tenant_id = #{tenantId} and name = #{name}
  </select>
</mapper>