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

    <select id="findPageList" resultType="com.jeeplus.business.drive.service.dto.DriveApplyDTO">
        select distinct
            a.id,
            a.create_by_id as createById,
            su.name as createName,
            a.create_time,
            a.update_by_id as updateById,
            a.update_time,
            a.project_id,
            (select group_concat(name) from jy_project_record pr where FIND_IN_SET(pr.id,(select project_id from jy_drive_apply where id=a.id))) as projectName,
            a.use_men,
            (select group_concat(name) from sys_user su where FIND_IN_SET(su.id,(select use_men from jy_drive_apply where id=a.id))) as userName,
            (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 driveApplyStatus,
            a.use_start_time,
            a.use_end_time,
            a.remarks,
            a.status,
            a.proc_ins_id,
            a.use_reason,
            a.process_definition_id,
            so.name as createOffice,
            art1.ID_ as task_id,
            art2.ID_ as task_id_account,
            da.process_definition_id as processDefinitionIdAccount,
            da.proc_ins_id as procInsIdAccount,
            da.status as accountStatus,
            da.id as accountId,
            so.name as officeName
        from
            jy_drive_apply a
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        LEFT JOIN act_ru_task art1 ON a.proc_ins_id = art1.PROC_INST_ID_
        left join jy_drive_account da on a.id = da.apply_id and da.del_flag = 0
        LEFT JOIN act_ru_task art2 ON da.proc_ins_id = art2.PROC_INST_ID_
        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="findList" resultType="com.jeeplus.sys.domain.WorkAttachmentInfo">
		SELECT * FROM work_attachment WHERE del_flag = 0 AND attachment_id = #{id}
	</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="findById" resultType="com.jeeplus.business.drive.service.dto.DriveApplyDTO">
        select
            a.id,
            a.create_by_id,
            su.name as createName,
            a.create_time,
            a.update_by_id,
            a.update_time,
            a.project_id,
            (select group_concat(name) from jy_project_record pr where FIND_IN_SET(pr.id,(select project_id from jy_drive_apply where id=a.id))) as projectName,
            a.use_men,
            (select group_concat(name) from sys_user su where FIND_IN_SET(su.id,(select use_men from jy_drive_apply where id=a.id))) as userName,
            a.use_start_time,
            a.use_end_time,
            a.remarks,
            a.status,
            a.use_reason,
            a.proc_ins_id,
            a.process_definition_id,
            so.name as createOffice,
            art1.ID_ as task_id,
            art2.ID_ as task_id_account,
            da.process_definition_id as processDefinitionIdAccount,
            da.proc_ins_id as procInsIdAccount,
            da.status as accountStatus,
            da.id as accountId,
            da.apply_id,
            da.start_mileage,
            da.end_mileage,
            da.mileage_count,
            so.name as officeName
        from
            jy_drive_apply a
        left join sys_user su on a.create_by_id = su.id and su.del_flag = '0'
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        LEFT JOIN act_ru_task art1 ON a.proc_ins_id = art1.PROC_INST_ID_
        left join jy_drive_account da on a.id = da.apply_id and da.del_flag = 0
        LEFT JOIN act_ru_task art2 ON da.proc_ins_id = art2.PROC_INST_ID_
        where (a.id=#{id} or da.id=#{id})
    </select>
    
    <update id="updateStatusById">
        update jy_drive_apply set status =#{status} where id=#{id}
    </update>

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

    <update id="updateInfoById">
        UPDATE
            jy_drive_apply
        SET
            update_by_id =#{driveApply.updateById},
            update_time = #{driveApply.updateTime},
            project_id = #{driveApply.projectId},
            use_men = #{driveApply.useMen},
            use_start_time = #{driveApply.useStartTime},
            use_end_time = #{driveApply.useEndTime},
            use_reason = #{driveApply.useReason},
            remarks = #{driveApply.remarks}
        WHERE id = #{driveApply.id}
    </update>




</mapper>