<?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.TProjectInvestigateMapper">
    
    <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="procInstId" column="proc_inst_id" />
    </resultMap>

    <resultMap type="TProjectInvestigateVo" id="TProjectInvestigateVoResult">
        <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="procInstId" column="proc_inst_id" />

        <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="procInsId" column="procInsId"/>
        <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"/>

        <association property="tProjectPool" javaType="TProjectPool"  resultMap="TProjectPoolResult" />
        <association property="tProjectCompany" javaType="TProjectCompany"  resultMap="TProjectCompanyResult" />
        <association property="tProjectContacts" javaType="TProjectContacts"  resultMap="TProjectContactsResult" />
        <association property="tProjectChannel" javaType="TProjectChannel"  resultMap="TProjectChannelResult" />
    </resultMap>

    <resultMap type="TProjectCompany" id="TProjectCompanyResult">
        <result property="companyId"    column="companyId"    />
        <result property="companyName"    column="company_name"    />
        <result property="projectPoolId"    column="project_pool_id"    />
        <result property="companyCode"    column="company_code"    />
        <result property="filingTime"    column="filing_time"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="registeredCapital"    column="registered_capital"    />
        <result property="registeredAddress"    column="registered_address"    />
        <result property="paidCapital"    column="paid_capital"    />
        <result property="businessAddress"    column="business_address"    />
        <result property="actualBod"    column="actual_bod"    />
        <result property="phone"    column="phone"    />
        <result property="type"    column="type"    />
    </resultMap>

    <resultMap type="TProjectContacts" id="TProjectContactsResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="position"    column="position"    />
        <result property="contact"    column="contact"    />
    </resultMap>

    <resultMap type="TProjectChannel" id="TProjectChannelResult">
        <result property="channelName"    column="channel_name"    />
        <result property="channelCode"    column="channel_code"    />
        <result property="channelType"    column="channel_type"    />
        <result property="channelGroup"    column="channel_group"    />
        <result property="channelBlurb"    column="channel_blurb"    />
        <result property="contacts"    column="contacts"    />
        <result property="telephone"    column="telephone"    />
        <result property="address"    column="address"    />
        <result property="channelHead"    column="channel_head"    />
    </resultMap>

    <resultMap type="TProjectPool" id="TProjectPoolResult">
        <result property="industry"    column="industry"    />
        <result property="projectName"    column="project_name"    />
        <result property="projectStage"    column="project_stage"    />
        <result property="projectState"    column="project_state"    />
        <result property="projectGroup"    column="project_group"    />
        <result property="projectCode"    column="project_code"    />
    </resultMap>

    <sql id="selectTProjectInvestigateVo">
        select 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,proc_inst_id from t_project_investigate
    </sql>

    <select id="selectTProjectInvestigateList" parameterType="TProjectInvestigate" resultMap="TProjectInvestigateResult">
        <include refid="selectTProjectInvestigateVo"/>
        <where>
            del_flag = 0
            <if test="investigateName != null  and investigateName != ''"> and investigate_name like concat('%', #{investigateName}, '%')</if>
            <if test="investigateCode != null  and investigateCode != ''"> and investigate_code = #{investigateCode}</if>
            <if test="projectPoolId != null  and projectPoolId != ''"> and project_pool_id = #{projectPoolId}</if>
            <if test="investigatePerson != null  and investigatePerson != ''"> and investigate_person = #{investigatePerson}</if>
            <if test="investigateCost != null  and investigateCost != ''"> and investigate_cost = #{investigateCost}</if>
            <if test="describe != null  and describe != ''"> and describe = #{describe}</if>
        </where>
    </select>
    
    <select id="selectTProjectInvestigateById" parameterType="String" resultMap="TProjectInvestigateResult">
        <include refid="selectTProjectInvestigateVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTProjectInvestigate" parameterType="TProjectInvestigate">
        insert into t_project_investigate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="investigateName != null">investigate_name,</if>
            <if test="investigateCode != null">investigate_code,</if>
            <if test="projectPoolId != null">project_pool_id,</if>
            <if test="investigatePerson != null">investigate_person,</if>
            <if test="investigatePersonId != null">investigate_person_id,</if>
            <if test="investigateCost != null">investigate_cost,</if>
            <if test="describe != null">describe_,</if>
            <if test="investigatePlace != null">investigate_place,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="readiness != null">readiness,</if>
            <if test="thirdParty != null">third_party,</if>
            <if test="deadDate != null">dead_date,</if>
            <if test="remark != null">remark,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="procInstId != null">proc_inst_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="investigateName != null">#{investigateName},</if>
            <if test="investigateCode != null">#{investigateCode},</if>
            <if test="projectPoolId != null">#{projectPoolId},</if>
            <if test="investigatePerson != null">#{investigatePerson},</if>
            <if test="investigatePersonId != null">#{investigatePersonId},</if>
            <if test="investigateCost != null">#{investigateCost},</if>
            <if test="describe != null">#{describe},</if>
            <if test="investigatePlace != null">#{investigatePlace},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="readiness != null">#{readiness},</if>
            <if test="thirdParty != null">#{thirdParty},</if>
            <if test="deadDate != null">#{deadDate},</if>
            <if test="remark != null">#{remark},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="procInstId != null">#{procInstId},</if>
         </trim>
    </insert>

    <update id="updateTProjectInvestigate" parameterType="TProjectInvestigate">
        update t_project_investigate
        <trim prefix="SET" suffixOverrides=",">
            <if test="investigateName != null">investigate_name = #{investigateName},</if>
            <if test="investigateCode != null">investigate_code = #{investigateCode},</if>
            <if test="projectPoolId != null">project_pool_id = #{projectPoolId},</if>
            <if test="investigatePerson != null">investigate_person = #{investigatePerson},</if>
            <if test="investigatePersonId != null">investigate_person_id = #{investigatePersonId},</if>
            <if test="investigateCost != null">investigate_cost = #{investigateCost},</if>
            <if test="describe != null">describe_ = #{describe},</if>
            <if test="investigatePlace != null">investigate_place = #{investigatePlace},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="readiness != null">readiness = #{readiness},</if>
            <if test="thirdParty != null">third_party = #{thirdParty},</if>
            <if test="deadDate != null">dead_date = #{deadDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="procInstId != null">proc_inst_id = #{procInstId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTProjectInvestigateById" parameterType="String">
        delete from t_project_investigate where id = #{id}
    </delete>

    <delete id="deleteTProjectInvestigateByIds" parameterType="String">
        delete from t_project_investigate where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <update id="updateTProjectInvestigateByIds" parameterType="String">
        update t_project_investigate
        set del_flag = 1
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="listProjectPoolId" parameterType="String" resultMap="TProjectInvestigateResult">
        <include refid="selectTProjectInvestigateVo"/>
        where project_pool_id = #{projectPoolId} and del_flag = 0
    </select>

    <select id="listProjectPoolIdNew" parameterType="String" resultMap="TProjectInvestigateResult">
        <include refid="selectTProjectInvestigateVo"/>
        where project_pool_id = #{projectPoolId} and del_flag = 0
        ORDER BY create_time DESC LIMIT 1
    </select>

    <update id="editStatus" parameterType="String">
        update t_project_investigate
        set status = 1
        where id = #{id}
    </update>

    <sql id="where_sql">
        <if test="projectInvestigate.projectName != null  and projectInvestigate.projectName != ''"> and a.project_name like concat('%', #{projectInvestigate.projectName}, '%')</if>
        <if test="projectInvestigate.projectGroup != null  and projectInvestigate.projectGroup != ''"> and a.project_group = #{projectInvestigate.projectGroup}</if>
        <if test="projectInvestigate.channel != null  and projectInvestigate.channel != ''"> and a.channel = #{projectInvestigate.channel}</if>
        <if test="projectInvestigate.industry != null  and projectInvestigate.industry != ''"> and a.industry = #{projectInvestigate.industry}</if>
        <if test="projectInvestigate.company != null  and projectInvestigate.company != ''"> and b.company_name like concat('%', #{projectInvestigate.company}, '%')</if>
        <if test="projectInvestigate.investHead != null  and projectInvestigate.investHead != ''"> and a.invest_head like concat('%', #{projectInvestigate.investHead}, '%')</if>
        <if test="projectInvestigate.projectStage != null  and projectInvestigate.projectStage != ''"> and a.project_stage = #{projectInvestigate.projectStage}</if>
        <if test="projectInvestigate.projectState != null  and projectInvestigate.projectState != ''"> and a.project_state = #{projectInvestigate.projectState}</if>
        <if test="projectInvestigate.projectContacts != null  and projectInvestigate.projectContacts != ''"> and c.name like concat('%', #{projectInvestigate.projectContacts}, '%')</if>
    </sql>

    <select id="getMyTaskList" resultMap="TProjectInvestigateVoResult">
        select tp.*,a.project_name, d.channel_group project_group, a.record_date, a.project_code, a.channel, a.contact_date, a.project_date, a.decision_date, a.industry, a.company, a.description, a.project_contacts, a.invest_head, a.previous_financing, a.financing_stage, a.financing_condition, a.financing_money, a.financing_date, a.invest_sparkle, a.invest_valuation, a.invest_money, a.invest_type, a.invest_ploy, a.invest_worth, a.shareholding, a.project_stage, a.project_state, a.mark, a.approval_flag, a.investigate_flag, a.decision_flag,
        b.id companyId, b.company_name, b.filing_time, b.company_code, b.registered_capital, b.registered_address, b.paid_capital, b.business_address, b.actual_bod, b.phone, b.type,
        c.id, c.name, c.position, c.contact,
        d.channel_name, d.channel_code, d.channel_type, d.channel_group, d.channel_blurb, d.contacts, d.telephone, d.address, d.channel_head,
        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 tp
        left join t_project_pool a on tp.project_pool_id = a.id
        left join t_project_company b ON a.id = b.project_pool_id
        left join t_project_contacts c ON a.project_contacts = c.id
        left join t_project_channel d ON a.channel = d.id
        left join ACT_HI_PROCINST p on tp.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 tp.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" and tp.del_flag = "0"
        <include refid="where_sql"></include>
    </select>
    <select id="getMyDoneTaskList" resultMap="TProjectInvestigateVoResult">
        select tp.*,a.project_name, d.channel_group project_group, a.record_date, a.project_code, a.channel, a.contact_date, a.project_date, a.decision_date, a.industry, a.company, a.description, a.project_contacts, a.invest_head, a.previous_financing, a.financing_stage, a.financing_condition, a.financing_money, a.financing_date, a.invest_sparkle, a.invest_valuation, a.invest_money, a.invest_type, a.invest_ploy, a.invest_worth, a.shareholding, a.project_stage, a.project_state, a.mark, a.approval_flag, a.investigate_flag, a.decision_flag,
        b.id companyId, b.company_name, b.filing_time, b.company_code, b.registered_capital, b.registered_address, b.paid_capital, b.business_address, b.actual_bod, b.phone, b.type,
        c.id contactId, c.name, c.position, c.contact,
        d.channel_name, d.channel_code, d.channel_type, d.channel_group, d.channel_blurb, d.contacts, d.telephone, d.address, d.channel_head,
        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',
        ht.ID_ AS 'taskId',
        ht.NAME_ AS 'taskName',
        ht.ASSIGNEE_ AS 'assignee',
        ui.nick_name AS 'assigneeName',
        rp.DEPLOYMENT_ID_ AS 'deployId'
        from t_project_investigate tp
        left join t_project_pool a on tp.project_pool_id = a.id
        left join t_project_company b ON a.id = b.project_pool_id
        left join t_project_contacts c ON a.project_contacts = c.id
        left join t_project_channel d ON a.channel = d.id
        left join ACT_HI_TASKINST ht on tp.proc_inst_id = ht.PROC_INST_ID_
        left join ACT_HI_PROCINST p on tp.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 (select * from sys_user limit 100000) ui on ht.ASSIGNEE_ = ui.user_id
        left join ACT_RU_TASK t on tp.proc_inst_id = t.PROC_INST_ID_
        left join act_re_procdef rp ON t.PROC_DEF_ID_ = rp.ID_
        where
        ht.ASSIGNEE_ =  #{userId}
        AND ht.END_TIME_ IS NOT NULL
        and a.del_flag="0" and tp.del_flag = "0"
        <include refid="where_sql"></include>
        GROUP BY a.id
        ORDER BY ht.END_TIME_ DESC;
    </select>
    <select id="getMyList" resultMap="TProjectInvestigateVoResult">
        select tp.*,a.project_name, d.channel_group project_group, a.record_date, a.project_code, a.channel, a.contact_date, a.project_date, a.decision_date, a.industry, a.company, a.description, a.project_contacts, a.invest_head, a.previous_financing, a.financing_stage, a.financing_condition, a.financing_money, a.financing_date, a.invest_sparkle, a.invest_valuation, a.invest_money, a.invest_type, a.invest_ploy, a.invest_worth, a.shareholding, a.project_stage, a.project_state, a.mark, a.approval_flag, a.investigate_flag, a.decision_flag,
        b.id companyId, b.company_name, b.filing_time, b.company_code, b.registered_capital, b.registered_address, b.paid_capital, b.business_address, b.actual_bod, b.phone, b.type,
        c.id contactId, c.name, c.position, c.contact,
        d.channel_name, d.channel_code, d.channel_type, d.channel_group, d.channel_blurb, d.contacts, d.telephone, d.address, d.channel_head,
        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_investigate tp
        left join t_project_pool a on tp.project_pool_id = a.id
        left join t_project_company b ON a.id = b.project_pool_id
        left join t_project_contacts c ON a.project_contacts = c.id
        left join t_project_channel d ON a.channel = d.id
        left join ACT_HI_PROCINST p on tp.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 tp.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 tp.proc_inst_id is not null and a.proc_inst_id != ''
        and a.del_flag="0" and tp.del_flag = "0"
        <include refid="where_sql"></include>
        GROUP BY a.id
    </select>
</mapper>