<?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.material.ma.mapper.TypeRepairMapper">
    <resultMap type="com.bonus.material.ma.domain.TypeRepair" id="TypeRepairResult">
        <result property="ID"    column="ID"    />
        <result property="typeId"    column="type_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectTypeRepairVo">
        select ID, type_id, user_id, create_time, update_time, company_id from ma_type_repair
    </sql>

    <select id="selectTypeRepairList" parameterType="com.bonus.material.ma.domain.TypeRepair" resultMap="TypeRepairResult">
        <include refid="selectTypeRepairVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
        </where>
    </select>

    <select id="selectTypeRepairByID" parameterType="Long" resultMap="TypeRepairResult">
        <include refid="selectTypeRepairVo"/>
        where ID = #{ID}
    </select>

    <insert id="insertTypeRepair" parameterType="com.bonus.material.ma.domain.TypeRepair" useGeneratedKeys="true" keyProperty="ID">
        insert into ma_type_repair (type_id,user_id,create_time,update_time,company_id)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.typeId},
            #{item.userId},
            #{item.createTime},
            #{item.updateTime},
            #{item.companyId}
            )
        </foreach>
    </insert>
    <insert id="insertTypeManage">
        insert into ma_type_manage (type_id,user_id,create_time,update_time,company_id)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.typeId},
            #{item.userId},
            #{item.createTime},
            #{item.updateTime},
            #{item.companyId}
            )
        </foreach>
    </insert>

    <update id="updateTypeRepair" parameterType="com.bonus.material.ma.domain.TypeRepair">
        update ma_type_repair
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where ID = #{ID}
    </update>

    <delete id="deleteTypeRepairByID" parameterType="Long">
        delete from ma_type_repair where ID = #{ID}
    </delete>

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

    <select id="selectTypeRepairListAndUserName" resultMap="TypeRepairResult">
        select
            m.type_id, mtk.user_id, su.nick_name as user_name, m.create_time
        from
            ma_type_repair mtk
        left join
             ma_type m ON m.type_id = mtk.type_id
        left join sys_user su on mtk.user_id = su.user_id
    </select>

    <select id="getRepairList" resultType="com.bonus.material.ma.domain.TypeRepair">
        SELECT DISTINCT
            mtk.user_id as userId,
            su.nick_name as repairer
        FROM
            ma_type_repair mtk
                LEFT JOIN sys_user su ON mtk.user_id = su.user_id
        <where>
            <if test="repairer != null and repairer != ''">
                AND su.nick_name = #{repairer}
            </if>
        </where>
        GROUP BY
            mtk.user_id,
            su.nick_name
    </select>
    <select id="getTypeManageList" resultType="com.bonus.material.ma.domain.vo.MaTypeVo">
        SELECT DISTINCT
        mt.type_id as typeId,
        mt.type_name as typeName,
        mtm.user_id as keeperUserId,
        su.nick_name as keeperUserName
        FROM
        ma_type mt
        LEFT JOIN wh_house_set mhs on mt.type_id = mhs.type_id
        LEFT JOIN ma_type_manage mtm on mt.type_id = mtm.type_id
        LEFT JOIN sys_user su on su.user_id = mtm.user_id
        <where>
            <if test="level == 1">
                and mt.type_id = #{typeId}
            </if>
            and mt.type_id is not null
            and mhs.house_id = #{houseId} and mhs.del_flag = 0
            <if test="keyword != null  and keyword != ''">
                and (
                mt.type_name like concat('%', #{keyword}, '%')
                or su.nick_name like concat('%', #{keyword}, '%')
                )
            </if>
        </where>
    </select>

    <delete id="deleteTypeRepairByUserIdAndTypeId">
        delete from ma_type_repair where
        type_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.typeId}
        </foreach>
        and
        user_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.userId}
        </foreach>
    </delete>
    <delete id="deleteTypeManageByUserIdAndTypeId">
        delete from ma_type_manage where
        type_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.typeId}
        </foreach>
        and
        user_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.userId}
        </foreach>
    </delete>
</mapper>
