<?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.project.zcustom.mapper.unit.LargePlanMapper">

    <resultMap type="com.project.zcustom.domain.addional.LargePlan" id="LargePlanResult">
        <result property="id"    column="id"    />
        <result property="projectId"    column="project_id"    />
        <result property="type"    column="type"    />
        <result property="head"    column="head"    />
        <result property="content"    column="content"    />
        <result property="projectName"    column="project_name"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectLargePlanVo">
        select id, project_id, type, head, content, start_time, end_time, create_by, create_time, update_by, update_time from large_plat_plan
    </sql>

    <select id="selectLargePlanList" parameterType="LargePlan" resultMap="LargePlanResult">
        <include refid="selectLargePlanVo"/>
        <where>
            <if test="projectId != null "> and project_id = #{projectId}</if>
            <if test="type != null  and type != ''"> and type = #{type}</if>
            <if test="head != null  and head != ''"> and head = #{head}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="endTime != null "> and end_time = #{endTime}</if>
        </where>
    </select>

    <select id="selectLargePlanById" parameterType="Long" resultMap="LargePlanResult">
        <include refid="selectLargePlanVo"/>
        where id = #{id}
    </select>

    <insert id="insertLargePlan" parameterType="LargePlan" useGeneratedKeys="true" keyProperty="id">
        insert into large_plat_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="projectId != null">project_id,</if>
            <if test="type != null">type,</if>
            <if test="head != null">head,</if>
            <if test="content != null">content,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="projectId != null">#{projectId},</if>
            <if test="type != null">#{type},</if>
            <if test="head != null">#{head},</if>
            <if test="content != null">#{content},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</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>
         </trim>
    </insert>

    <update id="updateLargePlan" parameterType="LargePlan">
        update large_plat_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="head != null">head = #{head},</if>
            <if test="content != null">content = #{content},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteLargePlanById" parameterType="Long">
        delete from large_plat_plan where id = #{id}
    </delete>

    <delete id="deleteLargePlanByIds" parameterType="String">
        delete from large_plat_plan where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="reallyDeleteByWrapper">
        DELETE FROM large_plat_plan ${ew.customSqlSegment}
    </delete>

    <select id="getCheckProjectNum" resultType="int">
        SELECT
            COUNT(*)
        FROM
            large_plat_plan
        WHERE
            type = 0
          AND DATE( start_time ) >= #{arg0}
          OR DATE( end_time ) &lt;= #{arg1}
        GROUP BY
            `type`,
            project_id
    </select>

    <select id="planCheckList" resultMap="LargePlanResult">
        SELECT
            head,
            content,
            project_name,
            start_time,
            end_time
        FROM
            `large_plat_plan` a
                LEFT JOIN large_plat_project b ON a.project_id = b.id
        WHERE
            a.del_flag = 0
          AND b.del_flag = 0
          AND a.type = '0'
          AND (
            DATE( a.start_time ) BETWEEN DATE_SUB( CURDATE(), INTERVAL ( DAYOFWEEK( CURDATE()) - 2 ) DAY )
            AND DATE_ADD( CURDATE(), INTERVAL ( 8 - DAYOFWEEK( CURDATE())) DAY )
            OR DATE( a.end_time ) BETWEEN DATE_SUB( CURDATE(), INTERVAL ( DAYOFWEEK( CURDATE()) - 2 ) DAY )
            AND DATE_ADD( CURDATE(), INTERVAL ( 8 - DAYOFWEEK( CURDATE())) DAY ))
    </select>

    <select id="planDoneList" resultMap="LargePlanResult">
        SELECT
            head,
            content,
            project_name,
            start_time,
            end_time
        FROM
            `large_plat_plan` a
                LEFT JOIN large_plat_project b ON a.project_id = b.id
        WHERE
            a.del_flag = 0
          AND b.del_flag = 0
          AND a.type = '1'
          AND (
            DATE( a.start_time ) BETWEEN DATE_SUB( CURDATE(), INTERVAL ( DAYOFWEEK( CURDATE()) - 2 ) DAY )
            AND DATE_ADD( CURDATE(), INTERVAL ( 8 - DAYOFWEEK( CURDATE())) DAY )
            OR DATE( a.end_time ) BETWEEN DATE_SUB( CURDATE(), INTERVAL ( DAYOFWEEK( CURDATE()) - 2 ) DAY )
            AND DATE_ADD( CURDATE(), INTERVAL ( 8 - DAYOFWEEK( CURDATE())) DAY ))
    </select>
</mapper>
