<?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.RiskJudgmentTaskMapper">

    <insert id="save" keyColumn="id" keyProperty="id"
            parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentTaskBo"
            useGeneratedKeys="true">
        insert into spc_risk_judgment_task
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskName != null">
                task_name,
            </if>
            <if test="departmentId != null">
                department_id,
            </if>
            <if test="taskStatus != null">
                task_status,
            </if>
            <if test="taskDate != null">
                task_date,
            </if>
            <if test="taskCommitDate != null">
                task_commit_date,
            </if>
            <if test="currentFlowTaskId != null">
                current_flow_task_id,
            </if>
            <if test="taskDefinitionKey != null">
                task_definition_key,
            </if>
            <if test="flowUserIds != null">
                flow_user_ids,
            </if>
            <if test="flowInstanceId != null">
                flow_instance_id,
            </if>
            <if test="orgCode != null">
                org_code,
            </if>
            <if test="deleted != null">
                deleted,
            </if>
            <if test="createDate != null">
                create_date,
            </if>
            <if test="updateDate != null">
                update_date,
            </if>
            <if test="businessKey != null and businessKey !=''">
                business_key,
            </if>

        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskName != null">
                #{taskName},
            </if>
            <if test="departmentId != null">
                #{departmentId},
            </if>
            <if test="taskStatus != null">
                #{taskStatus},
            </if>
            <if test="taskDate != null">
                #{taskDate},
            </if>
            <if test="taskCommitDate != null">
                #{taskCommitDate},
            </if>
            <if test="currentFlowTaskId != null">
                #{currentFlowTaskId},
            </if>
            <if test="taskDefinitionKey != null">
                #{taskDefinitionKey},
            </if>
            <if test="flowUserIds != null">
                #{flowUserIds},
            </if>
            <if test="flowInstanceId != null">
                #{flowInstanceId},
            </if>
            <if test="orgCode != null">
                #{orgCode},
            </if>
            <if test="deleted != null">
                #{deleted},
            </if>
            <if test="createDate != null">
                #{createDate},
            </if>
            <if test="updateDate != null">
                #{updateDate},
            </if>
            <if test="businessKey != null and businessKey !=''">
                #{businessKey}
            </if>
        </trim>
    </insert>

    <update id="update" parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentTaskBo">
        update spc_risk_judgment_task
        <set>
            <if test="taskName != null">
                task_name = #{taskName},
            </if>
            <if test="departmentId != null">
                department_id = #{departmentId},
            </if>
            <if test="taskStatus != null">
                task_status = #{taskStatus},
            </if>
            <if test="taskDate != null">
                task_date = #{taskDate},
            </if>
            <if test="taskCommitDate != null">
                task_commit_date = #{taskCommitDate},
            </if>
            <if test="currentFlowTaskId != null">
                current_flow_task_id = #{currentFlowTaskId},
            </if>
            <if test="taskDefinitionKey != null">
                task_definition_key = #{taskDefinitionKey},
            </if>
            <if test="flowUserIds != null">
                flow_user_ids = #{flowUserIds},
            </if>
            <if test="flowInstanceId != null">
                flow_instance_id = #{flowInstanceId},
            </if>
            <if test="orgCode != null">
                org_code = #{orgCode},
            </if>
            <if test="deleted != null">
                deleted = #{deleted},
            </if>
            <if test="createDate != null">
                create_date = #{createDate},
            </if>
            <if test="updateDate != null">
                update_date = #{updateDate},
            </if>
        </set>
        where id = #{id}
    </update>


    <update id="updateBatch" parameterType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentItemBo">
        <foreach collection="list" item="item" index="index" separator=";">
            update spc_risk_judgment_task
            <set>
                <if test="item.taskName != null">
                    task_name = #{item.taskName},
                </if>
                <if test="item.departmentId != null">
                    department_id = #{item.departmentId},
                </if>
                <if test="item.taskStatus != null">
                    task_status = #{item.taskStatus},
                </if>
                <if test="item.taskDate != null">
                    task_date = #{item.taskDate},
                </if>
                <if test="item.taskCommitDate != null">
                    task_commit_date = #{item.taskCommitDate},
                </if>
                <if test="item.currentFlowTaskId != null">
                    current_flow_task_id = #{item.currentFlowTaskId},
                </if>
                <if test="item.taskDefinitionKey != null">
                    task_definition_key = #{item.taskDefinitionKey},
                </if>
                <if test="item.flowUserIds != null">
                    flow_user_ids = #{item.flowUserIds},
                </if>
                <if test="item.flowInstanceId != null">
                    flow_instance_id = #{item.flowInstanceId},
                </if>
                <if test="item.orgCode != null">
                    org_code = #{item.orgCode},
                </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>
            </set>
            where id = #{item.id}
        </foreach>
    </update>

    <select id="listByDateAndStatus" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentTaskBo">
        select
            *
        from
            spc_risk_judgment_task
        where
            deleted = 0
        and
            task_date = #{date}
        and
            task_status = #{status}
    </select>

    <select id="listByMap" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentTaskBo">
        select
        a.*
        from
        spc_risk_judgment_task as a
        <where>
            <if test="true">
                and a.deleted = 0
            </if>
            <if test="true">
                and a.task_status != 0
            </if>
            <if test="userId != null and userId != '' and !hasPermission">
                and find_in_set(#{userId}, a.flow_user_ids)
            </if>
            <if test="orgCode != null and orgCode != ''">
                and a.org_code = #{orgCode}
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                and a.task_status = #{taskStatus}
            </if>
            <if test="beginTime != null and beginTime != ''">
                and a.task_date &gt;= #{beginTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and a.task_date &lt;= #{endTime}
            </if>
            <if test="departmentId != null">
                and a.department_id = #{departmentId}
            </if>
            <if test="departmentIds != null and departmentIds != ''">
                and find_in_set(a.department_id, #{departmentIds})
            </if>
        </where>
        order by create_date desc
        limit #{offset},#{limit}
    </select>

    <select id="countByMap" resultType="java.lang.Long">
        select
        count(1)
        from
        spc_risk_judgment_task as a
        <where>
            <if test="true">
                and a.deleted = 0
            </if>
            <if test="true">
                and a.task_status != 0
            </if>
            <if test="userId != null and userId != '' and !hasPermission">
                and find_in_set(#{userId}, a.flow_user_ids)
            </if>
            <if test="orgCode != null and orgCode != ''">
                and a.org_code = #{orgCode}
            </if>
            <if test="taskStatus != null and taskStatus != ''">
                and a.task_status = #{taskStatus}
            </if>
            <if test="beginTime != null and beginTime != ''">
                and a.task_date &gt;= #{beginTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and a.task_date &lt;= #{endTime}
            </if>
            <if test="departmentId != null">
                and a.department_id = #{departmentId}
            </if>
            <if test="departmentIds != null and departmentIds != ''">
                and find_in_set(a.department_id, #{departmentIds})
            </if>
        </where>
    </select>

    <select id="getById" resultType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentTaskBo">
        select
            *
        from
            spc_risk_judgment_task
        where
            deleted = 0
        and
            id = #{id}
    </select>

    <select id="getByInstanceIdAndTaskDefinitionKey"
            resultType="com.yeejoin.amos.patrol.business.entity.mybatis.RiskJudgmentTaskBo">
        select
            *
        from
            spc_risk_judgment_task
        where
            deleted = 0
        and
            flow_instance_id = #{instanceId}
        and
            task_definition_key = #{taskDefinitionKey}
    </select>

    <select id="countByFlowUserIds" resultType="java.lang.Long">
        select
            count(1)
        from
            spc_risk_judgment_task
        where
            deleted = 0
        and
            task_status = 1
        and
            find_in_set(#{userId},flow_user_ids)
    </select>

    <update id="updatetTaskStatusByInstanceId" >
    	update spc_risk_judgment_task
    		set task_status = #{taskStatus}
    	where flow_instance_id = #{flowInstanceId}
    </update>
</mapper>