<?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.basic.mapper.BmQrBoxMapper">

    <select id="find" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        select
        bqb.box_id as boxId,
        bqb.box_name as boxName,
        bqb.box_code as boxCode,
        bqb.box_type as boxType,
        bqb.create_by as createBy,
        bqb.create_time as createTime,
        bqb.update_by as updateBy,
        bqb.update_time as updateTime,
        COUNT(qbb.box_id) as devNum
        from bm_qrcode_box bqb
        left join qrcode_box_bind qbb on bqb.box_id = qbb.box_id
        <where>
            <if test="boxId != null">and bqb.box_id = #{boxId}</if>
            <if test="boxName != null and boxName != ''">and bqb.box_name like concat('%',#{boxName},'%')</if>
            <if test="boxCode != null and boxCode != ''">and bqb.box_code like concat('%',#{boxCode},'%')</if>
            <if test="boxType != null">and bqb.box_type = #{boxType}</if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                <![CDATA[ AND DATE_FORMAT( bqb.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
            </if>
            <if test="keyWord != null and keyWord != ''">
            AND (
            bqb.box_name like concat('%',#{keyWord},'%')
            OR bqb.box_code like concat('%',#{keyWord},'%')
            )
            </if>
        </where>
        GROUP BY bqb.box_id
        order by bqb.create_time desc
    </select>

    <select id="countBmQrcodeInfoByBoxId" resultType="java.lang.Integer">
        select count(1) from qrcode_box_bind where box_id = #{boxId}
    </select>

    <select id="getList" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        SELECT
            qb.id as id,
            qb.box_id as boxId,
            mt1.type_name as typeName,
            mt.type_name as typeModelName,
            mm.ma_code as maCode,
            qb.create_by as createBy
        FROM
            qrcode_box_bind qb
                LEFT JOIN ma_machine mm ON qb.ma_id = mm.ma_id
                LEFT JOIN ma_type mt ON mm.type_id = mt.type_id
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                AND mt1.del_flag = '0'
        WHERE
            1 = 1
            <if test="keyWord != null and keyWord != ''">
            AND (
            mt1.type_name like concat('%',#{keyWord},'%')
            OR mt.type_name like concat('%',#{keyWord},'%')
            OR mm.ma_code like concat('%',#{keyWord},'%')
            )
            </if>
    </select>

    <insert id="insertBmQrcodeInfo" parameterType="com.bonus.material.basic.domain.BmQrBoxInfo" useGeneratedKeys="true" keyProperty="boxId">
        insert into bm_qrcode_box
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="boxCode != null">box_code,</if>
            <if test="boxName != null and boxName != ''">box_name,</if>
            <if test="boxType != null">box_type,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="boxCode != null">#{boxCode},</if>
            <if test="boxName != null and boxName != ''">#{boxName},</if>
            <if test="boxType != null">#{boxType},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateBmQrcodeInfo" parameterType="com.bonus.material.basic.domain.BmQrBoxInfo">
        update bm_qrcode_box
        <trim prefix="SET" suffixOverrides=",">
            <if test="boxCode != null">box_code = #{boxCode},</if>
            <if test="boxType != null">box_type = #{boxType},</if>
            <if test="boxName != null and boxName != ''">box_name = #{boxName},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>

        </trim>
        where box_id = #{boxId}
    </update>

    <delete id="deleteById">
        delete
        from bm_qrcode_box
        where box_id = #{boxId}
    </delete>
</mapper>