<?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.shkj.wms.mapper.BasePunchMapper">

    <sql id="selectBasePunchVo">
        select  id, punch_no, punch_name, is_state, is_active, max_task, punch_qty, remaks, is_del,create_time, update_time, update_by, create_by from base_punch  where ifnull(is_del,0) !=1
    </sql>

    <select id="selectBasePunchList" parameterType="BasePunch" resultType="com.shkj.wms.vo.BasePunchVo">
        <include refid="selectBasePunchVo" />

        <if test="punchNo != null  and punchNo != ''"> and punch_no = #{punchNo}</if>
        <if test="punchName != null  and punchName != ''"> and punch_name like concat('%', #{punchName}, '%')</if>
        <if test="isState != null  and isState != ''"> and is_state = #{devType}</if>
        <if test="isActive != null  and isActive != ''"> and ifnull(is_active,0) = #{isActive}</if>

    </select>

    <select id="selectBasePunchById" parameterType="Long" resultType="com.shkj.wms.vo.BasePunchVo">
        <include refid="selectBasePunchVo"/>
        where id = #{id}
    </select>

    <select id="selectBasePunchRecord" parameterType="BasePunch" resultType="com.shkj.wms.vo.BasePunchRecordVo">
        SELECT
            punch.id,
            punch.punch_no,
            punch_name,
            max_task,
            is_state,
            is_active,
            IFNULL(record.recordQty, 0) AS recordQty,
            IFNULL(max_task - IFNULL(record.recordQty,0), 0) as qwe
        FROM base_punch punch
            LEFT JOIN (
            SELECT
            count(*) as recordQty,
            punch_no
            FROM busin_punch_record
            WHERE is_state = 0
            GROUP BY punch_no
            ) AS record
        ON punch.id = record.punch_no
        WHERE IFNULL(is_del,0) != 1
          AND IFNULL(is_active,0) = 1 and IFNULL(max_task - IFNULL(record.recordQty,0), 0) > 0
        ORDER BY max_task - recordQty DESC
            LIMIT 1;
    </select>


    <delete id="deleteBasePunchById" parameterType="Long">
        delete from base_punch where id = #{id}
    </delete>

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