<?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="cn.ey.biz.module.system.dal.mapper.category.CategoryMapper">

    <!-- 自定义ORM映射规则 -->
    <resultMap id="CategoryDO" type="cn.ey.biz.module.system.controller.admin.category.vo.CategoryRespVO">
        <result column="category_id" property="id"/>
        <result column="category_type" property="type"/>
        <result column="category_code" property="code"/>
        <result column="category_name" property="name"/>
        <result column="sys_flag" property="sysFlag"/>
        <result column="category_status" property="status"/>
        <result column="category_remark" property="remark"/>
        <result column="update_time" property="updateTime"/>
        <!-- 当关联属性为集合时用collection标签来描述关联属性
             ofType是关联属性集合中装的实体类的全类名 -->
        <collection property="apps" column="{categoryCode = category_code}"
                    ofType="cn.ey.biz.module.system.controller.admin.category.vo.CategoryAppRespVO" select="selectApps">
            <result property="categoryId" column="category_id"/>
            <result property="app" column="app"/>
        </collection>
    </resultMap>
    <select id="selectPage" resultMap="CategoryDO">
        SELECT
        DISTINCT
        sc.category_id,
        sc.category_type,
        sc.category_code,
        sc.category_name,
        sc.sys_flag,
        sc.category_status,
        sc.category_remark,
        sc.category_theme,
        sc.store_type,
        sc.store_table_name,
        sc.owner_app,
        sc.category_version,
        sc.is_modify,
        sc.is_new_version,
        sc.is_version_control,
        sc.create_time,
        sc.update_time
        FROM
        system_category sc
        LEFT JOIN system_category_app sca on  sca.category_id = sc.category_code
        where
        sc.deleted = 0
        <if test="c.type != null and c.type != ''">
            and sc.category_type=#{c.type}
        </if>
        <if test="c.sysFlag != null and c.sysFlag != ''">
            and sc.sys_flag=#{c.sysFlag}
        </if>
        <if test="c.code != null and c.code != ''">
            and sc.category_code like concat('%', #{c.code},'%')
        </if>
        <if test="c.name != null and c.name != ''">
            and sc.category_name like concat('%', #{c.name},'%')
        </if>
        <if test="c.ownerApp != null and c.ownerApp != ''">
            and sc.owner_app =  #{c.ownerApp}
        </if>
        <if test="c.app != null and c.app != ''">
            and sca.app in
            <foreach collection="c.app" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="selectApps" resultType="cn.ey.biz.module.system.controller.admin.category.vo.CategoryAppRespVO">
        SELECT sca.category_id,
               sca.app
        FROM system_category_app sca
        where sca.category_id = #{categoryCode}
          AND sca.deleted = 0
    </select>


    <select id="selectPageCategoryDataDynamic" resultType="java.util.HashMap">
        <choose>
            <when test="isVersionControl != null and isVersionControl== 0 ">
                SELECT T1.* FROM ${tableName} T1 WHERE T1.deleted = 0

            </when>
            <otherwise>
                SELECT T1.* FROM system_category T0
                JOIN ${tableName} T1 ON T0.category_code = T1.category_code
                AND T0.category_version = T1.category_version
                AND T1.deleted = 0
                AND T1.new_version = 1
                WHERE T0.deleted = 0
                AND T0.category_status = 0
                AND T0.is_new_version = 1
            </otherwise>
        </choose>
        <if test="queryParams !=null ">
            <foreach collection="queryParams.entrySet()" item="value" index="key">
                AND T1.${key} = #{value}
            </foreach>
        </if>

    </select>

    <select id="listCategoryCodeExtraInfo"
            resultType="cn.ey.biz.module.system.api.category.dto.CategoryCodeExtraRespDTO">
        SELECT sc.category_id,
               sc.category_type,
               sc.category_code,
               sc.category_name,
               sc.sys_flag,
               sc.category_status as `status`,
               sc.category_remark as `remark`,
               sc.category_theme,
               sc.store_type,
               sc.store_table_name,
               sc.owner_app,
               sc.category_version,
               sc.is_modify,
               sc.is_new_version,
               sc.is_version_control,
               sc.create_time,
               sc.update_time,
               sce.need_sync,
               sce.is_full_sync,
               sce.biz_key
        FROM system_category sc
                 LEFT JOIN dict_category_code_extra sce ON sce.category_code = sc.category_code
            and sce.deleted = 0 and sce.status = 0
        WHERE sc.deleted = 0
          and sc.category_status = 0;
    </select>

    <update id="deleteFullDataDynamic">
        update ${tableName} T1 set T1.deleted = 1,T1.update_time = now() where T1.deleted = 0;
    </update>
    
    <insert id="insertDataDynamic" parameterType="java.util.HashMap" >
        <foreach collection="list" item="line" separator=";">
            insert into
            ${tableName}
            (
            <foreach collection="line" index="key" item="value"  separator=",">
                ${key}
            </foreach>
            ) values (
            <foreach collection="line" index="key" item="value"  separator=",">
                #{value}
            </foreach>
            )
        </foreach>
    </insert>


    <update id="updateDataDynamic" parameterType="java.util.HashMap">
        update  ${tableName}  T1
        set
        <foreach collection="row" item="value" index="key" separator=",">
          T1.${key}=#{value}
        </foreach>
        where
        T1.status = 0
        <foreach collection="bizKey" item="value" index="key">
          AND T1.${key}=#{value}
        </foreach>

    </update>

</mapper>