<?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.SecExperimentMapper">


  <insert id="insertExp" keyProperty="id"
    parameterType="com.railway.business.safetool.domain.vo.SecExpAddParmVo">
    INSERT INTO sec_experiment (lab_id,lab_name,scheduled_id,end_time,dept_id,dept_name,
    scheduled_time,state,del_flag,create_time,create_by)
    select lab_id,d2.dept_name lab_name,s.id scheduled_id,
    ADDDATE(now(),3) end_time ,s.dept_id,d.dept_name,
    now() scheduled_time,
    '0' state ,'0' del_flag, now() create_time , #{createby} create_by
    from sec_scheduled s
    left join sys_dept d on s.dept_id = d.dept_id
    left join sys_dept d2 on lab_id = d2.dept_id
    where s.id = #{secid}
  </insert>


  <insert id="insertTool">
    INSERT INTO sec_experiment_tool (tool_id,isok,exp_id,del_flag,create_time,create_by)
    select tool_id,'1' isok ,#{expid} exp_id ,'0' del_flag, now() create_time , #{createby}
    create_by
    from sec_scheduled_tool
    where scheduled_id = #{secid}
  </insert>

  <update id="update" parameterType="com.railway.business.safetool.domain.SecExperiment">
    UPDATE sec_experiment
    <set>
      <if test='null != labId'>lab_id = #{labId},</if>
      <if test='null != scheduledId'>scheduled_id = #{scheduledId},</if>
      <if test='null != endTime'>end_time = #{endTime},</if>
      <if test='null != deptName'>dept_name = #{deptName},</if>
      <if test='null != labName'>lab_name = #{labName},</if>
      <if test='null != scheduledTime'>scheduled_time = #{scheduledTime},</if>
      <if test='null != expDate'>exp_date = #{expDate},</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 = (
    select scheduled_id from sec_experiment where id = #{id}
    )
  </update>

  <resultMap id="ToolsResultMap" type="com.railway.business.safetool.domain.vo.SecExpToolsVo">
    <result column="tool_id" property="toolId"/>
    <result column="tool_code" property="toolCode"/>
    <result column="id" property="id"/>
    <result column="isok" property="isok"/>
  </resultMap>

  <select id="listSafetyTools" resultMap="ToolsResultMap">
    select d3.dict_label toolName, t.unit,t.tool_id, t.tool_code, r.id, r.isok
    FROM sec_experiment_tool r
    LEFT JOIN sec_experiment s ON r.exp_id = s.id
    LEFT 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 t.del_flag = '0' and t.state = '1'
    and s.id=#{id}
  </select>


  <resultMap id="InfoResultMap" type="com.railway.business.safetool.domain.vo.SecExpInfoVo">
    <result column="id" property="id"/>
    <result column="state" property="state"/>
    <result column="end_time" property="endTime"/>
    <result column="exp_date" property="expDate"/>
    <result column="dept_name" property="deptName"/>
    <result column="lab_name" property="labName"/>
    <result column="sj_name" property="sjName"/>
    <result column="sy_name" property="syName"/>
    <result column="sjr_id" property="sjrId"/>
    <result column="syr_id" property="syrId"/>
    <result column="dept_id" property="deptId"/>
    <result column="lab_id" property="labId"/>
    <result column="scheduled_id" property="scheduledId"/>
    <result column="deptName" property="deptName"/>
    <result column="labName" property="labName"/>
  </resultMap>

  <select id="getInfo" resultMap="InfoResultMap">
    select e.id,e.state,e.scheduled_time,e.end_time,e.exp_date,e.dept_name,e.lab_name,
    u.real_name as sj_name, u1.real_name as sy_name, u.user_id as sjr_id, u1.user_id as syr_id
    from sec_experiment e
    left join sys_user u on e.create_by = u.user_name
    left join sys_user u1 on e.update_by = u1.user_name
    where e.id=#{id}
  </select>


  <resultMap id="listResultMap" type="com.railway.business.safetool.domain.vo.SecExpListVo">
    <result column="id" property="id"/>
    <result column="state" property="state"/>
    <result column="scheduled_time" property="scheduledTime"/>
    <result column="end_time" property="endTime"/>
    <result column="exp_date" property="expDate"/>
    <result column="dept_name" property="deptName"/>
    <result column="lab_name" property="labName"/>
    <result column="real_name" property="realName"/>
  </resultMap>

  <select id="getList" resultMap="listResultMap">
    select e.id,e.state,e.scheduled_time,e.end_time,e.exp_date,e.dept_name,e.lab_name,u.real_name
    from sec_experiment e
    left join sys_user u on e.create_by = u.user_name
    where (e.lab_id =#{deptId} or e.dept_id =#{deptId})
    <if test="scheduledTime!=null and scheduledTime!=''">
      and e.scheduled_time =#{scheduledTime}
    </if>
    <if test="orderby!=null and orderby!=''">
      order by end_time ${orderby}
    </if>
  </select>

  <resultMap id="listExperimentResultMap" type="com.railway.business.safetool.domain.SecExperiment">
    <result column="id" property="id"/>
    <result column="lab_id" property="labId"/>
    <result column="lab_name" property="labName"/>
    <result column="scheduled_id" property="scheduledId"/>
    <result column="end_time" property="endTime"/>
    <result column="dept_id" property="deptId"/>
    <result column="dept_name" property="deptName"/>
    <result column="scheduled_time" property="scheduledTime"/>
    <result column="state" property="state"/>
    <result column="exp_date" property="expDate"/>
    <result column="exp_by" property="expBy"/>
    <result column="exp_report" property="expReport"/>
    <result column="del_flag" property="delFlag"/>
  </resultMap>

  <sql id="list_Column_List">
    t.id,
    t.lab_id,
    t.lab_name,
    t.scheduled_id,
    t.end_time,
    t.dept_id,
    t.dept_name,
    t.scheduled_time,
    t.state,
    t.exp_date,
    t.exp_by,
    t.del_flag
  </sql>

  <!-- state='0' 待实验 -->
  <select id="listExperiment" resultMap="listExperimentResultMap">
    SELECT
    <include refid="list_Column_List"/>
    FROM sec_experiment t
    left join sec_experiment_tool t1 on t.id = t1.exp_id
    where t.del_flag='0' and t.state='0' and t1.tool_id=#{toolId}
  </select>

</mapper>