<?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.wcs.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.wcs.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.wcs.vo.BasePunchVo">
        <include refid="selectBasePunchVo"/>
        where id = #{id}
    </select>

    <select id="selectBasePunchRecord" parameterType="BasePunch" resultType="com.shkj.wcs.vo.BasePunchRecordVo">
        select punch.id,punch.punch_no,punch_name,max_task,is_state,is_active,ifnull(recordQty,0) as recordQty ,max_task-recordQty from base_punch punch
        left join(
        select count(*)as recordQty ,punch_no from busin_punch_record
        where ifnull(is_state,'0') ='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 max_task-ifnull(recordQty,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>
