<?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.back.mapper.BackApplyInfoMapper">
    <resultMap type="com.bonus.material.back.domain.BackApplyInfo" id="BackApplyInfoResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="taskId"    column="task_id"    />
        <result property="backPerson"    column="back_person"    />
        <result property="phone"    column="phone"    />
        <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="backTime"    column="back_time"    />
        <result property="status"    column="status"    />
        <result property="directId"    column="direct_id"    />
    </resultMap>

    <sql id="selectBackApplyInfoVo">
        select id, code, task_id, back_person, phone, direct_audit_by, direct_audit_time, direct_audit_remark, create_by, create_time, update_by, update_time, remark, company_id, back_time, status, direct_id from back_apply_info
    </sql>

    <select id="selectBackApplyInfoList" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
        bai.id as id,
        bai.task_id as taskId,
        bai.`code` as code,
        bai.back_person as backPerson,
        bai.phone as phone,
        bp.pro_id as proId,
        bai.remark as remark,
        bp.pro_name as proName,
        bu.unit_id as unitId,
        bu.unit_name as unitName,
        bai.back_time as backTime,
        tt.task_status as taskStatus,
        CASE
        WHEN bai.create_by REGEXP '^[0-9]+$' THEN su1.nick_name  -- 如果是纯数字，使用昵称
        ELSE bai.create_by  -- 否则直接使用create_by的值
        END AS createBy,
        bai.create_time as createTime,
        bai.back_sign_url as backSignUrl,
        bai.back_sign_type as backSignType,
        GROUP_CONCAT(DISTINCT mt2.type_id) as typeId,
        GROUP_CONCAT(DISTINCT mt2.type_name) AS typeName,
        bai.`status` AS status,
        bai.print_status as printStatus,
        GROUP_CONCAT(DISTINCT mt4.type_id) as firstId,
        bp.external_id as externalId,
        su.nick_name as materialMan,
        bai.confirm_time as confirmTime,
        bai.is_confirm as isConfirm,
        bai.confirm_remark as confirmRemark,
        SUM(bad.pre_num) as backNum
        FROM
        back_apply_info bai
        LEFT JOIN back_apply_details bad on bad.parent_id = bai.id
        LEFT JOIN tm_task tt on tt.task_id = bai.task_id
        LEFT JOIN tm_task_agreement tta on tta.task_id = tt.task_id
        LEFT JOIN bm_agreement_info bagi on bagi.agreement_id = tta.agreement_id
        AND bagi.`status` = '1'
        LEFT JOIN bm_project bp on bp.pro_id = bagi.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu on bu.unit_id = bagi.unit_id
        AND bu.del_flag = '0'
        LEFT JOIN ma_type mt1 ON mt1.type_id = bad.type_id AND mt1.del_flag = '0'
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt1.parent_id AND mt2.del_flag = '0'
        left join ma_type mt3 ON mt2.parent_id = mt3.type_id and mt3.del_flag = '0'
        left join ma_type mt4 ON mt3.parent_id = mt4.type_id and mt4.del_flag = '0'
        left join sys_user su on su.user_id = bai.material_man
        left join sys_user su1 on su1.user_id = bai.create_by
        <if test="userId != null">
            JOIN ma_type_repair mtr ON mtr.type_id = bad.type_id AND mtr.user_id = #{userId}
        </if>
        WHERE
        bai.back_style = '1'
        <if test="unitId != null">
            and bu.unit_id = #{unitId}
        </if>
        <if test="proId != null">
            and bp.pro_id = #{proId}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and bai.create_time BETWEEN CONCAT(#{startTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59')
        </if>
        <if test="appTaskStatus != null and appTaskStatus == 0">
            and tt.task_status = #{appTaskStatus}
        </if>
        <if test="appTaskStatus != null and appTaskStatus == 1">
            and (tt.task_status = 1 or tt.task_status = 2)
        </if>
        <if test="status != null">
            and tt.task_status = #{status}
        </if>
        <if test="isSign!=null">
            <if test="isSign==1">
                and bai.back_sign_url is null
            </if>
            <if test="isSign==2">
                and bai.back_sign_url is not null
            </if>
        </if>
        GROUP BY bai.`code`
        ORDER BY bai.create_time desc
    </select>

    <select id="selectTaskNumByMonth" resultType="java.lang.String">
        SELECT SUBSTRING(`code`, - 2) as code
        FROM tm_task
        WHERE DATE_FORMAT(create_time, '%y%m') = DATE_FORMAT(#{date}, '%y%m')
          AND task_type = #{taskType}
        ORDER BY create_time DESC LIMIT 1
    </select>

    <select id="getMachineById" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        mm.ma_id AS maId,
        mm.ma_code AS maCode,
        mm.ma_status AS maStatus,
        mt1.type_name AS typeName,
        mm.type_id AS typeId,
        mt.type_name AS materialName,
        mt2.type_name AS materialType
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
        LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
        AND mt2.del_flag = '0'
        LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
        WHERE
        sai.`status` = '0'
        AND mm.ma_status = '2'
        AND ba.unit_id = #{unitId}
        AND ba.project_id = #{proId}
        <if test="typeId != null">
            and mm.type_id = #{typeId}
        </if>
        <if test="keyWord != null  and keyWord != ''">
            and (
            mm.ma_code like concat('%', #{keyWord}, '%')
            )
        </if>
        GROUP BY mm.ma_id
    </select>

    <select id="selectBackApplyInfoById" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
            bai.id AS id,
            bai.CODE AS CODE,
            bai.task_id AS taskId,
            bai.back_person AS backPerson,
            bai.phone AS phone,
            CASE
                WHEN bai.create_by REGEXP '^[0-9]+$' THEN su.nick_name  -- 如果是纯数字，使用昵称
                ELSE bai.create_by  -- 否则直接使用create_by的值
            END AS createBy,
            bai.create_time AS createTime,
            bai.update_by AS updateBy,
            bai.update_time AS updateTime,
            bai.remark AS remark,
            bai.STATUS AS STATUS,
            bai.print_status AS printStatus,
            ba.agreement_id AS agreementId,
            bu.unit_id AS unitId,
            bu.unit_name AS unitName,
            bp.pro_id AS proId,
            bp.pro_name AS proName,
            bai.direct_audit_by AS directAuditBy,
            bai.back_sign_url AS backSignUrl,
            bai.back_sign_type AS backSignType,
            su.sign_type AS signType,
            su.sign_url AS signUrl
        FROM
            back_apply_info bai
                LEFT JOIN tm_task_agreement tta ON bai.task_id = tta.task_id
                LEFT JOIN bm_agreement_info ba ON ba.agreement_id = tta.agreement_id
                AND ba.`status` = 1
                LEFT JOIN bm_unit bu ON ba.unit_id = bu.unit_id
                AND bu.del_flag = '0'
                LEFT JOIN bm_project bp ON ba.project_id = bp.pro_id
                AND bp.del_flag = '0'
                LEFT JOIN sys_user su ON su.user_id = bai.create_by
        WHERE
            bai.id = #{id}
    </select>

    <select id="selectBackApplyDetailsListByTaskId" resultType="com.bonus.material.back.domain.BackApplyDetails">
        SELECT
        bai.CODE AS CODE,
        bcd.parent_id AS parentId,
        bcd.type_id AS typeId,
        mt2.type_name AS materialType,
        mt.type_name AS typeModel,
        mt1.type_name AS typeName,
        mt.unit_name AS unitName,
        mt.unit_value AS unitValue,
        mt.manage_type AS manageType,
        sum(bcd.bad_num)as badNum,
        sum(bcd.good_num)as goodNum,
        sum(bcd.back_num)as preNum,
        bcd.create_by AS createBy,
        bcd.create_time AS createTime,
        bcd.remark AS remark,
        mt.manage_type AS manageType,
        mt3.type_id AS firstId
        FROM
        back_check_details bcd
        LEFT JOIN back_apply_info bai  ON bcd.parent_id = bai.id
        LEFT JOIN ma_type mt ON mt.type_id = bcd.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 ma_type mt2 ON mt1.parent_id = mt2.type_id and mt2.del_flag = 0
        LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id and mt3.del_flag = 0
        <if test="userId != null">
            JOIN ma_type_repair mtr ON mtr.type_id = bcd.type_id AND mtr.user_id = #{userId}
        </if>
        WHERE bcd.parent_id = #{id}
        <if test="keyWord != null  and keyWord != ''">
            and (
            mt1.type_name like concat('%', #{keyWord}, '%') or
            mt.type_name like concat('%', #{keyWord}, '%') or
            mt2.type_name like concat('%', #{keyWord}, '%')
            )
        </if>
        group by bcd.type_id
    </select>

    <select id="selectByCode" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
            bcd.ma_id AS maId,
            mm.ma_code as maCode,
            bcd.type_id AS typeId,
            mt2.type_name AS materialType,
            mt.type_name AS materialName,
            mt1.type_name AS typeName,
            bcd.ap_detection AS apDetection,
            mm.ma_status AS maStatus,
            bcd.bad_num AS badNum,
            bcd.good_num AS goodNum
        FROM
            back_check_details bcd
        left join ma_machine mm on bcd.ma_id = mm.ma_id
        left join ma_type mt ON mt.type_id = bcd.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 ma_type mt2 ON mt1.parent_id = mt2.type_id and mt2.del_flag = 0
        where bcd.parent_id = #{id}
    </select>

    <select id="selectBackDetails" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
            bai.id AS id,
            tta.agreement_id AS agreementId,
            bai.task_id AS taskId,
            bcd.type_id AS typeId,
            SUM(bcd.back_num) AS backNum,
            bcd.parent_id AS parentId,
            bcd.create_by AS createBy,
            bcd.ma_id AS maId,
            mm.ma_status AS maStatus,
            mm.ma_code AS maCode
        FROM
            back_check_details bcd
                LEFT JOIN back_apply_info bai ON bai.id = bcd.parent_id
                LEFT JOIN tm_task_agreement tta ON tta.task_id = bai.task_id
                LEFT JOIN ma_machine mm ON bcd.ma_id = mm.ma_id
        WHERE
            bcd.parent_id = #{id} and (bcd.is_finished is null or bcd.is_finished != 1)
        <if test="typeId != '' and typeId != null">
            and bcd.type_id = #{typeId}
        </if>
        GROUP By bcd.type_id,bcd.ma_id
    </select>

    <select id="getWxList" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
            bai.id as id,
            tta.agreement_id as agreementId,
            bai.task_id as taskId,
            bcd.type_id as typeId,
            bcd.back_num as backNum,
            bcd.parent_id as parentId,
            bcd.create_by as createBy,
            bcd.ma_id as maId,
            bai.company_id as companyId
        FROM
            back_check_details bcd
                LEFT JOIN back_apply_info bai on bai.id=bcd.parent_id
                LEFT JOIN tm_task_agreement tta on tta.task_id=bai.task_id
        WHERE
            bcd.parent_id = #{id}
          and bcd.type_id = #{typeId}
          and bcd.back_status = '1'
    </select>

    <select id="selectTaskNumByMonthWx" resultType="java.lang.Integer">
        select count(*) from tm_task where DATE_FORMAT(create_time,'%y%m') = DATE_FORMAT(#{date},'%y%m') and task_type = 4
    </select>

    <select id="getStlInfo" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
        id AS id,
        agreement_id AS agreementId,
        type_id AS typeId,
        ma_id AS maId,
        num AS backNum,
        start_time AS startTime,
        end_time AS endTime,
        status AS status,
        lease_id AS leaseId,
        lease_type AS leaseType,
        back_id AS backId,
        lease_price AS leasePrice,
        buy_price AS buyPrice,
        company_id AS companyId
        FROM
        slt_agreement_info
        WHERE
        agreement_id = #{agreementId}
        AND type_id = #{typeId}
        AND status = 0
        <if test="maId != null">
            AND ma_id = #{maId}
        </if>
        order by start_time asc
    </select>

    <select id="getMachine" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT mm.ma_id        AS maId,
               mm.ma_code      AS maCode,
               mm.ma_status    AS maStatus,
               mt1.type_name   AS typeName,
               mm.type_id      AS typeId,
               mt.type_name    AS materialName,
               mt2.type_name   AS materialType,
               ba.unit_id      AS unitId,
               bu.unit_name    AS unitName,
               ba.project_id   AS proId,
               bp.pro_name     AS proName,
               ba.agreement_id AS agreementId
        FROM slt_agreement_info sai
                 LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
                 LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
                 LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
            AND mt2.del_flag = '0'

                 LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
                 LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
            AND bp.del_flag = '0'
                 LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
            AND bu.del_flag = '0'
        WHERE sai.`status`= 0
          and sai.is_slt = '0'
          and mm.ma_status = '2'
          and mtr.user_id = #{userId}
          and mm.ma_code = #{maCode}
          AND ba.unit_id = #{unitId}
          AND ba.project_id = #{proId}
    </select>

    <select id="getNum" resultType="java.lang.Integer">
        SELECT
            SUM( CASE WHEN sai.agreement_id = #{agreementId} AND sai.STATUS = '0' THEN sai.num ELSE 0 END ) AS num
        FROM
            ma_type mt
                LEFT JOIN slt_agreement_info sai ON mt.type_id = sai.type_id
        WHERE
            EXISTS ( SELECT 1 FROM slt_agreement_info sai2 WHERE sai2.type_id = mt.type_id AND sai2.agreement_id = #{agreementId}
                                                             AND sai2.STATUS = '0' and IFNULL(sai.lease_type,0) = 0 and sai2.num > 0)
        AND mt.type_id = #{typeId}
    </select>

    <select id="selectBackApplyDetailsById" resultType="com.bonus.material.back.domain.BackApplyDetails">
        select id as id,
               code as code,
               parent_id as parentId,
               type_id as typeId,
               pre_num as preNum
        from back_apply_details
        where parent_id = #{parentId} and type_id = #{typeId}
    </select>

    <select id="selectCheckDetails" resultType="com.bonus.material.back.domain.BackApplyDetails">
        SELECT
	      id AS id,
	      parent_id AS parentId,
	      type_id AS typeId
        FROM
	    back_check_details
        where parent_id = #{parentId} and type_id = #{typeId}
         <if test="maId != null">
             and ma_id = #{maId}
         </if>
    </select>

    <select id="selectByTypeId" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
            bcd.ma_id AS maId,
            mm.ma_code as maCode,
            bcd.type_id AS typeId,
            mt2.type_name AS materialType,
            mt.type_name AS materialName,
            mt1.type_name AS typeName,
            bcd.ap_detection AS apDetection,
            mm.ma_status AS maStatus,
            bcd.create_time AS createTime,
            bcd.create_by AS createBy
        FROM
            back_check_details bcd
        left join ma_machine mm on bcd.ma_id = mm.ma_id
        left join ma_type mt ON mt.type_id = mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id and mt2.del_flag = 0
        where bcd.parent_id = #{parentId} and bcd.type_id = #{typeId}
    </select>

    <select id="getMachineByQrCode" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        mm.ma_id AS maId,
        mm.ma_code AS maCode,
        mm.ma_status AS maStatus,
        mt1.type_name AS typeName,
        mm.type_id AS typeId,
        mt.type_name AS typeModelName,
        mt2.type_name AS materialType,
        ba.unit_id AS unitId,
        bu.unit_name AS unitName,
        ba.project_id AS proId,
        bp.pro_name AS proName,
        ba.agreement_id AS agreementId
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
        LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
        LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
        AND mt2.del_flag = '0'

        LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
        LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
        AND bu.del_flag = '0'
        WHERE
        sai.`status`= 0 and mm.qr_code = #{qrCode} and sai.is_slt = '0'
        and mtr.user_id = #{userId}
          <if test="unitId != null">
              AND ba.unit_id = #{unitId}
          </if>
          <if test="proId != null">
              AND ba.project_id = #{proId}
          </if>
        order by sai.start_time desc
        limit 1
    </select>

    <select id="getDetailsById" resultType="com.bonus.material.back.domain.BackApplyDetails">
        SELECT
            mt1.type_name as typeName,
            mt.type_name as typeModel,
            bcd.type_id as typeId,
            bcd.back_num as preNum,
            bcd.ma_id as maId,
            mm.ma_code as maCode,
            bcd.parent_id as parentId,
            bcd.create_by as createBy,
            bcd.create_time as createTime,
            bai.task_id as taskId
        FROM
            back_check_details bcd
                LEFT JOIN ma_type mt ON bcd.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 ma_machine mm ON bcd.ma_id = mm.ma_id
                LEFT JOIN back_apply_info bai ON bcd.parent_id = bai.id
        WHERE
            bcd.parent_id = #{parentId} and bcd.type_id = #{typeId}
    </select>

    <insert id="insertBackApplyInfo" parameterType="com.bonus.material.back.domain.BackApplyInfo" useGeneratedKeys="true" keyProperty="id">
        insert into back_apply_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="taskId != null">task_id,</if>
            <if test="backPerson != null">back_person,</if>
            <if test="phone != null">phone,</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="backTime != null">back_time,</if>
            <if test="status != null">status,</if>
            <if test="directId != null">direct_id,</if>
            <if test="backStyle != null and backStyle != ''">back_style,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="backPerson != null">#{backPerson},</if>
            <if test="phone != null">#{phone},</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="backTime != null">#{backTime},</if>
            <if test="status != null">#{status},</if>
            <if test="directId != null">#{directId},</if>
            <if test="backStyle != null and backStyle != ''">#{backStyle},</if>
         </trim>
    </insert>

    <insert id="insertBackApplyDetails" useGeneratedKeys="true" keyProperty="id">
        insert into back_apply_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="preNum != null">pre_num,</if>
            <if test="auditNum != null">audit_num,</if>
            <if test="badNum != null">bad_num,</if>
            <if test="goodNum != null">good_num,</if>
            <if test="num != null">use_num,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="apDetection != null and apDetection != ''">ap_detection,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="preNum != null">#{preNum},</if>
            <if test="auditNum != null">#{auditNum},</if>
            <if test="badNum != null">#{badNum},</if>
            <if test="goodNum != null">#{goodNum},</if>
            <if test="num != null">#{num},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="apDetection != null and apDetection != ''">#{apDetection},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>

    <insert id="insertCheckDetails">
        insert into back_check_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="preNum != null">back_num,</if>
            <if test="maId != null">ma_id,</if>
            <if test="status != null">status,</if>
            <if test="backStatus != null">back_status,</if>
            <if test="isFinished != null">is_finished,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="apDetection != null and apDetection != ''">ap_detection,</if>
            <if test="badNum != null">bad_num,</if>
            <if test="goodNum != null">good_num,</if>
            <if test="companyId != null">company_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="preNum != null">#{preNum},</if>
            <if test="maId != null">#{maId},</if>
            <if test="status != null">#{status},</if>
            <if test="backStatus != null">#{backStatus},</if>
            <if test="isFinished != null">#{isFinished},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="apDetection != null and apDetection != ''">#{apDetection},</if>
            <if test="badNum != null">#{badNum},</if>
            <if test="goodNum != null">#{goodNum},</if>
            <if test="companyId != null">#{companyId},</if>
        </trim>
    </insert>

    <insert id="insertRad">
        insert into repair_apply_details
        (
        <if test="taskId != null">
            task_id,
        </if>
        <if test="maId != null">
            ma_id,
        </if>
        <if test="typeId != null">
            type_id,
        </if>
        <if test="backNum != null">
            repair_num,
        </if>
        <if test="status != null">
            status,
        </if>
        <if test="createBy != null and createBy != ''">
            create_by,
        </if>
        <if test="remark != null and remark != ''">
            remark,
        </if>
        <if test="companyId != null">
            company_id,
        </if>
        <if test="id != null">
            back_id,
        </if>
         is_ds,
        create_time
        )
        values (
        <if test="taskId != null">
            #{taskId},
        </if>
        <if test="maId != null">
            #{maId},
        </if>
        <if test="typeId != null">
            #{typeId},
        </if>
        <if test="backNum != null">
            #{backNum},
        </if>
        <if test="status != null">
            #{status},
        </if>
        <if test="createBy != null and createBy != ''">
            #{createBy},
        </if>
        <if test="remark != null and remark != ''">
            #{remark},
        </if>
        <if test="companyId != null">
            #{companyId},
        </if>
        <if test="id != null">
            #{id},
        </if>
                1,
        NOW()
        )
    </insert>

    <update id="updateBackApplyInfo" parameterType="com.bonus.material.back.domain.BackApplyInfo">
        update back_apply_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="backPerson != null">back_person = #{backPerson},</if>
            <if test="phone != null">phone = #{phone},</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="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="backTime != null">back_time = #{backTime},</if>
            <if test="status != null">`status` = #{status},</if>
            <if test="directId != null">direct_id = #{directId},</if>
            <if test="printStatus != null">print_status = #{printStatus},</if>
            <if test="materialMan != null and materialMan != ''">material_man = #{materialMan},</if>
            <if test="confirmTime != null and confirmTime != ''">confirm_time = #{confirmTime},</if>
            <if test="isConfirm != null">is_confirm = #{isConfirm},</if>
            <if test="confirmRemark != null and confirmRemark != ''">confirm_remark = #{confirmRemark},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="updateTaskStatus">
        update tm_task set task_status = #{taskStatus} where task_id = #{taskId}
    </update>

    <update id="updateBack">
        update back_apply_info set status = #{taskStatus} where id = #{id}
    </update>

    <update id="updateSignById">
        update back_apply_info
            <trim prefix="SET" suffixOverrides=",">
                <if test="backSignUrl != null">back_sign_url = #{backSignUrl},</if>
                <if test="backSignType != null">back_sign_type = #{backSignType},</if>
            </trim>
        where id = #{id}
    </update>

    <update id="updateBackDetails">
        update back_apply_details set status = #{taskStatus} where parent_id = #{id}
    </update>

    <update id="updateCheckDetails">
        update back_check_details set is_finished = '1', update_time = now() where parent_id = #{id} and type_id = #{typeId} and (is_finished is null or is_finished != 1)
    </update>

    <update id="updateStlInfo">
        update slt_agreement_info
        set end_time = now(),
            update_time = now(),
            back_id = #{record.id},
            status = '1'
        where id = #{info.id}
    </update>

    <update id="updateStlInfoTwo">
        update slt_agreement_info
        set num = #{backNum},
            end_time = now(),
            update_time = now(),
            back_id = #{record.id},
            status = '1'
        where id = #{info.id}
    </update>

    <update id="updateBackApplyDetails">
        update back_apply_details set use_num = use_num - COALESCE(#{preNum} ,0) where parent_id = #{parentId} and type_id = #{typeId}
    </update>

    <update id="updateDetails">
        update back_apply_details set pre_num = pre_num + COALESCE(#{preNum} ,0) ,
                                      audit_num = audit_num + COALESCE(#{preNum} ,0)
        where id = #{id}
    </update>

    <update id="updateCheck">
        update back_check_details set back_num = back_num + COALESCE(#{preNum} ,0) where parent_id = #{parentId} and type_id = #{typeId}
        <if test="maId != null">
            and ma_id = #{maId}
        </if>
    </update>

    <update id="updateMaCode">
        update back_check_details
        set
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="apDetection != null">
                ap_detection = #{apDetection}
            </if>
        where parent_id = #{parentId} and ma_id = #{maId}
    </update>

    <insert id="insStlInfoTwo">
        insert into slt_agreement_info (agreement_id,type_id,ma_id,num,start_time,status,lease_id,lease_price,buy_price,is_slt,company_id,lease_type,create_time)
        values (#{info.agreementId},#{info.typeId},#{info.maId},#{many},#{info.startTime},#{info.status},#{info.leaseId},#{info.leasePrice},#{info.buyPrice},'0',#{info.companyId},#{info.leaseType},now());
    </insert>

    <insert id="addHandlingOrder">
        insert into bm_handling_order (pro_id, car_code, link_man, phone,reserve_date,type, create_by,
                                       create_time)
        values (#{proId}, #{carCode}, #{linkMan}, #{phone},#{reserveDate},#{type},
                #{createBy}, now())
    </insert>

    <insert id="addExitPermit">
        insert into bm_exit_permit (material,name,car_code,add_date,create_by,create_time)
        values (#{material}, #{name}, #{carCode}, #{addDate}, #{createBy}, now())
    </insert>
    <insert id="syncProject" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO archives_record_info
        (
            parent_id,`level`,archives_value,archives_name,del_flag,create_time
        ) VALUES (
                     '22','2',#{proId},#{proName},'0',NOW()
                 )
    </insert>
    <insert id="syncProjectDetails">
        INSERT INTO archives_record_details
        (
            info_id,parent_id,`level`,doc_name,doc_type,del_flag,create_time
        ) VALUES (
                     #{id},'0','1',#{typeName},'文件夹','0',NOW()
                 )
    </insert>

    <delete id="deleteBackApply">
        delete from back_apply_info where id = #{id}
    </delete>

    <delete id="deleteBackApplyDetails">
        delete from back_apply_details where parent_id = #{id}
    </delete>

    <delete id="deleteCheckDetails">
        delete from back_check_details where parent_id = #{id}
    </delete>

    <delete id="deleteApplyDetails">
        delete from back_apply_details where id = #{id}
    </delete>

    <delete id="deleteDetails">
        delete from back_check_details where parent_id = #{parentId} and type_id = #{typeId}
        <if test="maId != null">
            and ma_id = #{maId}
        </if>
    </delete>

    <update id="updateBackApplyDetailsTwo">
        UPDATE back_apply_details bad
        JOIN back_apply_info bai ON bad.parent_id = bai.id
        SET bad.use_num = IFNULL(bad.use_num, 0) + IFNULL(bad.audit_num, 0)
        WHERE bai.task_id = #{taskId};
    </update>

    <update id="update">
        update back_apply_details
        set pre_num = pre_num - COALESCE(#{preNum} ,0),
            audit_num = audit_num - COALESCE(#{preNum} ,0),
            use_num = use_num + COALESCE(#{preNum} ,0),
            status = '1',
            update_time = now()
        where
              parent_id = #{parentId} and type_id = #{typeId}
    </update>

    <update id="uploadSort">
        update bm_handling_order
        set sort = #{sort},time_type = #{timeType},queue_date=#{reserveDate},update_time = now(),update_by = #{updateBy}
        where id = #{id}
    </update>

    <delete id="removeBackApplyDetails">
        delete from back_apply_details where code = #{code} and status = '2'
    </delete>
    <delete id="deleteBackApplyDetailsById">
        delete from back_apply_details where parent_id = #{parentId} and type_id = #{typeId}
    </delete>

    <update id="delHandlingOrder">
        update bm_handling_order set is_active = '0' where id = #{id}
    </update>

    <!--   设置审批人为默认的李勇 防止代码冲突-->
    <select id="getDirectAuditBy"  resultType="Long">
        select
        sc.people_id as peopleId
        from sign_config sc
        where sc.process_id = 1 and sc.sign_type = 0 and sc.del_flag = 0
    </select>

    <select id="selectSignByTaskId" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
            bai.`code`,
            bai.direct_audit_by as directAuditBy,
            bai.back_sign_url as backSignUrl
        FROM
            back_apply_info bai
                LEFT JOIN repair_apply_details rad on rad.back_id=bai.id
                LEFT JOIN tm_task tt on tt.task_id=rad.task_id
        WHERE
            tt.task_id=#{taskId}
        GROUP BY bai.`code`
    </select>

    <select id="selectAgreementId" resultType="java.lang.Long">
        SELECT
            tt.agreement_id AS agreementId
        FROM
            back_apply_info ba
                LEFT JOIN tm_task_agreement tt ON ba.task_id = tt.task_id
        WHERE
            ba.id = #{id}
    </select>
    <select id="selectDto" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        bcd.ma_id AS maId,
        mm.ma_code as maCode,
        bcd.type_id AS typeId,
        mt2.type_name AS materialType,
        mt.type_name AS materialName,
        mt1.type_name AS typeName,
        bcd.ap_detection AS apDetection,
        mm.ma_status AS maStatus,
        bcd.bad_num AS badNum,
        bcd.good_num AS goodNum
        FROM
        back_check_details bcd
        left join ma_machine mm on bcd.ma_id = mm.ma_id
        left join ma_type mt ON mt.type_id = mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id and mt2.del_flag = 0
        where bcd.parent_id = #{id}
        <if test="maId != null">
            and bcd.ma_id = #{maId}
        </if>
    </select>

    <select id="selectBackApplyList" resultType="com.bonus.material.back.domain.vo.BackApplyVo">
        SELECT
            bagi.agreement_code AS agreementCode,
            bu.unit_name AS backUnitName,
            bp.pro_name AS backProName,
            mt2.type_name AS typeName,
            mt1.type_name AS typeModelName,
            mm.ma_code AS maCode,
            mt1.unit_name AS unitName,
            tt.`code` AS backCode,
            bcd.back_num AS backNum,
            bcd.create_by AS backPerson,
            bcd.create_time AS backTime,
            bai.id AS id,
            bcd.type_id AS typeId,
            bcd.ma_id AS maId
        FROM
            back_apply_info bai
                LEFT JOIN back_check_details bcd ON bai.id = bcd.parent_id
                LEFT JOIN tm_task tt ON tt.task_id = bai.task_id
                LEFT JOIN tm_task_agreement tta ON tta.task_id = tt.task_id
                LEFT JOIN bm_agreement_info bagi ON bagi.agreement_id = tta.agreement_id
                AND bagi.`status` = '1'
                LEFT JOIN bm_project bp ON bp.pro_id = bagi.project_id
                AND bp.del_flag = '0'
                LEFT JOIN bm_unit bu ON bu.unit_id = bagi.unit_id
                AND bu.del_flag = '0'
                LEFT JOIN ma_type mt1 ON mt1.type_id = bcd.type_id
                AND mt1.del_flag = '0'
                LEFT JOIN ma_type mt2 ON mt2.type_id = mt1.parent_id
                AND mt2.del_flag = '0'
                LEFT JOIN ma_machine mm ON bcd.ma_id = mm.ma_id
        WHERE
        1 = 1
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            <![CDATA[and DATE_FORMAT( bcd.create_time, '%Y-%m-%d' ) BETWEEN #{startTime} AND #{endTime} ]]>
        </if>
        <if test="typeName != null and typeName != ''">
            and mt2.type_name like CONCAT('%', #{typeName}, '%')
        </if>
        <if test="typeModelName != null and typeModelName != ''">
            and mt1.type_name like CONCAT('%', #{typeModelName}, '%')
        </if>
        <if test="unitId != null">
            and bu.unit_id = #{unitId}
        </if>
        <if test="proId != null">
            and bp.pro_id = #{proId}
        </if>
        ORDER BY bcd.create_time DESC
    </select>
    <select id="selectRepairCode" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
            GROUP_CONCAT( DISTINCT tt.`code` ) AS CODE
        FROM
            repair_apply_details ba
                LEFT JOIN tm_task tt ON ba.task_id = tt.task_id
                LEFT JOIN repair_audit_details rad ON ba.id = rad.repair_id
        WHERE
            rad.`status` = '1'
            AND ba.back_id = #{id}
        <if test="typeId != null">
            and ba.type_id = #{typeId}
        </if>
        <if test="maId != null">
            and ba.ma_id = #{maId}
        </if>
    </select>
    <select id="selectIdList" resultType="com.bonus.material.back.domain.vo.BackApplyInfoVo">
        SELECT
        id as backId
        FROM
        back_apply_details
        WHERE
        parent_id = #{id}
        <if test="typeId != null">
            and type_id = #{typeId}
        </if>
    </select>
    <select id="selectDetails" resultType="com.bonus.material.back.domain.vo.BackApplyInfoVo">
        SELECT
            mt1.type_name AS typeName,
            mt.type_name AS typeModelName,
            bcd.back_num AS backNum,
            bcd.good_num AS passNum,
            bcd.bad_num AS unPassNum,
            bcd.remark AS unPassReason
        FROM
            back_check_details bcd
                LEFT JOIN ma_type mt ON bcd.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
            bcd.parent_id = #{id}
        <if test="maId != null">
            and bcd.ma_id = #{maId}
        </if>
        <if test="typeId != null">
            and bcd.type_id = #{typeId}
        </if>
    </select>

    <select id="getMachineByIdAndCode" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        bcd.type_id AS typeId,
        bcd.ma_id AS maId,
        mm.ma_code AS maCode
        FROM
        back_check_details bcd
        LEFT JOIN ma_machine mm ON bcd.ma_id = mm.ma_id
        where
        bcd.parent_id = #{id}
        <if test="maCode != null and maCode != ''">
            and mm.ma_code = #{maCode}
        </if>
        <if test="qrCode != null and qrCode != ''">
            and mm.qr_code = #{qrCode}
        </if>
    </select>

    <select id="selectByTaskId" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
            bai.id AS id,
            bai.CODE AS CODE,
            bai.task_id AS taskId,
            bai.back_person AS backPerson,
            bai.phone AS phone,
            bai.create_by AS createBy,
            bai.create_time AS createTime,
            bai.update_by AS updateBy,
            bai.update_time AS updateTime,
            bai.remark AS remark,
            bai.print_status AS printStatus,
            ba.agreement_id AS agreementId,
            bu.unit_id AS unitId,
            bu.unit_name AS unitName,
            bp.pro_id AS proId,
            bp.pro_name AS proName,
            bai.direct_audit_by AS directAuditBy,
            bai.back_sign_url AS backSignUrl,
            bai.back_sign_type AS backSignType
        FROM
            back_apply_info bai
                LEFT JOIN tm_task_agreement tta ON bai.task_id = tta.task_id
                LEFT JOIN bm_agreement_info ba ON ba.agreement_id = tta.agreement_id
                AND ba.`status` = 1
                LEFT JOIN bm_unit bu ON ba.unit_id = bu.unit_id
                AND bu.del_flag = '0'
                LEFT JOIN bm_project bp ON ba.project_id = bp.pro_id
                AND bp.del_flag = '0'
        WHERE
            bai.task_id = #{taskId}
    </select>

    <select id="selectTypeIdList" resultType="java.lang.Long">
        select
            DISTINCT
            type_id
        from
            ma_type_manage
        where
            user_id = #{userId}
    </select>
    <select id="getHandlingOrderList" resultType="com.bonus.material.back.domain.HandlingOrder">
        SELECT bho.id,
        bp.pro_name as proName,
        bho.car_code as carCode,
        bho.link_man as linkMan,
        bho.phone,
        bho.time_type as timeType,
        bho.sort,
        bho.type,
        bho.reserve_date as reserveDate,
        su.sign_url as signUrl
        FROM bm_handling_order bho
        LEFT JOIN bm_project bp on bp.pro_id = bho.pro_id
        LEFT JOIN sys_user su on su.user_id=bho.update_by
        WHERE bho.is_active = '1'
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and bho.reserve_date BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="keyWord !=null and keyWord !=''">
            and (
            bho.car_code like concat('%',#{keyWord},'%') or
            bho.link_man like concat('%',#{keyWord},'%') or
            bp.pro_name like concat('%',#{keyWord},'%') or
            bho.type like concat('%',#{keyWord},'%')
            )
        </if>
        ORDER BY CASE WHEN bho.sort IS NULL THEN 0 ELSE 1 END,bho.update_time desc,bho.sort desc,bho.reserve_date desc
    </select>
    <select id="selectSort" resultType="java.lang.Integer">
        SELECT count(*)
        FROM bm_handling_order
        WHERE sort = #{sort}
          and queue_date=#{reserveDate}
          and is_active = '1'
    </select>

    <select id="getMachineByCode" resultType="com.bonus.common.core.web.domain.AjaxResult">

        SELECT mm.ma_id        AS maId,
               mm.ma_code      AS maCode,
               mm.ma_status    AS maStatus,
               sd.dict_label    AS maStatusName,
               mt1.type_name   AS typeName,
               mm.type_id      AS typeId,
               mt.type_name    AS typeModelName,
               mt2.type_name   AS materialType,
               ba.unit_id      AS unitId,
               bu.unit_name    AS unitName,
               ba.project_id   AS proId,
               bp.pro_name     AS proName,
               ba.agreement_id AS agreementId
        FROM slt_agreement_info sai
                 LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
                 LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
            AND mt2.del_flag = '0'

                 LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
                 LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
            AND bp.del_flag = '0'
                 LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
                 LEFT JOIN sys_dict_data sd on sd.dict_value = mm.ma_status and sd.dict_type = "ma_machine_status"
             AND bu.del_flag = '0'
                 LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
        WHERE sai.`status`=0 and mm.ma_status = '2' and sai.is_slt = 0
          <if test="userId != null">
            and mtr.user_id = #{userId}
          </if>
          and mm.ma_code = #{maCode}
        GROUP BY mm.ma_id
    </select>
    <select id="getSort" resultType="com.bonus.material.back.domain.HandlingOrder">
        SELECT time_type as timeType,
               sort
        FROM bm_handling_order
        WHERE queue_date = #{reserveDate}
          and time_type = #{timeType}
        ORDER BY sort desc LIMIT 1
    </select>

    <select id="selectBackApplyDetailsList" resultType="com.bonus.material.back.domain.BackApplyDetails">
        SELECT
        id AS id,
        parent_id AS parentId,
        type_id AS typeId
        FROM
        back_check_details
        where parent_id = #{parentId} and type_id = #{typeId}
    </select>
    <select id="getMachineByOne" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        mm.ma_id AS maId,
        mm.ma_code AS maCode,
        mm.ma_status AS maStatus,
        mt1.type_name AS typeName,
        mm.type_id AS typeId,
        mt.type_name AS typeModelName,
        mt2.type_name AS materialType,
        ba.unit_id AS unitId,
        bu.unit_name AS unitName,
        ba.project_id AS proId,
        bp.pro_name AS proName,
        ba.agreement_id AS agreementId
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
        LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
        LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
        AND mt2.del_flag = '0'

        LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
        LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
        AND bu.del_flag = '0'
        WHERE
        mm.qr_code = #{qrCode}

        GROUP BY mm.ma_id
    </select>
    <select id="getMachineByTwo" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        mm.ma_id AS maId,
        mm.ma_code AS maCode,
        mm.ma_status AS maStatus,
        mt1.type_name AS typeName,
        mm.type_id AS typeId,
        mt.type_name AS typeModelName,
        mt2.type_name AS materialType,
        ba.unit_id AS unitId,
        bu.unit_name AS unitName,
        ba.project_id AS proId,
        bp.pro_name AS proName,
        ba.agreement_id AS agreementId
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
        LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
        LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
        AND mt2.del_flag = '0'

        LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
        LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
        AND bu.del_flag = '0'
        WHERE
        sai.`status`= 0 and mm.qr_code = #{qrCode}
        GROUP BY mm.ma_id
    </select>
    <select id="getMachineByThree" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT
        mm.ma_id AS maId,
        mm.ma_code AS maCode,
        mm.ma_status AS maStatus,
        mt1.type_name AS typeName,
        mm.type_id AS typeId,
        mt.type_name AS typeModelName,
        mt2.type_name AS materialType,
        ba.unit_id AS unitId,
        bu.unit_name AS unitName,
        ba.project_id AS proId,
        bp.pro_name AS proName,
        ba.agreement_id AS agreementId
        FROM
        slt_agreement_info sai
        LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
        LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
        LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
        AND mt2.del_flag = '0'

        LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
        LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
        AND bp.del_flag = '0'
        LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
        AND bu.del_flag = '0'
        WHERE
        sai.`status`=0 and mm.qr_code = #{qrCode}
        <if test="unitId != null">
            AND ba.unit_id = #{unitId}
        </if>
        <if test="proId != null">
            AND ba.project_id = #{proId}
        </if>
        GROUP BY mm.ma_id
    </select>
    <select id="getMachineByOneMaCode" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT mm.ma_id        AS maId,
               mm.ma_code      AS maCode,
               mm.ma_status    AS maStatus,
               mt1.type_name   AS typeName,
               mm.type_id      AS typeId,
               mt.type_name    AS materialName,
               mt2.type_name   AS materialType,
               ba.unit_id      AS unitId,
               bu.unit_name    AS unitName,
               ba.project_id   AS proId,
               bp.pro_name     AS proName,
               ba.agreement_id AS agreementId
        FROM slt_agreement_info sai
                 LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
                 LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
                 LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
            AND mt2.del_flag = '0'

                 LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
                 LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
            AND bp.del_flag = '0'
                 LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
            AND bu.del_flag = '0'
        WHERE
           mm.ma_code = #{maCode}
    </select>
    <select id="getMachineByTwoMaCode" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT mm.ma_id        AS maId,
               mm.ma_code      AS maCode,
               mm.ma_status    AS maStatus,
               mt1.type_name   AS typeName,
               mm.type_id      AS typeId,
               mt.type_name    AS materialName,
               mt2.type_name   AS materialType,
               ba.unit_id      AS unitId,
               bu.unit_name    AS unitName,
               ba.project_id   AS proId,
               bp.pro_name     AS proName,
               ba.agreement_id AS agreementId
        FROM slt_agreement_info sai
                 LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
                 LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
                 LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
            AND mt2.del_flag = '0'

                 LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
                 LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
            AND bp.del_flag = '0'
                 LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
            AND bu.del_flag = '0'
        WHERE sai.`status`=0 and mm.ma_status = '2'
          and mm.ma_code = #{maCode}
    </select>
    <select id="getMachineByThreeMaCode" resultType="com.bonus.material.back.domain.vo.MaCodeVo">
        SELECT mm.ma_id        AS maId,
               mm.ma_code      AS maCode,
               mm.ma_status    AS maStatus,
               mt1.type_name   AS typeName,
               mm.type_id      AS typeId,
               mt.type_name    AS materialName,
               mt2.type_name   AS materialType,
               ba.unit_id      AS unitId,
               bu.unit_name    AS unitName,
               ba.project_id   AS proId,
               bp.pro_name     AS proName,
               ba.agreement_id AS agreementId
        FROM slt_agreement_info sai
                 LEFT JOIN ma_machine mm ON sai.ma_id = mm.ma_id
                 LEFT JOIN ma_type_repair mtr ON mm.type_id = mtr.type_id
                 LEFT JOIN ma_type mt ON mm.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 ma_type mt2 ON mt1.parent_id = mt2.type_id
            AND mt2.del_flag = '0'

                 LEFT JOIN bm_agreement_info ba ON sai.agreement_id = ba.agreement_id
                 LEFT JOIN bm_project bp on bp.pro_id = ba.project_id
            AND bp.del_flag = '0'
                 LEFT JOIN bm_unit bu on bu.unit_id = ba.unit_id
            AND bu.del_flag = '0'
        WHERE sai.`status`=0 and mm.ma_status = '2'
          and mm.ma_code = #{maCode}
          AND ba.unit_id = #{unitId}
          AND ba.project_id = #{proId}
    </select>

    <select id="selectInfo" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
            bm.agreement_id as agreementId,
            bm.unit_id as unitId,
            bm.project_id as proId
        FROM
            bm_agreement_info bmi
                LEFT JOIN bm_agreement_info bm ON bmi.project_id = bm.project_id
                AND bmi.project_unit_id = bm.unit_id
        WHERE
            bmi.unit_id = #{unitId}
          AND bmi.project_id = #{proId}
    </select>

    <select id="getStlInfoByTaskId" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
            id AS id,
            agreement_id AS agreementId,
            type_id AS typeId,
            lease_id AS leaseId
        FROM
            slt_agreement_info
        WHERE
            lease_id = #{leaseId}
          AND type_id = #{typeId}
          AND STATUS = 0
          AND agreement_id != #{agreementId}
    </select>

    <select id="selectClzList" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
        bai.id AS id,
        tta.agreement_id AS agreementId,
        bai.task_id AS taskId,
        bcd.type_id AS typeId,
        SUM(bcd.back_num) AS backNum,
        bcd.parent_id AS parentId,
        bcd.create_by AS createBy,
        bcd.ma_id AS maId,
        mm.ma_status AS maStatus,
        mm.ma_code AS maCode
        FROM
        clz_back_check_details bcd
        LEFT JOIN clz_back_apply_info bai ON bai.id = bcd.parent_id
        LEFT JOIN tm_task_agreement tta ON tta.task_id = bai.task_id
        LEFT JOIN ma_machine mm ON bcd.ma_id = mm.ma_id
        WHERE
        bcd.parent_id = #{parentId}
        <if test="typeId != '' and typeId != null">
            and bcd.type_id = #{typeId}
        </if>
        GROUP By bcd.type_id,bcd.ma_id
    </select>

    <select id="selectXmList" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
        bai.id AS id,
        tta.agreement_id AS agreementId,
        bai.task_id AS taskId,
        bcd.type_id AS typeId,
        SUM(bcd.back_num) AS backNum,
        bcd.parent_id AS parentId,
        bcd.create_by AS createBy,
        bcd.ma_id AS maId,
        mm.ma_status AS maStatus,
        mm.ma_code AS maCode
        FROM
        back_check_details bcd
        LEFT JOIN back_apply_info bai ON bai.id = bcd.parent_id
        LEFT JOIN tm_task_agreement tta ON tta.task_id = bai.task_id
        LEFT JOIN ma_machine mm ON bcd.ma_id = mm.ma_id
        WHERE
        bcd.parent_id = #{parentId}
        <if test="typeId != '' and typeId != null">
            and bcd.type_id = #{typeId}
        </if>
        GROUP By bcd.type_id,bcd.ma_id
    </select>

    <select id="getTbList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
        id AS id,
        agreement_id AS agreementId,
        type_id AS typeId,
        ma_id AS maId,
        num AS backNum,
        start_time AS startTime,
        end_time AS endTime,
        status AS status,
        lease_id AS leaseId,
        lease_type AS leaseType,
        back_id AS backId,
        lease_price AS leasePrice,
        buy_price AS buyPrice,
        company_id AS companyId
        FROM
        slt_agreement_info
        WHERE
        source = '2'
        AND type_id = #{typeId}
        AND status = 0
        <if test="maId != null">
            AND ma_id = #{maId}
        </if>
        order by start_time asc
    </select>
    <select id="getExitPermitList" resultType="com.bonus.material.back.domain.ExitPermit">
        SELECT
            id,
            `name`,
            material,
            car_code as carCode,
            add_date as addDate
        FROM
            bm_exit_permit
        WHERE
            is_active='1'
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            and add_date BETWEEN #{startTime} AND #{endTime}
        </if>
        <if test="keyWord !=null and keyWord !=''">
            and (
            `name` like concat('%',#{keyWord},'%') or
            material like concat('%',#{keyWord},'%') or
            car_code like concat('%',#{keyWord},'%')
            )
        </if>
        order by add_date desc
    </select>

    <select id="getUserList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
        id AS id,
        agreement_id AS agreementId,
        type_id AS typeId,
        ma_id AS maId,
        num AS backNum,
        start_time AS startTime,
        end_time AS endTime,
        status AS status,
        lease_id AS leaseId,
        lease_type AS leaseType,
        back_id AS backId,
        lease_price AS leasePrice,
        buy_price AS buyPrice,
        company_id AS companyId
        FROM
        slt_agreement_info
        WHERE
        status = 0
        AND type_id = #{typeId}
        AND ma_id = #{maId}
        order by start_time asc
    </select>

    <select id="getClzUserList" resultType="com.bonus.material.settlement.domain.SltAgreementInfo">
        SELECT
        id AS id,
        agreement_id AS agreementId,
        type_id AS typeId,
        ma_id AS maId,
        num AS backNum,
        start_time AS startTime,
        end_time AS endTime,
        status AS status,
        lease_id AS leaseId,
        lease_type AS leaseType,
        back_id AS backId,
        lease_price AS leasePrice,
        buy_price AS buyPrice,
        company_id AS companyId
        FROM
        clz_slt_agreement_info
        WHERE
        status = 0
        AND type_id = #{typeId}
        AND ma_id = #{maId}
        order by start_time asc
    </select>

    <select id="getDirectAuditUrl" resultType="com.bonus.material.back.domain.BackApplyInfo">
        select
            su.sign_type as directAuditSignType,
            su.sign_url as directAuditSignUrl
        from sys_user su
        where su.user_id = 474 and su.del_flag = 0
    </select>
    <select id="selectNotSyncProject" resultType="com.bonus.material.back.domain.BackApplyInfo">
        SELECT
            pro_id as proId,
            pro_name as proName
        FROM
            bm_project
        WHERE
            del_flag='0'
          and pro_id not in (
            SELECT
                archives_value
            FROM
                archives_record_info
            WHERE
                parent_id='22'
              and archives_value is not null
        )
    </select>
</mapper>
