<?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.yeejoin.amos.fas.business.dao.mapper.FmeaMapper">
	<!--统计-->
	<select id="countPageData" resultType="long">
		SELECT
		  COUNT(1)
		FROM
		    (
	        SELECT
	            f.*
	        FROM
	            f_fmea f
	        <trim prefix="WHERE" prefixOverrides="AND ">
	            <if test="riskSourceId!=null">f.risk_source_id = #{riskSourceId}</if>
	        </trim>
		    ) fm
		LEFT JOIN f_risk_factor rf ON rf.id = fm.risk_factors_id
		LEFT JOIN f_accident_type fat ON fat.id = rf.accident_type_id
	</select>

	<!--分页查询 -->
	<select id="queryFmeaPage" resultType="java.util.HashMap">
		SELECT
		    fm.id,
		    fm.risk_source_id AS riskSourceId,
		    fm.risk_factors_id AS riskFactorsId,
		    fems.id AS evaluationSid,
			femo.id AS evaluationOid,
			femd.id AS evaluationDid,
			fems.coefficient AS evaluationS,
			femo.coefficient AS evaluationO,
			femd.coefficient AS evaluationD,
			femo1.coefficient AS newEvaluationO,
		    fm.rpni,
			fm.rpn,
		    fm.engineering,
		    fm.management,
		    fm.train,
		    fm.protection,
		    fm.disposal,
		    fm.remark,
		    fm.company_leader as companyLeader,
			fm.department_leader as departmentLeader,
			fm.group_leader as groupLeader,
			fm.person_leader as personLeader,
			fm.company_leader as companyLeaderId,
			fm.department_leader as departmentLeaderId,
			fm.group_leader as groupLeaderId,
			fm.person_leader as personLeaderId,
			fm.identify_user as identifyUserId,
		    rf.`name` AS riskFactorName,
		    fat.`name` AS accidentTypeName,
		    fat.id AS accidentTypeId,
		    concat(fems.influence,'-',fems.describe) as influence,
		    fm.identify_user as identifyUser,
		    fm.identify_method as identifyMethod,
			fr.name AS level
		FROM
		    (
	        SELECT
	            f.*
	        FROM
	            f_fmea f
	        <trim prefix="WHERE" prefixOverrides="AND ">
	            <if test="riskSourceId!=null">f.risk_source_id = #{riskSourceId}</if>
	        </trim>
		    ) fm
	    LEFT JOIN f_evaluation_model fems ON fems.id = fm.evaluation_sid
		LEFT JOIN f_evaluation_model femo ON femo.id = fm.evaluation_oid
		LEFT JOIN f_evaluation_model femd ON femd.id = fm.evaluation_did
		LEFT JOIN f_evaluation_model femo1 ON femo1.id = fm.new_evaluation_oid
		LEFT JOIN f_risk_factor rf ON rf.id = fm.risk_factors_id
		LEFT JOIN f_accident_type fat ON fat.id = rf.accident_type_id
		LEFT JOIN f_risk_level fr ON fm.rpn BETWEEN fr.down_limit and fr.top_limit
		order by fm.id desc
		<choose>
         <when test="pageSize==-1"></when>
         <when test="pageSize!=-1">limit #{offset},#{pageSize}</when>  
        </choose>
	</select>

	<select id="queryForFmeaList" resultType="map">
		SELECT
			(select rf.`name` from f_risk_factor rf where rf.id = f.risk_factors_id)riskFactor,
			(select fat.`name` from f_accident_type fat where EXISTS(select 1 from f_risk_factor rf where rf.accident_type_id = fat.id and rf.id = f.risk_factors_id))accidentType,
			(select em.describe from f_evaluation_model em where em.id = f.evaluation_sid)severity,
		(select CONCAT(em.describe,em.influence) from f_evaluation_model em where em.id = f.evaluation_oid)occurrence
		FROM
			f_fmea f
		WHERE
			f.risk_source_id = ${riskSourceId};
	</select>

	<select id="getById" resultType="com.yeejoin.amos.fas.dao.entity.Fmea">
		select
			a.id,
			a.risk_source_id as riskSourceId,
			a.risk_factors_id as riskFactorsId,
			a.evaluation_sid as evaluationSid,
			a.evaluation_oid as evaluationOid,
			a.evaluation_did as evaluationDid,
			a.new_evaluation_oid as newEvaluationOid,
			a.company_leader as companyLeader,
			a.department_leader as departmentLeader,
			a.person_leader as personLeader,
			a.group_leader as groupLeader,
			a.rpni,
			a.rpn,
			(select coefficient from f_evaluation_model where id = a.evaluation_sid) as sidValue,
			(select coefficient from f_evaluation_model where id = a.evaluation_oid) as oidValue,
			(select coefficient from f_evaluation_model where id = a.evaluation_did) as didValue,
			(select coefficient from f_evaluation_model where id = a.new_evaluation_oid) as newOidValue
		from
			f_fmea as a
		where
			a.id = #{id}
	</select>

	<select id="listByRiskSourceId" resultType="com.yeejoin.amos.fas.dao.entity.Fmea">
		select
			a.id,
			a.risk_source_id as riskSourceId,
			a.risk_factors_id as riskFactorsId,
			a.evaluation_sid as evaluationSid,
			a.evaluation_oid as evaluationOid,
			a.evaluation_did as evaluationDid,
			a.new_evaluation_oid as newEvaluationOid,
			a.rpni,
			a.rpn,
			(select coefficient from f_evaluation_model where id = a.evaluation_sid) as sidValue,
			(select coefficient from f_evaluation_model where id = a.evaluation_oid) as oidValue,
			(select coefficient from f_evaluation_model where id = a.evaluation_did) as didValue,
			(select coefficient from f_evaluation_model where id = a.new_evaluation_oid) as newOidValue
		from
			f_fmea as a
		where
			a.risk_source_id = #{riskSourceId}
	</select>

	<update id="updateRpn">
		update f_fmea
		<set>
			<if test="rpn != null">
				rpn = #{rpn},
			</if>
			<if test="rpni != null">
				rpni = #{rpni},
			</if>
			<if test="newEvaluationOid != null">
				new_evaluation_oid = #{newEvaluationOid},
			</if>
		</set>
		where id = #{id}
	</update>
	<select id="listByRiskSourceIds" resultType="com.yeejoin.amos.fas.dao.entity.Fmea">
		select
			a.id,
			a.risk_source_id as riskSourceId,
			a.risk_factors_id as riskFactorsId,
			a.evaluation_sid as evaluationSid,
			a.evaluation_oid as evaluationOid,
			a.evaluation_did as evaluationDid,
			a.new_evaluation_oid as newEvaluationOid,
			a.rpni,
			a.rpn,
			(select coefficient from f_evaluation_model where id = a.evaluation_sid) as sidValue,
			(select coefficient from f_evaluation_model where id = a.evaluation_oid) as oidValue,
			(select coefficient from f_evaluation_model where id = a.evaluation_did) as didValue,
			(select coefficient from f_evaluation_model where id = a.new_evaluation_oid) as newOidValue
		from
			f_fmea as a
		where
			a.risk_source_id in
		<foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>
</mapper>