<?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.DirectRotationMapper">
    <update id="updateDirectApplyInfo">
        update direct_apply_info
        set
        <if test="backAgreementId != null">
            back_agreement_id = #{backAgreementId},
        </if>
        <if test="backMan != null">
            back_man = #{backMan},
        </if>
        <if test="backPhone != null">
            back_phone = #{backPhone},
        </if>
        <if test="leaseAgreementId != null">
            lease_agreement_id = #{leaseAgreementId},
        </if>
        <if test="leaseMan != null">
            lease_man = #{leaseMan},
        </if>
        <if test="leasePhone != null">
            lease_phone = #{leasePhone},
        </if>
        <if test="dirUrl != null">
            dir_url = #{dirUrl},
        </if>
        <if test="status != null">
            status = #{status},
        </if>
        <if test="auditor != null">
            auditor = #{auditor},
        </if>
        <if test="auditTime != null">
            audit_time = #{auditTime},
        </if>
        update_time= NOW()
        where id = #{id}
    </update>
    <delete id="deleteDirectApplyDetails">
        delete
        from direct_apply_details
        where direct_id = #{id}
    </delete>
    <delete id="deleteDirectApply">
        delete
        from direct_apply_info
        where id = #{id};
        delete
        from direct_apply_details
        where direct_id = #{id}
    </delete>

    <select id="getUseringData" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
        mt2.type_name AS typeName,
        mt.type_name AS typeModelName,
        mt.unit_name as unitName,
        sum( sai.num ) AS useNum ,
        mm.ma_code AS maCode,
        lai.lease_person as leasePerson,
        DATE(sai.start_time) as startTime,
        sai.ma_id as maId,
        sai.type_id as typeId,
        lai.create_time as createTime,
        sai.*
        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
        LEFT JOIN lease_apply_info lai on lai.id=sai.lease_id
        where sai.agreement_id = #{agreementId} and sai.status = '0'
        <if test="typeId != null">
            and sai.type_id = #{typeId}
        </if>
        <if test="maId != null">
            and mt.parent_id = #{maId}
        </if>
        <if test="startDate != null and endDate != ''">
            AND DATE_FORMAT(lai.create_time, '%Y-%m-%d' ) BETWEEN #{startDate} AND #{endDate}
        </if>
        <if test="keyWord != null  and keyWord != ''">
            and (mt2.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%') or
            mt.ma_code like concat('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY
        sai.ma_id,sai.type_id
    </select>

    <select id="getList" resultType="com.bonus.material.ma.domain.DirectApplyExportInfo">
        SELECT * FROM (
            SELECT DISTINCT
            dai.id AS 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,
            GROUP_CONCAT(DISTINCT mt2.type_name) as typeName,
            IFNULL(swr.workflow_status,2) as status
            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 ma_type mt1 on mt1.type_id=dad.type_id
            LEFT JOIN ma_type mt2 on mt2.type_id=mt1.parent_id
            left join sys_user su on dai.create_by = su.user_id
            left join sys_workflow_record swr on swr.task_id = dai.id
            <where>
                <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="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 dai.id
            order by dai.create_time desc
       ) a
        <where>
            <if test="keyWord!=null and keyWord!=''">
                AND (
                    INSTR(a.typeName,#{keyWord}) > 0 OR
                    INSTR(a.backUnitName,#{keyWord}) > 0 OR
                    INSTR(a.backProName,#{keyWord}) > 0 OR
                    INSTR(a.leaseUnitName,#{keyWord}) > 0 OR
                    INSTR(a.leaseProName,#{keyWord}) > 0
                )
            </if>

        </where>
    </select>

    <select id="getInfoById" resultType="com.bonus.material.ma.domain.DirectApplyInfo">
        SELECT
            dai.id,
            dai.`code`,
            bui.unit_id AS backUnitId,
            bui.unit_name AS backUnitName,
            bpl.pro_id AS backProId,
            bpl.pro_name AS backProName,
            dai.back_agreement_id AS backAgreementId,
            bai.agreement_code AS backAgreementCode,
            dai.back_man AS backMan,
            dai.back_phone AS backPhone,
            bui1.unit_id AS leaseUnitId,
            bui1.unit_name AS leaseUnitName,
            bpl1.pro_id AS leaseProId,
            bpl1.pro_name AS leaseProName,
            dai.lease_agreement_id AS leaseAgreementId,
            bai1.agreement_code AS leaseAgreementCode,
            dai.lease_man AS leaseMan,
            dai.lease_phone AS leasePhone,
            dai.dir_url AS dirUrl,
            dai.`status`
        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,
            mt2.type_name AS typeName,
            mt.type_name AS typeModelName,
            mt.unit_name AS unitName,
            sum( sai.num ) AS useNum,
            mm.ma_code AS maCode,
            mm.ma_id AS maId,
            lai.lease_person as leasePerson,
            DATE(sai.start_time) as startTime,
            mt.manage_type as manageType,
            dai.back_agreement_id as backAgreementId,
            dai.lease_agreement_id as leaseAgreementId
        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 lease_apply_info lai on lai.id=sai.lease_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_machine mm ON dad.ma_id = mm.ma_id
        WHERE
            dai.id = #{id} AND sai.STATUS = '0'
        <if test="typeId != null">
            and sai.type_id = #{typeId}
        </if>
        <if test="maId != null">
            and mt.parent_id = #{maId}
        </if>
        <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="getTypeNameList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT mt2.type_name    AS typeName,
               mt.type_name     AS typeModelName,
               mt.unit_name     as unitName,
               sum(sai.num)     AS useNum,
               mm.ma_code       AS maCode,
               lai.lease_person as leasePerson, DATE (sai.start_time) as startTime, sai.ma_id as maId, sai.type_id as typeId, mt.parent_id as parentId, sai.*
        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
            LEFT JOIN lease_apply_info lai on lai.id=sai.lease_id
        where sai.agreement_id = #{agreementId} and sai.status = '0'
        <if test="parentId != null">
            and mt.parent_id=#{parentId}
        </if>
        <if test="typeId != null">
            and sai.type_id=#{typeId}
        </if>
        <if test="type != null and type==1">
            GROUP BY
            mt.parent_id
        </if>
        <if test="type != null and type==2">
            GROUP BY
            sai.ma_id,sai.type_id
        </if>

    </select>
    <select id="selectDetails" resultType="com.bonus.material.ma.domain.DirectApplyDetails">
        SELECT
            dad.type_id AS typeId,
            mt2.type_name AS typeName,
            mt.type_name AS typeModelName,
            mt.unit_name AS unitName,
            mt.manage_type AS manageType,
            SUM( dad.direct_num ) AS directNum
        FROM
            direct_apply_info dai
                LEFT JOIN direct_apply_details dad ON dai.id = dad.direct_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
        WHERE
            dai.id = #{id}
        GROUP BY
            dad.type_id
    </select>
</mapper>
