<?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.railway.business.safetool.mapper.SecScheduledMapper">

  <sql id="Base_Column_List">
    id,
    dept_id,
    store_place,
    lab_id,
    scheduled_time,
    state,
    del_flag,
    create_by,
    create_time,
    update_by,
    update_time
  </sql>

  <insert id="insert" parameterType="com.railway.business.safetool.domain.SecScheduled">

    <selectKey keyProperty="id" order="AFTER" resultType="Long">
      select @@IDENTITY as id
    </selectKey>

    INSERT INTO sec_scheduled
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test='null != deptId'>
        dept_id,
      </if>

      <if test='null != labId'>
        lab_id,
      </if>
      <if test='null != scheduledTime'>
        scheduled_time,
      </if>
      <if test='null != state'>
        state,
      </if>
      <if test='null != delFlag'>
        del_flag,
      </if>
      <if test='null != createBy'>
        create_by,
      </if>
      <if test='null != createTime'>
        create_time,
      </if>
      <if test='null != updateBy'>
        update_by,
      </if>
      <if test='null != updateTime'>
        update_time
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test='null != deptId'>
        #{deptId},
      </if>

      <if test='null != labId'>
        #{labId},
      </if>
      <if test='null != scheduledTime'>
        #{scheduledTime},
      </if>
      <if test='null != state'>
        #{state},
      </if>
      <if test='null != delFlag'>
        #{delFlag},
      </if>
      <if test='null != createBy'>
        #{createBy},
      </if>
      <if test='null != createTime'>
        #{createTime},
      </if>
      <if test='null != updateBy'>
        #{updateBy},
      </if>
      <if test='null != updateTime'>
        #{updateTime}
      </if>
    </trim>
  </insert>

  <delete id="delete">
    UPDATE sec_scheduled
    set del_flag='1'
    WHERE id = #{id}
  </delete>

  <update id="update" parameterType="com.railway.business.safetool.domain.SecScheduled">
    UPDATE sec_scheduled
    <set>
      <if test='null != deptId'>dept_id = #{deptId},</if>
      <if test='null != labId'>lab_id = #{labId},</if>
      <if test='null != scheduledTime'>scheduled_time = #{scheduledTime},</if>
      <if test='null != state'>state = #{state},</if>
      <if test='null != delFlag'>del_flag = #{delFlag},</if>
      <if test='null != createBy'>create_by = #{createBy},</if>
      <if test='null != createTime'>create_time = #{createTime},</if>
      <if test='null != updateBy'>update_by = #{updateBy},</if>
      <if test='null != updateTime'>update_time = #{updateTime}</if>
    </set>
    WHERE id = #{id}
  </update>

  <update id="updateScheduled">
    update sec_scheduled
    set state = #{state}
    where id = #{secid}
  </update>

  <select id="getInfo" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"/>
    FROM sec_experiment
    WHERE del_flag='0' and id = #{id}
  </select>

  <resultMap id="BaseResultMap2" type="com.railway.business.safetool.domain.vo.SecScheduledCountVo">
    <result column="id" property="id"/>
    <result column="dept_id" property="deptId"/>
    <result column="lab_id" property="labId"/>
    <result column="scheduled_time" property="scheduledTime"/>
    <result column="deptName" property="deptName"/>
    <result column="labName" property="labName"/>
    <result column="c" property="c"/>
  </resultMap>

  <select id="getList" resultMap="BaseResultMap2">
    select s.id,s.dept_id,s.lab_id,s.scheduled_time ,d1.dept_name deptName,d2.dept_name labName
    ,concat(count(1),'件') c
    from sec_scheduled s
    left join sec_scheduled_tool r on r.scheduled_id = s.id
    inner join base_safety_tool t on t.tool_id = r.tool_id
    left join sys_dept d1 on s.dept_id = d1.dept_id
    left join sys_dept d2 on s.lab_id = d2.dept_id
    where s.del_flag = '0' and s.state = 0 and t.del_flag = '0' and t.state = '1'
    and s.dept_id=#{deptId}
    <if test="labId!=null and labId!=''">
      and s.lab_id=#{labId}
    </if>
    group by s.id
    <if test="orderby!=null and orderby!=''">
      order by scheduled_time ${orderby}
    </if>
  </select>

  <resultMap id="BaseResultMap"
    type="com.railway.business.safetool.domain.vo.SecScheduledToolListVo">
    <result column="id" property="id"/>
    <result column="dept_id" property="deptId"/>
    <result column="lab_id" property="labId"/>
    <result column="scheduled_time" property="scheduledTime"/>
    <result column="deptName" property="deptName"/>
    <result column="labName" property="labName"/>
    <collection property="baseSafetyTools"
      ofType="com.railway.business.safetool.domain.vo.SecScheduledToolVo">
      <result column="tool_id" property="toolId"/>
      <result column="tool_code" property="toolCode"/>
      <result column="scheduled_tool_id" property="id"/>
      <result column="unit" property="unit"/>
      <result column="toolName" property="toolName"/>
    </collection>
  </resultMap>

  <select id="getDetail" resultMap="BaseResultMap">
    select s.id, s.dept_id, s.lab_id, s.scheduled_time,
    d1.dept_name deptName, d2.dept_name labName,
    d3.dict_label toolName ,t.unit,t.tool_id,t.tool_code, r.id as scheduled_tool_id
    FROM sec_scheduled s
    LEFT JOIN sec_scheduled_tool r ON r.scheduled_id = s.id
    INNER JOIN base_safety_tool t ON t.tool_id = r.tool_id
    left join sys_dept d1 on s.dept_id = d1.dept_id
    left join sys_dept d2 on s.lab_id = d2.dept_id
    left join sys_dict_data d3 on d3.dict_type = 'tool_type' and d3.dict_value = t.tool_type
    WHERE s.del_flag = '0' and s.state = 0 and t.del_flag = '0' and t.state = '1'
    and s.id=#{id}
  </select>

  <resultMap id="listResultMap" type="com.railway.business.safetool.domain.SecScheduled">
    <result column="id" property="id"/>
    <result column="dept_id" property="deptId"/>
    <result column="lab_id" property="labId"/>
    <result column="scheduled_time" property="scheduledTime"/>
    <result column="state" property="state"/>
    <result column="del_flag" property="delFlag"/>
  </resultMap>

  <sql id="list_Column_List">
    t.id,
    t.dept_id,
    t.lab_id,
    t.scheduled_time,
    t.state,
    t.del_flag
  </sql>

  <!-- state='0' 待送检 -->
  <select id="listScheduled" resultMap="listResultMap">
    SELECT
    <include refid="list_Column_List"/>
    FROM sec_scheduled t
    left join sec_scheduled_tool t1 on t.id = t1.scheduled_id
    where t.del_flag='0' and t.state='0' and t1.tool_id=#{toolId}
  </select>

</mapper>