EntmicroDepartmentMapper.xml 9.76 KB
<?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.com.glsx.entmicro.mapper.EntmicroDepartmentMapper">
    <resultMap id="BaseResultMap" type="cn.com.glsx.entmicro.entity.EntmicroDepartment">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="department_name" jdbcType="VARCHAR" property="departmentName"/>
        <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
        <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="merchantId" jdbcType="VARCHAR" property="merchantid"/>
    </resultMap>

    <resultMap id="resultMap" type="cn.com.glsx.entmicro.model.response.EntmicroDepartmentResponse">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="department_name" jdbcType="VARCHAR" property="departmentName"/>
        <result column="created_by" jdbcType="INTEGER" property="createdBy"/>
        <result column="created_time" jdbcType="TIMESTAMP" property="createdTime"/>
        <result column="updated_by" jdbcType="INTEGER" property="updatedBy"/>
        <result column="updated_time" jdbcType="TIMESTAMP" property="updatedTime"/>
        <result column="del_flag" jdbcType="TINYINT" property="delFlag"/>
        <result column="merchantId" jdbcType="VARCHAR" property="merchantId"/>
        <result column="department_Id" jdbcType="VARCHAR" property="departmentId"/>
        <result column="entmicro_parent_id" jdbcType="INTEGER" property="entmicroParentId"/>
        <result column="relevance_merchant" jdbcType="INTEGER" property="relevanceMerchant"/>
        <result column="childrenNum" jdbcType="INTEGER" property="childrenNum"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--
          WARNING - @mbg.generated
        -->
        id, department_name, created_by, created_time, updated_by, updated_time, del_flag,
        merchantId, department_Id, entmicro_parent_id, relevance_merchant
    </sql>

    <update id="updateEntmicroDepartment"
            parameterType="cn.com.glsx.entmicro.model.request.EntmicroDepartmentRequest">
        update entmicro_department
        set
        <if test="entmicroDepartmentRequest.departmentName != null and entmicroDepartmentRequest.departmentName != null">
            department_name = #{entmicroDepartmentRequest.departmentName},
        </if>
        updated_time = #{entmicroDepartmentRequest.updatedTime},
        <if test="entmicroDepartmentRequest.entmicroParentId != null">
            entmicro_parent_id = #{entmicroDepartmentRequest.entmicroParentId},
        </if>
        updated_by = #{entmicroDepartmentRequest.updatedBy}
        where id=#{entmicroDepartmentRequest.id}
    </update>

    <update id="deleteEntmicroDepartment"
            parameterType="cn.com.glsx.entmicro.model.request.EntmicroDepartmentRequest">
    update entmicro_department
    set del_flag = #{entmicroDepartmentRequest.delFlag}
    where department_id = #{entmicroDepartmentRequest.departmentId}
    and merchantId = #{entmicroDepartmentRequest.merchantId}
  </update>

    <update id="updateMerchantBind">
        update entmicro_department
        set
        merchantId = #{merchantNo},
        relevance_merchant = #{merchantNo}
        where id in
        <foreach collection="list" open="(" separator="," close=")" item="id">
            #{id}
        </foreach>
    </update>

    <select id="selectByDepartmentId"
            resultMap="resultMap">
        SELECT * from entmicro_department where id in (select sub_id from  entmicro_department_path  where superior_id =(SELECT min(id) from entmicro_department where relevance_merchant =#{merchantId} and del_flag = 0)) and del_flag = 0
  </select>

    <select id="selectByEntmicroDepartmentId" resultMap="resultMap">
        select
        <include refid="Base_Column_List"/>
        from entmicro_department
        where department_id = #{departmentId}
        <if test="merchantId != null">
            and merchantId in
            (SELECT merchantId from entmicro_department where id in(select sub_id from entmicro_department_path where
            superior_id=(select min(superior_id) from entmicro_department_path where sub_id =(SELECT min(id) from
            entmicro_department where relevance_merchant =#{merchantId} and del_flag = 0))))
        </if>
        and del_flag = 0
    </select>

    <select id="selectSubDepartmentById" resultMap="resultMap">
        select
        <include refid="Base_Column_List"/>
        from entmicro_department
        where relevance_merchant = #{merchantId}
        and del_flag = 0
    </select>

    <select id="findSupMerchantNo" resultType="java.lang.Integer">
         select merchantId from entmicro_department where id=(select min(superior_id) from entmicro_department_path  where sub_id =(SELECT min(id) from entmicro_department where relevance_merchant =#{merchantId} and del_flag = 0))
    </select>

    <select id="findAuthorizeMerchantNo" resultType="java.lang.Integer">
           select relevance_merchant from entmicro_department where id in (select sub_id  from entmicro_department_path  where superior_id =(SELECT min(id) from entmicro_department where relevance_merchant =#{merchantId} and del_flag = 0))
          and relevance_merchant is not null  group by relevance_merchant
    </select>

    <select id="findAuthorizeDepartment" resultMap="resultMap">
           select department_Id,relevance_merchant from entmicro_department where id in (select sub_id  from entmicro_department_path  where superior_id =(SELECT min(id) from entmicro_department where relevance_merchant =#{merchantId} and del_flag = 0))
          and relevance_merchant is not null  group by relevance_merchant
    </select>

    <select id="getDepartmentNameByStaffId" resultType="cn.com.glsx.entmicro.model.response.EntmicroStaffResponse">
        SELECT d.staff_id AS staffId,
        es.staff_name AS staffName,
        p.department_name AS departmentName,
        es.`thumb_avatar` AS thumbAvatar
        FROM entmicro_staff_depart_relation d
        LEFT JOIN entmicro_department p ON d.department_id = p.id
        LEFT JOIN entmicro_staff es ON es.id = d.staff_id
        WHERE d.del_flag = 0
        AND d.staff_id IN
        <foreach collection="staffIds" open="(" separator="," close=")" item="id">
            #{id}
        </foreach>
        AND d.relevance_merchant = #{merchantId}
        GROUP BY d.staff_id
    </select>

    <select id="findSubChildDepartment" resultMap="resultMap">
        SELECT
        <include refid="Base_Column_List"/>
        from entmicro_department
        where entmicro_parent_id = #{entmicroParentId}
        and department_name = #{departmentName}
        and del_flag=0
        and merchantId in
        (SELECT merchantId from entmicro_department where id in(select sub_id from entmicro_department_path where
        superior_id=(select min(superior_id) from entmicro_department_path where sub_id =(SELECT min(id) from
        entmicro_department where relevance_merchant = #{merchantId} and del_flag = 0))))
    </select>
    <select id="findIdByDepartmentId" resultType="java.lang.Integer">
        select id from entmicro_department where department_id=#{departmentId} and created_by=#{merchantId} and del_flag=0 limit 1
    </select>

    <!--查询当前关联商户的最小部门-->
    <select id="findMinDepartment" resultType="integer">
        SELECT MIN(id) FROM entmicro_department WHERE relevance_merchant = #{merchantId} AND del_flag = 0
    </select>
    <select id="findSubDepartmentBySupId"
            resultMap="resultMap">
        SELECT
            ed.*,count(edp.sub_id) as childrenNum
        FROM
            entmicro_department ed LEFT JOIN entmicro_department_path edp ON edp.superior_id = ed.id AND edp.superior_id != edp.sub_id AND edp.del_flag = 0
        WHERE
            ed.id IN (
            SELECT
            sub_id
            FROM
            entmicro_department_path
            WHERE
           del_flag = 0
        <choose>
        <when test="departmentId != null">
        and superior_id =#{departmentId}
        </when>
        <when test="departmentId ==null || departmentId == ''">
        and superior_id =
            ( SELECT min( id ) FROM entmicro_department WHERE merchantId = #{merchantId} AND del_flag = 0 )
        </when>
        </choose>
          AND depth =1
            )
          AND ed.del_flag = 0
        GROUP BY ed.id
    </select>
    <select id="findDepartmentListByPage"
            resultMap="resultMap">
        SELECT
            dep.*,
            dep1.department_name as entmicroParentName,
            dep1.department_id as entmicroParentId
        FROM
            entmicro_department dep
                LEFT JOIN entmicro_department dep1 ON dep.entmicro_parent_id = dep1.department_id
                AND dep1.id IN (SELECT sub_id FROM entmicro_department_path WHERE superior_id = ( SELECT MIN( id ) FROM entmicro_department WHERE merchantId = #{condition.merchantId} AND del_flag = 0 ))
        WHERE
            dep.del_flag = 0
          AND dep.id IN (SELECT sub_id FROM entmicro_department_path WHERE superior_id = ( SELECT MIN( id ) FROM entmicro_department WHERE merchantId = #{condition.merchantId} AND del_flag = 0 ))
          AND ISNULL(dep.relevance_merchant)
          <if test="condition.departmentName != null and condition.departmentName != ''">
              AND dep.department_name LIKE concat('%',#{condition.departmentName},'%')
          </if>
    </select>


</mapper>