<?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.material.mapper.InventoryAndWarehousingMapper">
    <insert id="saveDetails" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ma_type_put_in_storage_details (
        <trim prefix="" suffixOverrides=",">
            <choose>
                <when test="isCode and num != null and num != 0">
                    NUM,
                </when>
                <otherwise>
                    <if test="putInStoreNum != null and putInStoreNum != 0">
                        NUM,
                    </if>
                </otherwise>
            </choose>
            <if test="infoId != null and infoId != ''">
                INFO,
            </if>
            <if test="typeId != null and typeId != ''">
                TYPE,
            </if>
            <if test="maId != null and maId != ''">
                MACHINE,
            </if>
            <if test="remark != null and remark != ''">
                REMARKS,
            </if>
            <if test="maCode != null and maCode != ''">
                MACODE,
            </if>
        </trim>
        )
        VALUES (
        <choose>
            <when test="isCode and num != null and num != 0">
                1,
            </when>
            <otherwise>
                <if test="putInStoreNum != null and putInStoreNum != 0">
                    #{putInStoreNum},
                </if>
            </otherwise>
        </choose>
        <trim prefix="" suffixOverrides=",">
            <if test="infoId != null and infoId != ''">
                #{infoId},
            </if>
            <if test="typeId != null and typeId != ''">
                #{typeId},
            </if>
            <if test="maId != null and maId != ''">
                #{maId},
            </if>
            <if test="remark != null and remark != ''">
                #{remark},
            </if>
            <if test="maCode != null and maCode != ''">
                #{maCode},
            </if>
        </trim>
        )
    </insert>
    <insert id="saveInfo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ma_type_put_in_storage_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="putInType != null and putInType != ''">PUT_IN_TYPE,</if>
            <choose>
                <when test="isCode and num != null and num != 0">
                    NUM,
                </when>
                <otherwise>
                    <if test="totalNum != null and totalNum != 0">
                        NUM,
                    </if>
                </otherwise>
            </choose>
            <if test="typeId != null and typeId != ''">TYPE,</if>
            <if test="creator != null and creator != ''">CREATOR,</if>
            <if test="unitId != null and unitId != ''">UNIT_ID,</if>
            <if test="proId != null and proId != ''">PROJECT_ID,</if>
            <if test="code != null and code != ''">CODE,</if>
            <if test="remarks != null and remarks != ''">REMARKS,</if>
            CREATE_DATE
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="putInType != null and putInType != ''">#{putInType},</if>
            <choose>
                <when test="isCode and num != null and num != 0">
                    #{num},
                </when>
                <otherwise>
                    <if test="totalNum != null and totalNum != 0">
                        #{totalNum},
                    </if>
                </otherwise>
            </choose>
            <if test="typeId != null and typeId != ''">#{typeId},</if>
            <if test="creator != null and creator != ''">#{creator},</if>
            <if test="unitId != null and unitId != ''">#{unitId},</if>
            <if test="proId != null and proId != ''">#{proId},</if>
            <if test="code != null and code != ''">#{code},</if>
            <if test="remarks != null and remarks != ''">#{remarks},</if>
            sysdate()
        </trim>
    </insert>
    <insert id="insertMachine" useGeneratedKeys="true" keyProperty="maId">
        insert into ma_machine (
        <if test="typeId != null and typeId != '' ">type_id,</if>
        <if test="maCode != null and maCode != '' ">ma_code,</if>
        ma_status,
        <if test="qrCode != null and qrCode != ''">qr_code,</if>
        <if test="buyPrice != null and buyPrice != ''">buy_price,</if>
        <if test="maVender != null and maVender != ''">ma_vender,</if>
        <if test="checkMan != null and checkMan != ''">check_man,</if>
        <if test="outFacCode != null and outFacCode != ''">out_fac_code,</if>
        <if test="thisCheckTime != null">this_check_time,</if>
        <if test="nextCheckTime != null">next_check_time,</if>
        create_time
        )values(
        <if test="typeId != null and typeId != ''">#{typeId},</if>
        <if test="maCode != null and maCode != ''">#{maCode},</if>
        15,
        <if test="qrCode != null and qrCode != ''">#{qrCode},</if>
        <if test="buyPrice != null and buyPrice != ''">#{buyPrice},</if>
        <if test="maVender != null and maVender != ''">#{maVender},</if>
        <if test="checkMan != null and checkMan != ''">#{checkMan},</if>
        <if test="outFacCode != null and outFacCode != ''">#{outFacCode},</if>
        <if test="thisCheckTime != null">#{thisCheckTime},</if>
        <if test="nextCheckTime != null">#{nextCheckTime},</if>
        sysdate()
        )
    </insert>
    <insert id="insertMachineLabel">
        insert into ma_machine_label
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="qrCode != null">label_code,</if>
            <if test="maId != null">ma_id,</if>
            is_bind,
            label_type,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="qrCode != null">#{qrCode},</if>
            <if test="maId != null">#{maId},</if>
            1,
            9,
            now()
        </trim>
    </insert>
    <insert id="insertLabelBind">
        insert into ma_label_bind
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="maId != null">ma_id,</if>
            <if test="qrCode != null">label_code,</if>
            <if test="typeId != null">type_id,</if>
            <if test="creator != null">binder,</if>
            label_type,
            bind_time,
            status
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="maId != null">#{maId},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="creator != null">#{creator},</if>
            9,
            now(),
            1
        </trim>
    </insert>
    <update id="updateMaType">
        UPDATE ma_type
        SET num = IFNULL(num, 0) + #{num},
        update_time = now()
        <where>
            <if test="typeId != null ">and type_id = #{typeId}</if>
        </where>
    </update>

    <select id="getList" resultType="com.bonus.sgzb.material.domain.PutInStorageBean">
        SELECT
        sub.putInType,
        sub.projectName,
        sub.unitName,
        sub.kindName,
        sub.typeName,
        sub.modelName,
        sub.createDate,
        sub.remark
        FROM (
        SELECT
        pisi.PUT_IN_TYPE AS putInType,
        lot.lot_name AS projectName,
        bui.unit_name AS unitName,
        pisi.`CODE` AS kindName,
        mt2.type_name AS typeName,
        su.user_name AS modelName,
        pisi.CREATE_DATE AS createDate,
        pisi.REMARKS AS remark,
        (
        SELECT COUNT(*)
            FROM ma_type_put_in_storage_info pisi2
            WHERE pisi2.`CODE` = pisi.`CODE` AND pisi2.CREATE_DATE >= pisi.CREATE_DATE
        ) AS row_num
        FROM
        ma_type_put_in_storage_info pisi
        LEFT JOIN bm_project_lot lot ON lot.lot_id = pisi.PROJECT_ID
        LEFT JOIN bm_unit_info bui ON bui.unit_id = pisi.UNIT_ID
        LEFT JOIN ma_type mt ON mt.type_id = pisi.TYPE
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN sys_user su ON su.user_id = pisi.CREATOR
        WHERE
        1 = 1
        <if test="keyWord != null and keyWord != ''">
            AND (
            pisi.`CODE` LIKE CONCAT('%',#{keyWord},'%') OR
            pisi.PUT_IN_TYPE LIKE CONCAT('%',#{keyWord},'%') OR
            lot.lot_name LIKE CONCAT('%',#{keyWord},'%') OR
            bui.unit_name LIKE CONCAT('%',#{keyWord},'%') OR
            mt2.type_name LIKE CONCAT('%',#{keyWord},'%') OR
            su.user_name LIKE CONCAT('%',#{keyWord},'%') OR
            pisi.REMARKS LIKE CONCAT('%',#{keyWord},'%')
            )
        </if>
        ) AS sub
        WHERE sub.row_num = 1
        ORDER BY createDate DESC
    </select>
    <select id="selectTaskNumByMonth" resultType="java.lang.Integer">
        select count(*) from ma_type_put_in_storage_info where DATE_FORMAT(CREATE_DATE,'%y%m') = DATE_FORMAT(#{date},'%y%m')
    </select>
    <select id="selectByCode" resultType="java.lang.Integer">
        select count(*)
        from ma_machine
        <where>
            <if test="code != null ">and qr_code = #{code}</if>
        </where>
    </select>
    <select id="getDetails" resultType="com.bonus.sgzb.material.domain.PutInStorageBean">
        SELECT
        pisi.`CODE`      as kindName,
        pisi.PUT_IN_TYPE as putInType,
        pisd.MACODE      as maCode,
        pisd.NUM         as num,
        mt2.type_name    as typeName,
        mt.type_name     as typeModelName,
        su.user_name     as modelName,
        pisi.CREATE_DATE as createDate,
        pisd.REMARKS as remark
        FROM ma_type_put_in_storage_info pisi
        LEFT JOIN ma_type_put_in_storage_details pisd on pisi.id = pisd.INFO
        LEFT JOIN bm_project_lot lot on lot.lot_id = pisi.PROJECT_ID
        LEFT JOIN bm_unit_info bui on bui.unit_id = pisi.UNIT_ID
        LEFT JOIN ma_type mt on mt.type_id = pisd.TYPE
        LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
        LEFT JOIN sys_user su on su.user_id = pisi.CREATOR
        where
        1 = 1
        <if test="kindName != null and kindName != ''">
            and pisi.`CODE` = #{kindName}
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (
            pisi.PUT_IN_TYPE like concat('%',#{keyWord},'%') or
            mt2.type_name like concat('%',#{keyWord},'%') or
            su.user_name like concat('%',#{keyWord},'%') or
            mt.type_name like concat('%',#{keyWord},'%') or
            pisd.MACODE like concat('%',#{keyWord},'%')
            )
        </if>
    </select>
    <select id="selectByMaCode" resultType="java.lang.Integer">
        select count(*)
        from ma_machine
        <where>
            <if test="maCode != null ">and ma_code = #{maCode}</if>
        </where>
    </select>
</mapper>