<?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.shkj.system.mapper.BaseBranchMapper">


    <resultMap type="com.shkj.common.core.domain.entity.BaseBranch" id="BaseBranchResult">
        <result property="id" column="id"/>
        <result property="branchCode" column="branch_code"/>
        <result property="branchName" column="branch_name"/>
        <result property="isActivity" column="is_activity"/>
        <result property="extend1" column="extend1"/>
        <result property="extend2" column="extend2"/>
        <result property="extend3" column="extend3"/>
        <result property="extend4" column="extend4"/>
        <result property="extend5" column="extend5"/>
        <result property="remark" column="remark"/>
        <result property="isDel" column="is_del"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>
    <insert id="copyDictTypeByBranch" parameterType="java.lang.Long">
        INSERT INTO `sys_dict_type` (
            `branch_id`,
            `dict_name`,
            `dict_type`,
            `status`,
            `create_by`,
            `create_time`,
            `update_by`,
            `update_time`,
            `remark`
        ) SELECT
            #{brachId},
            `dict_name`,
            `dict_type`,
            `status`,
            `create_by`,
            NOW(),
            `update_by`,
           NOW(),
            `remark`
        FROM
            sys_dict_type
        WHERE
            branch_id = 0
    </insert>
    <insert id="copyDictDataByBranch" parameterType="java.lang.Long">
        INSERT INTO `sys_dict_data` (
            `branch_id`,
            `dict_sort`,
            `dict_label`,
            `dict_value`,
            `dict_type`,
            `css_class`,
            `list_class`,
            `is_default`,
            `status`
        ) SELECT
            #{branchId},
            `dict_sort`,
            `dict_label`,
            `dict_value`,
            `dict_type`,
            `css_class`,
            `list_class`,
            `is_default`,
            `status`
        FROM
            sys_dict_data
        WHERE
            branch_id = 0
    </insert>
    <insert id="copyParameterByBranch" parameterType="java.lang.Long">
        INSERT INTO `sys_parameter` (
            `para_code`,
            `para_name`,
            `para_value`,
            `para_type`,
            `is_use`,
            `remark`,
            `branch_id`,
            `is_del`,
            `create_time`,
            `create_by`,
            `update_time`,
            `update_by`
        ) SELECT
            `para_code`,
            `para_name`,
            `para_value`,
            `para_type`,
            `is_use`,
            `remark`,
            #{branchId},
            `is_del`,
            NOW(),
            `create_by`,
            NOW(),
            `update_by`
        FROM
            sys_parameter
        WHERE
            branch_id = 0
    </insert>
    <insert id="copySeqManaByBranch" parameterType="java.lang.Long">
        INSERT INTO `sys_seq_mana` (
            `seq_name`,
            `seq_type`,
            `seq_code`,
            `seq_prefix`,
            `seq_digit`,
            `seq_date`,
            `seq_increase`,
            `seq_max`,
            `seq_current`,
            `remark`,
            `branch_id`,
            `is_del`
        )
        SELECT
            `seq_name`,
            `seq_type`,
            `seq_code`,
            `seq_prefix`,
            `seq_digit`,
            `seq_date`,
            `seq_increase`,
            `seq_max`,
            `seq_current`,
            `remark`,
            #{branchId},
            `is_del`
        FROM
            sys_seq_mana
        WHERE
            branch_id = 0
    </insert>

    <delete id="deleteBranchByIds">
        delete from base_branch where id in
        <foreach collection="array" item="branchId" open="(" separator="," close=")">
            #{branchId}
        </foreach>
    </delete>
    <select id="countUserBranchById" resultType="java.lang.Integer" parameterType="java.lang.Long">
        select count(1) from sys_user_branch where branch_id=#{branchId}
    </select>
    <select id="optionBranchByUserName" resultType="com.shkj.common.core.domain.entity.BaseBranch"
            parameterType="java.lang.String">
        SELECT w.* from base_branch w
            join sys_user_branch uw on w.id = uw.branch_id
            join sys_user u on u.user_id  = uw.user_id
            where w.is_del = '0' and user_name = #{userName}
    </select>
    <select id="getConsignorCount" resultType="java.lang.Integer" parameterType="java.lang.Long">
        select count(1) from base_consignor where is_del = '0' and branch_id = #{branchId}
    </select>
    <select id="getUserList" resultType="com.shkj.common.core.domain.entity.SysUser"
            parameterType="java.lang.String">
        select * from sys_user where user_name = #{userName}
    </select>
</mapper>
