<?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.ma.mapper.WorkSiteDirectManageMapper">
    <insert id="saveDirectApplyInfo" parameterType="com.bonus.material.ma.domain.DirectApplyInfo" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into direct_apply_info
        (
        <if test="code != null and code != ''">
            code,
        </if>
        <if test="backAgreementId != null">
            back_agreement_id,
        </if>
        <if test="backMan != null and backMan != ''">
            back_man,
        </if>
        <if test="backPhone != null and backPhone != ''">
            back_phone,
        </if>
        <if test="backRemark != null and backRemark != ''">
            back_remark,
        </if>
        <if test="leaseAgreementId != null">
            lease_agreement_id,
        </if>
        <if test="leaseMan != null and leaseMan != ''">
            lease_man,
        </if>
        <if test="leasePhone != null and leasePhone != ''">
            lease_phone,
        </if>
        <if test="dirUrl != null and dirUrl != ''">
            dir_url,
        </if>
        <if test="status != null and status != ''">
            status,
        </if>
        <if test="auditor != null and auditor != ''">
            auditor,
        </if>
        <if test="auditTime != null">
            audit_time,
        </if>
        <if test="auditRemark != null and auditRemark != ''">
            audit_remark,
        </if>
         <if test="createBy != null">
            create_by,
        </if>
        create_time
        ) values (
        <if test="code != null and code != ''">
            #{code},
        </if>
        <if test="backAgreementId != null">
            #{backAgreementId},
        </if>
        <if test="backMan != null and backMan != ''">
            #{backMan},
        </if>
        <if test="backPhone != null and backPhone != ''">
            #{backPhone},
        </if>
        <if test="backRemark != null and backRemark != ''">
            #{backRemark},
        </if>
        <if test="leaseAgreementId != null">
            #{leaseAgreementId},
        </if>
        <if test="leaseMan != null and leaseMan != ''">
            #{leaseMan},
        </if>
        <if test="leasePhone != null and leasePhone != ''">
            #{leasePhone},
        </if>
        <if test="dirUrl != null and dirUrl != ''">
            #{dirUrl},
        </if>
        <if test="status != null and status != ''">
            #{status},
        </if>
        <if test="auditor != null and auditor != ''">
            #{auditor},
        </if>
        <if test="auditTime != null">
            #{auditTime},
        </if>
        <if test="auditRemark != null and auditRemark != ''">
            #{auditRemark},
        </if>
        <if test="createBy != null">
            #{createBy},
        </if>
        NOW()
        )
    </insert>

    <insert id="saveDirectApplyDetails" parameterType="com.bonus.material.ma.domain.DirectApplyDetails">
        insert into direct_apply_details(direct_id,type_id,ma_id,direct_num,create_time)
        values (#{directId},#{typeId},#{maId}, #{directNum},NOW())
    </insert>

    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.bonus.common.biz.domain.lease.LeaseApplyInfo" useGeneratedKeys="true">
        <!--@mbg.generated-->
        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 and leasePerson != ''">
                lease_person,
            </if>
            <if test="phone != null and phone != ''">
                phone,
            </if>
            <if test="type != null and type != ''">
                `type`,
            </if>
            <if test="companyAuditBy != null">
                company_audit_by,
            </if>
            <if test="companyAuditTime != null and companyAuditTime != ''">
                company_audit_time,
            </if>
            <if test="companyAuditRemark != null and companyAuditRemark != ''">
                company_audit_remark,
            </if>
            <if test="deptAuditBy != null">
                dept_audit_by,
            </if>
            <if test="deptAuditTime != null and deptAuditTime != ''">
                dept_audit_time,
            </if>
            <if test="deptAuditRemark != null and deptAuditRemark != ''">
                dept_audit_remark,
            </if>
            <if test="createBy != null and createBy != ''">
                create_by,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="updateBy != null and updateBy != ''">
                update_by,
            </if>
            <if test="updateTime != null">
                update_time,
            </if>
            <if test="remark != null and remark != ''">
                remark,
            </if>
            <if test="companyId != null">
                company_id,
            </if>
            <if test="status != null and status != ''">
                status,
            </if>
            <if test="leaseType != null and leaseType != ''">
                lease_type,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">
                #{code,jdbcType=VARCHAR},
            </if>
            <if test="taskId != null">
                #{taskId,jdbcType=INTEGER},
            </if>
            <if test="leasePerson != null and leasePerson != ''">
                #{leasePerson,jdbcType=VARCHAR},
            </if>
            <if test="phone != null and phone != ''">
                #{phone,jdbcType=VARCHAR},
            </if>
            <if test="type != null and type != ''">
                #{type,jdbcType=VARCHAR},
            </if>
            <if test="companyAuditBy != null">
                #{companyAuditBy,jdbcType=INTEGER},
            </if>
            <if test="companyAuditTime != null and companyAuditTime != ''">
                #{companyAuditTime,jdbcType=VARCHAR},
            </if>
            <if test="companyAuditRemark != null and companyAuditRemark != ''">
                #{companyAuditRemark,jdbcType=VARCHAR},
            </if>
            <if test="deptAuditBy != null">
                #{deptAuditBy,jdbcType=INTEGER},
            </if>
            <if test="deptAuditTime != null and deptAuditTime != ''">
                #{deptAuditTime,jdbcType=VARCHAR},
            </if>
            <if test="deptAuditRemark != null and deptAuditRemark != ''">
                #{deptAuditRemark,jdbcType=VARCHAR},
            </if>
            <if test="createBy != null and createBy != ''">
                #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="updateBy != null and updateBy != ''">
                #{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime != null">
                #{updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="remark != null and remark != ''">
                #{remark,jdbcType=VARCHAR},
            </if>
            <if test="companyId != null">
                #{companyId,jdbcType=INTEGER},
            </if>
            <if test="status != null and status != ''">
                #{status,jdbcType=VARCHAR},
            </if>
            <if test="leaseType != null and leaseType != ''">
                #{leaseType,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>

    <insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
        <!--@mbg.generated-->
        insert into lease_apply_details
        (parennt_id, type_id, pre_num, al_num, `status`, create_by, create_time, update_by,
        update_time, remark, company_id)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.parenntId,jdbcType=INTEGER}, #{item.typeId,jdbcType=INTEGER}, #{item.preNum,jdbcType=FLOAT},
            #{item.alNum,jdbcType=FLOAT}, #{item.status,jdbcType=VARCHAR}, #{item.createBy,jdbcType=VARCHAR},
            NOW(), #{item.updateBy,jdbcType=VARCHAR}, NOW(),
            #{item.remark,jdbcType=VARCHAR}, #{item.companyId,jdbcType=INTEGER})
        </foreach>
    </insert>

    <insert id="insertLeaseOutDetails">
        insert into lease_out_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId!= null">
                parent_id,
            </if>
            <if test="typeId!= null">
                type_id,
            </if>
            <if test="maId!= null">
                ma_id,
            </if>
            <if test="outNum!= null">
                out_num,
            </if>
            <if test="outType!= null">
                out_type,
            </if>
            <if test="createBy!= null">
                create_by,
            </if>
            <if test="updateBy!= null">
                update_by,
            </if>
            <if test="remark!= null">
                remark,
            </if>
            <if test="companyId!= null">
                company_id,
            </if>
            <if test="carCode!= null">
                car_code,
            </if>
            create_time,
            update_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId!= null">
                #{parentId},
            </if>
            <if test="typeId!= null">
                #{typeId},
            </if>
            <if test="maId!= null">
                #{maId},
            </if>
            <if test="outNum!= null">
                #{outNum},
            </if>
            <if test="outType!= null">
                #{outType},
            </if>
            <if test="createBy!= null">
                #{createBy},
            </if>
            <if test="updateBy!= null">
                #{updateBy},
            </if>
            <if test="remark!= null">
                #{remark},
            </if>
            <if test="companyId!= null">
                #{companyId},
            </if>
            <if test="carCode!= null">
                #{carCode},
            </if>
            NOW(),
            NOW()
        </trim>
    </insert>

    <insert id="insertBackApplyInfo" keyColumn="task_id" keyProperty="taskId" parameterType="com.bonus.material.back.domain.BackApplyInfo" useGeneratedKeys="true">
            insert into tm_task (
            <if test="taskType != null">task_type, </if>
            <if test="taskStatus != null">task_status, </if>
            <if test="code != null and code != ''">code, </if>
            <if test="createBy != null and createBy != ''">create_by, </if>
            <if test="updateBy != null and updateBy != ''">update_by, </if>
            <if test="updateTime != null">update_time, </if>
            <if test="remark != null and remark != ''">remark, </if>
            <if test="companyId != null">company_id, </if>
            create_time
            ) values (
            <if test="taskType != null">#{taskType}, </if>
            <if test="taskStatus != null">#{taskStatus}, </if>
            <if test="code != null and code != ''">#{code}, </if>
            <if test="createBy != null and createBy != ''">#{createBy}, </if>
            <if test="updateBy != null and updateBy != ''">#{updateBy}, </if>
            <if test="updateTime != null">#{updateTime}, </if>
            <if test="remark != null and remark != ''">#{remark}, </if>
            <if test="companyId != null">#{companyId}, </if>
            NOW()
            )
    </insert>

    <update id="refuseDirectApplyInfo">
        update
            direct_apply_info
        set
            status = #{status},auditor=#{auditor},audit_time= now(),update_time = now()
        where
            id = #{id}
    </update>

    <update id="updateLeaseApplyDetailsOutNum">
        UPDATE
        lease_apply_details
        SET
        al_num = IF(al_num IS NULL, #{record.outNum}, al_num + #{record.outNum}),
        <if test="record.updateBy != null and record.updateBy!= '' ">
            update_by = #{record.updateBy},
        </if>
        update_time = now(),
        status = '2'
        WHERE
        parennt_id = #{record.parentId} and type_id = #{record.typeId}
    </update>

    <update id="updateMaTypeStockNum">
        UPDATE
            ma_type
        SET
            num = num - #{record.outNum} ,update_time = NOW()
        WHERE
            type_id = #{record.typeId}
    </update>

    <update id="updateMaMachineStatus">
        UPDATE
        ma_machine
        SET
        ma_status = '16' , create_time = NOW()
        <where>
            type_id = #{record.typeId}
            <if test="record.maId != null and record.maId != ''">
                and ma_id = #{record.maId}
            </if>
        </where>
    </update>

    <delete id="batchDel">
        delete
        from lease_user_book
        where id in
        <foreach close=")" collection="ids" item="id" open="(" separator=", ">
            #{id}
        </foreach>
    </delete>

    <select id="getUseringData" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
            sai.*,
            mt.type_name AS typeModelName,
            mt2.type_name AS typeName,
            mm.ma_code AS maCode,
            sum(sai.num) AS useNum
        FROM
            slt_agreement_info sai
        LEFT JOIN ma_type mt on sai.type_id = mt.type_id
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
        LEFT JOIN ma_machine mm on sai.ma_id = mm.ma_id
        where sai.agreement_id = #{agreementId} and sai.status = '0'
        <if test="keyWord != null  and keyWord != ''">
            and (mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%'))
        </if>
        GROUP BY
            sai.ma_id,sai.type_id
    </select>

    <select id="getList" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        SELECT DISTINCT
            dai.*,
            dai.create_time as createTime,
            dai.back_man as backMan,
            dai.back_phone as backPhone,
            dai.lease_man as leaseMan,
            dai.lease_phone as leasePhone,
            bpl.pro_id AS backProId,
            bpl.pro_name AS backProName,
            bui.unit_id AS backUnitId,
            bui.unit_name AS backUnitName,
            bpl1.pro_id AS leaseProId,
            bpl1.pro_name AS leaseProName,
            bui1.unit_id AS leaseUnitId,
            bui1.unit_name AS leaseUnitName
        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
        <where>
            <if test="keyWord != null and keyWord != ''">
                and (bpl.pro_name like concat('%', #{keyWord}, '%') or
                bui.unit_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 dai.status = #{status}
            </if>
        </where>
        order by dai.create_time desc
    </select>

    <select id="getDirectApplyInfoById" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        select * from direct_apply_info where id = #{id}
    </select>

    <select id="getMachineStatus" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        select ma_status as status
        from ma_machine
        where ma_id = #{maId}
    </select>

    <select id="getListLease" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        SELECT DISTINCT
            bpl.pro_id as backProId,bpl.pro_name as backProName,
            bui.unit_id as backUnitId,bui.unit_name as backUnitName
        FROM
            direct_apply_info dai
        LEFT JOIN bm_agreement_info bai ON dai.agreement_id = bai.agreement_id
        LEFT JOIN bm_project bpl ON bpl.lot_id = bai.project_id
        LEFT JOIN bm_unit bui ON bui.unit_id = bai.unit_id
    </select>

    <select id="getInfoById" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        SELECT
            dai.*,
            bai.agreement_code as backAgreementCode,
            bai1.agreement_code as leaseAgreementCode,
            bpl.pro_id AS backProId,
            bpl.pro_name AS backProName,
            bui.unit_id AS backUnitId,
            bui.unit_name AS backUnitName,
            bpl1.pro_id AS leaseProId,
            bpl1.pro_name AS leaseProName,
            bui1.unit_id AS leaseUnitId,
            bui1.unit_name AS leaseUnitName
        FROM
            direct_apply_info dai
            LEFT JOIN direct_apply_details dad ON dai.id = dad.direct_id
            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
        WHERE
            dai.id = #{id}  GROUP BY dai.id
    </select>

    <select id="getDetailById" resultType="com.bonus.material.ma.domain.DirectApplyDetails">
        SELECT
            dad.direct_num AS directNum,
            dad.type_id AS typeId,
            mt3.type_name AS typeName,
            mt2.type_name AS kindName,
            mt.type_name AS modelName,
            mt.unit_name AS unitName,
            sum( sai.num ) AS useNum,
            mm.ma_code AS maCode,
            mm.ma_id AS maId
        FROM
            direct_apply_info dai
            LEFT JOIN direct_apply_details dad ON dai.id = dad.direct_id
            LEFT JOIN slt_agreement_info sai ON dai.back_agreement_id = sai.agreement_id AND sai.type_id = dad.type_id
            LEFT JOIN ma_type mt ON mt.type_id = dad.type_id
            LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id
            LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id
            LEFT JOIN ma_machine mm ON dad.ma_id = mm.ma_id
        WHERE
            dai.id = #{id} AND sai.STATUS = '0'
        <if test="keyWord != null  and keyWord != ''">
            and (mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%'))
        </if>
        GROUP BY
            dad.id,sai.type_id
    </select>

    <select id="getListAll" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        SELECT
            dai.*,
            bpl.pro_id AS backProId,
            bpl.pro_name AS backProName,
            bui.unit_id AS backUnitId,
            bui.unit_name AS backUnitName,
            bpl1.pro_id AS leaseProId,
            bpl1.pro_name AS leaseProName,
            bui1.unit_id AS leaseUnitId,
            bui1.unit_name AS leaseUnitName
        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
    </select>

</mapper>