<?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.InspectionItemPlanDao">
    <resultMap id="InspectionItemPlan" type="com.ym.mec.biz.dal.entity.InspectionItemPlan">
        <!--@mbg.generated-->
        <!--@Table inspection_item_plan-->
        <id column="id_" property="id"/>
        <result column="inspection_id_" property="inspectionId"/>
        <result column="item_id_" property="itemId"/>
        <result column="user_id_" property="userId"/>
        <result column="operator_user_id_" property="operatorUserId"/>
        <result column="plan_start_" property="planStart"/>
        <result column="plan_end_" property="planEnd"/>
        <result column="organ_id_" property="organId"/>
        <result column="cooperation_organ_id_" property="cooperationOrganId"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="school_gps_" property="schoolGps"/>
        <result column="submited_gps_" property="submitedGps"/>
        <result column="submited_time_" property="submitedTime"/>
        <result column="memo_" property="memo"/>
        <result column="conclusion_status_" property="conclusionStatus"/>
        <result column="status_" property="status"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="cooperationName" property="cooperationName"/>
        <result column="musicGroupName" property="musicGroupName"/>
        <result column="organName" property="organName"/>
        <result column="realName" property="realName"/>
		<result column="tenant_id_" property="tenantId" />
    </resultMap>
    <select id="get" parameterType="java.lang.Long" resultMap="InspectionItemPlan">
        <!--@mbg.generated-->
        select * from inspection_item_plan
        where id_ = #{id}
    </select>
    <delete id="delete" parameterType="java.lang.Long">
        <!--@mbg.generated-->
        delete from inspection_item_plan
        where id_ = #{id}
    </delete>
    <insert id="insert" keyColumn="id_" keyProperty="id" parameterType="com.ym.mec.biz.dal.entity.InspectionItemPlan"
            useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into inspection_item_plan (inspection_id_, item_id_, user_id_,operator_user_id_, plan_start_, plan_end_,
        organ_id_,cooperation_organ_id_,
        music_group_id_, school_gps_, submited_gps_, submited_time_, memo_, conclusion_status_, status_,
        create_time_, update_time_,tenant_id_)
        values (#{inspectionId}, #{itemId}, #{userId},#{operatorUserId}, #{planStart}, #{planEnd}, #{organId},#{cooperationOrganId},
        #{musicGroupId}, #{schoolGps}, #{submitedGps}, #{submitedTime}, #{memo},#{conclusionStatus},#{status},
        #{createTime}, #{updateTime},#{tenantId})
    </insert>
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.InspectionItemPlan">
        <!--@mbg.generated-->
        update inspection_item_plan
        <set>
            <if test="inspectionId != null">
                inspection_id_ = #{inspectionId},
            </if>
            <if test="itemId != null">
                item_id_ = #{itemId},
            </if>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="operatorUserId != null">
                operator_user_id_ = #{userId},
            </if>
            <if test="planStart != null">
                plan_start_ = #{planStart},
            </if>
            <if test="planEnd != null">
                plan_end_ = #{planEnd},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="cooperationOrganId != null">
                cooperation_organ_id_ = #{cooperationOrganId},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
            <if test="schoolGps != null">
                school_gps_ = #{schoolGps},
            </if>
            <if test="submitedGps != null">
                submited_gps_ = #{submitedGps},
            </if>
            <if test="submitedTime != null">
                submited_time_ = #{submitedTime},
            </if>
            <if test="memo != null">
                memo_ = #{memo},
            </if>
            <if test="conclusionStatus != null">
                conclusion_status_ = #{conclusionStatus},
            </if>
            <if test="status != null">
                status_ = #{status},
            </if>
            <if test="createTime != null">
                create_time_ = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
        </set>
        where id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="InspectionItemPlan" parameterType="map">
        SELECT iip.*,su.real_name_ realName,o.name_ organName,co.name_ cooperationName,mg.name_ musicGroupName FROM
        inspection_item_plan iip
        LEFT JOIN sys_user su ON su.id_ = iip.user_id_
        LEFT JOIN organization o ON o.id_=iip.organ_id_
        LEFT JOIN cooperation_organ co ON co.id_=iip.cooperation_organ_id_
        LEFT JOIN music_group mg ON mg.id_ = iip.music_group_id_
        <include refid="queryPageSql"/>
        ORDER BY iip.plan_start_ ASC
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM inspection_item_plan iip
        LEFT JOIN music_group mg ON mg.id_ = iip.music_group_id_
        <include refid="queryPageSql"/>
    </select>

    <sql id="queryPageSql">
        <where>
        	iip.tenant_id_ = #{tenantId}
            <if test="itemId != null">
                AND iip.item_id_ = #{itemId}
            </if>
            <if test="userId != null">
                AND iip.user_id_ = #{userId}
            </if>
            <if test="organId != null">
                AND FIND_IN_SET(iip.organ_id_,#{organId})
            </if>
            <if test="cooperationOrganId != null">
                AND iip.cooperation_organ_id_ = #{cooperationOrganId}
            </if>
            <if test="musicGroupId != null">
                AND iip.music_group_id_ = #{musicGroupId}
            </if>
            <if test="conclusionStatus != null">
                AND iip.conclusion_status_ = #{conclusionStatus}
            </if>
            <if test="status !=null">
                AND iip.status_ = #{status}
            </if>
            <if test='hasSubmit != null and hasSubmit.toString()=="1".toString()'>
                AND iip.status_ > 0
            </if>
            <if test='hasSubmit != null and hasSubmit.toString()=="0".toString()'>
                AND iip.status_ = 0
            </if>
            <if test="searchType != null and searchType != ''">
                <if test="searchType == 'MUSIC_PATROL_ITEM'">
                    AND iip.conclusion_status_ = 1 AND iip.memo_='' AND iip.plan_start_ >= #{searchStartTime}
                </if>
                <if test="searchType == 'INSPECTION_ITEM_PLAN'">
                    AND iip.status_ = 0 AND iip.memo_ = '' AND iip.plan_start_ >= #{searchStartTime} AND DATE_FORMAT(iip.plan_start_,'%Y-%m-%d') &lt; DATE_FORMAT(NOW(),'%Y-%m-%d')
                </if>
            </if>
            <if test="startTime != null">
                AND iip.plan_start_ >= #{startTime}
            </if>
            <if test="endTime != null">
                <![CDATA[AND iip.plan_start_ <= #{endTime}]]>
            </if>
            <if test="search != null">
                AND mg.name_ LIKE CONCAT('%', #{search}, '%')
            </if>
            <if test="ids != null and ids != ''">
                AND FIND_IN_SET(iip.id_,#{ids})
            </if>
        </where>
    </sql>

    <select id="getStartPlan" resultMap="InspectionItemPlan">
        <![CDATA[
        SELECT *
        FROM inspection_item_plan
        WHERE plan_start_ >= #{startTime}
          AND plan_start_ <= #{endTime}
        ]]>
    </select>

    <select id="getPlanInfo" resultMap="InspectionItemPlan">
        SELECT iip.*, su.real_name_ realName, o.name_ organName, co.name_ cooperationName, mg.name_ musicGroupName
        FROM inspection_item_plan iip
                 LEFT JOIN sys_user su ON su.id_ = iip.user_id_
                 LEFT JOIN organization o ON o.id_ = iip.organ_id_
                 LEFT JOIN cooperation_organ co ON co.id_ = iip.cooperation_organ_id_
                 LEFT JOIN music_group mg ON mg.id_ = iip.music_group_id_
        WHERE iip.id_ = #{id}
    </select>

    <select id="getMusicGroupItemPlanCount" resultType="int">
        <![CDATA[
        SELECT COUNT(*)
        FROM inspection_item_plan
        WHERE music_group_id_ = #{musicGroupId}
          AND item_id_ = #{itemId}
          AND plan_start_ >= #{startTime}
          AND plan_start_ <= #{endTime}
        ]]>
        <if test="id != null">
            AND id_ != #{id}
        </if>
    </select>
</mapper>