<?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.payment.mapper.PaymentMapper">

    <select id="findPageList" resultType="com.jeeplus.business.payment.service.dto.PaymentDTO">
        select distinct
            a.id,
            a.create_by_id,
            a.no,
            a.project_id,
            a.proceed_type,
            a.duty_number,
            a.amount_paid,
            a.reason,
            a.payment_lower,
            a.payment,
            a.payment_type,
            a.remarks,
            a.status,
            (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 paymentStatus,
            a.proc_ins_id,
            a.process_definition_id,
            a.refund,
            a.final_money,
            a.proceed_office_id,
            su.name as paymentUser,
            a.payment_time,
            so.name as paymentOffice,
            art1.ID_ as task_id
        from jy_payment a
        left join sys_user su on a.payment_user = 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 sys_user_manage_office sumo on sumo.office_id = su.office_id
        LEFT JOIN act_ru_task art1 ON a.proc_ins_id = art1.PROC_INST_ID_
        ${ew.customSqlSegment}
        ORDER BY a.create_time DESC
    </select>


    <select id="findById" resultType="com.jeeplus.business.payment.service.dto.PaymentDTO">
        select
            a.id,
            a.create_by_id,
            a.no,
            a.project_id,
            a.proceed_type,
            a.duty_number,
            a.amount_paid,
            a.reason,
            a.payment_lower,
            a.payment,
            a.payment_type,
            a.remarks,
            a.status,
            a.proc_ins_id,
            a.process_definition_id,
            a.refund,
            a.final_money,
            a.proceed_office_id,
            a.payment_user as paymentUser,
            a.payment_time,
            so.name as paymentOffice,
            art1.ID_ as task_id
        from jy_payment a
        left join sys_user su on a.payment_user = 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_
        where a.id = #{id}
    </select>

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

    <update id="updateInfoById">
        update
            jy_payment
        set
            project_id = #{payment.projectId},
            proceed_type = #{payment.proceedType},
            duty_number = #{payment.dutyNumber},
            amount_paid = #{payment.amountPaid},
            reason = #{payment.reason},
            payment_lower = #{payment.paymentLower},
            payment = #{payment.payment},
            payment_type = #{payment.paymentType},
            remarks = #{payment.remarks},
            proceed_office_id = #{payment.proceedOfficeId},
            payment_user = #{payment.paymentUser},
            payment_time = #{payment.paymentTime},
            our_bank = #{payment.ourBank},
            bank_number = #{payment.bankNumber}
        where id = #{payment.id}
    </update>

    <select id="listByUserId" resultType="com.jeeplus.business.payment.domain.Payment">
        select
            a.id,
            a.no,
            a.payment_lower,
            a.reason,
            a.payment_time,
            so.name as officeName,
            su.name as createName
        from jy_payment a
        left join sys_user su on a.payment_user = su.id and su.del_flag = '0'
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        ${ew.customSqlSegment}
    </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="findFiles" 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="queryByProjectId" resultType="com.jeeplus.business.payment.service.dto.PaymentDTO">
        select
            a.id,
            a.no,
            a.payment_lower,
            a.payment_time,
            a.project_id,
            su.name as payment_user,
            so.name as paymentOffice
        from jy_payment a
        left join sys_user su on su.id = a.payment_user and su.del_flag = '0'
        left join sys_office so on su.office_id = so.id and so.del_flag = '0'
        where find_in_set (#{projectId},project_id) and a.del_flag = '0'
        order by a.create_time DESC
    </select>

</mapper>