<?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.ruoyi.group.mapper.CustGroupMapper">

    <select id="selectCustGroupList" resultType="CustGroupVO">
        SELECT
            cg.id,
            cg.group_name,
            cg.group_mode,
            cg.create_mode,
            cg.user_name,
            cg.nick_name,
            cg.dept_id,
            cg.share_enabled,
            cg.share_dept_ids,
            cg.group_status,
            cg.create_by,
            cg.create_time,
            cg.update_by,
            cg.update_time,
            cg.remark,
            cg.del_flag,
            (SELECT COUNT(*) FROM ibs_cust_group_member cgm WHERE cgm.group_id = cg.id AND cgm.del_flag = '0') AS cust_count
        FROM ibs_cust_group cg
        <where>
            cg.del_flag = '0'
            and create_status = '1'
            <if test="dto.groupName != null and dto.groupName != ''">
                AND cg.group_name LIKE CONCAT('%', #{dto.groupName}, '%')
            </if>
            <if test="dto.groupMode != null and dto.groupMode != ''">
                AND cg.group_mode = #{dto.groupMode}
            </if>
            <if test="dto.createMode != null and dto.createMode != ''">
                AND cg.create_mode = #{dto.createMode}
            </if>
            <if test="dto.groupStatus != null and dto.groupStatus != ''">
                AND cg.group_status = #{dto.groupStatus}
            </if>
        </where>
        ORDER BY cg.create_time DESC
    </select>

    <select id="selectCustGroupById" resultType="CustGroupVO">
        SELECT
            cg.id,
            cg.group_name,
            cg.group_mode,
            cg.create_mode,
            cg.user_name,
            cg.nick_name,
            cg.dept_id,
            cg.share_enabled,
            cg.share_dept_ids,
            cg.group_status,
            cg.valid_time,
            cg.create_by,
            cg.create_time,
            cg.update_by,
            cg.update_time,
            cg.remark,
            cg.create_status,
            cg.grid_type,
            cg.cmpm_biz_type,
            cg.grid_user_names,
            cg.region_grid_ids,
            cg.draw_grid_ids,
            (SELECT COUNT(*) FROM ibs_cust_group_member cgm WHERE cgm.group_id = cg.id AND cgm.del_flag = '0') AS cust_count
        FROM ibs_cust_group cg
        WHERE cg.id = #{id} AND cg.del_flag = '0'
    </select>

</mapper>
