<?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.ProductCategoryMapper">
  <resultMap id="BaseResultMap" type="com.zcxk.rmcp.core.entity.ProductCategory">
    <!--@mbg.generated-->
    <!--@Table rmcp_product_category-->
    <id column="id" property="id" />
    <result column="name" property="name" />
    <result column="icon" property="icon" />
    <result column="status" property="status" />
    <result column="create_date" property="createDate" />
    <result column="create_by" property="createBy" />
    <result column="update_date" property="updateDate" />
    <result column="update_by" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, `name`, icon, `status`, create_date, create_by, update_date, update_by
  </sql>

  <sql id="permissionCondition">
    <if test="userCondition.tenantId != null">and rd.tenant_id = #{userCondition.tenantId}</if>
    <if test="userCondition.userType!=null and userCondition.userType!=-999 and userCondition.userType!=-9999 and  userCondition.programItems != null and userCondition.programItems.size() > 0">
      <if test="userCondition.permissionType == 5 or userCondition.permissionType == 2">
        and ( rd.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        or
        rd.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        )
      </if>
      <if test="userCondition.permissionType == 4 or userCondition.permissionType == 3">
        and rd.dept_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
      </if>
      <if test="userCondition.permissionType == 1">
        and rd.company_org_id in
        <foreach collection="userCondition.programItems" item="item" open="(" close=")" separator=",">
          #{item.orgId}
        </foreach>
        and (rd.dept_org_id is null or rd.dept_org_id =0)
      </if>
    </if>
  </sql>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zcxk.rmcp.core.entity.ProductCategory" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into rmcp_product_category
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="name != null">
        `name`,
      </if>
      <if test="icon != null">
        icon,
      </if>
      <if test="status != null">
        `status`,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateDate != null">
        update_date,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="name != null">
        #{name},
      </if>
      <if test="icon != null">
        #{icon},
      </if>
      <if test="status != null">
        #{status},
      </if>
      <if test="createDate != null">
        #{createDate},
      </if>
      <if test="createBy != null">
        #{createBy},
      </if>
      <if test="updateDate != null">
        #{updateDate},
      </if>
      <if test="updateBy != null">
        #{updateBy},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zcxk.rmcp.core.entity.ProductCategory">
    <!--@mbg.generated-->
    update rmcp_product_category
    <set>
      <if test="name != null">
        `name` = #{name},
      </if>
      <if test="icon != null">
        icon = #{icon},
      </if>
      <if test="status != null">
        `status` = #{status},
      </if>
      <if test="createDate != null">
        create_date = #{createDate},
      </if>
      <if test="createBy != null">
        create_by = #{createBy},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy},
      </if>
    </set>
    where id = #{id}
  </update>

  <select id="findById" resultMap="BaseResultMap">
    select <include refid="Base_Column_List"/> from rmcp_product_category
    where status = 1 and id = #{id}
  </select>

  <select id="listProductCategory" resultType="com.zcxk.rmcp.api.vo.ProductCategoryVo">
    select
    rc.id, rc.name
    from
    rmcp_device rd
    INNER JOIN rmcp_product_category rc on rd.category_id = rc.id
    where
    rd.status = 1 and rc.status = 1
    <include refid="permissionCondition"/>
    GROUP BY rc.id, rc.name
  </select>
    <select id="queryList" resultType="com.zcxk.rmcp.api.vo.ProductCategoryVo">
      select * from rmcp_product_category
    </select>
</mapper>