<?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.psimanage.purchase.mapper.PsiMaterialBasicMapper">
    <update id="updateStatusById">
		UPDATE psi_management_pruchase_request_basics SET status = #{type}
		WHERE id = #{id}
	</update>
    <select id="findList" resultType="com.jeeplus.psimanage.purchase.service.dto.PsiMaterialInfoDto">
        SELECT
        	DISTINCT
			a.id,
			a.create_by_id as createBy,
			a.create_time as createDate,
			a.purchase_no,
			a.purchase_sketch,
			a.handled_by,
			a.handled_by_office,
			a.purchase_date,
			a.purchase_mode as purchaseModeType,
			(case a.purchase_mode
				when 1 then '办公室采购'
				when 2 then '自行采购'
				end
			) as purchase_mode,
			a.status,
			a.proc_ins_id,
			a.process_definition_id,
			b.trade_total_price,
			so.name as handledByOfficeName,
			su.name as handledByName,
			art.ID_ as task_id
		FROM
			psi_management_pruchase_request_basics a
			left join sys_user su on a.handled_by = su.id
			left join sys_office so on a.handled_by_office = so.id
			left join psi_management_pruchase_request_detailed b
			on a.id = b.basic_id and b.del_flag = '0'
			left join act_ru_task art ON a.proc_ins_id = art.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="findChildIds" resultType="java.lang.String">
		select id from sys_office where parent_id = #{department}
	</select>
	<select id="getHandledName" resultType="java.lang.String">
		select name from sys_user where id = #{handledBy}
	</select>
    <select id="getInfoByPurchaseNo"
            resultType="com.jeeplus.psimanage.purchase.domain.PsiMaterialBasic">
		select * from psi_management_pruchase_request_basics where purchase_no = #{purchaseNo}
	</select>
    <select id="getUserNameByUserId" resultType="com.jeeplus.psimanage.purchase.service.dto.PsiMaterialInfoDto">
		select su.name as handledBy,so.name as handledByOfficeName from sys_user su
		left join sys_office so on su.office_id = so.id
		where su.id = #{id}
	</select>
    <select id="findFileList" resultType="com.jeeplus.psimanage.oss.domain.PsiWorkAttachment">
		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>
</mapper>
