<?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.base.mapper.MaReceiveMapper">

    <insert id="saveDataReceiveInfo" keyProperty="id" useGeneratedKeys="true">
        insert into data_receive_info(push_num,receive_date)
        values(#{pushNum},now())
    </insert>
    <insert id="saveDataReceiveDetails">
        insert into data_receive_detail(receive_id,check_code,check_unit,check_date,is_new,ma_code,ma_user_name,next_check_date,out_factory_time,rent_price,rent_time,supplier,type_id,unit_id)
        values(#{receiveId},#{checkCode},#{checkUnit},#{checkDate},#{isNew},#{maCode},#{maUserName},#{nextCheckDate},#{outFactoryTime},#{rentPrice},#{rentTime},#{supplier},#{typeId},#{unitId})
    </insert>
    <update id="updateStatus">
        UPDATE data_receive_detail
        SET STATUS = 1,
            ma_id = #{maId}
        where receive_id = #{receiveId} and type_id = #{typeId}
        AND ma_code = #{maCode}
    </update>
    <update id="updateInfoStatus">
        UPDATE data_receive_info
        SET receive_status = 1
        where id = #{receiveId}
    </update>
    <select id="getDataReceive" resultType="com.bonus.sgzb.base.domain.DataReceiveInfo">
        select * from data_receive_info where 1=1
        <if test="receiveStatus != null">
            AND receive_status = #{receiveStatus}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND receive_date BETWEEN CONCAT(#{startTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59')
        </if>
    </select>
    <select id="getDataReceiveDetails" resultType="com.bonus.sgzb.base.domain.DataReceiveDetail">
        SELECT
            drd.*,
            mt.type_name modelName,
            mt1.type_name typeName,
            mt2.type_name machineName
        FROM
            data_receive_detail drd
                LEFT JOIN ma_type mt ON drd.type_id = mt.type_id
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                LEFT JOIN ma_type mt2 ON mt2.type_id = mt1.parent_id
        WHERE

            drd.receive_id = #{receiveId}
        <if test="modelName != null and modelName != ''">
            AND mt.type_name like concat('%', #{modelName}, '%')
        </if>
        <if test="typeName != null and typeName != ''">
            AND mt1.type_name like concat('%', #{typeName}, '%')
        </if>
        <if test="machineName != null and machineName != ''">
            AND mt2.type_name like concat('%', #{machineName}, '%')
        </if>
        <if test="keyWord != null and keyWord != ''">
            AND (drd.ma_code like concat('%', #{keyWord}, '%') or
            drd.check_code like concat('%', #{keyWord}, '%'))
        </if>
    </select>
    <select id="getDateReceiveMachine" resultType="com.bonus.sgzb.base.domain.DataReceiveDetail">
        SELECT
            mm.ma_code maCode,
            mt2.type_name machineName,
            mt1.type_name typeName,
            mt.type_name modelName,
            drd.rent_time rentTime,
            drd.check_date checkDate,
            drd.next_check_date nextCheckDate,
            mm.souce_by souceBy,
            sd.NAME maStatus
        FROM
            data_receive_detail drd
                LEFT JOIN ma_type mt ON drd.type_id = mt.type_id
                LEFT JOIN ma_type mt1 ON mt.parent_id = mt1.type_id
                LEFT JOIN ma_type mt2 ON mt1.parent_id = mt2.type_id
                LEFT JOIN ma_machine mm ON drd.type_id = mm.type_id
                LEFT JOIN sys_dic sd ON sd.id = mm.ma_status
        WHERE
            mm.souce_by = 2
    </select>
    <select id="getDataReceiveDetailsById" resultType="com.bonus.sgzb.base.domain.DataReceiveDetail">
        SELECT * FROM data_receive_detail WHERE receive_id = #{receiveId}
    </select>
</mapper>