<?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.purchase.mapper.PurchaseMacodeInfoMapper">
    <resultMap type="com.bonus.material.purchase.domain.PurchaseMacodeInfo" id="PurchaseMacodeInfoResult">
        <result property="id"    column="id"    />
        <result property="taskId"    column="task_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="maCode"    column="ma_code"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="fixCode"    column="fix_code"    />
        <result property="codeType"    column="code_type"    />
        <result property="status"    column="status"    />
        <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"    />
        <result property="outFacCode"    column="out_fac_code"    />
        <result property="unitName"    column="unit_name"    />
        <result property="typeName"    column="type_name"    />
        <result property="maTypeName"    column="ma_type_name"    />
        <result property="productionTime"    column="production_time"    />
    </resultMap>

    <sql id="selectPurchaseMacodeInfoVo">
        select pmi.id, pmi.task_id, pmi.type_id, pmi.ma_code, pmi.qr_code, if(mm.ma_status = '0' or mm.ma_status = '5' or mm.ma_status = '9', 0, 1) as status,
               su.nick_name as createBy, pmi.create_time, pmi.update_by, pmi.update_time, pmi.remark, pmi.company_id, pmi.out_fac_code,
               mt.type_name, mt.unit_name, mtp.type_name as ma_type_name, pcd.production_time
        from bm_qrcode_info pmi
        left join ma_type mt on pmi.type_id = mt.type_id
        left join ma_type mtp on mt.parent_id = mtp.type_id
        left join purchase_check_details pcd on pmi.task_id = pcd.task_id and pmi.type_id = pcd.type_id
        left join ma_machine mm on pmi.type_id = mm.type_id and pmi.ma_code = mm.ma_code
        left join sys_user su on pmi.bind_user = su.user_id
    </sql>

    <select id="selectPurchaseMacodeInfoList" parameterType="com.bonus.material.purchase.domain.PurchaseMacodeInfo" resultType="com.bonus.material.purchase.domain.PurchaseMacodeInfo">
        select pmi.id, pmi.task_id as taskId, pmi.type_id as typeId, pmi.ma_code as maCode, pmi.qr_code as qrCode, if(mm.ma_status = '0' or mm.ma_status = '5' or mm.ma_status = '9', 0, 1) as status,
        su.nick_name as createBy, pmi.create_time as createTime, pmi.update_by as updateBy, pmi.update_time as updateTime, pmi.remark, pmi.company_id as companyId, pmi.out_fac_code as outFacCode,
        mt.type_name as typeName, mt.unit_name as unitName, mtp.type_name as maTypeName, pcd.production_time as productionTime
        from bm_qrcode_info pmi
        left join ma_type mt on pmi.type_id = mt.type_id
        left join ma_type mtp on mt.parent_id = mtp.type_id
        left join purchase_check_details pcd on pmi.task_id = pcd.task_id and pmi.type_id = pcd.type_id
        left join ma_machine mm on pmi.type_id = mm.type_id and pmi.ma_code = mm.ma_code
        left join sys_user su on pmi.bind_user = su.user_id
        where
            1=1
            <if test="taskId != null ">and  pmi.task_id = #{taskId}</if>
            <if test="typeId != null "> and pmi.type_id = #{typeId}</if>
            <if test="maCode != null  and maCode != ''"> and pmi.ma_code = #{maCode}</if>
            <if test="outFacCode != null  and outFacCode != ''"> and pmi.out_fac_code = #{outFacCode}</if>
            <if test="qrCode != null  and qrCode != ''"> and pmi.qr_code = #{qrCode}</if>
            <if test="fixCode != null  and fixCode != ''"> and pmi.fix_code = #{fixCode}</if>
            <if test="codeType != null  and codeType != ''"> and pmi.code_type = #{codeType}</if>
            <if test="status != null  and status != ''"> and pmi.status = #{status}</if>
            <if test="companyId != null "> and pmi.company_id = #{companyId}</if>
            <if test="keyWord != null  and keyWord != ''">
                and (pmi.ma_code like concat('%', #{keyWord}, '%') or pmi.qr_code like concat('%', #{keyWord}, '%'))
            </if>
            and pmi.ma_code is not null

    </select>
    
    <select id="selectPurchaseMacodeInfoById" parameterType="Long" resultMap="PurchaseMacodeInfoResult">
        <include refid="selectPurchaseMacodeInfoVo"/>
        where pmi.id = #{id}
    </select>
        
    <insert id="insertPurchaseMacodeInfo" parameterType="com.bonus.material.purchase.domain.PurchaseMacodeInfo" useGeneratedKeys="true" keyProperty="id">
        insert into bm_qrcode_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="maCode != null">ma_code,</if>
            <if test="outFacCode != null">out_fac_code,</if>
            <if test="qrCode != null">qr_code,</if>
            <if test="fixCode != null">fix_code,</if>
            <if test="codeType != null">code_type,</if>
            <if test="status != null">status,</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="taskId != null">#{taskId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="maCode != null">#{maCode},</if>
            <if test="outFacCode != null">#{outFacCode},</if>
            <if test="qrCode != null">#{qrCode},</if>
            <if test="fixCode != null">#{fixCode},</if>
            <if test="codeType != null">#{codeType},</if>
            <if test="status != null">#{status},</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="updatePurchaseMacodeInfo" parameterType="com.bonus.material.purchase.domain.PurchaseMacodeInfo">
        update bm_qrcode_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="maCode != null">ma_code = #{maCode},</if>
            <if test="outFacCode != null">out_fac_code = #{outFacCode},</if>
            <if test="qrCode != null">qr_code = #{qrCode},</if>
            <if test="fixCode != null">fix_code = #{fixCode},</if>
            <if test="codeType != null">code_type = #{codeType},</if>
            <if test="status != null">status = #{status},</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 id = #{id}
    </update>

    <delete id="deletePurchaseMacodeInfoById" parameterType="Long">
        delete from bm_qrcode_info where id = #{id}
    </delete>

    <delete id="deletePurchaseMacodeInfoByIds" parameterType="String">
        delete from bm_qrcode_info where id in 
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="updatePurchaseMacodeInfoByIds" parameterType="String">
        update bm_qrcode_info set ma_code = null where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getPurchaseMaCodeCount" resultType="Integer">
        select count(1) from bm_qrcode_info where task_id = #{taskId} and type_id = #{typeId}
    </select>
</mapper>