<?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.gic.haoban.manage.service.dao.mapper.DepartmentMapper">
    <resultMap id="BaseResultMap" type="com.gic.haoban.manage.service.entity.TabHaobanDepartment">
        <id column="department_id" property="departmentId" jdbcType="VARCHAR"/>
        <result column="department_name" property="departmentName" jdbcType="VARCHAR"/>
        <result column="parent_department_id" property="parentDepartmentId" jdbcType="VARCHAR"/>
        <result column="related_id" property="relatedId" jdbcType="VARCHAR"/>
        <result column="related_code" property="relatedCode" jdbcType="VARCHAR"/>
        <result column="chain_id" property="chainId" jdbcType="VARCHAR"/>
        <result column="chain_name" property="chainName" jdbcType="VARCHAR"/>
        <result column="is_store" property="isStore" jdbcType="INTEGER"/>
        <result column="status_flag" property="statusFlag" jdbcType="INTEGER"/>
        <result column="recycle_flag" property="recycleFlag" jdbcType="INTEGER"/>
        <result column="gic_del_flag" property="gicDelFlag" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="wx_department_id" property="wxDepartmentId" jdbcType="VARCHAR"/>
        <result column="wx_enterprise_id" property="wxEnterpriseId" jdbcType="VARCHAR"/>
        <result column="enterprise_id" property="enterpriseId" jdbcType="VARCHAR"/>
        <result column="sort" property="sort" jdbcType="BIGINT"/>
        <result column="level" property="level" jdbcType="INTEGER"/>
        <result column="temp_flag" property="tempFlag" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        department_id, department_name, parent_department_id, related_id, related_code,chain_id, is_store,
    status_flag, recycle_flag, create_time,
    update_time,wx_department_id,sort,level,wx_enterprise_id,chain_name,enterprise_id,gic_del_flag,temp_flag
    </sql>

    <sql id="Base_Column_List_short">
        department_id, department_name, parent_department_id,wx_department_id,sort,level
    </sql>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where department_id = #{departmentId,jdbcType=VARCHAR}
        and status_flag = 1
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
        delete
        from tab_haoban_department
        where department_id = #{departmentId,jdbcType=VARCHAR}
    </delete>
    <insert id="insert" parameterType="com.gic.haoban.manage.service.entity.TabHaobanDepartment">
        insert into tab_haoban_department (department_id, department_name, parent_department_id,
                                           related_id, related_code, chain_id, is_store,
                                           status_flag, recycle_flag, create_time,
                                           update_time, wx_department_id, sort, level, wx_enterprise_id, chain_name,
                                           enterprise_id, temp_flag)
        values (#{departmentId,jdbcType=VARCHAR}, #{departmentName,jdbcType=VARCHAR},
                #{parentDepartmentId,jdbcType=VARCHAR},
                #{relatedId,jdbcType=VARCHAR}, #{relatedCode,jdbcType=VARCHAR}, #{chainId,jdbcType=VARCHAR},
                #{isStore,jdbcType=INTEGER},
                #{statusFlag,jdbcType=INTEGER}, #{recycleFlag,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP},
                #{updateTime,jdbcType=TIMESTAMP}, #{wxDepartmentId}, #{sort}, #{level}, #{wxEnterpriseId}, #{chainName},
                #{enterpriseId}, #{tempFlag})
    </insert>
    <insert id="insertSelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanDepartment">
        insert into tab_haoban_department
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="departmentId != null">
                department_id,
            </if>
            <if test="departmentName != null">
                department_name,
            </if>
            <if test="parentDepartmentId != null">
                parent_department_id,
            </if>
            <if test="relatedId != null">
                related_id,
            </if>
            <if test="relatedCode != null">
                related_code,
            </if>
            <if test="chainId != null">
                chain_id,
            </if>
            <if test="chainName != null">
                chain_name,
            </if>
            <if test="isStore != null">
                is_store,
            </if>
            <if test="statusFlag != null">
                status_flag,
            </if>
            <if test="recycleFlag != null">
                recycle_flag,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="wxDepartmentId != null">
                wx_department_id,
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id,
            </if>
            <if test="enterpriseId != null">
                enterprise_id,
            </if>
            <if test="sort != null">
                sort,
            </if>
            <if test="level != null">
                level,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="departmentId != null">
                #{departmentId,jdbcType=VARCHAR},
            </if>
            <if test="departmentName != null">
                #{departmentName,jdbcType=VARCHAR},
            </if>
            <if test="parentDepartmentId != null">
                #{parentDepartmentId,jdbcType=VARCHAR},
            </if>
            <if test="relatedId != null">
                #{relatedId,jdbcType=VARCHAR},
            </if>
            <if test="relatedCode != null">
                #{relatedCode,jdbcType=VARCHAR},
            </if>
            <if test="chainId != null">
                #{chainId,jdbcType=VARCHAR},
            </if>
            <if test="chainName != null">
                #{chainName,jdbcType=VARCHAR},
            </if>
            <if test="isStore != null">
                #{isStore,jdbcType=INTEGER},
            </if>
            <if test="statusFlag != null">
                #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="recycleFlag != null">
                #{recycleFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="wxDepartmentId != null">
                #{wxDepartmentId},
            </if>
            <if test="wxEnterpriseId != null">
                #{wxEnterpriseId},
            </if>
            <if test="enterpriseId != null">
                #{enterpriseId},
            </if>
            <if test="sort != null">
                #{sort},
            </if>
            <if test="level != null">
                #{level},
            </if>
        </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.gic.haoban.manage.service.entity.TabHaobanDepartment">
        update tab_haoban_department
        <set>
            <if test="departmentName != null">
                department_name = #{departmentName,jdbcType=VARCHAR},
            </if>
            <if test="parentDepartmentId != null">
                parent_department_id = #{parentDepartmentId,jdbcType=VARCHAR},
            </if>
            <if test="relatedId != null">
                related_id = #{relatedId,jdbcType=VARCHAR},
            </if>
            <if test="relatedCode != null">
                related_code = #{relatedCode,jdbcType=VARCHAR},
            </if>
            <if test="chainId != null">
                chain_id = #{chainId,jdbcType=VARCHAR},
            </if>
            <if test="chainName != null">
                chain_name = #{chainName,jdbcType=VARCHAR},
            </if>
            <if test="isStore != null">
                is_store = #{isStore,jdbcType=INTEGER},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag,jdbcType=INTEGER},
            </if>
            <if test="recycleFlag != null">
                recycle_flag = #{recycleFlag,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="wxDepartmentId != null">
                wx_department_id = #{wxDepartmentId,jdbcType=VARCHAR},
            </if>
            <if test="wxEnterpriseId != null">
                wx_enterprise_id = #{wxEnterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="enterpriseId != null">
                enterprise_id = #{enterpriseId,jdbcType=VARCHAR},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="level != null">
                level = #{level,jdbcType=INTEGER},
            </if>
            <if test="gicDelFlag != null">
                gic_del_flag = #{gicDelFlag,jdbcType=INTEGER},
            </if>
        </set>
        where department_id = #{departmentId,jdbcType=VARCHAR}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.gic.haoban.manage.service.entity.TabHaobanDepartment">
        update tab_haoban_department
        set department_name      = #{departmentName,jdbcType=VARCHAR},
            parent_department_id = #{parentDepartmentId,jdbcType=VARCHAR},
            related_id           = #{relatedId,jdbcType=VARCHAR},
            related_code         = #{relatedCode,jdbcType=VARCHAR},
            chain_id             = #{chainId,jdbcType=VARCHAR},
            chain_name           = #{chainName,jdbcType=VARCHAR},
            wx_department_id     = #{wxDepartmentId,jdbcType=VARCHAR},
            is_store             = #{isStore,jdbcType=INTEGER},
            status_flag          = #{statusFlag,jdbcType=INTEGER},
            sort                 = #{sort,jdbcType=INTEGER},
            level                = #{level,jdbcType=INTEGER},
            recycle_flag         = #{recycleFlag,jdbcType=INTEGER},
            create_time          = #{createTime,jdbcType=TIMESTAMP},
            update_time          = #{updateTime,jdbcType=TIMESTAMP},
            gic_del_flag         = #{gicDelFlag},
            wx_enterprise_id     = #{wxEnterpriseId},
            enterprise_id        = #{enterpriseId}
        where department_id = #{departmentId,jdbcType=VARCHAR}
    </update>

    <select id="listByParentId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where parent_department_id = #{parentId,jdbcType=VARCHAR}
        and status_flag = 1
        and recycle_flag != 1
        order by sort desc
    </select>

    <select id="listDepartmentLikeName" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where department_name like CONCAT('%',#{departmentName,jdbcType=VARCHAR},'%')
        and status_flag = 1
    </select>

    <select id="selectMaxSort" resultType="java.lang.Long" parameterType="java.lang.String">
        select ifnull(max(sort), 0)
        from tab_haoban_department
        where parent_department_id = #{parentDepartmentId,jdbcType=VARCHAR}
          and status_flag = 1
    </select>

    <select id="selectByRelatedId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where related_id = #{relatedId,jdbcType=VARCHAR}
        and status_flag = 1
    </select>

    <select id="getByWxId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where wx_department_id = #{wxDepartmentId,jdbcType=VARCHAR}
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1
    </select>

    <select id="pageDepartmentByParams" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where status_flag = 1
        <if test="wxEnterpriseId != null and wxEnterpriseId != ''">
            and wx_enterprise_id = #{wxEnterpriseId}
        </if>
        <if test="keyword != null and keyword != ''">
            and (department_name like CONCAT('%',#{keyword},'%') or wx_department_id like CONCAT('%',#{keyword},'%') or
            related_code like CONCAT('%',#{keyword},'%'))
        </if>
        <if test="recycleFlag != null">
            and recycle_flag = #{recycleFlag}
        </if>
        <if test="storeFlag != null">
            and is_store = #{storeFlag}
        </if>
        order by sort desc
    </select>

    <select id="listByDepartmentIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where status_flag = 1
        <if test="storeFlag != null">
            and is_store = #{storeFlag}
        </if>
        <if test="departmentIds != null and departmentIds.size() > 0">
            and department_id IN
            <foreach collection="departmentIds" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
        order by sort desc
    </select>
    <select id="listByRelatedIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where status_flag = 1
        and is_store = 1
        <if test="relatedIds != null and relatedIds.size() > 0">
            and related_id IN
            <foreach collection="relatedIds" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
        order by sort desc
    </select>

<!--    <select id="listStoreByWxEnterpriseId" resultMap="BaseResultMap">-->
<!--        select-->
<!--        <include refid="Base_Column_List"/>-->
<!--        from tab_haoban_department-->
<!--        where status_flag = 1-->
<!--        and is_store = 1-->
<!--        and wx_enterprise_id = #{wxEnterpriseId}-->
<!--        order by create_time-->
<!--    </select>-->

    <select id="getRootByEnterpriseId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where level = 1
        and wx_enterprise_id = #{wxEnterpriseId}
        and status_flag = 1
    </select>
    <select id="pageFullStoreByWxEnterpriseId" resultMap="BaseResultMap">
        SELECT
        t.*
        FROM
        (
        select
        @rownum := @rownum + 1 AS rownum,
        <include refid="Base_Column_List"/>
        from tab_haoban_department,
        (SELECT(@rowNum := 0)) b
        where status_flag = 1
        and is_store = 1
        and wx_enterprise_id = #{wxEnterpriseId}
        <if test="search != null and search != ''">
            and wx_department_id like CONCAT('%',#{search},'%')
        </if>
        <if test="storeIds != null and storeIds.size != 0">
            and related_id IN
            <foreach collection="storeIds" item="id" index="index" open="(" close=")" separator=",">
                #{id,jdbcType=VARCHAR}
            </foreach>
        </if>
        order by create_time
        ) t WHERE t.rownum > #{minCount}
    </select>
    <select id="pageFullStoreByWxEnterpriseId2" resultMap="BaseResultMap">
        SELECT
        t.*
        FROM
        (
        select
        @rownum := @rownum + 1 AS rownum,
        <include refid="Base_Column_List"/>
        from tab_haoban_department,
        (SELECT(@rowNum := 0)) b
        where status_flag = 1
        and is_store = 1
        and wx_enterprise_id = #{wxEnterpriseId}
        and ( wx_department_id like CONCAT('%',#{search},'%')
        or related_id IN
        <foreach collection="storeIds" item="id" index="index" open="(" close=")" separator=",">
            #{id,jdbcType=VARCHAR}
        </foreach>
        )
        order by create_time
        ) t WHERE t.rownum > #{minCount}
    </select>


    <select id="pageByParentId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where status_flag = 1
        and recycle_flag != 1
        <if test="parentId != null and parentId != ''">
            and parent_department_id = #{parentId}
        </if>
        <if test="keyword != null and keyword != ''">
            and (department_name like CONCAT('%',#{keyword},'%') or wx_department_id like CONCAT('%',#{keyword},'%'))
        </if>
        order by sort desc
    </select>


    <select id="listByChainId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where wx_enterprise_id = #{wxEnterpriseId}
        and chain_id like CONCAT('%', #{chainId},'%')
        and status_flag = 1
    </select>

    <select id="departmentRecycleCount" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(1)
        from tab_haoban_department
        where wx_enterprise_id = #{wxEnterpriseId}
          and status_flag = 1
          and recycle_flag = 1
    </select>

    <select id="countSyncDepartmentByEnterpriseId" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(1)
        from tab_haoban_department
        where enterprise_id = #{enterpriseId}
          and status_flag = 1
          and gic_del_flag is null
    </select>

    <select id="countByDepartment" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(1)
        from tab_haoban_department
        where parent_department_id = #{departmentId}
          and status_flag = 1
          and recycle_flag != 1
    </select>



    <select id="totalStoreCountByEnterpriseId" resultType="java.lang.Integer" parameterType="java.lang.String">
        select count(1)
        from tab_haoban_department
        where wx_enterprise_id = #{wxEnterpriseId}
          and status_flag = 1
          and recycle_flag != 1
          and is_store = 1
    </select>

    <update id="cleanDepartment">
        update tab_haoban_department
        set
        status_flag = 0,
        update_time=now()
        where wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1 and
        wx_department_id not in
        <foreach collection="dIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    <select id="listDepartmentByWxDepartIds" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_department
        where wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1 and
        wx_department_id in
        <foreach collection="dIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="listAllDepartment" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List_short"/>
        from tab_haoban_department
        where wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1
        order by sort desc
    </select>

    <select id="searchDepartment" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List_short"/>
        from tab_haoban_department
        where wx_enterprise_id = #{wxEnterpriseId} and status_flag = 1
        and department_name likelike CONCAT('%',#{search},'%')

    </select>
</mapper>