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

    <resultMap type="TProjectInvestigate" id="TProjectInvestigateResult">
        <result property="id"    column="id"    />
        <result property="investigateName"    column="investigate_name"    />
        <result property="investigateCode"    column="investigate_code"    />
        <result property="projectPoolId"    column="project_pool_id"    />
        <result property="investigatePerson"    column="investigate_person"    />
        <result property="investigatePersonId"    column="investigate_person_id"    />
        <result property="investigateCost"    column="investigate_cost"    />
        <result property="describe"    column="describe_"    />
        <result property="investigatePlace"    column="investigate_place"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="readiness"    column="readiness"    />
        <result property="thirdParty"    column="third_party"    />
        <result property="deadDate"    column="dead_date"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="status"    column="status"    />
        <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="projectName" column="project_name" />
        <result property="procInstId" column="proc_inst_id" />
    </resultMap>
    <resultMap type="ProjectInvestigateVo" id="ProjectInvestigateVoResult">
        <result property="id"    column="id"    />
        <result property="procInstId"    column="proc_inst_id"    />
        <result property="projectPoolId"    column="project_pool_id"    />
        <result property="investigateName"    column="investigate_name"    />
        <result property="investigateCode"    column="investigate_code"    />
        <result property="investigatePerson"    column="investigate_person"    />
        <result property="investigatePersonId"    column="investigate_person_id"    />
        <result property="investigateCost"    column="investigate_cost"    />
        <result property="describe"    column="describe_"    />
        <result property="investigatePlace"    column="investigate_place"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="readiness"    column="readiness"    />
        <result property="thirdParty"    column="third_party"    />
        <result property="deadDate"    column="dead_date"    />
        <result property="remark"    column="remark"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="status"    column="status"    />
        <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="selectTProjectInvestigateVo">
        select id, proc_inst_id, investigate_name, investigate_code, project_pool_id, investigate_place, start_time, end_time, readiness, third_party, dead_date, investigate_person, investigate_person_id, investigate_cost, describe_, remark, status, del_flag, create_by, create_time, update_by, update_time from t_project_investigate
    </sql>
    <sql id="where_sql">
        <if test="projectInvestigate.projectPoolId != null  and projectInvestigate.projectPoolId != ''"> and a.project_pool_id = #{projectInvestigate.projectPoolId}</if>
        <if test="projectInvestigate.projectName != null  and projectInvestigate.projectName != ''"> and tp.project_name like concat('%', #{projectInvestigate.projectName}, '%')</if>
        <if test="projectInvestigate.investigateName != null  and projectInvestigate.investigateName != ''"> and a.investigate_name like concat('%', #{projectInvestigate.investigateName}, '%')</if>
        <if test="projectInvestigate.investigatePlace != null  and projectInvestigate.investigatePlace != ''"> and a.investigate_place like concat('%', #{projectInvestigate.investigatePlace}, '%')</if>
        <if test="projectInvestigate.status != null"> and a.status = #{projectInvestigate.status}</if>
    </sql>
    <select id="selectAllInvestigateList" resultMap="ProjectInvestigateVoResult">
        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.investigate_name, a.investigate_code,a.investigate_place, a.start_time, a.end_time, a.readiness, a.third_party, a.dead_date,
        a.investigate_person, a.investigate_person_id, a.investigate_cost, a.describe_,
        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_investigate 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="ProjectInvestigateVoResult">
        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.investigate_name, a.investigate_code,a.investigate_place, a.start_time, a.end_time, a.readiness, a.third_party, a.dead_date,
        a.investigate_person, a.investigate_person_id, a.investigate_cost, a.describe_,
        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_investigate 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="ProjectInvestigateVoResult">
        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.investigate_name, a.investigate_code,a.investigate_place, a.start_time, a.end_time, a.readiness, a.third_party, a.dead_date,
        a.investigate_person, a.investigate_person_id, a.investigate_cost, a.describe_,
        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',
        GROUP_CONCAT(DISTINCT t.NAME_ SEPARATOR '\r\n' ) 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_investigate 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="ProjectInvestigateVoResult">
        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.investigate_name, a.investigate_code,a.investigate_place, a.start_time, a.end_time, a.readiness, a.third_party, a.dead_date,
        a.investigate_person, a.investigate_person_id, a.investigate_cost, a.describe_,
        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',
        GROUP_CONCAT(DISTINCT t.NAME_ SEPARATOR '\r\n' ) 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_investigate 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="selectTProjectInvestigateByProcInstId" parameterType="java.lang.String" resultMap="TProjectInvestigateResult">
        select a.id,a.proc_inst_id, a.project_pool_id, a.investigate_name, a.investigate_code,a.investigate_place, a.start_time, a.end_time, a.readiness, a.third_party, a.dead_date,
               a.investigate_person, a.investigate_person_id, a.investigate_cost, a.describe_,
               a.remark, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time ,a.status,
               tp.project_name,tp.project_status, t.TASK_DEF_KEY_ as 'taskDefKey'
        from t_project_investigate 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>
    <select id="selectProjectInvestigateByProcInstId" parameterType="java.lang.String" resultMap="ProjectInvestigateVoResult">
        select a.id,a.proc_inst_id, a.project_pool_id, a.investigate_name, a.investigate_code,a.investigate_place, a.start_time, a.end_time, a.readiness, a.third_party, a.dead_date,
               a.investigate_person, a.investigate_person_id, a.investigate_cost, a.describe_,
               a.remark, a.del_flag, a.create_by, a.create_time, a.update_by, a.update_time ,a.status,
               tp.project_name,tp.project_status, t.TASK_DEF_KEY_ as 'taskDefKey'
        from t_project_investigate 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>