<?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.ruoyi.system.mapper.visit.VisitPlanMapper">
  <resultMap id="BaseResultMap" type="com.ruoyi.system.domain.visit.VisitPlan">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="plan_title" jdbcType="VARCHAR" property="planTitle" />
    <result column="plan_obj_type" jdbcType="VARCHAR" property="planObjType" />
    <result column="plan_type" jdbcType="VARCHAR" property="planType" />
    <result column="plan_status" jdbcType="VARCHAR" property="planStatus" />
    <result column="plan_fl" jdbcType="VARCHAR" property="planFl" />
    <result column="plan_time" jdbcType="VARCHAR" property="planTime" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="is_valid" jdbcType="VARCHAR" property="isValid" />
    <result column="ylzd1" jdbcType="VARCHAR" property="ylzd1" />
    <result column="ylzd2" jdbcType="VARCHAR" property="ylzd2" />
    <result column="ylzd3" jdbcType="VARCHAR" property="ylzd3" />
    <result column="ylzd4" jdbcType="VARCHAR" property="ylzd4" />
    <result column="ylzd5" jdbcType="VARCHAR" property="ylzd5" />
    <result column="form_id" jdbcType="VARCHAR" property="formId" />
    <result column="template_id" jdbcType="VARCHAR" property="templateId" />
  </resultMap>
  <resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.ruoyi.system.domain.visit.VisitPlan">
    <result column="plan_file" jdbcType="LONGVARCHAR" property="planFile" />
    <result column="plan_explain" jdbcType="LONGVARCHAR" property="planExplain" />
  </resultMap>

  <resultMap id="ResultVisitPlanRateVO" type="com.ruoyi.system.domain.visit.vo.VisitPlanRate">
    <result column="tj_month" property="tjMonth"/>
    <result column="cnt_plan_safe" property="cntPlanSafe"/>
    <result column="cnt_plan_house" property="cntPlanHouse"/>
    <result column="cnt_plan" property="cntPlan"/>
    <result column="cnt_safe" property="cntSafe"/>
    <result column="cnt_house" property="cntHouse"/>
    <result column="cnt" property="cnt"/>
    <result column="rate" property="rate"/>
  </resultMap>

  <sql id="Example_Where_Clause">
    <where>
      <foreach collection="oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Update_By_Example_Where_Clause">
    <where>
      <foreach collection="example.oredCriteria" item="criteria" separator="or">
        <if test="criteria.valid">
          <trim prefix="(" prefixOverrides="and" suffix=")">
            <foreach collection="criteria.criteria" item="criterion">
              <choose>
                <when test="criterion.noValue">
                  and ${criterion.condition}
                </when>
                <when test="criterion.singleValue">
                  and ${criterion.condition} #{criterion.value}
                </when>
                <when test="criterion.betweenValue">
                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
                </when>
                <when test="criterion.listValue">
                  and ${criterion.condition}
                  <foreach close=")" collection="criterion.value" item="listItem" open="(" separator=",">
                    #{listItem}
                  </foreach>
                </when>
              </choose>
            </foreach>
          </trim>
        </if>
      </foreach>
    </where>
  </sql>
  <sql id="Base_Column_List">
    id, plan_title, plan_obj_type, plan_type, plan_status, plan_fl, plan_time,
    create_time, create_by, update_time, update_by, is_valid, ylzd1, ylzd2, ylzd3, ylzd4,
    ylzd5, form_id, template_id
  </sql>
  <sql id="Blob_Column_List">
    plan_file, plan_explain
  </sql>
  <select id="selectByExampleWithBLOBs" parameterType="com.ruoyi.system.domain.visit.VisitPlanExample" resultMap="ResultMapWithBLOBs">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from visit_plan
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByExample" parameterType="com.ruoyi.system.domain.visit.VisitPlanExample" resultMap="BaseResultMap">
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from visit_plan
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="ResultMapWithBLOBs">
    select
    <include refid="Base_Column_List" />
    ,
    <include refid="Blob_Column_List" />
    from visit_plan
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from visit_plan
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <delete id="deleteByExample" parameterType="com.ruoyi.system.domain.visit.VisitPlanExample">
    delete from visit_plan
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </delete>
  <insert id="insert" parameterType="com.ruoyi.system.domain.visit.VisitPlan">
    insert into visit_plan (id, plan_title, plan_obj_type,
      plan_type, plan_status, plan_fl,
      plan_time, create_time,
      create_by, update_time, update_by,
      is_valid, ylzd1, ylzd2,
      ylzd3, ylzd4, ylzd5,
      plan_file, plan_explain, form_id, template_id)
    values (#{id,jdbcType=VARCHAR}, #{planTitle,jdbcType=VARCHAR}, #{planObjType,jdbcType=VARCHAR},
      #{planType,jdbcType=VARCHAR}, #{planStatus,jdbcType=VARCHAR}, #{planFl,jdbcType=VARCHAR},
      #{planTime,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP},
      #{createBy,jdbcType=VARCHAR}, #{updateTime,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR},
      #{isValid,jdbcType=VARCHAR}, #{ylzd1,jdbcType=VARCHAR}, #{ylzd2,jdbcType=VARCHAR},
      #{ylzd3,jdbcType=VARCHAR}, #{ylzd4,jdbcType=VARCHAR}, #{ylzd5,jdbcType=VARCHAR},
      #{planFile,jdbcType=LONGVARCHAR}, #{planExplain,jdbcType=LONGVARCHAR}, #{formId,jdbcType=VARCHAR},#{templateId,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.ruoyi.system.domain.visit.VisitPlan">
    insert into visit_plan
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="planTitle != null">
        plan_title,
      </if>
      <if test="planObjType != null">
        plan_obj_type,
      </if>
      <if test="planType != null">
        plan_type,
      </if>
      <if test="planStatus != null">
        plan_status,
      </if>
      <if test="planFl != null">
        plan_fl,
      </if>
      <if test="planTime != null">
        plan_time,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="isValid != null">
        is_valid,
      </if>
      <if test="ylzd1 != null">
        ylzd1,
      </if>
      <if test="ylzd2 != null">
        ylzd2,
      </if>
      <if test="ylzd3 != null">
        ylzd3,
      </if>
      <if test="ylzd4 != null">
        ylzd4,
      </if>
      <if test="ylzd5 != null">
        ylzd5,
      </if>
      <if test="planFile != null">
        plan_file,
      </if>
      <if test="planExplain != null">
        plan_explain,
      </if>
      <if test="formId != null">
        form_id,
      </if>
      <if test="templateId != null">
        template_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=VARCHAR},
      </if>
      <if test="planTitle != null">
        #{planTitle,jdbcType=VARCHAR},
      </if>
      <if test="planObjType != null">
        #{planObjType,jdbcType=VARCHAR},
      </if>
      <if test="planType != null">
        #{planType,jdbcType=VARCHAR},
      </if>
      <if test="planStatus != null">
        #{planStatus,jdbcType=VARCHAR},
      </if>
      <if test="planFl != null">
        #{planFl,jdbcType=VARCHAR},
      </if>
      <if test="planTime != null">
        #{planTime,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="isValid != null">
        #{isValid,jdbcType=VARCHAR},
      </if>
      <if test="ylzd1 != null">
        #{ylzd1,jdbcType=VARCHAR},
      </if>
      <if test="ylzd2 != null">
        #{ylzd2,jdbcType=VARCHAR},
      </if>
      <if test="ylzd3 != null">
        #{ylzd3,jdbcType=VARCHAR},
      </if>
      <if test="ylzd4 != null">
        #{ylzd4,jdbcType=VARCHAR},
      </if>
      <if test="ylzd5 != null">
        #{ylzd5,jdbcType=VARCHAR},
      </if>
      <if test="planFile != null">
        #{planFile,jdbcType=LONGVARCHAR},
      </if>
      <if test="planExplain != null">
        #{planExplain,jdbcType=LONGVARCHAR},
      </if>
      <if test="formId != null">
        #{formId,jdbcType=VARCHAR},
      </if>
      <if test="templateId != null">
        #{templateId,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <select id="countByExample" parameterType="com.ruoyi.system.domain.visit.VisitPlanExample" resultType="java.lang.Long">
    select count(*) from visit_plan
    <if test="_parameter != null">
      <include refid="Example_Where_Clause" />
    </if>
  </select>
  <update id="updateByExampleSelective" parameterType="map">
    update visit_plan
    <set>
      <if test="record.id != null">
        id = #{record.id,jdbcType=VARCHAR},
      </if>
      <if test="record.planTitle != null">
        plan_title = #{record.planTitle,jdbcType=VARCHAR},
      </if>
      <if test="record.planObjType != null">
        plan_obj_type = #{record.planObjType,jdbcType=VARCHAR},
      </if>
      <if test="record.planType != null">
        plan_type = #{record.planType,jdbcType=VARCHAR},
      </if>
      <if test="record.planStatus != null">
        plan_status = #{record.planStatus,jdbcType=VARCHAR},
      </if>
      <if test="record.planFl != null">
        plan_fl = #{record.planFl,jdbcType=VARCHAR},
      </if>
      <if test="record.planTime != null">
        plan_time = #{record.planTime,jdbcType=VARCHAR},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.createBy != null">
        create_by = #{record.createBy,jdbcType=VARCHAR},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="record.updateBy != null">
        update_by = #{record.updateBy,jdbcType=VARCHAR},
      </if>
      <if test="record.isValid != null">
        is_valid = #{record.isValid,jdbcType=VARCHAR},
      </if>
      <if test="record.ylzd1 != null">
        ylzd1 = #{record.ylzd1,jdbcType=VARCHAR},
      </if>
      <if test="record.ylzd2 != null">
        ylzd2 = #{record.ylzd2,jdbcType=VARCHAR},
      </if>
      <if test="record.ylzd3 != null">
        ylzd3 = #{record.ylzd3,jdbcType=VARCHAR},
      </if>
      <if test="record.ylzd4 != null">
        ylzd4 = #{record.ylzd4,jdbcType=VARCHAR},
      </if>
      <if test="record.ylzd5 != null">
        ylzd5 = #{record.ylzd5,jdbcType=VARCHAR},
      </if>
      <if test="record.planFile != null">
        plan_file = #{record.planFile,jdbcType=LONGVARCHAR},
      </if>
      <if test="record.planExplain != null">
        plan_explain = #{record.planExplain,jdbcType=LONGVARCHAR},
      </if>
      <if test="record.formId != null">
        form_id = #{record.formId,jdbcType=VARCHAR},
      </if>
      <if test="record.templateId != null">
        template_id = #{record.templateId,jdbcType=VARCHAR},
      </if>
    </set>
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExampleWithBLOBs" parameterType="map">
    update visit_plan
    set id = #{record.id,jdbcType=VARCHAR},
      plan_title = #{record.planTitle,jdbcType=VARCHAR},
      plan_obj_type = #{record.planObjType,jdbcType=VARCHAR},
      plan_type = #{record.planType,jdbcType=VARCHAR},
      plan_status = #{record.planStatus,jdbcType=VARCHAR},
      plan_fl = #{record.planFl,jdbcType=VARCHAR},
      plan_time = #{record.planTime,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      create_by = #{record.createBy,jdbcType=VARCHAR},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      update_by = #{record.updateBy,jdbcType=VARCHAR},
      is_valid = #{record.isValid,jdbcType=VARCHAR},
      ylzd1 = #{record.ylzd1,jdbcType=VARCHAR},
      ylzd2 = #{record.ylzd2,jdbcType=VARCHAR},
      ylzd3 = #{record.ylzd3,jdbcType=VARCHAR},
      ylzd4 = #{record.ylzd4,jdbcType=VARCHAR},
      ylzd5 = #{record.ylzd5,jdbcType=VARCHAR},
      plan_file = #{record.planFile,jdbcType=LONGVARCHAR},
      plan_explain = #{record.planExplain,jdbcType=LONGVARCHAR},
    form_id = #{record.formId,jdbcType=VARCHAR},
    template_id = #{record.templateId,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByExample" parameterType="map">
    update visit_plan
    set id = #{record.id,jdbcType=VARCHAR},
      plan_title = #{record.planTitle,jdbcType=VARCHAR},
      plan_obj_type = #{record.planObjType,jdbcType=VARCHAR},
      plan_type = #{record.planType,jdbcType=VARCHAR},
      plan_status = #{record.planStatus,jdbcType=VARCHAR},
      plan_fl = #{record.planFl,jdbcType=VARCHAR},
      plan_time = #{record.planTime,jdbcType=VARCHAR},
      create_time = #{record.createTime,jdbcType=TIMESTAMP},
      create_by = #{record.createBy,jdbcType=VARCHAR},
      update_time = #{record.updateTime,jdbcType=TIMESTAMP},
      update_by = #{record.updateBy,jdbcType=VARCHAR},
      is_valid = #{record.isValid,jdbcType=VARCHAR},
      ylzd1 = #{record.ylzd1,jdbcType=VARCHAR},
      ylzd2 = #{record.ylzd2,jdbcType=VARCHAR},
      ylzd3 = #{record.ylzd3,jdbcType=VARCHAR},
      ylzd4 = #{record.ylzd4,jdbcType=VARCHAR},
      ylzd5 = #{record.ylzd5,jdbcType=VARCHAR},
    form_id = #{record.formId,jdbcType=VARCHAR},
    template_id = #{record.templateId,jdbcType=VARCHAR}
    <if test="_parameter != null">
      <include refid="Update_By_Example_Where_Clause" />
    </if>
  </update>
  <update id="updateByPrimaryKeySelective" parameterType="com.ruoyi.system.domain.visit.VisitPlan">
    update visit_plan
    <set>
      <if test="planTitle != null">
        plan_title = #{planTitle,jdbcType=VARCHAR},
      </if>
      <if test="planObjType != null">
        plan_obj_type = #{planObjType,jdbcType=VARCHAR},
      </if>
      <if test="planType != null">
        plan_type = #{planType,jdbcType=VARCHAR},
      </if>
      <if test="planStatus != null">
        plan_status = #{planStatus,jdbcType=VARCHAR},
      </if>
      <if test="planFl != null">
        plan_fl = #{planFl,jdbcType=VARCHAR},
      </if>
      <if test="planTime != null">
        plan_time = #{planTime,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="createBy != null">
        create_by = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="isValid != null">
        is_valid = #{isValid,jdbcType=VARCHAR},
      </if>
      <if test="ylzd1 != null">
        ylzd1 = #{ylzd1,jdbcType=VARCHAR},
      </if>
      <if test="ylzd2 != null">
        ylzd2 = #{ylzd2,jdbcType=VARCHAR},
      </if>
      <if test="ylzd3 != null">
        ylzd3 = #{ylzd3,jdbcType=VARCHAR},
      </if>
      <if test="ylzd4 != null">
        ylzd4 = #{ylzd4,jdbcType=VARCHAR},
      </if>
      <if test="ylzd5 != null">
        ylzd5 = #{ylzd5,jdbcType=VARCHAR},
      </if>
      <if test="planFile != null">
        plan_file = #{planFile,jdbcType=LONGVARCHAR},
      </if>
      <if test="planExplain != null">
        plan_explain = #{planExplain,jdbcType=LONGVARCHAR},
      </if>
      <if test="formId != null">
        form_id = #{formId,jdbcType=VARCHAR},
      </if>
      <if test="templateId != null">
        template_id = #{templateId,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKeyWithBLOBs" parameterType="com.ruoyi.system.domain.visit.VisitPlan">
    update visit_plan
    set plan_title = #{planTitle,jdbcType=VARCHAR},
      plan_obj_type = #{planObjType,jdbcType=VARCHAR},
      plan_type = #{planType,jdbcType=VARCHAR},
      plan_status = #{planStatus,jdbcType=VARCHAR},
      plan_fl = #{planFl,jdbcType=VARCHAR},
      plan_time = #{planTime,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      create_by = #{createBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      is_valid = #{isValid,jdbcType=VARCHAR},
      ylzd1 = #{ylzd1,jdbcType=VARCHAR},
      ylzd2 = #{ylzd2,jdbcType=VARCHAR},
      ylzd3 = #{ylzd3,jdbcType=VARCHAR},
      ylzd4 = #{ylzd4,jdbcType=VARCHAR},
      ylzd5 = #{ylzd5,jdbcType=VARCHAR},
      plan_file = #{planFile,jdbcType=LONGVARCHAR},
      plan_explain = #{planExplain,jdbcType=LONGVARCHAR},
    form_id = #{formId,jdbcType=VARCHAR},
    template_id = #{templateId,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.ruoyi.system.domain.visit.VisitPlan">
    update visit_plan
    set plan_title = #{planTitle,jdbcType=VARCHAR},
      plan_obj_type = #{planObjType,jdbcType=VARCHAR},
      plan_type = #{planType,jdbcType=VARCHAR},
      plan_status = #{planStatus,jdbcType=VARCHAR},
      plan_fl = #{planFl,jdbcType=VARCHAR},
      plan_time = #{planTime,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      create_by = #{createBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      is_valid = #{isValid,jdbcType=VARCHAR},
      ylzd1 = #{ylzd1,jdbcType=VARCHAR},
      ylzd2 = #{ylzd2,jdbcType=VARCHAR},
      ylzd3 = #{ylzd3,jdbcType=VARCHAR},
      ylzd4 = #{ylzd4,jdbcType=VARCHAR},
      ylzd5 = #{ylzd5,jdbcType=VARCHAR},
    form_id = #{formId,jdbcType=VARCHAR},
    template_id = #{templateId,jdbcType=VARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>


  <select id="getPlanRate" resultMap="ResultVisitPlanRateVO" parameterType="map">
    select #{tjMonth} tj_month,
           count(case when plan.plan_fl=1 then task.id else null end) cnt_plan_safe,
           count(case when plan.plan_fl=2 then task.id else null end) cnt_plan_house,
           count(*) cnt_plan,
           count(case when plan.plan_fl=1 and DATE_FORMAT(task.update_time,'%Y-%m') = #{tjMonth} then task.id else null end) cnt_safe,
           count(case when plan.plan_fl=2 and DATE_FORMAT(task.update_time,'%Y-%m') = #{tjMonth} then task.id else null end) cnt_house,
           count(case when DATE_FORMAT(task.update_time,'%Y-%m') = #{tjMonth} then task.id else null end) cnt
    FROM visit_plan plan,visit_plan_task task
    where plan.id=task.plan_id
    and task.is_valid=1
    and DATE_FORMAT(task.create_time,'%Y-%m') &lt;= #{tjMonth}
    and (DATE_FORMAT(task.update_time,'%Y-%m') >= #{tjMonth} or task.update_time is null)
  </select>
 </mapper>
