<?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.bonus.sgzb.base.mapper.WarehouseKeeperMapper">
    <insert id="add">
        insert into ma_type_keeper(type_id, user_id,company_id,time) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.typeIds},#{item.userId},#{item.companyId},NOW())
        </foreach>
    </insert>

    <delete id="deleteByIds">
        DELETE FROM ma_type_keeper WHERE `type_id` = #{typeId}
    </delete>

    <delete id="deleteByIdsAll">
        delete from ma_type_keeper where type_id in
        <foreach item="typeIds" collection="array" open="(" separator="," close=")">
            #{typeIds}
        </foreach>
    </delete>

    <select id="getWarehouseKeeperInfoAll" resultType="com.bonus.sgzb.base.domain.WarehouseKeeper">
        SELECT mt.type_id as modelId ,mt.type_name as modelName,
        mt2.type_name  as typeName,
        mt4.type_name as kindName,
        su.user_id as userId, su.nick_name as userName
        FROM ma_type mt
            LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id AND mt2.`level` = '3'
            LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id AND mt3.`level` = '2'
            LEFT JOIN ma_type mt4 ON mt3.parent_id = mt4.type_id AND mt4.`level` = '1'
            LEFT JOIN ma_type_keeper mtk ON mtk.type_id = mt.type_id
            LEFT JOIN sys_user su ON su.user_id = mtk.user_id
        WHERE mt.`level` = '4' AND mt.`del_flag` = '0'
        <if test="typeName != null and typeName != ''">
            AND mt2.type_name like concat('%', #{typeName}, '%')
        </if>
        ORDER BY mt.type_id DESC
    </select>

    <select id="getMaUserList" resultType="com.bonus.sgzb.base.domain.WarehouseKeeper">
        SELECT distinct
        su.user_id AS userId,
        su.nick_name AS userName
        FROM
        `sys_user` su
        LEFT JOIN sys_user_role sur ON su.user_id = sur.user_id
        LEFT JOIN sys_role sr ON sr.role_id = sur.role_id
        WHERE
        sr.role_id IN ( '132', '139' )
        AND su.del_flag = '0'
        <if test="userName != null and userName != ''">
            AND su.nick_name like concat('%', #{userName}, '%')
        </if>
        ORDER BY su.user_id DESC
    </select>
</mapper>