<?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.InspectionDao">
    <resultMap id="Inspection" type="com.ym.mec.biz.dal.entity.Inspection">
        <!--@mbg.generated-->
        <!--@Table inspection-->
        <id column="id_" property="id"/>
        <result column="organ_id_" property="organId"/>
        <result column="user_id_" property="userId"/>
        <result column="month_" property="month"/>
        <result column="memo_" property="memo"/>
        <result column="operation_" property="operation"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="organName" property="organName"/>
        <result column="userName" property="userName"/>
        <collection property="inspectionItems" columnPrefix="ie_" ofType="com.ym.mec.biz.dal.entity.InspectionItem"
                    resultMap="com.ym.mec.biz.dal.dao.InspectionItemDao.InspectionItem"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id_, organ_id_, user_id_, month_, memo_, operation_, create_time_, update_time_
    </sql>
    <select id="get" parameterType="java.lang.Long" resultMap="Inspection">
        <!--@mbg.generated-->
        select
        <include refid="Base_Column_List"/>
        from inspection
        where id_ = #{id}
    </select>
    <delete id="delete" parameterType="java.lang.Long">
        delete
        from inspection
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.Inspection"
            useGeneratedKeys="true">
        insert into inspection (organ_id_, user_id_, month_, operation_, create_time_, update_time_)
        values (#{organId}, #{userId}, #{month}, #{operation}, #{createTime}, #{updateTime})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.Inspection">
        <!--@mbg.generated-->
        update inspection
        <set>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="month != null">
                month_ = #{month},
            </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="queryPage" resultMap="Inspection" parameterType="map">
        SELECT i.*,o.name_ organName,su.real_name_ userName FROM inspection i
        LEFT JOIN organization o ON o.id_=i.organ_id_
        LEFT JOIN sys_user su ON su.id_ = i.user_id_
        <include refid="queryPageSql"/>
            ORDER BY i.month_ DESC, i.id_ DESC
        <include refid="global.limit"/>
    </select>

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

    <sql id="queryPageSql">
        <where>
            <if test="organId != null">
                AND FIND_IN_SET(i.organ_id_,#{organId})
            </if>
            <if test="userId != null">
                AND i.user_id_ = #{userId}
            </if>
            <if test="operation != null">
                AND i.operation_ = #{operation}
            </if>
            <if test="startTime != null">
                AND i.month_ >= #{startTime}
            </if>
            <if test="endTime != null">
                <![CDATA[AND i.month_ <= #{endTime}]]>
            </if>
        </where>
    </sql>
    <select id="getInfo" resultMap="Inspection">
        SELECT i.*,
               ii.id_              ie_id_,
               ii.inspection_id_   ie_inspection_id_,
               ii.organ_id_        ie_organ_id_,
               ii.user_id_         ie_user_id_,
               ii.item_            ie_item_,
               ii.times_           ie_times_,
               ii.planned_times_   ie_planned_times_,
               ii.submitted_times_ ie_submitted_times_,
               ii.memo_            ie_memo_,
               ii.operation_       ie_operation_,
               ii.create_time_     ie_create_time_,
               ii.update_time_     ie_update_time_,
               o.name_             organName,
               su.real_name_       userName
        FROM inspection i
                 LEFT JOIN organization o ON o.id_ = i.organ_id_
                 LEFT JOIN sys_user su ON su.id_ = i.user_id_
                 LEFT JOIN inspection_item ii ON ii.inspection_id_ = i.id_
        WHERE i.id_ = #{id}
    </select>
</mapper>