<?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.ym.mec.biz.dal.dao.InspectionItemDao">
    <resultMap id="InspectionItem" type="com.ym.mec.biz.dal.entity.InspectionItem">
        <!--@mbg.generated-->
        <!--@Table inspection_item-->
        <id column="id_" property="id"/>
        <result column="inspection_id_" property="inspectionId"/>
        <result column="organ_id_" property="organId"/>
        <result column="user_id_" property="userId"/>
        <result column="item_" property="item" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="times_" property="times"/>
        <result column="planned_times_" property="plannedTimes"/>
        <result column="submitted_times_" property="submittedTimes"/>
        <result column="memo_" property="memo"/>
        <result column="operation_" property="operation"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="month" property="month"/>
        <result column="organName" property="organName"/>
        <result column="userName" property="userName"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_, inspection_id_, organ_id_, user_id_, item_, times_, planned_times_, submitted_times_,
        memo_, operation_, create_time_, update_time_
    </sql>
    <select id="get" parameterType="java.lang.Long" resultMap="InspectionItem">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from inspection_item
        where id_ = #{id}
    </select>
    <delete id="delete" parameterType="java.lang.Long">
        <!--@mbg.generated-->
        delete from inspection_item
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.InspectionItem"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into inspection_item (inspection_id_, organ_id_, user_id_, item_, times_, planned_times_,
        submitted_times_, memo_, operation_, create_time_, update_time_)
        values (#{inspectionId}, #{organId}, #{userId}, #{item,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{times}, #{plannedTimes},
        #{submittedTimes}, #{memo}, #{operation}, #{createTime}, #{updateTime})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.InspectionItem">
        <!--@mbg.generated-->
        update inspection_item
        <set>
            <if test="inspectionId != null">
                inspection_id_ = #{inspectionId},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="item != null">
                item_ = #{item,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="times != null">
                times_ = #{times},
            </if>
            <if test="plannedTimes != null">
                planned_times_ = #{plannedTimes},
            </if>
            <if test="submittedTimes != null">
                submitted_times_ = #{submittedTimes},
            </if>
            <if test="memo != null">
                memo_ = #{memo},
            </if>
            <if test="operation != null">
                operation_ = #{operation},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
        </set>
        where id_ = #{id}
    </update>
    <!-- 获取任务项数目及总次数 -->
    <select id="getItemCountAndTimes" resultMap="InspectionItem">
        SELECT inspection_id_ ,IFNULL(COUNT(*),0) planned_times_,SUM(times_) times_ FROM inspection_item
        WHERE inspection_id_ IN
        <foreach collection="inspectionIds" item="inspectionId" open="(" close=")" separator=",">
            #{inspectionId}
        </foreach>
        GROUP BY inspection_id_
    </select>

    <insert id="batchInsert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.InspectionItem"
            useGeneratedKeys="true">
        INSERT INTO inspection_item (inspection_id_, organ_id_, user_id_, item_, times_,operation_, create_time_,
        update_time_)
        VALUE
        <foreach collection="inspectionItems" item="inspectionItem" separator=",">
            (#{inspectionItem.inspectionId}, #{inspectionItem.organId}, #{inspectionItem.userId},
            #{inspectionItem.item,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{inspectionItem.times},
            #{inspectionItem.operation},
            #{inspectionItem.createTime}, #{inspectionItem.updateTime})
        </foreach>
    </insert>

    <select id="getItemByInspectionId" resultMap="InspectionItem">
        SELECT *
        FROM inspection_item
        WHERE inspection_id_ = #{inspectionId}
    </select>

    <delete id="deleteByIds">
        DELETE FROM inspection_item WHERE id_ IN
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!-- 批量更新任务次数 -->
    <update id="batchUpdate">
        <foreach collection="inspectionItems" item="inspectionItem" index="index" open="" close="" separator=";">
            update inspection_item
            <set>
                <if test="inspectionItem.times != null">
                    times_ = #{inspectionItem.times},
                </if>
                <if test="inspectionItem.operation != null">
                    operation_ = #{inspectionItem.operation},
                </if>
                <if test="inspectionItem.updateTime != null">
                    update_time_ = #{inspectionItem.updateTime}
                </if>
            </set>
            where id_ = #{inspectionItem.id}
        </foreach>
    </update>
    <delete id="deleteByInspectionId">
        DELETE
        FROM inspection_item
        WHERE inspection_id_ = #{InspectionId}
    </delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="InspectionItem" parameterType="map">
        SELECT ii.*,i.month_ month,o.name_ organName,su.real_name_ userName FROM inspection_item ii
        LEFT JOIN inspection i ON i.id_ = ii.inspection_id_
        LEFT JOIN organization o ON o.id_=ii.organ_id_
        LEFT JOIN sys_user su ON su.id_ = ii.user_id_
        <include refid="queryPageSql"/>
        ORDER BY i.month_ DESC ,ii.id_ DESC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM inspection_item ii
        LEFT JOIN inspection i ON i.id_ = ii.inspection_id_
        <include refid="queryPageSql"/>
    </select>

    <sql id="queryPageSql">
        <where>
            <if test="searchType != null and searchType != ''">
                <if test="searchType == 'INSPECTION_ITEM'">
                    AND ii.times_ > planned_times_ AND ii.item_ = 'INSPECT' AND ii.memo_ ='' AND DATE_FORMAT(i.month_,'%Y-%m-%d') >= #{searchStartTime}
                </if>
                <if test="searchType == 'STUDENT_VISIT'">
                    AND ii.item_ = 'VISIT' AND ii.memo_ ='' AND DATE_FORMAT(i.month_,'%Y-%m-%d') >= #{searchStartTime}
                    AND ii.times_ > (SELECT COUNT(DISTINCT sv.id_) FROM student_visit sv
                    WHERE sv.teacher_id_ = ii.user_id_ AND DATE_FORMAT(i.month_,'%Y-%m') = DATE_FORMAT(sv.visit_time_,'%Y-%m'))
                </if>
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(ii.organ_id_,#{organId})
            </if>
            <if test="userId != null">
                AND ii.user_id_ = #{userId}
            </if>
            <if test="operation != null">
                AND ii.operation_ = #{operation}
            </if>
            <if test="ids != null">
                AND FIND_IN_SET(ii.id_,#{ids})
            </if>
            <if test="startTime != null">
                AND i.month_ >= #{startTime}
            </if>
            <if test="endTime != null">
                <![CDATA[AND i.month_ <= #{endTime}]]>
            </if>
        </where>
    </sql>
</mapper>