<?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.workreview.dao.WorkReviewStandardDao">
    
	<sql id="workReviewStandardColumns">
		a.id AS "id",
		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",
		a.parent_id AS "parent.id",
		a.standard_detail AS "standardDetail",
		a.parent_ids AS "parentIds",
		a.max_deduct AS "maxDeduct",
		a.order_num AS "orderNum",
		a.is_proofread AS "proofread"
	</sql>
	
	<sql id="workReviewStandardJoins">
	</sql>
	
    
	<select id="get" resultType="WorkReviewStandard" >
		SELECT 
			<include refid="workReviewStandardColumns"/>
		FROM work_review_standard a
		<include refid="workReviewStandardJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="WorkReviewStandard" >
		SELECT 
			<include refid="workReviewStandardColumns"/>
		FROM work_review_standard a
		<include refid="workReviewStandardJoins"/>
		<where>
            <if test="standardDetail!=null and standardDetail != ''">
                and a.standard_detail like concat('%',#{standardDetail},'%')
            </if>
            <if test="parentIds!=null and parentIds != ''">
                and a.parent_ids like concat('%',#{parentIds},'%')
            </if>
            <choose>
				<when test="companyId!=null and companyId != ''">
					and a.company_id = #{companyId}
				</when>
				<otherwise>
					and a.company_id = '0'
				</otherwise>
			</choose>
            <if test="orderNum != null and orderNum!=0">
                and a.order_num = #{orderNum}
            </if>
            <if test="proofread != null and proofread == true">
                AND a.parent_id in (select id from work_review_standard WHERE standard_detail = '标准部校核' and is_proofread = 1)
            </if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.order_num
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="WorkReviewStandard" >
		SELECT 
			<include refid="workReviewStandardColumns"/>
		FROM work_review_standard a
		<include refid="workReviewStandardJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</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 work_review_standard(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			parent_id,
			standard_detail,
			parent_ids,
			max_deduct,
			company_id,
			order_num,
			is_proofread
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{parent.id},
			#{standardDetail},
			#{parentIds},
			#{maxDeduct},
			#{companyId},
			#{orderNum},
			#{proofread}
		)
	</insert>
	
	<update id="update">
		UPDATE work_review_standard SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			parent_id = #{parent.id},
			standard_detail = #{standardDetail},
			parent_ids = #{parentIds},
			max_deduct = #{maxDeduct},
			company_id = #{companyId},
			order_num = #{orderNum},
			is_proofread = #{proofread}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM work_review_standard
		WHERE id = #{id} OR find_in_set(#{id},parent_ids)
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE work_review_standard SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id} OR find_in_set(#{id},parent_ids)
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="WorkReviewStandard" statementType="STATEMENT">
		select * FROM work_review_standard  where ${propertyName} = '${value}'
	</select>

    <select id="fingIds" resultType="WorkReviewStandard">
        SELECT <include refid="workReviewStandardColumns"/>
        FROM work_review_standard a
        <where>
            <if test="parentIdSet!=null and parentIdSet.size!=0">
                a.id in
                <foreach collection="parentIdSet" item="parentId" separator="," open="(" close=")">
                      #{parentId}
                </foreach>
            </if>
            AND del_flag ='0'
			AND company_id = #{companyId}
        </where>
        ORDER BY a.order_num
    </select>
    <select id="fingByIds" resultType="WorkReviewStandard">
        SELECT <include refid="workReviewStandardColumns"/>
        FROM work_review_standard a
        <where>
            <if test="parentIdSet!=null and parentIdSet.size!=0">
                a.id in
                <foreach collection="parentIdSet" item="parentId" separator="," open="(" close=")">
                      #{parentId}
                </foreach>
            </if>
            AND del_flag ='0'
			AND company_id = #{companyId}
			AND parent_ids like concat('%',#{parentIds},'%')
        </where>
        ORDER BY a.order_num
    </select>

    <select id="findByParentIdsLike" resultType="WorkReviewStandard">
        SELECT
        <include refid="workReviewStandardColumns"/>
        FROM work_review_standard a
        <include refid="workReviewStandardJoins"/>
        WHERE a.del_flag = #{DEL_FLAG_NORMAL} AND (a.parent_ids LIKE
        <if test="dbName == 'mysql'">concat('%,',#{parentIds},',%')</if>
        OR a.id = #{id})
        ORDER BY a.order_num
    </select>

    <update id="updateParentIds">
        UPDATE work_review_standard SET
			parent_id = #{parent.id},
			parent_ids = #{parentIds}
		WHERE id = #{id}
    </update>

    <select id="selectOrderNum" resultType="java.lang.String">
        SELECT MAX(order_num) FROM work_review_standard WHERE parent_id = #{parentId}
    </select>

    <select id="getByDetail" resultType="WorkReviewStandard">
        SELECT * FROM work_review_standard WHERE standard_detail = '标准部校核' and is_proofread = 1
    </select>
</mapper>