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

    <resultMap id="BaseResultMap" type="com.railway.business.safetool.domain.vo.SecScheduledDetailVo">
                <result column="toolName" property="toolName"/>
                <result column="unit" property="unit"/>
        <collection property="baseSafetyTools" ofType="com.railway.business.safetool.domain.vo.SecScheduledToolsVo">
            <result column="tool_id" property="toolId"/>
            <result column="tool_code" property="toolCode"/>
        </collection>

    </resultMap>
    <resultMap id="BaseResultMap2" type="com.railway.business.safetool.domain.vo.SecScheduledListVo2">
        <result column="id" property="id"/>
        <result column="dept_id" property="deptId"/>
        <result column="store_place" property="storePlace"/>
        <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>

    <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 != storePlace'>
                    store_place,
                    </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 != storePlace'>
                    #{storePlace},
                    </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 != storePlace'>store_place = #{storePlace},</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>


    <select id="getInfo" resultMap="BaseResultMap">
select  d3.dict_label toolName ,t.unit,t.tool_id,t.tool_code
 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>

    <select id="getList" resultMap="BaseResultMap2">

        select s.id,s.dept_id,s.store_place,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'
        <if test="deptId!=null and deptId!=''">
            and s.dept_id=#{deptId}
        </if>
        <if test="storePlace!=null and storePlace!=''">
            and  s.store_place like concat(#{storePlace},'%')
        </if>

        <if test="orderby!=null and orderby!=''">
            order by scheduled_time ${orderby}
        </if>

    </select>

</mapper>