<?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.ruoyi.invest.mapper.DecisionMapper">

    <resultMap type="TProjectDecision" id="TProjectDecisionResult">
        <result property="id"    column="id"    />
        <result property="procInstId" column="proc_inst_id"/>
        <result property="projectPoolId"    column="project_pool_id"    />
        <result property="participantsId"    column="participants_id"    />
        <result property="participants"    column="participants"    />
        <result property="provision"    column="provision"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>
    <resultMap type="ProjectDecisionVo" id="ProjectDecisionVoResult">
        <result property="id"    column="id"    />
        <result property="procInstId" column="proc_inst_id"/>
        <result property="projectPoolId"    column="project_pool_id"    />
        <result property="participantsId"    column="participants_id"    />
        <result property="participants"    column="participants"    />
        <result property="provision"    column="provision"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="status"     column="status"  />

        <result property="projectName"    column="project_name"    />
        <result property="industry"    column="industry"    />
        <result property="investHead"    column="invest_head"    />
        <result property="projectStage"    column="project_stage"    />
        <result property="projectState"    column="project_state"    />
        <result property="projectStatus"    column="project_status"    />

        <result property="taskName" column="taskName"/>
        <result property="taskId" column="taskId"/>
        <result property="taskCreateTiem" column="taskCreateTiem"/>
        <result property="assignee" column="assignee"/>
        <result property="assigneeName" column="assigneeName"/>
        <result property="taskDefKey" column="taskDefKey"/>
        <result property="procDefId" column="procDefId"/>
        <result property="procInstId" column="procInstId"/>
        <result property="startUserId" column="startUserId"/>
        <result property="startUserName" column="startUserName"/>
        <result property="startDeptName" column="startDeptName"/>
        <result property="executionId" column="executionId"/>
        <result property="deployId" column="deployId"/>
    </resultMap>
    <sql id="selectTProjectDecisionVo">
        select id, project_pool_id, participants_id, participants, provision, remark, del_flag, create_by, create_time, update_by, update_time from t_project_decision
    </sql>
    <sql id="where_sql">
        <if test="projectDecision.projectPoolId != null  and projectDecision.projectPoolId != ''"> and a.project_pool_id = #{projectDecision.projectPoolId}</if>
        <if test="projectDecision.projectName != null  and projectDecision.projectName != ''"> and tp.project_name like concat('%', #{projectDecision.projectName}, '%')</if>
        <if test="projectDecision.provision != null  and projectDecision.provision != ''"> and a.provision like concat('%', #{projectDecision.provision}, '%')</if>
        <if test="projectDecision.status != null"> and a.status = #{projectDecision.status}</if>
    </sql>
    <select id="selectAllDecisionList" resultMap="ProjectDecisionVoResult">
        select a.id,a.proc_inst_id, a.project_pool_id,tp.project_name, tp.industry,tp.project_stage,
        tp.project_state,tp.invest_head,tp.project_status, a.participants_id,a.participants,
        a.provision, a.remark, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time ,a.status,
        p.PROC_INST_ID_ as 'procInsId',
        p.PROC_DEF_ID_ as 'procDefId',
        date_format(p.START_TIME_, '%Y-%m-%d %H:%i:%s') as 'applyTime',
        date_format(p.END_TIME_, '%Y-%m-%d %H:%i:%s') as 'endTime',
        p.START_USER_ID_ AS 'startUserId',
        uu.nick_name AS 'startUserName',
        dd.dept_name AS 'startDeptName',
        t.NAME_ as 'taskName',
        t.TASK_DEF_KEY_ as 'taskDefKey',
        DATE_FORMAT(t.CREATE_TIME_ ,'%Y-%m-%d %H:%i') as 'taskCreateTiem',
        t.ASSIGNEE_ AS 'assignee',
        ui.nick_name AS 'assigneeName',
        t.ID_ as 'taskId',
        t.EXECUTION_ID_ AS 'executionId',
        rp.DEPLOYMENT_ID_ AS 'deployId'
        from t_project_decision a
        left join t_project_pool tp on tp.id=a.project_pool_id
        left join ACT_HI_PROCINST p on a.proc_inst_id = p.PROC_INST_ID_
        LEFT JOIN (SELECT * FROM sys_user LIMIT 100000) uu ON p.START_USER_ID_ = uu.user_id
        LEFT JOIN (SELECT * FROM sys_dept LIMIT 100000) dd ON uu.dept_id = dd.dept_id
        left join ACT_RU_TASK t on a.proc_inst_id = t.PROC_INST_ID_
        left join (select * from sys_user limit 100000) ui on t.ASSIGNEE_ = ui.user_id
        left join ACT_RU_IDENTITYLINK ru on ru.TASK_ID_ = t.ID_
        LEFT JOIN act_re_procdef rp ON t.PROC_DEF_ID_ = rp.ID_
        where a.del_flag="0"
        <include refid="where_sql"></include>
        order by a.create_time desc
    </select>
    <select id="getMyTaskList" resultMap="ProjectDecisionVoResult">
        select a.id,a.proc_inst_id, a.project_pool_id,tp.project_name, tp.industry,tp.project_stage,
        tp.project_state,tp.invest_head, tp.project_status, a.participants_id,a.participants,
        a.provision, a.remark, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time ,a.status,
        p.PROC_INST_ID_ as 'procInsId',
        p.PROC_DEF_ID_ as 'procDefId',
        date_format(p.START_TIME_, '%Y-%m-%d %H:%i:%s') as 'applyTime',
        date_format(p.END_TIME_, '%Y-%m-%d %H:%i:%s') as 'endTime',
        p.START_USER_ID_ AS 'startUserId',
        uu.nick_name AS 'startUserName',
        dd.dept_name AS 'startDeptName',
        t.NAME_ as 'taskName',
        t.TASK_DEF_KEY_ as 'taskDefKey',
        DATE_FORMAT(t.CREATE_TIME_ ,'%Y-%m-%d %H:%i') as 'taskCreateTiem',
        t.ASSIGNEE_ AS 'assignee',
        ui.nick_name AS 'assigneeName',
        t.ID_ as 'taskId',
        t.EXECUTION_ID_ AS 'executionId',
        rp.DEPLOYMENT_ID_ AS 'deployId'
        from t_project_decision a
        left join t_project_pool tp on tp.id=a.project_pool_id
        left join ACT_HI_PROCINST p on a.proc_inst_id = p.PROC_INST_ID_
        LEFT JOIN (SELECT * FROM sys_user LIMIT 100000) uu ON p.START_USER_ID_ = uu.user_id
        LEFT JOIN (SELECT * FROM sys_dept LIMIT 100000) dd ON uu.dept_id = dd.dept_id
        left join ACT_RU_TASK t on a.proc_inst_id = t.PROC_INST_ID_
        left join (select * from sys_user limit 100000) ui on t.ASSIGNEE_ = ui.user_id
        left join ACT_RU_IDENTITYLINK ru on ru.TASK_ID_ = t.ID_
        left join ACT_ID_MEMBERSHIP m on m.GROUP_ID_ = ru.GROUP_ID_ and m.USER_ID_ = #{userId}
        LEFT JOIN act_re_procdef rp ON t.PROC_DEF_ID_ = rp.ID_
        where
        (t.ASSIGNEE_ = #{userId}
        or (t.ASSIGNEE_ is null AND ru.TYPE_ = 'candidate'
        AND (ru.USER_ID_ = #{userId}
        OR ru.GROUP_ID_ IN (
        select g.ID_ from ACT_ID_GROUP g, ACT_ID_MEMBERSHIP membership where g.ID_ = membership.GROUP_ID_ AND
        membership.USER_ID_ = #{userId} )
        )
        )
        )
        and a.del_flag="0"
        <include refid="where_sql"></include>
        GROUP BY a.id
        order by a.create_time desc
    </select>
    <select id="getMyDoneTaskList" resultMap="ProjectDecisionVoResult">
        select a.id,a.proc_inst_id, a.project_pool_id,tp.project_name, tp.industry,tp.project_stage,
        tp.project_state,tp.invest_head,tp.project_status, a.participants_id,a.participants,
        a.provision, a.remark, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time ,a.status,
        p.PROC_INST_ID_ as 'procInsId',
        p.PROC_DEF_ID_ as 'procDefId',
        date_format(p.START_TIME_, '%Y-%m-%d %H:%i:%s') as 'applyTime',
        date_format(p.END_TIME_, '%Y-%m-%d %H:%i:%s') as 'endTime',
        p.START_USER_ID_ AS 'startUserId',
        uu.nick_name AS 'startUserName',
        dd.dept_name AS 'startDeptName',
        t.ID_ AS 'taskId',
        t.NAME_ AS 'taskName',
        t.ASSIGNEE_ AS 'assignee',
        GROUP_CONCAT(DISTINCT ui.nick_name SEPARATOR '\r\n') as 'assigneeName',
        t.TASK_DEF_KEY_ as 'taskDefKey',
        rp.DEPLOYMENT_ID_ AS 'deployId'
        from t_project_decision a
        LEFT JOIN t_project_pool tp on tp.id=a.project_pool_id
        LEFT JOIN (select * from ACT_HI_PROCINST limit 10000000) p on a.proc_inst_id = p.PROC_INST_ID_
        LEFT JOIN ACT_RU_TASK t on a.proc_inst_id = t.PROC_INST_ID_
        LEFT JOIN ACT_RU_IDENTITYLINK ru on a.proc_inst_id = ru.PROC_INST_ID_
        LEFT JOIN (SELECT * FROM sys_user LIMIT 100000) uu ON p.START_USER_ID_ = uu.user_id
        LEFT JOIN (SELECT * FROM sys_dept LIMIT 100000) dd ON uu.dept_id = dd.dept_id
        LEFT JOIN (select * from sys_user limit 100000) ui on t.ASSIGNEE_ = ui.user_id
        LEFT JOIN act_re_procdef rp ON t.PROC_DEF_ID_ = rp.ID_
        where ru.USER_ID_ = #{userId} and a.del_flag="0"
        <include refid="where_sql"></include>
        GROUP BY a.id
        order by a.create_time desc
    </select>
    <select id="getMyList" resultMap="ProjectDecisionVoResult">
        select a.id,a.proc_inst_id, a.project_pool_id,tp.project_name, tp.industry,tp.project_stage,
        tp.project_state,tp.invest_head,tp.project_status, a.participants_id, a.participants,
        a.provision, a.remark, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time ,a.status,
        u.nick_name as 'applyUserName',
        p.PROC_INST_ID_ as 'procInsId',
        p.PROC_DEF_ID_ as 'procDefId',
        date_format(p.START_TIME_, '%Y-%m-%d %H:%i:%s') as 'applyTime',
        date_format(p.END_TIME_, '%Y-%m-%d %H:%i:%s') as 'endTime',
        p.START_USER_ID_ AS 'startUserId',
        uu.nick_name AS 'startUserName',
        dd.dept_name AS 'startDeptName',
        t.NAME_ as 'taskName',
        t.TASK_DEF_KEY_ as 'taskDefKey',
        DATE_FORMAT(t.CREATE_TIME_ ,'%Y-%m-%d %H:%i') as 'taskCreateTiem',
        t.ASSIGNEE_ as 'assignee',
        GROUP_CONCAT(DISTINCT ui.nick_name SEPARATOR '\r\n') as 'assigneeName',
        t.ID_ as 'taskId',
        t.EXECUTION_ID_ AS 'executionId',
        rp.DEPLOYMENT_ID_ AS 'deployId'
        from t_project_decision a
        left join t_project_pool tp on tp.id=a.project_pool_id
        left join ACT_HI_PROCINST p on a.proc_inst_id = p.PROC_INST_ID_
        LEFT JOIN (SELECT * FROM sys_user LIMIT 100000) uu ON p.START_USER_ID_ = uu.user_id
        LEFT JOIN (SELECT * FROM sys_dept LIMIT 100000) dd ON uu.dept_id = dd.dept_id
        left join sys_user u on a.create_by = u.user_id
        left join ACT_RU_TASK t on a.proc_inst_id = t.PROC_INST_ID_
        left join (select * from sys_user limit 100000) ui on t.ASSIGNEE_ = ui.user_id
        left join ACT_RU_IDENTITYLINK ru on ru.TASK_ID_ = t.ID_
        left join ACT_ID_MEMBERSHIP m on m.GROUP_ID_ = ru.GROUP_ID_ and m.USER_ID_ = #{userId}
        left join act_re_procdef rp ON t.PROC_DEF_ID_ = rp.ID_
        where a.create_by = #{userId} and a.proc_inst_id is not null and a.proc_inst_id != ''
        and a.del_flag="0"
        <include refid="where_sql"></include>
        GROUP BY a.id
        order by a.create_time desc
    </select>
    <!--<select id="selectTProjectPoolById" parameterType="String" resultMap="TProjectPoolResult">
        <include refid="selectTProjectPoolVo"/>
        where a.id = #{id}
    </select>-->
    <select id="selectTProjectDecisionByProcInstId" parameterType="java.lang.String" resultMap="TProjectDecisionResult">
        select a.id,a.proc_inst_id, a.project_pool_id, a.participants_id,a.participants,
               a.provision, a.remark, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time ,a.need_meeting,
               t.TASK_DEF_KEY_ as 'taskDefKey'
        from t_project_decision a
        left join ACT_RU_TASK t on a.proc_inst_id = t.PROC_INST_ID_
        left join (select * from sys_user limit 100000) ui on t.ASSIGNEE_ = ui.user_id
        left join sys_user u on a.create_by = u.user_id
        where a.proc_inst_id = #{procInstId,jdbcType=VARCHAR}
        GROUP BY a.id
    </select>
    <select id="selectProjectDecisionByProcInstId" parameterType="java.lang.String" resultMap="ProjectDecisionVoResult">
        select a.id,a.proc_inst_id, a.project_pool_id, a.participants_id,a.participants,
               a.provision, a.remark, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time ,a.need_meeting,
               t.TASK_DEF_KEY_ as 'taskDefKey',tp.project_name
        from t_project_decision a
        left join t_project_pool tp on tp.id=a.project_pool_id
        left join ACT_RU_TASK t on a.proc_inst_id = t.PROC_INST_ID_
        left join (select * from sys_user limit 100000) ui on t.ASSIGNEE_ = ui.user_id
        left join sys_user u on a.create_by = u.user_id
        where a.proc_inst_id = #{procInstId,jdbcType=VARCHAR}
        GROUP BY a.id
    </select>
</mapper>