<?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.wisenergy.mapper.WorkCollectMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.wisenergy.model.app.WorkCollect">
        <id column="id" property="id" />
        <result column="user_id" property="userId" />
        <result column="work_day" property="workDay" />
        <result column="total_time" property="totalTime" />
        <result column="status" property="status" />
        <result column="create_time" property="createTime" />
        <result column="modify_time" property="modifyTime" />
        <result column="reject_time" property="rejectTime" />
    </resultMap>

    <sql id="vals">
    #{userId},#{workDay},#{totalTime},#{status},#{rejectTime},now(),now()
    </sql>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, user_id, work_day, total_time, status,reject_time,create_time,modify_time
    </sql>
    <sql id="cols_exclude_id">
        user_id, work_day, total_time, status,reject_time,create_time,modify_time
    </sql>

    <sql id="table">
        work_collect
    </sql>

    <sql id="CalendarDto_List" >
       user_id, work_day,status
    </sql>
    <insert id="insertWorkCollect">
        INSERT INTO
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        VALUES (
        <include refid="vals"/>
        )
    </insert>

    <insert id="addManyLeaveCollect" keyProperty="id" useGeneratedKeys="true">
        insert into
        <include refid="table"/>
        (<include refid="cols_exclude_id"/>)
        values
        <foreach collection="workCollects"  item="workCollect" separator=",">
            (#{workCollect.userId},#{workCollect.workDay},#{workCollect.totalTime},
            #{workCollect.status},null,#{workCollect.createTime},#{workCollect.modifyTime}
            )
        </foreach>
    </insert>

    <update id="updateStatusOrTotalTime">
        UPDATE
        <include refid="table"/>
        <set>

            <if test="null != status">
                status=#{status},
            </if>
            <if test="null !=totalTime">
                total_time = #{totalTime},
            </if>
            <if test="3 ==status ">
               reject_time = now(),
            </if>
            modify_time = now()
        </set>
        WHERE id = #{id}

    </update>


    <select id="getWorkMonth" resultType="cn.wisenergy.model.dto.CalendarDto">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        <include refid="table"/>
        <where>
            <if test="null != userId">
            AND    user_id = #{userId}
            </if>
            <if test="null != workMonth">
                AND DATE_FORMAT(work_day, '%Y%m') = DATE_FORMAT(#{workMonth} ,'%Y%m')
            </if>
        </where>
    </select>
    <select id="getCompletedByUserIdAndMonth" resultType="cn.wisenergy.model.app.WorkCollect">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        <include refid="table"/>
        <where>
        total_time = 8 AND status !=3
        <if test="userId !=null">
        AND    user_id = #{userId}
        </if>
        <if test="month !=null">
        AND    DATE_FORMAT(work_day, '%Y%m') = DATE_FORMAT(#{month} ,'%Y%m')
        </if>

        </where>

    </select>

    <select id="getWorkCollectByUserIdAndWorkDay" resultType="cn.wisenergy.model.app.WorkCollect">
      SELECT
        <include refid="Base_Column_List"/>
        FROM
        <include refid="table"/>
        where user_id = #{userId} and work_day = #{workDay}
    </select>

</mapper>
