<?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.modules.deductiondetails.mapper.DeductionDetailsMapper">

	<sql id="deductionDetailsColumns">
		a.id AS "id",
		a.unitid AS "unitId",
		a.classid AS "classId",
		a.subclassid AS "subClassId",
		a.deductpoints AS "deductPoints",
		a.deductiondetails AS "deductionDetails",
		a.deductiondate AS "deductionDate",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag"
	</sql>

	<sql id="deductionDetailsJoins">

	</sql>


	<select id="get" resultType="DeductionDetails" >
		SELECT
			<include refid="deductionDetailsColumns"/>
		FROM deduction_details a
		<include refid="deductionDetailsJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="findList" resultType="DeductionDetails" >
		SELECT
			<include refid="deductionDetailsColumns"/>
		FROM deduction_details a
		<include refid="deductionDetailsJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultType="DeductionDetails" >
		SELECT
			<include refid="deductionDetailsColumns"/>
		FROM deduction_details a
		<include refid="deductionDetailsJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			${dataScope}
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<insert id="insert">
		INSERT INTO deduction_details(
			id,
			unitid,
			classid,
			subclassid,
			deductpoints,
			deductiondetails,
			deductiondate,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag
		) VALUES (
			#{id},
			#{unitId},
			#{classId},
			#{subClassId},
			#{deductPoints},
			#{deductionDetails},
			#{deductionDate},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag}
		)
	</insert>
	<insert id="insertDetails">
		insert into deduction_details(
		      id,
			  unitid,
			  classid,
			  subclassid,
			  deductpoints,
			  deductiondetails,
			  deductiondate,
		      veto,
			  create_by,
			  create_date,
			  update_by,
			  update_date,
			  remarks,
			  del_flag
		) values
			 (
				 #{id},
				 #{unitId},
				 #{classId},
				 #{subClassId},
				 #{deductPoints},
				 #{deductionDetails},
				 #{deductionDate},
				 #{veto},
				 #{createBy.id},
				 #{createDate},
				 #{updateBy.id},
				 #{updateDate},
				 #{remarks},
				 #{delFlag}
			 )
	</insert>

	<update id="update">
		UPDATE deduction_details SET
			unitid = #{unitId},
			classid = #{classId},
			subclassid = #{subClassId},
			deductpoints = #{deductPoints},
			deductiondetails = #{deductionDetails},
			deductiondate = #{deductionDate},
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks}
		WHERE id = #{id}
	</update>


	<!--物理删除-->
	<update id="delete">
		DELETE FROM deduction_details
		WHERE id = #{id}
	</update>

	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE deduction_details SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>


	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="DeductionDetails" statementType="STATEMENT">
		select * FROM deduction_details  where ${propertyName} = '${value}'
	</select>
    <select id="findSummaryList" resultType="com.jeeplus.modules.deductiondetails.entity.EvaluationSummary">
		SELECT
		@id := id,
		a.id AS "id",
		a.yearofevaluation as "yearOfEvaluation",
		a.unitname as "evaName",
		(25-(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '8c9fe0f62745471e8c341e0e7327aa9b' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '8c9fe0f62745471e8c341e0e7327aa9b' GROUP BY unitid ) else 0 end )) AS safetyEva,
		(25-(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '7a77a8251b124e858a1bef1bb353372b' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '7a77a8251b124e858a1bef1bb353372b' GROUP BY unitid ) else 0 end )) AS progressEva,
		(25-(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '783ba447e0774b139fa1dc6ad4eeb4f3' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '783ba447e0774b139fa1dc6ad4eeb4f3' GROUP BY unitid ) else 0 end )) AS costEva,
		(25-(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = 'bc91443b714c4993a3b136e000eb8bc2' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = 'bc91443b714c4993a3b136e000eb8bc2' GROUP BY unitid ) else 0 end )) AS qualityEva,
		(25-(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '2f0e74b9e50b4bbb8d6322feac578076' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '2f0e74b9e50b4bbb8d6322feac578076' GROUP BY unitid ) else 0 end )) AS pointsEva,
		(
		CASE WHEN ( SELECT sum( veto ) FROM deduction_details WHERE unitid = @id  GROUP BY unitid ) > 0 THEN '是' ELSE '否'
		END
		) AS veto,
		(case when (
		CASE WHEN ( SELECT sum( veto ) FROM deduction_details WHERE unitid = @id  GROUP BY unitid ) > 0 THEN '是' ELSE '否'
		END
		) = '否' then ((case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '8c9fe0f62745471e8c341e0e7327aa9b' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '8c9fe0f62745471e8c341e0e7327aa9b' GROUP BY unitid ) else 0 end )+
		(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '7a77a8251b124e858a1bef1bb353372b' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '7a77a8251b124e858a1bef1bb353372b' GROUP BY unitid ) else 0 end )+
		(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '783ba447e0774b139fa1dc6ad4eeb4f3' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '783ba447e0774b139fa1dc6ad4eeb4f3' GROUP BY unitid ) else 0 end )+
		(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = 'bc91443b714c4993a3b136e000eb8bc2' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = 'bc91443b714c4993a3b136e000eb8bc2' GROUP BY unitid ) else 0 end )+
		(case when ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '2f0e74b9e50b4bbb8d6322feac578076' GROUP BY unitid ) is not null then ( SELECT sum( deductpoints ) FROM deduction_details WHERE unitid = @id AND classid = '2f0e74b9e50b4bbb8d6322feac578076' GROUP BY unitid ) else 0 end )) else '暂停施工' end) 	AS sumScore
		FROM
		lishu_construction_unit a
		<where>
			<if test="yearOfEvaluation!=null and yearOfEvaluation !=''">
				and a.yearOfEvaluation = #{yearOfEvaluation}
			</if>
			<if test="evaName!=null and evaName!=''">
				AND a.unitName LIKE
				<if test="_databaseId == 'postgre'">'%'||#{EvaName}||'%'</if>
				<if test="_databaseId == 'oracle'">'%'||#{EvaName}||'%'</if>
				<if test="_databaseId == 'mssql'">'%'+#{EvaName}+'%'</if>
				<if test="_databaseId == 'mysql'">concat('%',#{EvaName},'%')</if>
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	<select id="getSummary" resultType="com.jeeplus.modules.deductiondetails.entity.EvaluationSummary">
		select a.id as "ido",a.yearofevaluation as "yearofevaluation",a.unitname as "EvaName" from lishu_construction_unit a
		where a.id = #{id}
	</select>
	<select id="getBigClass" resultType="com.jeeplus.modules.deductiondetails.entity.SelectClass">
		select a.id as "value",a.name as "label" from evaluation_criterion a where a.parent_id = #{id} order by sort
	</select>
	<select id="getSubClass" resultType="com.jeeplus.modules.deductiondetails.entity.SelectClass">
		select a.id as "value",a.name as "label"  from evaluation_criterion a where a.parent_id = #{id} order by sort
	</select>
	<select id="getVeto" resultType="java.lang.String">
		select a.veto as "veto" from evaluation_criterion a where id = #{id}
	</select>
	<select id="returnHeader" resultType="java.lang.String">
		select a.name from evaluation_criterion a where parent_id = #{id} order by sort
	</select>

</mapper>