<?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.lease.mapper.LeaseTaskMapper">
    <resultMap type="com.bonus.common.biz.domain.lease.LeaseApplyInfo" id="LeaseApplyInfoResult">
        <result property="id" column="id"/>
        <result property="code" column="code"/>
        <result property="taskId" column="task_id"/>
        <result property="leasePerson" column="lease_person"/>
        <result property="phone" column="phone"/>
        <result property="type" column="type"/>
        <result property="companyAuditBy" column="company_audit_by"/>
        <result property="companyAuditTime" column="company_audit_time"/>
        <result property="companyAuditRemark" column="company_audit_remark"/>
        <result property="deptAuditBy" column="dept_audit_by"/>
        <result property="deptAuditTime" column="dept_audit_time"/>
        <result property="deptAuditRemark" column="dept_audit_remark"/>
        <result property="directAuditBy" column="direct_audit_by"/>
        <result property="directAuditTime" column="direct_audit_time"/>
        <result property="directAuditRemark" column="direct_audit_remark"/>
        <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="remark" column="remark"/>
        <result property="companyId" column="company_id"/>
        <result property="directId" column="direct_id"/>
        <result property="leaseType" column="lease_type"/>
        <result property="estimateLeaseTime" column="estimate_lease_time"/>
        <result property="costBearingParty" column="cost_bearing_party"/>
        <result property="leaseProject" column="pro_name"/>
        <result property="leaseProjectId" column="project_id"/>
        <result property="leaseUnit" column="unit_name"/>
        <result property="leaseUnitId" column="unit_id"/>
        <result property="agreementId" column="agreement_id"/>
        <result property="agreementCode" column="agreement_code"/>
        <result property="leaseSignUrl" column="lease_sign_url"/>
        <result property="leaseSignType" column="lease_sign_type"/>
        <result property="applyCode" column="apply_code"/>
        <result property="nextNodeId" column="next_node_id"/>
        <result property="nodeId" column="node_id"/>
        <result property="recordId" column="record_id"/>
    </resultMap>

    <resultMap type="com.bonus.material.lease.domain.LeaseApplyDetails" id="LeaseApplyDetailsResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="typeId" column="type_id"/>
        <result property="typeName" column="type_name"/>
        <result property="maTypeName" column="ma_type_name"/>
        <result property="unitName" column="unit_name"/>
        <result property="unitValue" column="unit_value"/>
        <result property="storageNum" column="storage_num"/>
        <result property="preNum" column="pre_num"/>
        <result property="auditNum" column="audit_num"/>
        <result property="alNum" column="al_num"/>
        <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="remark" column="remark"/>
        <result property="companyId" column="company_id"/>
    </resultMap>

    <resultMap type="com.bonus.material.ma.domain.Type" id="TypeResult">
        <result property="typeId" column="type_id"/>
        <result property="typeName" column="type_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="keeperNickName" column="keeper_nick_name"/>
        <result property="repairNickName" column="repair_nick_name"/>
        <result property="houseId" column="house_id"/>
        <result property="houseName" column="house_name"/>
        <result property="storageNum" column="storage_num"/>
        <result property="typeCode" column="type_code"/>
        <result property="modelCode" column="model_code"/>
        <result property="unitId" column="unit_id"/>
        <result property="unitName" column="unit_name"/>
        <result property="unitValue" column="unit_value"/>
        <result property="sortNum" column="sort_num"/>
        <result property="manageType" column="manage_type"/>
        <result property="leasePrice" column="lease_price"/>
        <result property="effTime" column="eff_time"/>
        <result property="rentPrice" column="rent_price"/>
        <result property="buyPrice" column="buy_price"/>
        <result property="payRatio" column="pay_ratio"/>
        <result property="payPrice" column="pay_price"/>
        <result property="taxRatio" column="tax_ratio"/>
        <result property="level" column="level"/>
        <result property="ratedLoad" column="rated_load"/>
        <result property="testLoad" column="test_load"/>
        <result property="holdingTime" column="holding_time"/>
        <result property="warnNum" column="warn_num"/>
        <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="isPlan" column="is_plan"/>
        <result property="isAncuo" column="is_ancuo"/>
        <result property="remark" column="remark"/>
        <result property="facModel" column="fac_model"/>
        <result property="intelligentCode" column="intelligent_code"/>
        <result property="isTest" column="is_test"/>
    </resultMap>

    <resultMap type="com.bonus.common.biz.domain.lease.LeasePublishInfo" id="LeasePublishInfoResult">
        <result property="id" column="id"/>
        <result property="code" column="code"/>
        <result property="taskId" column="task_id"/>
        <result property="leasePerson" column="lease_person"/>
        <result property="phone" column="phone"/>
        <result property="type" column="type"/>
        <result property="companyAuditBy" column="company_audit_by"/>
        <result property="companyAuditTime" column="company_audit_time"/>
        <result property="companyAuditRemark" column="company_audit_remark"/>
        <result property="deptAuditBy" column="dept_audit_by"/>
        <result property="deptAuditTime" column="dept_audit_time"/>
        <result property="deptAuditRemark" column="dept_audit_remark"/>
        <result property="directAuditBy" column="direct_audit_by"/>
        <result property="directAuditTime" column="direct_audit_time"/>
        <result property="directAuditRemark" column="direct_audit_remark"/>
        <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="remark" column="remark"/>
        <result property="companyId" column="company_id"/>
        <result property="directId" column="direct_id"/>
        <result property="leaseType" column="lease_type"/>
        <result property="estimateLeaseTime" column="estimate_lease_time"/>
        <result property="costBearingParty" column="cost_bearing_party"/>
        <result property="leaseProject" column="pro_name"/>
        <result property="leaseProjectId" column="project_id"/>
        <result property="leaseUnit" column="unit_name"/>
        <result property="leaseUnitId" column="unit_id"/>
        <result property="agreementId" column="agreement_id"/>
        <result property="agreementCode" column="agreement_code"/>
        <result property="leaseSignUrl" column="lease_sign_url"/>
        <result property="leaseSignType" column="lease_sign_type"/>
        <result property="applyCode" column="apply_code"/>
    </resultMap>



    <sql id="selectLeaseApplyInfoVo">
        select
        lai.id, lai.code, lai.task_id, lai.lease_person, lai.phone, lai.type, lai.company_audit_by,lai.apply_code,
        lai.company_audit_time, lai.company_audit_remark, lai.dept_audit_by, lai.dept_audit_time,
        lai.dept_audit_remark, lai.direct_audit_by, lai.direct_audit_time, lai.direct_audit_remark,
        lai.create_by, lai.create_time, lai.update_by, lai.update_time, lai.remark, lai.company_id,
        lai.direct_id, lai.lease_type, lai.estimate_lease_time, lai.cost_bearing_party, lai.lease_sign_url,
        lai.lease_sign_type,tt.task_id as taskId,
        bai.unit_id,bai.project_id,bu.unit_name, bp.pro_name,bai.agreement_id, bai.agreement_code, tt.task_status as taskStatus,

        case tt.task_status
        when 0 then '待审核'
        when 1 then '待审核'
        when 2 then '审核中'
        when 3 then '已完成'
        when 4 then '已完成'
            end as taskStatusName,
        IFNULL(sum(lad.pre_num),0) as preCountNum,
        IFNULL(sum(lad.al_num),0) as alNum,
        GROUP_CONCAT(DISTINCT mt1.type_name) AS maTypeNames,
        bp.contract_part as contractPart,
        sd.dept_name as impUnitName
        from
        lease_apply_info lai
        left join tm_task tt on lai.task_id = tt.task_id
        left join lease_apply_details lad on lai.id = lad.parent_id
        left join tm_task_agreement tta on lai.task_id = tta.task_id
        left join bm_agreement_info bai on tta.agreement_id = bai.agreement_id
        left join bm_unit bu on bu.unit_id = bai.unit_id
        left join bm_project bp on bp.pro_id = bai.project_id
        left join sys_dept sd on sd.dept_id = bp.imp_unit
        left join sys_dict_data sda on tt.task_status = sda.dict_value
        and sda.dict_type = 'lease_task_status'
        left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        <!--        <if test="userId != null">-->
        <!--            JOIN ma_type_keeper mtk ON mtk.type_id = lad.type_id AND mtk.user_id = #{userId}-->
        <!--        </if>-->
    </sql>

    <sql id="selectLeaseApplyDetailsVo">
        select
        lad.id, lad.parent_id, mt.type_id, mt.type_name, mt2.type_name as ma_type_name,
        CASE mt.manage_type
        WHEN 0 THEN
        IFNULL(subquery0.num, 0)
        ELSE
        IFNULL(mt.storage_num, 0)
        END as storage_num,
        mt.manage_type as manageType,
        (lad.pre_num - IF(lad.al_num IS NULL,'0',lad.al_num)) AS outNum,
        IFNULL(lad.pre_num,0) as pre_num,
        IFNULL(lad.audit_num,0) as audit_num,
        IFNULL(lad.al_num,0) as al_num,
        IFNULL(lad.status,0) as status, mt.unit_name,mt.unit_value,
        lad.create_by, lad.create_time, lad.update_by, lad.update_time, lad.remark, lad.company_id
        from
        lease_apply_details lad
        left join
        ma_type mt on lad.type_id = mt.type_id and mt.`level` = '4' and mt.del_flag = '0'
        left join
        ma_type mt2 on mt2.type_id = mt.parent_id and mt2.`level` = '3' and mt2.del_flag = '0'
        left join (SELECT mt.type_id,
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        count(mm.ma_id) num
        FROM ma_machine mm
        LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        WHERE mm.ma_code is not null and mm.ma_status in (1)
        GROUP BY mt.type_id) AS subquery0 ON subquery0.type_id = mt.type_id
        <!--        <if test="userId != null">-->
        <!--            JOIN ma_type_keeper mtk ON mtk.type_id = lad.type_id AND mtk.user_id = #{userId}-->
        <!--        </if>-->
    </sql>

    <insert id="insertLeaseApplyInfo" parameterType="com.bonus.common.biz.domain.lease.LeaseApplyInfo"
            useGeneratedKeys="true" keyProperty="id">
        insert into lease_apply_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="taskId != null">task_id,</if>
            <if test="leasePerson != null">lease_person,</if>
            <if test="phone != null">phone,</if>
            <if test="type != null">type,</if>
            <if test="companyAuditBy != null">company_audit_by,</if>
            <if test="companyAuditTime != null">company_audit_time,</if>
            <if test="companyAuditRemark != null">company_audit_remark,</if>
            <if test="deptAuditBy != null">dept_audit_by,</if>
            <if test="deptAuditTime != null">dept_audit_time,</if>
            <if test="deptAuditRemark != null">dept_audit_remark,</if>
            <if test="directAuditBy != null">direct_audit_by,</if>
            <if test="directAuditTime != null">direct_audit_time,</if>
            <if test="directAuditRemark != null">direct_audit_remark,</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="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            <if test="directId != null">direct_id,</if>
            <if test="leaseType != null">lease_type,</if>
            <if test="estimateLeaseTime != null">estimate_lease_time,</if>
            <if test="costBearingParty != null">cost_bearing_party,</if>
            <if test="applyCode != null">apply_code,</if>
            <if test="supplierTime != null">supplier_time,</if>
            <if test="supplierPlace != null">supplier_place,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="projectId != null">project_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="leasePerson != null">#{leasePerson},</if>
            <if test="phone != null">#{phone},</if>
            <if test="type != null">#{type},</if>
            <if test="companyAuditBy != null">#{companyAuditBy},</if>
            <if test="companyAuditTime != null">#{companyAuditTime},</if>
            <if test="companyAuditRemark != null">#{companyAuditRemark},</if>
            <if test="deptAuditBy != null">#{deptAuditBy},</if>
            <if test="deptAuditTime != null">#{deptAuditTime},</if>
            <if test="deptAuditRemark != null">#{deptAuditRemark},</if>
            <if test="directAuditBy != null">#{directAuditBy},</if>
            <if test="directAuditTime != null">#{directAuditTime},</if>
            <if test="directAuditRemark != null">#{directAuditRemark},</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="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="directId != null">#{directId},</if>
            <if test="leaseType != null">#{leaseType},</if>
            <if test="estimateLeaseTime != null">#{estimateLeaseTime},</if>
            <if test="costBearingParty != null">#{costBearingParty},</if>
            <if test="applyCode != null">#{applyCode},</if>
            <if test="supplierTime != null">#{supplierTime},</if>
            <if test="supplierPlace != null">#{supplierPlace},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="projectId != null">#{projectId},</if>
        </trim>
    </insert>

    <insert id="insertLeaseApplyDetailsList" keyColumn="id" keyProperty="id" parameterType="map"
            useGeneratedKeys="true">
        insert into lease_apply_details
        (parent_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by,
        update_time, remark, company_id,ma_type_ids,is_manual)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.parentId,jdbcType=INTEGER}, #{item.typeId,jdbcType=INTEGER}, #{item.preNum,jdbcType=INTEGER},
            #{item.alNum,jdbcType=INTEGER}, #{item.status,jdbcType=VARCHAR}, #{item.createBy,jdbcType=VARCHAR},
            NOW(), #{item.updateBy,jdbcType=VARCHAR}, NOW(),
            #{item.remark,jdbcType=VARCHAR}, #{item.companyId,jdbcType=INTEGER},#{item.maTypeIdsStr,jdbcType=VARCHAR},
            #{item.isManual,jdbcType=INTEGER})
        </foreach>
    </insert>

    <insert id="addPublish">
        insert into lease_publish_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="newTypeId != null">new_type,</if>
            <if test="num != null">num,</if>
            <if test="leasePerson != null">lease_person,</if>
            <if test="phone != null">phone,</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="remark != null">remark,</if>
            <if test="unitId != null">unit_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="monthOrder != null">month_order,</if>
            <if test="publishTask != null and publishTask != ''">publish_task,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="newTypeId != null">#{newTypeId},</if>
            <if test="num != null">#{num},</if>
            <if test="leasePerson != null">#{leasePerson},</if>
            <if test="phone != null">#{phone},</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="remark != null">#{remark},</if>
            <if test="unitId != null">#{unitId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="monthOrder != null">#{monthOrder},</if>
            <if test="publishTask != null and publishTask != ''">#{publishTask},</if>
        </trim>
    </insert>

    <update id="updateLeaseApplyInfo">
        update lease_apply_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="leasePerson != null">lease_person = #{leasePerson},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="type != null">`type` = #{type},</if>
            <if test="companyAuditBy != null">company_audit_by = #{companyAuditBy},</if>
            <if test="companyAuditTime != null">company_audit_time = #{companyAuditTime},</if>
            <if test="companyAuditRemark != null">company_audit_remark = #{companyAuditRemark},</if>
            <if test="deptAuditBy != null">dept_audit_by = #{deptAuditBy},</if>
            <if test="deptAuditTime != null">dept_audit_time = #{deptAuditTime},</if>
            <if test="deptAuditRemark != null">dept_audit_remark = #{deptAuditRemark},</if>
            <if test="directAuditBy != null">direct_audit_by = #{directAuditBy},</if>
            <if test="directAuditTime != null">direct_audit_time = #{directAuditTime},</if>
            <if test="directAuditRemark != null">direct_audit_remark = #{directAuditRemark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="applyCode != null">apply_code = #{applyCode},</if>
            <if test="supplierTime != null">supplier_time = #{supplierTime},</if>
            <if test="supplierPlace != null">supplier_place = #{supplierPlace},</if>
            update_time = now(),
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="directId != null">direct_id = #{directId},</if>
            <if test="leaseType != null">lease_type = #{leaseType},</if>
            <if test="estimateLeaseTime != null">estimate_lease_time = #{estimateLeaseTime},</if>
            <if test="costBearingParty != null">cost_bearing_party = #{costBearingParty},</if>
        </trim>
        where id = #{id}
    </update>

    <select id="selectLeaseApplyInfoList" parameterType="com.bonus.common.biz.domain.lease.LeaseApplyInfo"
            resultMap="LeaseApplyInfoResult">
        select
        lai.id, lai.code, lai.task_id, lai.lease_person, lai.phone, lai.type, lai.company_audit_by,lai.apply_code,
        lai.company_audit_time, lai.company_audit_remark, lai.dept_audit_by, lai.dept_audit_time,
        lai.dept_audit_remark, lai.direct_audit_by, lai.direct_audit_time, lai.direct_audit_remark,
        lai.create_by, lai.create_time, lai.update_by, lai.update_time, lai.remark, lai.company_id,
        lai.direct_id, lai.lease_type, lai.estimate_lease_time, lai.cost_bearing_party, lai.lease_sign_url,
        lai.lease_sign_type,tt.task_id as taskId,
        lai.unit_id,lai.project_id,bu.unit_name, bp.pro_name, tt.task_status as taskStatus,

        case tt.task_status
        when 0 then '待审核'
        when 6 then '审核中'
        when 3 then '已驳回'
        when 2 then '已完成'
        when 5 then '待提交'
        end as taskStatusName,
        IFNULL(sum(lad.pre_num),0) as preCountNum,
        IFNULL(sum(lad.al_num),0) as alNum,
        GROUP_CONCAT(DISTINCT mt1.type_name) AS maTypeNames,
        bp.contract_part as contractPart,
        sd.dept_name as impUnitName,
        bai.agreement_code
        from
        lease_apply_info lai
        left join tm_task tt on lai.task_id = tt.task_id
        left join lease_apply_details lad on lai.id = lad.parent_id
        LEFT JOIN (SELECT parent_id, unit_id, project_id from lease_publish_details
        GROUP BY parent_id ) a on a.parent_id = lai.id
        LEFT JOIN bm_agreement_info bai on a.unit_id = bai.unit_id
        and a.project_id = bai.project_id
        left join bm_unit bu on bu.unit_id = lai.unit_id
        left join bm_project bp on bp.pro_id = lai.project_id
        left join sys_dept sd on sd.dept_id = bp.imp_unit
        left join sys_dict_data sda on tt.task_status = sda.dict_value
        and sda.dict_type = 'lease_apply_task_status'
        left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        left join sys_workflow_record swr on swr.task_id = lai.task_id
            where tt.task_type = '19' and sda.dict_type = 'lease_apply_task_status'
            <if test="code != null  and code != ''">and lai.code = #{code}</if>
            <if test="taskId != null ">and lai.task_id = #{taskId}</if>
            <if test="leasePerson != null  and leasePerson != ''">and lai.lease_person = #{leasePerson}</if>
            <if test="phone != null  and phone != ''">and lai.phone = #{phone}</if>
            <if test="type != null  and type != ''">and lai.type = #{type}</if>
            <if test="companyAuditBy != null ">and lai.company_audit_by = #{companyAuditBy}</if>
            <if test="companyAuditTime != null ">and lai.company_audit_time = #{companyAuditTime}</if>
            <if test="companyAuditRemark != null  and companyAuditRemark != ''">and lai.company_audit_remark =
                #{companyAuditRemark}
            </if>
            <if test="deptAuditBy != null ">and lai.dept_audit_by = #{deptAuditBy}</if>
            <if test="deptAuditTime != null ">and lai.dept_audit_time = #{deptAuditTime}</if>
            <if test="deptAuditRemark != null  and deptAuditRemark != ''">and lai.dept_audit_remark =
                #{deptAuditRemark}
            </if>
            <if test="directAuditBy != null ">and lai.direct_audit_by = #{directAuditBy}</if>
            <if test="directAuditTime != null ">and lai.direct_audit_time = #{directAuditTime}</if>
            <if test="directAuditRemark != null  and directAuditRemark != ''">and lai.direct_audit_remark =
                #{directAuditRemark}
            </if>
            <if test="companyId != null ">and lai.company_id = #{companyId}</if>
            <if test="statusList != null  and statusList.size() > 0">
                and tt.task_status in
                <foreach item="item" collection="statusList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
            </if>
            <if test="directId != null ">and lai.direct_id = #{directId}</if>
            <if test="leaseType != null  and leaseType != ''">and lai.lease_type = #{leaseType}</if>
            <if test="estimateLeaseTime != null ">and lai.estimate_lease_time = #{estimateLeaseTime}</if>
            <if test="costBearingParty != null  and costBearingParty != ''">and lai.cost_bearing_party =
                #{costBearingParty}
            </if>
        <if test="isApp != null and taskStatus==1">and (tt.task_status = 0 or tt.task_status = 6 or tt.task_status = 5) </if>
        <if test="isApp != null and taskStatus==3">and (tt.task_status = 2 or tt.task_status = 3)</if>
        GROUP BY lai.id
        ORDER BY tt.task_status,tt.create_time desc
    </select>

    <select id="selectLeaseApplyDetailsList" parameterType="com.bonus.material.lease.domain.LeaseApplyDetails"
            resultMap="LeaseApplyDetailsResult">
        <include refid="selectLeaseApplyDetailsVo"/>
        <where>
            <if test="parentId != null ">and lad.parent_id = #{parentId}</if>
            <if test="keyword != null and keyword != ''">
                AND (mt.type_name LIKE CONCAT('%', #{keyword}, '%')
                OR mt2.type_name LIKE CONCAT('%', #{keyword}, '%'))
            </if>
            <if test="typeId != null ">and lad.type_id = #{typeId}</if>
            <if test="preNum != null ">and lad.pre_num = #{preNum}</if>
            <if test="auditNum != null ">and lad.audit_num = #{auditNum}</if>
            <if test="alNum != null ">and lad.al_num = #{alNum}</if>
            <if test="status != null  and status != ''">and lad.status = #{status}</if>
            <if test="companyId != null ">and lad.company_id = #{companyId}</if>
        </where>
    </select>

    <select id="selectLeaseApplyInfoById" parameterType="Long" resultMap="LeaseApplyInfoResult">
        select
            lai.id, lai.code, lai.task_id, lai.lease_person, lai.phone, lai.type, lai.company_audit_by,lai.apply_code,
            lai.company_audit_time, lai.company_audit_remark, lai.dept_audit_by, lai.dept_audit_time,
            lai.dept_audit_remark, lai.direct_audit_by, lai.direct_audit_time, lai.direct_audit_remark,
            lai.create_by, lai.create_time, lai.update_by, lai.update_time, lai.remark, lai.company_id,
            lai.direct_id, lai.lease_type, lai.estimate_lease_time, lai.cost_bearing_party, lai.lease_sign_url,
            lai.lease_sign_type,tt.task_id as taskId,
            lai.unit_id,lai.project_id,bu.unit_name, bp.pro_name, tt.task_status as taskStatus,
            lai.supplier_time as supplierTime,
            lai.supplier_place as supplierPlace,
            case tt.task_status
                when 0 then '待审核'
                when 1 then '待审核'
                when 2 then '审核中'
                when 3 then '已完成'
                when 4 then '已完成'
                end as taskStatusName,
            IFNULL(sum(lad.pre_num),0) as preCountNum,
            IFNULL(sum(lad.al_num),0) as alNum,
            GROUP_CONCAT(DISTINCT mt1.type_name) AS maTypeNames,
            bp.contract_part as contractPart,
            sd.dept_name as impUnitName
        from
            lease_apply_info lai
                left join tm_task tt on lai.task_id = tt.task_id
                left join lease_apply_details lad on lai.id = lad.parent_id
                left join tm_task_agreement tta on lai.task_id = tta.task_id
                left join bm_unit bu on bu.unit_id = lai.unit_id
                left join bm_project bp on bp.pro_id = lai.project_id
                left join sys_dept sd on sd.dept_id = bp.imp_unit
                left join sys_dict_data sda on tt.task_status = sda.dict_value
                and sda.dict_type = 'lease_task_status'
                left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
                left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        where lai.id = #{id}
    </select>

    <select id="getTypeDataList" resultType="com.bonus.material.ma.domain.Type">
        SELECT DISTINCT m.type_id     as typeId,
                        m.type_name   as typeName,
                        m2.type_name  as maTypeName,
                        m.parent_id   as parentId,
                        m.unit_name   as unitName,
                        m.manage_type as manageType,
                        m.lease_price as leasePrice,
                        m.LEVEL,
                        CASE m.manage_type
                            WHEN 0 THEN
                                IFNULL(subquery0.num, 0)
                            ELSE
                                IFNULL(m.storage_num, 0)
                            END       as storageNum
        from ma_type m
                 LEFT JOIN ma_type m2 on m2.type_id = m.parent_id and m2.del_flag = 0
                 left join (SELECT mt.type_id,
                                   mt2.type_name AS typeName,
                                   mt.type_name  AS typeModelName,
                                   count(mm.ma_id)  num
                            FROM ma_machine mm
                                     LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
                                     LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                            WHERE mm.ma_code is not null
                              and mm.ma_status in (1)
                            GROUP BY mt.type_id) AS subquery0 ON subquery0.type_id = m.type_id
        where m.del_flag = '0'
          and m.parent_id = #{typeId}
    </select>
    <select id="selectAuditLeaseApplyInfoList" resultMap="LeaseApplyInfoResult">
        select
        lai.id, lai.code, lai.task_id, lai.lease_person, lai.phone, lai.type, lai.company_audit_by,lai.apply_code,
        lai.company_audit_time, lai.company_audit_remark, lai.dept_audit_by, lai.dept_audit_time,
        lai.dept_audit_remark, lai.direct_audit_by, lai.direct_audit_time, lai.direct_audit_remark,
        lai.create_by, lai.create_time, lai.update_by, lai.update_time, lai.remark, lai.company_id,
        lai.direct_id, lai.lease_type, lai.estimate_lease_time, lai.cost_bearing_party, lai.lease_sign_url,
        tt.task_id as taskId,swn.node_sign_type as nodeSignType,
        lai.unit_id,lai.project_id,bu.unit_name, bp.pro_name, swr.workflow_status as taskStatus,bai.agreement_code,
        swrs.node_id,swrs.next_node_id,swrs.record_id,
        case swr.workflow_status
        when 0 then '待审核'
        when 1 then '审核中'
        when 2 then '已完成'
        when 3 then '已驳回'
        end as taskStatusName,
        IFNULL(sum(lad.pre_num),0) as preCountNum,
        IFNULL(sum(lad.al_num),0) as alNum,
        GROUP_CONCAT(DISTINCT mt1.type_name) AS maTypeNames,
        bp.contract_part as contractPart,
        sd.dept_name as impUnitName
        from
        lease_apply_info lai
        left join tm_task tt on lai.task_id = tt.task_id
        left join lease_apply_details lad on lai.id = lad.parent_id
        left join tm_task_agreement tta on lai.task_id = tta.task_id
        left join bm_agreement_info bai on tta.agreement_id = bai.agreement_id
        left join bm_unit bu on bu.unit_id = lai.unit_id
        left join bm_project bp on bp.pro_id = lai.project_id
        left join sys_dept sd on sd.dept_id = bp.imp_unit
        left join sys_dict_data sda on tt.task_status = sda.dict_value
        and sda.dict_type = 'lease_task_status'
        left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        left join sys_workflow_record swr on tt.task_id = swr.task_id
        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 1=1
<!--        <if test="userId != null  and userId != ''">and swc.config_value  LIKE CONCAT('%', #{userId}, '%')</if>-->
        <if test="code != null  and code != ''">and lai.code = #{code}</if>
        <if test="taskId != null ">and lai.task_id = #{taskId}</if>
        <if test="leasePerson != null  and leasePerson != ''">and lai.lease_person = #{leasePerson}</if>
        <if test="phone != null  and phone != ''">and lai.phone = #{phone}</if>
        <if test="type != null  and type != ''">and lai.type = #{type}</if>
        <if test="companyAuditBy != null ">and lai.company_audit_by = #{companyAuditBy}</if>
        <if test="companyAuditTime != null ">and lai.company_audit_time = #{companyAuditTime}</if>
        <if test="companyAuditRemark != null  and companyAuditRemark != ''">and lai.company_audit_remark =
            #{companyAuditRemark}
        </if>
        <if test="deptAuditBy != null ">and lai.dept_audit_by = #{deptAuditBy}</if>
        <if test="deptAuditTime != null ">and lai.dept_audit_time = #{deptAuditTime}</if>
        <if test="deptAuditRemark != null  and deptAuditRemark != ''">and lai.dept_audit_remark =
            #{deptAuditRemark}
        </if>
        <if test="directAuditBy != null ">and lai.direct_audit_by = #{directAuditBy}</if>
        <if test="directAuditTime != null ">and lai.direct_audit_time = #{directAuditTime}</if>
        <if test="directAuditRemark != null  and directAuditRemark != ''">and lai.direct_audit_remark =
            #{directAuditRemark}
        </if>
        <if test="companyId != null ">and lai.company_id = #{companyId}</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="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="directId != null ">and lai.direct_id = #{directId}</if>
        <if test="leaseType != null  and leaseType != ''">and lai.lease_type = #{leaseType}</if>
        <if test="estimateLeaseTime != null ">and lai.estimate_lease_time = #{estimateLeaseTime}</if>
        <if test="costBearingParty != null  and costBearingParty != ''">and lai.cost_bearing_party =
            #{costBearingParty}
        </if>
        <if test="taskStatus==0">and swr.workflow_status = 0 </if>
        <if test="taskStatus==1">and swr.workflow_status = 1 </if>
        <if test="taskStatus==2">and swr.workflow_status = 2 </if>
        <if test="taskStatus==3">and swr.workflow_status = 3 </if>
        GROUP BY lai.id
        ORDER BY tt.create_time desc
    </select>

    <select id="getDeptIdByUserId" resultType="long">
        select
            dept_id as deptId
        from sys_user su
        where user_id = #{userId} and del_flag = 0
    </select>

    <select id="getDeptIdByParentId" resultType="com.bonus.material.lease.domain.vo.LeaseDeptInfo">
        select
            dept_id as deptId,
            parent_id as parentId,
            dept_name as deptName,
            ancestors as ancestors,
            company_sx as companySx
        from sys_dept
        where dept_id = #{deptId} and del_flag = 0
    </select>

    <select id="selectMaTypeThreeLists" resultMap="TypeResult">
        SELECT m.type_id,
               m.type_name,
               m.parent_id,
               m.manage_type,
               m.LEVEL,
               m.type_id   AS id,
               m.type_name AS label
        FROM ma_type m
    </select>

    <select id="selectParentId" resultType="com.bonus.material.ma.domain.vo.MaTypeVoLevelTwo">
        SELECT DISTINCT
            mt.type_name AS typeName,
            mt2.type_name AS materialName,
            mt3.type_name AS materialType,
            mt4.type_name AS itemType,
            mt.manage_type as manageType,
            mt.unit_name as unitName,
            mt.lease_price as leasePrice,
            mt.rent_price as rentPrice,
            mt.LEVEL as level,
            mt.storage_num as storageNum
        FROM
            ma_type mt
                LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
                LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id
                LEFT JOIN ma_type mt4 ON mt3.parent_id = mt4.type_id
        where  mt2.type_id is not null and mt.del_flag = '0'
          and mt.`level` = 4 and mt2.`level` = 3 and mt3.`level` = 2 and mt4.`level` = 1

    </select>

    <update id="updateLeaseTaskStatus">
        update tm_task
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskStatus != null">task_status = #{taskStatus},</if>
        </trim>
        where task_Id = #{taskId}
    </update>

    <update id="updatePublish">
        update lease_apply_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="num != null">publish_num = IFNULL(publish_num, 0) + #{num},</if>
        </trim>
        where parent_id = #{parentId} and type_id = #{typeId}
    </update>

    <select id="getApplyInfo" resultType="com.bonus.common.biz.domain.lease.LeaseApplyInfo">
        select
            lai.code as code,lai.lease_person as leasePerson,lai.phone as phone,lai.create_by as createBy,
            lai.create_time as createTime,lai.supplier_time as supplierTime,lai.supplier_place as supplierPlace,
            bu.unit_name as unitName,bp.pro_name as projectName,lai.company_id as companyId,sd.dept_name as companyName

        from lease_apply_info lai
                 left join bm_unit bu on lai.unit_id = bu.unit_id
                 left join bm_project bp on lai.project_id = bp.pro_id
                 left join sys_dept sd on lai.company_id = sd.dept_id and sd.del_flag = 0
        where task_id = #{taskId} and bu.del_flag = 0 and bp.del_flag = 0
    </select>

    <select id="getApplyDetails" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        select
            lad.pre_num as preNum,lad.remark as remark,ma1.unit_name as unitName,ma1.type_name as typeName,
            ma2.type_name as maTypeName

        from lease_apply_details lad
                 left join lease_apply_info lai on lad.parent_id = lai.id
                 left join ma_type ma1 on lad.type_id = ma1.type_id
                 left join ma_type ma2 on ma1.parent_id = ma2.type_id
        where lai.task_id = #{taskId} and ma1.del_flag = 0 and ma2.del_flag = 0
    </select>

    <select id="selectPublishDetails" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            lad.id AS id,
            lad.parent_id AS parentId,
            mt.type_id AS typeId,
            mt.type_name AS typeName,
            mt2.type_id AS thirdTypeId,
            mt2.type_name AS maTypeName,
            CASE
                mt.manage_type
                WHEN 0 THEN
                    IFNULL( subquery0.num, 0 ) ELSE IFNULL( mt.storage_num, 0 )
                END AS storageNum,
            mt.manage_type AS manageType,
            IFNULL( lad.pre_num, 0 ) AS preNum,
            mt.unit_name AS unitName,
            IFNULL( a.num, 0 ) AS publishNum,
            IFNULL( b.num, 0 ) AS outNum,
            IFNULL( lad.pre_num, 0 ) - IFNULL( a.num, 0 ) AS pendingNum
        FROM
            lease_apply_details lad
                LEFT JOIN ma_type mt ON lad.type_id = mt.type_id
                AND mt.`level` = '4'
                AND mt.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                AND mt2.`level` = '3'
                AND mt2.del_flag = '0'
                LEFT JOIN ( SELECT parent_id, type_id, sum( num ) AS num FROM lease_publish_details GROUP BY parent_id, type_id ) a
                          ON a.parent_id = lad.parent_id AND a.type_id = lad.type_id
                LEFT JOIN ( SELECT parent_id, type_id, sum( out_num ) AS num FROM lease_out_details GROUP BY parent_id, type_id ) b
                          ON b.parent_id = lad.parent_id AND b.type_id = lad.type_id
                LEFT JOIN (
                SELECT
                    mt.type_id,
                    mt2.type_name AS typeName,
                    mt.type_name AS typeModelName,
                    count( mm.ma_id ) num
                FROM
                    ma_machine mm
                        LEFT JOIN ma_type mt ON mt.type_id = mm.type_id
                        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
                WHERE
                    mm.ma_code IS NOT NULL
                  AND mm.ma_status IN ( 1 )
                GROUP BY
                    mt.type_id
            ) AS subquery0 ON subquery0.type_id = mt.type_id
        <if test="userId != null">
            JOIN ma_type_keeper mtk ON mtk.type_id = lad.type_id AND mtk.user_id = #{userId}
        </if>
        WHERE
            lad.parent_id = #{id}
    </select>

    <select id="getPublishList" resultType="com.bonus.common.biz.domain.lease.LeasePublishInfo"
            resultMap="LeasePublishInfoResult">
        select
        lai.id, lai.code, lai.task_id, lai.lease_person, lai.phone, lai.type, lai.company_audit_by,lai.apply_code,
        lai.company_audit_time, lai.company_audit_remark, lai.dept_audit_by, lai.dept_audit_time,
        lai.dept_audit_remark, lai.direct_audit_by, lai.direct_audit_time, lai.direct_audit_remark,
        lai.create_by, lai.create_time, lai.update_by, lai.update_time, lai.remark, lai.company_id,
        lai.direct_id, lai.lease_type, lai.estimate_lease_time, lai.cost_bearing_party, lai.lease_sign_url,
        lai.lease_sign_type,tt.task_id as taskId,
        lai.unit_id,lai.project_id,bu.unit_name, bp.pro_name, tt.task_status as taskStatus,

        case tt.task_status
        when 1 then '未完成'
        when 2 then '已终止'
        when 3 then '已完成'
        end as taskStatusName,
        IFNULL(sum(lad.pre_num),0) as preCountNum,
        IFNULL(sum(lad.al_num),0) as alNum,
        GROUP_CONCAT(DISTINCT mt1.type_name) AS maTypeNames,
        bp.contract_part as contractPart,
        sd.dept_name as impUnitName
        from
        lease_apply_info lai
        left join tm_task tt on lai.task_id = tt.task_id
        left join lease_apply_details lad on lai.id = lad.parent_id
        left join tm_task_agreement tta on lai.task_id = tta.task_id
        left join bm_unit bu on bu.unit_id = lai.unit_id
        left join bm_project bp on bp.pro_id = lai.project_id
        left join sys_dept sd on sd.dept_id = bp.imp_unit
        left join sys_dict_data sda on tt.task_status = sda.dict_value
        and sda.dict_type = 'lease_task_status'
        left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        <if test="userId != null">
            JOIN ma_type_keeper mtk ON mtk.type_id = lad.type_id AND mtk.user_id = #{userId}
        </if>
        where tt.task_type = '19'
        and tt.task_status in (1, 2, 3)
        <if test="taskStatus != null and taskStatus != ''">
            and tt.task_status = #{taskStatus}
        </if>
        <if test="taskId != null ">and lai.task_id = #{taskId}</if>
        <if test="statusList != null  and statusList.size() > 0">
            and tt.task_status in
            <foreach item="item" collection="statusList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND DATE_FORMAT( lai.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="isApp != null and taskStatus==1">and (tt.task_status = 0 or tt.task_status = 1 or tt.task_status = 2) </if>
        <if test="isApp != null and taskStatus==3">and (tt.task_status = 3 or tt.task_status = 4)</if>
        GROUP BY lai.id
        ORDER BY tt.create_time desc
    </select>

    <select id="selectLeaseApplyDetailsById" resultType="com.bonus.material.lease.domain.LeaseApplyDetails">
        SELECT
            parent_id AS parentId,
            type_id AS typeId,
            IFNULL( SUM(pre_num), 0 ) AS preNum,
            IFNULL( SUM(publish_num), 0 ) AS publishNum
        FROM
            lease_apply_details
        WHERE
            parent_id = #{parentId}
        GROUP BY
            parent_id
    </select>

    <select id="getMonthMaxOrderByDate" resultType="java.lang.Integer">
        select COALESCE(max(month_order), 0) from lease_publish_details
        where
        month(create_time) = #{month} and year(create_time) = #{year}
    </select>

    <select id="selectStatusByTaskId" resultType="java.lang.Integer">
        select workflow_status from sys_workflow_record
        where
            task_id = #{taskId}
    </select>

    <insert id="insertWorkOrderPeople">
        insert into uni_org.sys_information_people
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <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="isExample != null">is_examine,</if>

        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="createTime != null">now(),</if>
            <if test="updateTime != null">now(),</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="isExample != null">#{isExample},</if>
        </trim>
    </insert>

    <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>

    <select id="selectWorkPeopleInfoList" resultType="com.bonus.common.biz.domain.lease.WorkPeopleInfo">
        SELECT
            wh.id,
            wh.next_node_id,
            wc.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="updateWorkPeopleInfo">
        UPDATE   uni_org.sys_information_people
        SET
            read_state = 1,
            handle_state = 1,
            is_examine =1,
            update_user = #{receiverUsername},
            update_time = now()
        WHERE information_id = #{informationId}
          AND receiver_username = #{receiverUsername}
    </update>

    <select id="getWorkPeopleInfo" resultType="com.bonus.common.biz.domain.lease.WorkPeopleInfo">
        SELECT
            si.id as informationId
        FROM
            uni_org.sys_information si
                LEFT JOIN uni_org.sys_information_people sp on si.id = sp.information_id
        WHERE si.business_id =#{flowId} and sp.receiver_username = #{receiverUsername} and sp.is_examine = 0
            limit 1

    </select>
</mapper>
