<?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.BmQrcodeInfoMapper">
    <resultMap type="com.bonus.material.basic.domain.BmQrcodeInfo" id="BmQrcodeInfoResult">
        <result property="qrId"    column="qr_id"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="typeId"    column="type_id"    />
        <result property="qrType"    column="qr_type"    />
        <result property="taskId"    column="task_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectBmQrcodeInfoVo">
        select qr_id, qr_code, type_id, qr_type, task_id, create_by, create_time, update_by, update_time, remark, company_id from bm_qrcode_info
    </sql>

    
    <select id="selectBmQrcodeInfoByQrId" parameterType="Long" resultMap="BmQrcodeInfoResult">
        <include refid="selectBmQrcodeInfoVo"/>
        where qr_id = #{qrId}
    </select>

    <select id="selectBmQrcodeInfoList" resultType="com.bonus.material.basic.domain.BmQrcodeInfo">
        select bqi.id as qrId, bqi.qr_code as qrCode, mt1.type_name as typeName, mt.type_name as typeModelName,
        bqi.ma_code as maCode, bqi.type_id as typeId, bqi.task_id as taskId, bqi.create_by as
        createBy,
        CASE
        WHEN bqi.qr_type IS NULL THEN '新购'
        ELSE bqi.qr_type
        END AS qrType,
        bqi.create_time as createTime, bqi.update_by as updateBy,
        bqi.update_time as updateTime,
        bqi.remark as remark,
        bqi.company_id as companyId,
        bqi.is_bind as bindStatus,
        u.nick_name as bindUser,
        msi.supplier as supplierName
        from bm_qrcode_info bqi
        left join ma_type mt on bqi.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt1.type_id = mt.parent_id and mt1.del_flag = '0'
        left join purchase_check_info pci on bqi.task_id = pci.task_id
        left join ma_supplier_info msi on pci.supplier_id = msi.supplier_id and msi.del_flag = '0'
        left join sys_user u ON bqi.bind_user = u.user_id
        <where>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( bqi.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="bindStatus != null and bindStatus != ''">and bqi.is_bind = #{bindStatus}</if>
        <if test="keyWord != null  and keyWord != ''">
            and (
            bqi.qr_code like concat('%', #{keyWord}, '%') or
            mt1.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%') or
            bqi.ma_code like concat('%', #{keyWord}, '%') or
            msi.supplier like concat('%', #{keyWord}, '%') or
            u.nick_name like concat('%', #{keyWord}, '%')
            )
        </if>
        </where>
        order by bqi.create_time desc
    </select>

    <select id="selectByMonth" resultType="com.bonus.material.basic.domain.BmQrcodeInfo">
        SELECT
            qr_code as qrCode
        FROM
            bm_qrcode_info
        WHERE
            DATE_FORMAT(create_time, '%Y-%m') = #{genMonth}
        ORDER BY
            qr_code DESC
    </select>

    <select id="getDetails" resultType="com.bonus.material.basic.domain.BmQrcodeInfo">
        select bqi.id as qrId, bqi.qr_code as qrCode, bqi.qr_url as qrUrl, mt1.type_name as typeName, mt.type_name as typeModelName,
        bqi.ma_code as maCode, bqi.type_id as typeId, bqi.qr_type as qrType, bqi.task_id as taskId, bqi.create_by as
        createBy,
        bqi.create_time as createTime, bqi.update_by as updateBy,
        bqi.update_time as updateTime,
        bqi.remark as remark,
        bqi.company_id as companyId,
        bqi.is_bind as bindStatus
        from bm_qrcode_info bqi
        left join ma_type mt on bqi.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt1.type_id = mt.parent_id and mt1.del_flag = '0'
        where bqi.id in
        <foreach collection="list" item="qrId" open="(" separator="," close=")">
            #{qrId}
        </foreach>
    </select>

    <insert id="insertBmQrcodeInfo" parameterType="com.bonus.material.basic.domain.BmQrcodeInfo" useGeneratedKeys="true" keyProperty="qrId">
        insert into bm_qrcode_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="qrCode != null">qr_code,</if>
            <if test="qrUrl != null">qr_url,</if>
            <if test="typeId != null">type_id,</if>
            <if test="qrType != null">qr_type,</if>
            <if test="taskId != null">task_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="qrCode != null">#{qrCode},</if>
            <if test="qrUrl != null">#{qrUrl},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="qrType != null">#{qrType},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
         </trim>
    </insert>

    <update id="updateBmQrcodeInfo" parameterType="com.bonus.material.basic.domain.BmQrcodeInfo">
        update bm_qrcode_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="qrType != null">qr_type = #{qrType},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where qr_id = #{qrId}
    </update>

    <delete id="deleteBmQrcodeInfoByQrId" parameterType="Long">
        delete from bm_qrcode_info where qr_id = #{qrId}
    </delete>

    <delete id="deleteBmQrcodeInfoByQrIds" parameterType="String">
        delete from bm_qrcode_info where qr_id in 
        <foreach item="qrId" collection="array" open="(" separator="," close=")">
            #{qrId}
        </foreach>
    </delete>
</mapper>