<?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="cn.ezhizao.project.business.daywork.mapper.BizDayworkMapper">

    <resultMap type="cn.ezhizao.project.business.daywork.domain.BizDaywork" id="BizDayworkResult">
        <id column="id" property="id"/>
    </resultMap>
    <resultMap type="cn.ezhizao.project.business.daywork.domain.BizDaywork" id="BizTurnoverDayworkResult">
        <id column="id" property="id"/>
        <association property="turnoverDayworkItem" javaType="cn.ezhizao.project.business.turnover.domain.BizTurnover" column="id" select="getTurnoverItem"/>
    </resultMap>

    <select id="getList" parameterType="BizDaywork" resultMap="BizDayworkResult">
        SELECT * FROM biz_daywork
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="deptId != null and deptId !=''"> AND dept_id = #{deptId}</if>
            <if test="id != null "> AND id = #{id}</if>
            <if test="userName != null  and userName != ''"> AND user_name like concat('%', #{userName}, '%')</if>
            <if test="nickName != null  and nickName != ''"> AND nick_name like concat('%', #{nickName}, '%')</if>
            <if test="startTime != null "> AND start_time = #{startTime}</if>
            <if test="endTime != null "> AND end_time = #{endTime}</if>
            <if test="productionPlanDetailId != null and productionPlanDetailId != ''">AND production_plan_detail_id = #{productionPlanDetailId}</if>
            <if test="userId != null">AND id IN (SELECT daywork_id from biz_daywork_item where deleted = 0 and user_id = #{userId} )</if>
            <if test="tenantId != null  and tenantId != 0"> AND tenant_id = #{tenantId}</if>
            <if test="keywords != null  and keywords != ''"> AND lot_id in (select id from biz_lot where deleted = 0 and lot_code like concat('%', #{keywords}, '%'))</if>
        </trim>
        ORDER BY create_time DESC
    </select>

    <select id="getTurnoverDayworkList" resultMap="BizTurnoverDayworkResult">
        SELECT * FROM biz_daywork
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
           AND status = 1
        </trim>
        ORDER BY create_time DESC
    </select>
    <select id="getTurnoverItem" resultType="cn.ezhizao.project.business.daywork.domain.BizDayworkItem">
        select * from biz_daywork_item where deleted = 0 and daywork_id = #{id} order by create_time desc limit 1
    </select>
    <delete id="physicalDelete">
        DELETE FROM biz_daywork
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
