<?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.patrol.business.dao.mapper.RiskJudgmentFillRecordMapper">

    <insert id="save" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentFillRecordBo"
            useGeneratedKeys="true">
        insert into spc_risk_judgment_fill_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">
                task_id,
            </if>
            <if test="itemId != null">
                item_id,
            </if>
            <if test="selectResult != null">
                select_result,
            </if>
            <if test="inputResult != null">
                input_result,
            </if>
            <if test="remarkResult != null">
                remark_result,
            </if>
            <if test="photoResult != null">
                photo_result,
            </if>
            <if test="itemFinish != null">
                item_finish,
            </if>
            <if test="deleted != null">
                deleted,
            </if>
            <if test="createDate != null">
                create_date,
            </if>
            <if test="updateDate != null">
                update_date,
            </if>
            <if test="orgCode != null">
                org_code,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">
                #{taskId},
            </if>
            <if test="itemId != null">
                #{itemId},
            </if>
            <if test="selectResult != null">
                #{selectResult},
            </if>
            <if test="inputResult != null">
                #{inputResult},
            </if>
            <if test="remarkResult != null">
                #{remarkResult},
            </if>
            <if test="photoResult != null">
                #{photoResult},
            </if>
            <if test="itemFinish != null">
                #{itemFinish},
            </if>
            <if test="deleted != null">
                #{deleted},
            </if>
            <if test="createDate != null">
                #{createDate},
            </if>
            <if test="updateDate != null">
                #{updateDate},
            </if>
            <if test="orgCode != null">
                #{orgCode},
            </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentFillRecordBo">
        update spc_risk_judgment_fill_record
        <set>
            <if test="taskId != null">
                task_id = #{taskId},
            </if>
            <if test="itemId != null">
                item_id = #{itemId},
            </if>
            <if test="selectResult != null">
                select_result = #{selectResult},
            </if>
            <if test="inputResult != null">
                input_result = #{inputResult},
            </if>
            <if test="remarkResult != null">
                remark_result = #{remarkResult},
            </if>
            <if test="itemFinish != null">
                item_finish = #{itemFinish},
            </if>
            <if test="photoResult != null">
                photo_result = #{photoResult},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="createDate != null">
                create_date = #{createDate},
            </if>
            <if test="updateDate != null">
                update_date = #{updateDate},
            </if>
            <if test="orgCode != null">
                org_code = #{orgCode},
            </if>
        </set>
        where id = #{id}
    </update>

    <insert id="saveBatch" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentFillRecordBo"
            useGeneratedKeys="true">
        insert into spc_risk_judgment_fill_record
            (
                task_id,
                item_id,
                select_result,
                input_result,
                remark_result,
                photo_result,
                org_code
            )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
                #{item.taskId},
                #{item.itemId},
                #{item.selectResult},
                #{item.inputResult},
                #{item.remarkResult},
                #{item.photoResult},
                #{item.orgCode}
            )
        </foreach>
    </insert>
    
    <insert id="saveBatchWithStatus" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentFillRecordBo"
            useGeneratedKeys="true">
        insert into spc_risk_judgment_fill_record
            (
                task_id,
                item_id,
                select_result,
                input_result,
                remark_result,
                photo_result,
                org_code,
                item_finish
            )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
                #{item.taskId},
                #{item.itemId},
                #{item.selectResult},
                #{item.inputResult},
                #{item.remarkResult},
                #{item.photoResult},
                #{item.orgCode},
                #{item.itemFinish}
            )
        </foreach>
    </insert>

    <update id="updateBatch" parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentFillRecordBo">
        <foreach collection="list" item="item" index="index" separator=";">
            update spc_risk_judgment_fill_record
            <set>
                <if test="item.taskId != null">
                    task_id = #{item.taskId},
                </if>
                <if test="item.itemId != null">
                    item_id = #{item.itemId},
                </if>
                <if test="item.selectResult != null">
                    select_result = #{item.selectResult},
                </if>
                <if test="item.inputResult != null">
                    input_result = #{item.inputResult},
                </if>
                <if test="item.remarkResult != null">
                    remark_result = #{item.remarkResult},
                </if>
                <if test="item.itemFinish != null">
                    item_finish = #{item.itemFinish},
                </if>
                <if test="item.photoResult != null">
                    photo_result = #{item.photoResult},
                </if>
                <if test="item.deleted != null">
                    deleted = #{item.deleted},
                </if>
                <if test="item.createDate != null">
                    create_date = #{item.createDate},
                </if>
                <if test="item.updateDate != null">
                    update_date = #{item.updateDate},
                </if>
                <if test="item.orgCode != null">
                    org_code = #{item.orgCode},
                </if>
                <if test="item.extraIds != null">
                    extra_ids = #{item.extraIds},
                </if>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <select id="listFillRecordData" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentFillRecordBo">
      select
			b.order_num,
			a.*, 
			b.item_flag,
			b.item_level,
			b.item_type,
			b.item_name,
			b.item_can_copy,
			b.item_parent_id,
			b.select_name,
			b.select_is_need,
			b.select_is_required,
			b.select_json,
			b.input_name,
			b.input_type,
			b.input_can_edit,
			b.input_is_need,
			b.input_is_required,
			b.remark_is_need,
			b.remark_is_required,
			b.photo_is_need,
			b.photo_is_required,
            (select c.department_id from spc_risk_judgment_task c where a.task_id = c.id) as department_id
        from
            spc_risk_judgment_fill_record a
        left join
            spc_risk_judgment_item b on a.item_id = b.id
        <where>
            <if test="true">
                and a.deleted = 0
            </if>
            <if test="taskId != null">
                and a.task_id = #{taskId}
            </if>
            <if test="type != null">
                and b.item_type = #{type}
            </if>
            <if test="level != null">
                and b.item_level = #{level}
            </if>
            <if test="itemParentId != null">
                and b.item_parent_id = #{itemParentId}
            </if>
        </where>
        order by b.order_num
    </select>

    <select id="countNotFinishRecordByTaskId" resultType="java.lang.Long">
        select
            count(1)
        from
        spc_risk_judgment_fill_record
        where
            deleted = 0
        and
            task_id = #{taskId}
        and
            item_finish = 0
    </select>
       
    <update id='updateInputResultAndselectResult'>
    	update 
		 spc_risk_judgment_fill_record a,
		 spc_risk_judgment_task b,
	 	 spc_risk_judgment_item c
		 set a.input_result = #{inputResult}
		 <if test="selectResult !=null">,a.select_result = #{selectResult}</if>
		WHERE
		   b.task_date = CURRENT_DATE ()
		   AND b.department_id = #{departmentId}
		   AND a.item_id = c.id
		   and b.id = a.task_id
		   and c.item_flag = #{itemFlag}
		   and b.task_status <![CDATA[<=]]> #{taskStatus}
    </update>

</mapper>