<?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.jeeplus.business.drive.mapper.DriveMaintainMapper">

    <select id="findPageList" resultType="com.jeeplus.business.drive.service.dto.DriveMaintainDTO">
        select distinct
            a.id,
            a.create_by_id,
            su.name as "createName",
            a.create_time,
            a.drive_info_id,
            a.drive_maintain_type,
            (select dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.value=a.status and dt.type='program_project_list_info_status') as maintainStatus,
            (select dv.label from sys_dict_value dv LEFT JOIN sys_dict_type dt on dt.id=dv.dict_type_id where dv.value=a.drive_maintain_type and dt.type='jy_drive_maintain_type') as maintainType,
            a.start_time,
            a.end_time,
            a.proc_ins_id,
            a.process_definition_id,
            a.status,
            art.ID_ as task_id,
            di.drive_number
        from jy_drive_maintain a
        left join jy_drive_info di on a.drive_info_id = di.id
        left join act_ru_task art ON a.proc_ins_id = art.PROC_INST_ID_
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        left join sys_user_manage_office sumo on sumo.office_id = su.office_id
        ${ew.customSqlSegment}
        ORDER BY a.update_time DESC
    </select>

    <select id="findIsExit" resultType="java.lang.Integer">
		SELECT
			COUNT( 0 )
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
			AND attachment_name = #{name}
	</select>

    <select id="findList" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
		SELECT * FROM work_attachment WHERE del_flag = 0 AND attachment_id = #{id}
	</select>

    <select id="findDtos" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
		SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by_id AS `by`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
	</select>

    <select id="findById" resultType="com.jeeplus.business.drive.domain.DriveMaintain">
        select
            a.id,
            a.create_by_id,
            a.create_time,
            a.drive_info_id,
            a.drive_maintain_type,
            a.start_time,
            a.end_time,
            a.proc_ins_id,
            a.process_definition_id,
            a.status,
            art.ID_ as task_id,
            di.drive_number
        from jy_drive_maintain a
        left join jy_drive_info di on a.drive_info_id = di.id
        left join act_ru_task art ON a.proc_ins_id = art.PROC_INST_ID_
        where a.id=#{id}
    </select>

    <update id="updateStatusById">
        update jy_drive_maintain set status =#{status} where id=#{id}
    </update>

    <update id="updateInfo">
        update
            jy_drive_maintain
        set
            update_by_id = #{maintain.updateById},
            update_time = #{maintain.updateTime},
            drive_info_id = #{maintain.driveInfoId},
            drive_maintain_type = #{maintain.driveMaintainType},
            start_time = #{maintain.startTime},
            end_time = #{maintain.endTime},
            remarks = #{maintain.remarks}
        where
            id=#{maintain.id}
    </update>

</mapper>