<?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="findBoxInfoByKey" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        select  bqb.box_id as boxId,bqb.box_code as boxCode from bm_qrcode_box bqb
        <where>
            <if test="boxId != null">and bqb.box_id = #{boxId}</if>
            <if test="boxCode != null and boxCode != ''">and bqb.box_code = #{boxCode}</if>
            <if test="boxType != null">and bqb.box_type = #{boxType}</if>
        </where>
    </select>

    <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.box_status as status,
            case bqb.box_status
                when '0' then '待创建'
                when '1' then '待录入'
                when '2' then '待移交'
                when '3' then '待接收'
                when '4' then '已接收'
                when '5' then '移交被驳回'
                else ''
                end as statusName,
            bqb.create_by as createBy,
            bqb.create_time as createTime,
            bqb.update_by as updateBy,
            bqb.update_time as updateTime,
            bqb.transfer_user as transferUser,
            bqb.input_user as inputUser,
            mm.type_id as maTypeId,
            COUNT(qbb.box_id) as devNum
        from bm_qrcode_box bqb
        left join bm_qrcode_box_bind qbb on bqb.box_id = qbb.box_id
        left join ma_machine mm on qbb.ma_id = mm.ma_id
        <where>
            1=1
            <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>
            <if test="statusList != null and statusList != ''">
                AND bqb.box_status in
                <foreach item="key" collection="statusList" open="(" separator="," close=")">
                    #{key}
                </foreach>
            </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 bm_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,qb.create_by as createBy,
            mt1.type_name as typeName,
            mt.type_name as typeModelName,
            mm.ma_id as maId,mm.ma_code as maCode,mm.type_id as maTypeId,
            mm.buy_task as taskId
        FROM
            bm_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>
            <if test="boxId != null">and qb.box_id = #{boxId}</if>
            <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>
        </where>
    </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="updateBmQrcodeInfoById" 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="status != null and status != ''">box_status = #{status} ,</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="transferUser != null">transfer_user = #{transferUser},</if>
            <if test="inputUser != null">input_user = #{inputUser},</if>
        </trim>
        where box_id = #{boxId}
    </update>

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

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

    <delete id="unBindQrcodeBox">
        delete from bm_qrcode_box_bind where id = #{id}
    </delete>

    <delete id="deleteBoxBind">
        delete from bm_qrcode_box_bind where ma_id = #{maId}
    </delete>

    <insert id="addQrcodeBoxBind">
        insert into bm_qrcode_box_bind
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="maId != null">ma_id,</if>
            <if test="boxId != null">box_id,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="maId != null">#{maId},</if>
            <if test="boxId != null">#{boxId},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateBmQrcodeStatus">
        update bm_qrcode_box
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null and status != ''">box_status = IF(box_status = 1,#{status},box_status) ,</if>
        </trim>
        where box_id = #{boxId}
    </update>

    <select id="getBoxMaTypeList" resultType="java.lang.Long">
        SELECT DISTINCT(mm.type_id) from bm_qrcode_box_bind qbb
        LEFT JOIN ma_machine mm on mm.ma_id = qbb.ma_id
        WHERE qbb.box_id = #{boxId}
    </select>

    <select id="existsMaId" resultType="int">
        select count(1) from bm_qrcode_box_bind where ma_id = #{maId}
    </select>

    <select id="getBoxBindList" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        SELECT
        qb.id as id, qb.box_id as boxId, qb.create_by as createBy, qb.create_time,
        mt1.type_name as typeName,
        mt.type_name as typeModelName,
        mm.ma_id as maId,mm.ma_code as maCode,mm.type_id as maTypeId,
        sdd.dict_label as maStatus
        FROM
        bm_qrcode_box_bind qb
        LEFT JOIN ma_machine mm ON qb.ma_id = mm.ma_id
        LEFT JOIN sys_dict_data sdd ON sdd.dict_type = 'ma_machine_status' and sdd.dict_value = mm.ma_status
        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>
            qb.box_id = #{boxId}
        </where>
    </select>

    <select id="getBoxBindLists" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        SELECT
        qb.id as id, qb.box_id as boxId, qb.create_by as createBy, qb.create_time,
        mt1.type_name as typeName,
        mt.type_name as typeModelName,
        mm.ma_id as maId,mm.ma_code as maCode,mm.type_id as maTypeId,
        sdd.dict_label as maStatus
        FROM
        bm_qrcode_box_bind qb
        LEFT JOIN ma_machine mm ON qb.ma_id = mm.ma_id
        LEFT JOIN sys_dict_data sdd ON sdd.dict_type = 'ma_machine_status' and sdd.dict_value = mm.ma_status
        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>
            qb.box_id = #{boxId}
            <if test="keyWord != null and keyWord !=''">
                and mm.ma_code LIKE concat('%',#{keyWord},'%')
            </if>
        </where>
    </select>

    <select id="getBoxBindListByCode" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        SELECT qb.id         as id,
               qb.box_id     as boxId,
               qb.create_by  as createBy,
               qb.create_time as createTime,
               mt1.type_name as typeName,
               mt.type_name  as typeModelName,
               mm.ma_id      as maId,
               mm.ma_code    as maCode,
               mm.type_id    as maTypeId,
               mm.ma_status  as maStatus,
               mt.jiju_type  as jijuType
        FROM bm_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'
                 LEFT JOIN bm_qrcode_box bqb ON qb.box_id = bqb.box_id
        where
            bqb.box_code = #{boxCode} AND
            mt.type_id = #{maTypeId}
    </select>

    <select id="getMaInfoByQrCode" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        select
            bqi.id as id, bqi.qr_code as qrCode, bqi.type_id as maTypeId, bqi.ma_code as maCode, mm.ma_id as maId, mm.ma_status as maStatus
        from
            bm_qrcode_info bqi
        left join
            ma_machine mm on bqi.type_id = mm.type_id and bqi.ma_code = mm.ma_code
        where
            bqi.del_flag is null or bqi.del_flag = '0' and bqi.qr_code = #{qrCode}
        group by
            bqi.id limit 1
    </select>

    <select id="getBoxCodeList" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        SELECT qb.id         as id,
               qb.box_id     as boxId,
               qb.create_by  as createBy,
               qb.create_time as createTime,
               mt1.type_name as typeName,
               mt.type_name  as typeModelName,
               mm.ma_id      as maId,
               mm.ma_code    as maCode,
               mm.type_id    as maTypeId,
               mm.ma_status  as maStatus
        FROM bm_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'
                 LEFT JOIN bm_qrcode_box bqb ON qb.box_id = bqb.box_id
        where
            bqb.box_code = #{boxCode} and bqb.box_status = 4
    </select>

    <select id="getBoxBindMaBuyTaskByBoxId" resultType="java.lang.String">
        select
            mm.buy_task
        from
            bm_qrcode_box_bind qbb
        LEFT JOIN
            ma_machine mm ON mm.ma_id = qbb.ma_id
        where
            qbb.box_id = #{boxId}
        group by
            qbb.box_id LIMIT 1
    </select>

    <select id="getBoxBindListById" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        SELECT
        qb.id as id,qb.box_id as boxId,qb.create_by as createBy,
        mt1.type_name as typeName,
        mt.type_name as typeModelName,
        mm.ma_id as maId,mm.ma_code as maCode,mm.type_id as maTypeId,
        mm.buy_task as taskId
        FROM
        bm_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
            (mm.ma_status = 0 or mm.ma_status =5) and
            qb.box_id = #{boxId}
    </select>

    <select id="selectByMaId" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        SELECT
            qb.id as id,qb.box_id as boxId,qb.create_by as createBy,
            mt1.type_name as typeName,
            mt.type_name as typeModelName,
            mm.ma_id as maId,mm.ma_code as maCode,mm.type_id as maTypeId,
            mm.buy_task as taskId
        FROM
            bm_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
            qb.ma_id = #{maId}
    </select>

    <select id="getBoxList" resultType="com.bonus.material.basic.domain.dto.BoxBindWarehouseDto">
        SELECT
            qb.id AS id,
            qb.box_id AS boxId,
            qb.create_by AS createBy,
            mt1.type_name AS typeName,
            mt.type_name AS typeModelName,
            mm.ma_id AS maId,
            mm.ma_code AS maCode,
            mm.ma_status AS maStatus,
            mm.type_id AS maTypeId,
            mm.buy_task AS taskId,
            qb.create_time AS createTime,
            tt.code AS purchaseCode
        FROM
            bm_qrcode_box_bind qb
                LEFT JOIN bm_qrcode_box bb on qb.box_id = bb.box_id
                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'
                LEFT JOIN tm_task tt ON mm.buy_task = tt.task_id
        WHERE
            bb.box_code = #{boxCode}
            <if test="maCode != null and maCode != ''">
               AND mm.ma_code LIKE concat('%',#{maCode},'%')
            </if>
    </select>

    <update id="updateNewInputNum">
        update purchase_check_details set input_num = IFNULL(input_num,0)  + 1 where task_id = #{taskId} and type_id = #{maTypeId}
    </update>



    <select id="getSingleInfo" resultType="com.bonus.material.basic.domain.dto.BoxBindWarehouseDto">
        SELECT
            pcd.task_id as taskId,
            pcd.type_id as typeId,
            pcd.purchase_num as purchaseNum,
            IFNULL(pcd.input_num,0) as inpuNum ,
            pcd.purchase_num - IFNULL(pcd.input_num,0) as waitNum
        FROM
            purchase_check_details pcd
        WHERE pcd.task_id = #{taskId} and pcd.type_id = #{maTypeId}
        HAVING waitNum > 0
    </select>

    <update id="updatePurchaseStatus">
        update purchase_check_details set status =19 where task_id = #{taskId} and type_id = #{maTypeId}
    </update>

    <select id="getTaskInfo" resultType="com.bonus.material.basic.domain.dto.BoxBindWarehouseDto">
        SELECT
            pcd.task_id as taskId,
            pcd.type_id as typeId,
            pcd.purchase_num as purchaseNum,
            IFNULL(pcd.input_num,0) as inpuNum ,
            pcd.purchase_num - IFNULL(pcd.input_num,0) as waitNum
        FROM
            purchase_check_details pcd
        WHERE pcd.task_id = #{taskId} and pcd.type_id = #{maTypeId}
        AND pcd.status NOT IN (19)
    </select>

    <select id="getBoxInfo" resultType="com.bonus.material.basic.domain.dto.BoxBindWarehouseDto">
        SELECT
            box_id AS boxId,
            box_name AS boxName,
            box_status AS boxStatus,
            box_code AS boxCode,
            create_by AS createBy,
            create_time AS createTime
        FROM
            bm_qrcode_box
        WHERE
            box_code = #{boxCode}
    </select>

    <select id="getBoxWsTypeList" resultType="java.lang.Long">
        SELECT DISTINCT(wmi.model_id)
        from bm_qrcode_box_bind qbb
        LEFT JOIN ws_ma_info wmi on wmi.id = qbb.ma_id
        WHERE qbb.box_id = #{boxId}
    </select>

    <select id="getBoxBindWsList" resultType="com.bonus.material.basic.domain.BmQrBoxInfo">
        SELECT
        qb.id as id, qb.box_id as boxId, qb.create_by as createBy, qb.create_time,
        mt1.type_name as typeName,
        mt.type_name as typeModelName,
        wsi.id as maId,wsi.ma_code as maCode,wsi.model_id as maTypeId
        FROM
        bm_qrcode_box_bind qb
        LEFT JOIN ws_ma_info wsi ON qb.ma_id = wsi.id
        LEFT JOIN ma_type mt ON wsi.model_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>
            qb.box_id = #{boxId}
            <if test="keyWord != null and keyWord !=''">
                and wsi.ma_code LIKE concat('%',#{keyWord},'%')
            </if>
        </where>
    </select>

    <select id="getMonthMaxOrderByDate" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            bm_qrcode_box
        WHERE
            MONTH ( create_time ) = #{month}
          AND YEAR ( create_time ) = #{year}
    </select>

    <update id="updateTaskStatus">
        UPDATE tm_task SET task_status = 22 WHERE task_id = #{taskId}
    </update>

    <update id="updateStorageNum">
        update ma_type set storage_num = ifnull(storage_num,0) + #{inputNum} where type_id = #{maTypeId}
    </update>

    <update id="updateBoxStatus">
        update bm_qrcode_box set box_status = 6 where box_id = #{boxId}
    </update>
</mapper>
