<?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.ibs.grid.mapper.SysGridUserCustMapper">

    <resultMap type="SysGridUserCust" id="SysGridUserCustResult">
        <result property="id"    column="id"    />
        <result property="deptBranchId"    column="dept_branch_id"    />
        <result property="deptOutletId"    column="dept_outlet_id"    />
        <result property="custType"    column="cust_type"    />
        <result property="custId"    column="cust_id"    />
        <result property="custName"    column="cust_name"    />
        <result property="regionCode"    column="region_code"    />
        <result property="gridType"    column="grid_type"    />
        <result property="gridId"    column="grid_id"    />
        <result property="parentGridId"    column="parent_grid_id"    />
        <result property="userName"    column="user_name"    />
        <result property="relationStatus"    column="relation_status"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateDept"    column="update_dept"    />
    </resultMap>

    <resultMap type="CustManageSelectDTO" id="CustManageSelectDTOResult">
        <result property="custId"    column="cust_id"    />
        <result property="custName"    column="cust_name"    />
        <result property="userId"    column="user_name"    />
        <result property="userName"    column="nick_name"    />
        <result property="outletId"    column="dept_branch_id"    />
        <result property="branchId"    column="dept_outlet_id"    />
        <result property="custType"    column="cust_type"    />
    </resultMap>

    <sql id="selectSysGridUserCustVo">
        select id, dept_branch_id, dept_outlet_id, cust_type, cust_id, cust_name, region_code, grid_type, grid_id, parent_grid_id, user_name, relation_status, update_by, update_time, update_dept from sys_grid_user_cust
    </sql>

    <select id="selectSysGridUserCustList" parameterType="SysGridUserCust" resultMap="SysGridUserCustResult">
        <include refid="selectSysGridUserCustVo"/>
        <where>  
            <if test="deptBranchId != null  and deptBranchId != ''"> and dept_branch_id = #{deptBranchId}</if>
            <if test="deptOutletId != null  and deptOutletId != ''"> and dept_outlet_id = #{deptOutletId}</if>
            <if test="custId != null  and custId != ''"> and cust_id = #{custId}</if>
            <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%')</if>
            <if test="regionCode != null  and regionCode != ''"> and region_code = #{regionCode}</if>
            <if test="gridType != null  and gridType != ''"> and grid_type = #{gridType}</if>
            <if test="gridTopId != null  and gridTopId != ''"> and grid_top_id = #{gridTopId}</if>
            <if test="gridSecId != null  and gridSecId != ''"> and grid_sec_id = #{gridSecId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="relationStatus != null  and relationStatus != ''"> and relation_status = #{relationStatus}</if>
            <if test="updateDept != null  and updateDept != ''"> and update_dept = #{updateDept}</if>
        </where>
    </select>
    
    <select id="selectSysGridUserCustById" parameterType="Long" resultMap="SysGridUserCustResult">
        <include refid="selectSysGridUserCustVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysGridUserCust" parameterType="SysGridUserCust" useGeneratedKeys="true" keyProperty="id">
        insert into sys_grid_user_cust
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptBranchId != null">dept_branch_id,</if>
            <if test="deptOutletId != null">dept_outlet_id,</if>
            <if test="custId != null">cust_id,</if>
            <if test="custName != null">cust_name,</if>
            <if test="regionCode != null">region_code,</if>
            <if test="gridType != null">grid_type,</if>
            <if test="gridTopId != null">grid_top_id,</if>
            <if test="gridSecId != null">grid_sec_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="relationStatus != null">relation_status,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateDept != null">update_dept,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptBranchId != null">#{deptBranchId},</if>
            <if test="deptOutletId != null">#{deptOutletId},</if>
            <if test="custId != null">#{custId},</if>
            <if test="custName != null">#{custName},</if>
            <if test="regionCode != null">#{regionCode},</if>
            <if test="gridType != null">#{gridType},</if>
            <if test="gridTopId != null">#{gridTopId},</if>
            <if test="gridSecId != null">#{gridSecId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="relationStatus != null">#{relationStatus},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateDept != null">#{updateDept},</if>
         </trim>
    </insert>

    <update id="updateSysGridUserCust" parameterType="SysGridUserCust">
        update sys_grid_user_cust
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptBranchId != null">dept_branch_id = #{deptBranchId},</if>
            <if test="deptOutletId != null">dept_outlet_id = #{deptOutletId},</if>
            <if test="custId != null">cust_id = #{custId},</if>
            <if test="custName != null">cust_name = #{custName},</if>
            <if test="regionCode != null">region_code = #{regionCode},</if>
            <if test="gridType != null">grid_type = #{gridType},</if>
            <if test="gridTopId != null">grid_top_id = #{gridTopId},</if>
            <if test="gridSecId != null">grid_sec_id = #{gridSecId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="relationStatus != null">relation_status = #{relationStatus},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateDept != null">update_dept = #{updateDept},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysGridUserCustById" parameterType="Long">
        delete from sys_grid_user_cust where id = #{id}
    </delete>

    <delete id="deleteSysGridUserCustByIds" parameterType="String">
        delete from sys_grid_user_cust where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectOutletNameByCustId" parameterType="String">
        select group_concat(distinct b.dept_name) from sys_grid_user_cust a , sys_dept b
        where a.dept_outlet_id = b.dept_id and a.cust_id =#{custId} and a.dept_branch_id =#{deptBranchId} and a.relation_status = '0'
    </select>

    <select id="selectManageByCust" parameterType="String">
        select group_concat(distinct b.nick_name) from sys_grid_user_cust a , sys_user b
        where a.user_name = b.user_name and a.cust_id =#{custId} and a.relation_status = '0'
        <if test="deptBranchId != null  and deptBranchId != ''"> and dept_branch_id = #{deptBranchId}</if>
    </select>

    <select id="selectCustInfoByManage" parameterType="CustManageInfo">
        select * from sys_grid_user_cust
        where (grid_id = #{gridId} or parent_grid_id = #{gridId}) and grid_type =#{gridPattern}
        <if test="custPattern != null  and custPattern != ''"> and cust_type = #{custPattern}</if>
        <if test="custNo != null  and custNo != ''"> and cust_id like concat('%', #{custNo}, '%')</if>
        <if test="custName != null  and custName != ''"> and cust_name like concat('%', #{custName}, '%')</if>
        <if test="distributeTag != null  and distributeTag != ''"> and
            CASE WHEN #{distributeTag} = '0' THEN relation_status = '0' ELSE relation_status in('1','2') END
        </if>
        group by dept_branch_id,cust_id
    </select>

    <select id="updateRetailManage" parameterType="String">
        update sys_grid_user_cust set relation_status = #{operationType}
        where dept_branch_id = #{belongUserId} and cust_id = #{custId} and grid_type = #{gridPattern}
    </select>

    <select id="selectManageInfoByCust"  resultMap="CustManageSelectDTOResult">
        select a.cust_id,a.cust_name,a.user_name,b.nick_name,a.dept_branch_id,a.dept_outlet_id,a.cust_type from sys_grid_user_cust a,sys_user b,sys_dept c
        where a.grid_id = #{gridId} and a.grid_type =#{gridPattern} and a.relation_status ='0'
        and a.cust_id in
            <foreach item="custId" index="index" collection="custIds" open="(" separator="," close=")">
                #{custId}
            </foreach>
        and a.user_name = b.user_name and (a.dept_branch_id = c.dept_id or a.dept_outlet_id = c.dept_id)
        <if test="userName != null  and userName != ''">
         and (b.nick_name like concat('%', #{userName}, '%')
            or b.user_name like concat('%', #{userName}, '%')
            or c.dept_name like concat('%', #{userName}, '%'))
        </if>
        group by a.cust_id
    </select>

</mapper>