<?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.bonus.material.work.mapper.DirectAuditMapper">

    <resultMap type="com.bonus.common.biz.domain.lease.WorkPeopleInfo" id="WorkPeopleInfoResult">
        <result property="id" column="id"/>
        <result property="nextNodeId" column="next_node_id"/>
        <result property="roleIds" column="roleIds"/>
    </resultMap>

    <select id="getList" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        SELECT
            dai.Id,
            dai.create_time AS createTime,
            su.user_name AS createName,
            bui.unit_id AS backUnitId,
            bui.unit_name AS backUnitName,
            bpl.pro_id AS backProId,
            bpl.pro_name AS backProName,
            bui1.unit_id AS leaseUnitId,
            bui1.unit_name AS leaseUnitName,
            bpl1.pro_id AS leaseProId,
            bpl1.pro_name AS leaseProName,
            dai.lease_man as leaseMan ,
            swr.workflow_status as flowStatus,
            swr.task_id as flowId,
            swrs.node_id AS nodeId,
            swrs.next_node_id AS nextNodeId,
            swrs.record_id AS recordId,
            swn.node_sign_type as nodeSignType
        FROM
            direct_apply_info dai
        LEFT JOIN bm_agreement_info bai ON dai.back_agreement_id = bai.agreement_id
        LEFT JOIN bm_agreement_info bai1 ON dai.lease_agreement_id = bai1.agreement_id
        LEFT JOIN bm_project bpl ON bpl.pro_id = bai.project_id
        LEFT JOIN bm_project bpl1 ON bpl1.pro_id = bai1.project_id
        LEFT JOIN bm_unit bui ON bui.unit_id = bai.unit_id
        LEFT JOIN bm_unit bui1 ON bui1.unit_id = bai1.unit_id
        LEFT JOIN direct_apply_details dad on dad.direct_id=dai.id
        left join sys_user su on dai.create_by = su.user_id
        left join sys_workflow_record swr on dai.id = swr.task_id and swr.task_type = '16'
        left join sys_workflow_type swt on swr.workflow_id = swt.id
        left join sys_workflow_node swn on swt.id = swn.type_id
        left join sys_workflow_config swc on swn.id = swc.node_id
        left join sys_workflow_record_history  swrs on swr.id = swrs.record_id
        <where>
            swr.task_id is not null
            <if test="keyWord != null and keyWord != ''">
                and (
                bui.unit_name like concat('%', #{keyWord}, '%') or
                bpl.pro_name like concat('%', #{keyWord}, '%') or
                bui1.unit_name like concat('%', #{keyWord}, '%') or
                bpl1.pro_name like concat('%', #{keyWord}, '%'))
            </if>
            <if test="lotId != null and lotId != ''">
                and bpl.pro_id = #{lotId}
            </if>
            <if test="unitId != null and unitId != ''">
                and bui.unit_id = #{unitId}
            </if>

            <if test="status != null and status != ''">
                and swr.workflow_status = #{status}
            </if>
            <if test="statusList != null  and statusList.size() > 0">
                and swr.workflow_status in
                <foreach item="item" collection="statusList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="isApp != null and taskStatus==0">and dai.status = 0 </if>
            <if test="isApp != null and taskStatus==1">and (dai.status = 1 or dai.status = 2)</if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                AND DATE_FORMAT( dai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
            </if>
        </where>
        GROUP BY swr.task_id order by dai.create_time desc
    </select>



    <select id="getAuditInfo" resultType="com.bonus.material.work.domain.DirectAudit">
        SELECT
            wh.record_id AS flowId,
            wh.node_id AS nodeId,
            wn.node_name AS nodeName,
            wn2.node_name AS nextNodeName,
            wh.next_node_id AS nextNodeId,
            wh.create_time AS createTime,
            wh.create_by AS creator,
            wh.is_accept as isAccept,
            wc.config_value as userId,
            wh.remark as remark
        FROM
            sys_workflow_record_history wh
                LEFT JOIN sys_workflow_config wc on wh.next_node_id = wc.node_id
                LEFT JOIN sys_workflow_node wn on wh.node_id = wn.id
                LEFT JOIN sys_workflow_node wn2 on wc.node_id = wn2.id
        WHERE wh.record_id = #{flowId}
    </select>

    <select id="getImpUnit" resultType="java.lang.Integer">
        select
            imp_unit as impUnit
        from
            bm_project
        where
            pro_id = #{proId}
    </select>

    <select id="getProjectIdById" resultType="java.lang.Integer">
        select
            project_id as projectId
        from
            bm_agreement_info
        where
            agreement_Id = #{leaseAgreementId}
    </select>
    <select id="getProjectNameById" resultType="java.lang.String">
        select
            pro_name as projectName
        from
            bm_project
        where
            pro_id = #{projectId}
    </select>

    <select id="selectWorkPeopleInfoList" resultType="com.bonus.common.biz.domain.lease.WorkPeopleInfo">
        SELECT
            wh.id as id,
            wh.next_node_id as nextNodeId,
            wc.roleIds as roleIds
        FROM
            sys_workflow_record_history wh
                LEFT JOIN sys_workflow_config wc on wh.next_node_id = wc.node_id
        WHERE wh.record_id = #{flowId}
        ORDER BY wh.id desc
    </select>

    <select id="selectUserListByRoleAndDept" resultType="com.bonus.common.biz.domain.lease.WorkPeopleInfo">
        SELECT
        su.user_id,
        su.user_name as receiverUsername
        FROM
        sys_user su
        LEFT JOIN sys_user_role sr on su.user_id = sr.user_id
        WHERE
        sr.role_id IN
        <foreach item="item" index="index" collection="roleIdArray" open="(" separator="," close=")">
            #{item}
        </foreach>

<!--        <if test="deptId != 0">-->
<!--            and  su.dept_id =#{deptId}-->
<!--        </if>-->

    </select>

    <update id="updateWorkflowRecord">
        update sys_workflow_record set workflow_status = #{flowStatus} where id = #{id}
    </update>
    <update id="updateDirectAudit">
        update direct_apply_info set status = #{status} where id = #{id}
    </update>

    <insert id="insertWorkOrder" useGeneratedKeys="true" keyProperty="id">
        insert into uni_org.sys_information
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUserName != null">create_user_name,</if>
            <if test="title != null">title,</if>
            <if test="content != null">content,</if>
            <if test="webAppId != null">web_app_id,</if>
            <if test="type != null">type,</if>
            <if test="extend != null">extend,</if>
            <if test="sendUserName != null">send_user_name,</if>
            <if test="webUrl != null">web_url,</if>
            <if test="typeId != null">type_id,</if>
            <if test="businessId != null">business_id,</if>
            <if test="flowState != null">flow_state,</if>
            <if test="rout != null">rout,</if>
            <if test="nextStatus != null">next_status,</if>
            <if test="extendName != null">extend_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createTime != null">now(),</if>
            <if test="createUserName != null">#{createUserName},</if>
            <if test="title != null">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="webAppId != null">#{webAppId},</if>
            <if test="type != null">#{type},</if>
            <if test="extend != null">#{extend},</if>
            <if test="sendUserName != null">#{sendUserName},</if>
            <if test="webUrl != null">#{webUrl},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="flowState != null">#{flowState},</if>
            <if test="rout != null">#{rout},</if>
            <if test="nextStatus != null">#{nextStatus},</if>
            <if test="extendName != null">#{extendName},</if>
        </trim>
    </insert>

    <insert id="insertWorkPeople">
        insert into uni_org.sys_information_people
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="informationId != null">information_id,</if>
            <if test="readState != null">read_state,</if>
            <if test="handleState != null">handle_state,</if>
            <if test="receiverUsername != null">receiver_username,</if>
            <if test="updateUser != null">update_user,</if>
            <if test="isExamine != null">is_examine,</if>

        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createTime != null">now(),</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="informationId != null">#{informationId},</if>
            <if test="readState != null">#{readState},</if>
            <if test="handleState != null">#{handleState},</if>
            <if test="receiverUsername != null">#{receiverUsername},</if>
            <if test="updateUser != null">#{updateUser},</if>
            <if test="isExamine != null">#{isExamine},</if>
        </trim>
    </insert>
</mapper>
