<?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.repair.mapper.RepairMapper">
    <insert id="addRecord">
        insert into repair_apply_record (task_id,ma_id,type_id,repair_num,scrap_num,repair_type,create_by,create_time,repair_content,company_id,scrap_reason,scrap_type,supplier_id,part_num,part_price,part_type,part_name,repairer,file_ids,remark,part_id)
        values (#{taskId},#{maId},#{typeId},#{repairNum},#{scrapNum},#{repairType},#{createBy},now(),#{repairContent},#{companyId},#{scrapReason},#{scrapType},#{supplierId},#{partNum},#{partPrice},#{partType},#{partName},#{repairer},#{fileIds},#{remark},#{partId});
    </insert>

    <insert id="addPart">
        insert into repair_part_details (task_id,ma_id,type_id,part_id,part_num,part_cost,part_type,create_by,create_time,company_id,repair_content)
        values (#{taskId},#{maId},#{typeId},#{partId},#{partNum},#{partCost},#{partType},#{createBy},now(),#{companyId},#{repairContent});
    </insert>

    <insert id="addTask" useGeneratedKeys="true" keyProperty="taskId">
        insert into tm_task (task_status,task_type,code,create_by,create_time,company_id)
        values (#{taskStatus},#{taskType},#{repairCode},#{createBy},now(),#{companyId});
    </insert>

    <insert id="createAgreementTask">
        insert into tm_task_agreement (task_id,agreement_id,create_by,create_time,company_id)
        values (#{taskId},#{agreementId},#{createBy},now(),#{companyId});
    </insert>

    <insert id="addAuditDetails">
        insert into repair_audit_details (task_id,repair_id,ma_id,type_id,repair_num,repaired_num,scrap_num,status,
                                          create_by,create_time,company_id)
        values (#{taskId},#{id},#{maId},#{typeId},#{repairNum},#{repairedNum},#{scrapNum},'0',#{createBy},now(),
                #{companyId});
    </insert>

    <insert id="addRepairCost">
        insert into repair_cost (task_id,repair_id,type_id,ma_id,repair_num,costs,part_type,status,repair_type)
        values (#{bean.taskId},#{bean.id},#{bean.typeId},#{bean.maId},#{bean.repairNum},#{costs},#{partType},'0',#{bean.repairType});
    </insert>

    <update id="updateRepairedNum">
        update repair_apply_details
        set  repaired_num = #{repairNum},
            repairer = #{repairer},
            update_by = #{userId},
            update_time = now()
        where id = #{id}
    </update>

    <update id="updateRepairNum">
        update repair_apply_details
        set  repair_num = #{repairNum},
             update_time = now()
        where id = #{id}
    </update>

    <update id="updateThisRepairedAndScrapNum">
        update repair_apply_details
        set this_repaired_num = this_repaired_num + #{thisRepairedNum},
            this_scrap_num = this_scrap_num + #{thisScrapNum},
            update_time = now()
        where id = #{id}
    </update>

    <update id="updateRepairedNumAndStatus">
        update repair_apply_details
        set  repaired_num = #{repairNum},
             status = #{status},
             repairer = #{repairer},
             update_by = #{userId},
             update_time = now()
        where id = #{id}
    </update>

    <update id="updateRepairedNumTwo">
        update repair_apply_details
        set  repaired_num = #{repairNum},
             update_by = #{userId},
             update_time = now()
        where id = #{id}
    </update>

    <update id="updateRepairedNumTwoAndStatus">
        update repair_apply_details
        set  repaired_num = #{repairNum},
             status = #{status},
             update_by = #{userId},
             update_time = now()
        where id = #{id}
    </update>

    <update id="updateScrapNum">
        update repair_apply_details
        set  scrap_num = #{scrapNum},
            update_by = #{userId},
            update_time = now()
        where id = #{id}
    </update>

    <update id="updateScrapNumAndStatus">
        update repair_apply_details
        set  scrap_num = #{scrapNum},
             status = #{status},
             update_by = #{userId},
             update_time = now()
        where id = #{id}
    </update>

    <update id="batchQualified">
        update repair_apply_details
        set status = '1',
        repaired_num = repair_num,
        update_by = #{userId},
        update_time = now()
        where id in
            <foreach item="id" collection="ids" open="(" separator="," close=")">
                #{id}
            </foreach>
    </update>

    <update id="updateTaskStatus">
        update tm_task
        set task_status = #{taskStatus},
        update_by = #{userId},
        update_time = now()
        where task_id in
        <foreach item="task" collection="taskList" open="(" separator="," close=")">
            #{task.taskId}
        </foreach>
    </update>

    <update id="updateSingleTaskStatus">
        update tm_task
        set task_status = #{taskStatus},
            update_by = #{userId},
            update_time = now()
        where task_id = #{task.taskId}
    </update>

    <select id="getRepairTaskInfoByTaskId" resultType="com.bonus.material.repair.domain.RepairTask">
        SELECT
            rd.task_id as taskId,
            rd.repairer,
            tt.CODE AS repairCode,
            tt.create_time AS createTime,
            tt.task_status AS repairStatusCode,
            tt.company_id AS companyId,
            tt.remark,
            bui.unit_name AS backUnit,
            bpi.pro_name AS backPro,
            su.nick_name AS createName,
            bai.CODE AS backCode,
            GROUP_CONCAT(DISTINCT mt2.type_name) as type
        FROM
            repair_apply_details rd
        LEFT JOIN ma_type mt on rd.type_id = mt.type_id
        LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        LEFT JOIN tm_task tt on rd.task_id = tt.task_id
        LEFT JOIN back_apply_info bai ON rd.back_id = bai.id
        LEFT JOIN tm_task_agreement tta ON rd.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai2 ON tta.agreement_id = bai2.agreement_id
        LEFT JOIN bm_unit bui ON bai2.unit_id = bui.unit_id
        LEFT JOIN bm_project bpi ON bai2.project_id = bpi.pro_id and bpi.del_flag = '0'
        left join sys_user su on rd.create_by = su.user_id
        where rd.task_id = #{taskId}
        GROUP BY rd.task_id
    </select>

    <select id="getRepairTaskList" resultType="com.bonus.material.repair.domain.RepairTask">
        SELECT
            rd.task_id as taskId,
            rd.repairer,
            tt.CODE AS repairCode,
            tt.create_time AS createTime,
            tt.task_status AS repairStatusCode,
            tt.company_id AS companyId,
            tt.remark,
            bui.unit_name AS backUnit,
            bpi.pro_name AS backPro,
            su.nick_name AS createName,
            bai.CODE AS backCode,
            GROUP_CONCAT(DISTINCT mt2.type_name) as type
        FROM
            repair_apply_details rd
        LEFT JOIN ma_type mt on rd.type_id = mt.type_id
        LEFT JOIN ma_type mt2 on mt.parent_id = mt2.type_id
        LEFT JOIN tm_task tt on rd.task_id = tt.task_id
        LEFT JOIN back_apply_info bai ON rd.back_id = bai.id
        LEFT JOIN tm_task_agreement tta ON rd.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai2 ON tta.agreement_id = bai2.agreement_id
        LEFT JOIN bm_unit bui ON bai2.unit_id = bui.unit_id
        LEFT JOIN bm_project bpi ON bai2.project_id = bpi.pro_id and bpi.del_flag = '0'
        left join sys_user su on rd.create_by = su.user_id
        <where>
            <if test="keyword != null and keyword != ''">
                AND (locate(#{keyword}, su.nick_name) > 0
                or locate(#{keyword}, tt.CODE) > 0)
            </if>
            <if test="backUnit != null and backUnit != ''">
                AND bui.unit_id = #{backUnit}
            </if>
            <if test="backPro != null and backPro != ''">
                AND bpi.pro_id = #{backPro}
            </if>
            <if test="type != null and type != ''">
                AND mt2.type_id = #{type}
            </if>
            <if test="backCode != null and backCode != ''">
                AND locate(#{backCode}, bai.CODE) > 0
            </if>
            <if test="repairStatus != null and repairStatus != ''">
                AND tt.task_status = #{repairStatus}
            </if>
            <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
                AND tt.create_time BETWEEN CONCAT(#{startTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59')
            </if>
        </where>
        GROUP BY rd.task_id,bui.unit_name,bpi.pro_name,bai.code,su.nick_name
        order by tt.create_time desc
    </select>

    <select id="exportRepairTaskList" resultType="com.bonus.material.repair.domain.RepairTask">
        SELECT
            rd.task_id,
            tt.CODE AS repairCode,
            bui.unit_name AS backUnit,
            bpi.lot_name AS backPro,
            su.nick_name AS createName,
            tt.create_time AS createTime,
            bai.CODE AS backCode,
            tt.task_status AS repairStatusCode,
            tt.company_id AS companyId,
            mt2.type_name AS type,
            mt.type_name AS typeName,
            mm.ma_code AS CODE,
            rd.repair_num AS repairNum,
            rd.repaired_num AS repairedNum,
            rd.scrap_num AS scrapNum,
            case rd.status when '0' then '未完成' when '1' then '已完成' else '' end as status,
            su2.nick_name as repairer,
            rd.update_time as updateTime
        FROM
            repair_apply_details rd
                LEFT JOIN ma_type mt ON rd.type_id = mt.type_id
                LEFT JOIN ma_type mt2 ON mt.parent_id = mt2.type_id
                LEFT JOIN tm_task tt ON rd.task_id = tt.task_id
                LEFT JOIN back_apply_info bai ON rd.back_id = bai.id
                LEFT JOIN tm_task_agreement tta ON rd.task_id = tta.task_id
                LEFT JOIN bm_agreement_info bai2 ON tta.agreement_id = bai2.agreement_id
                LEFT JOIN bm_unit_info bui ON bai2.unit_id = bui.unit_id
                LEFT JOIN bm_project bpi ON bai2.project_id = bpi.lot_id AND bpi.STATUS = '0' AND bpi.del_flag = '0'
                LEFT JOIN sys_user su ON rd.create_by = su.user_id
                left join sys_user su2 on rd.repairer = su2.user_id
                LEFT JOIN ma_machine mm ON mm.ma_id = rd.ma_id
        where 1=1
        <if test="keyword != null and keyword != ''">
            AND (locate(#{keyword}, su.nick_name) > 0
            or locate(#{keyword}, tt.CODE) > 0)
        </if>
        <if test="backUnit != null and backUnit != ''">
            AND bui.unit_id = #{backUnit}
        </if>
        <if test="backPro != null and backPro != ''">
            AND bpi.pro_id = #{backPro}
        </if>
        <if test="type != null and type != ''">
            AND mt2.type_id = #{type}
        </if>
        <if test="backCode != null and backCode != ''">
            AND locate(#{backCode}, bai.CODE) > 0
        </if>
        <if test="repairStatus != null and repairStatus != ''">
            AND tt.task_status = #{repairStatus}
        </if>
        <if test="startTime != null and startTime != '' and endTime != null and endTime != ''">
            AND tt.create_time BETWEEN CONCAT(#{startTime}, ' 00:00:00') AND CONCAT(#{endTime}, ' 23:59:59')
        </if>
        <if test="dataCondition != null and dataCondition.size()>0">
            AND rd.task_id in
            <foreach collection="dataCondition" item="taskId" index="index" open="(" separator="," close=")">
                #{taskId}
            </foreach>
        </if>
        ORDER BY
            tt.create_time DESC
    </select>

    <select id="getRepairDeviceList" resultType="com.bonus.material.repair.domain.vo.RepairDeviceVO">
        select
            rad.id as id,
            rad.task_id as taskId,
            rad.ma_id as maId,
            mt2.type_name as typeName,
            mt2.type_id as typeNameId,
            mt.type_name as type,
            mt.unit_name as unitName,
            mt.manage_type as manageType,
            mm.ma_code as code,
            rad.repair_num as repairNum,
            sum(ifnull(rad.repaired_num,0) + ifnull(rad.this_repaired_num,0)) as repairedNum,
            sum(ifnull(rad.scrap_num,0) + ifnull(rad.this_scrap_num,0)) as scrapNum,
            rad.status as status,
            su.nick_name as repairer,
            rad.update_time as updateTime,
            rad.type_id as typeId,
            rad.back_id as backId,
            rad.create_time,
            count(rc.id) AS totalCostRecords,
            sum(ifnull(rc.costs,0)) AS totalCost
        from
            repair_apply_details rad
            left join ma_type mt on rad.type_id = mt.type_id
            left join ma_machine mm on mm.ma_id = rad.ma_id
            left join sys_user su on rad.repairer = su.user_id
            left join ma_type mt2 on mt.parent_id = mt2.type_id
            left join repair_cost rc ON rad.id = rc.repair_id
        where
            rad.task_id = #{taskId}
        <if test="companyId != null and companyId != ''">
            and rad.company_id = #{companyId}
        </if>
        <if test="keyword != null and keyword != ''">
            AND (locate(#{keyword}, mm.ma_code) > 0
            or locate(#{keyword}, su.nick_name) > 0
            or locate(#{keyword}, mt2.type_name) > 0
            or locate(#{keyword}, mt.type_name) > 0)
        </if>
        <if test="type != null and type != ''">
            AND mt.type_id = #{type}
        </if>
        <if test="typeName != null and typeName != ''">
            AND mt2.type_id = #{typeName}
        </if>
        GROUP BY rad.id
        order by rad.create_time desc
    </select>

<!--    <select id="getRepairMaTypeList" resultType="com.bonus.material.repair.domain.vo.RepairDeviceListVo">-->
<!--        select-->
<!--            rad.id as id,-->
<!--            rad.task_id as taskId,-->
<!--            rad.ma_id as maId,-->
<!--            mt2.type_name as typeName,-->
<!--            mt2.type_id as typeNameId,-->
<!--            mt.type_name as type,-->
<!--            mt.unit_name as unitName,-->
<!--            mt.manage_type as manageType,-->
<!--            mm.ma_code as code,-->
<!--            rad.repair_num as repairNum,-->
<!--            rad.repaired_num as repairedNum,-->
<!--            rad.scrap_num as scrapNum,-->
<!--            rad.status as status,-->
<!--            su.nick_name as repairer,-->
<!--            rad.update_time as updateTime,-->
<!--            rad.type_id as typeId,-->
<!--            rad.create_time,-->
<!--            count(rc.id) AS totalCostRecords,-->
<!--            sum(rc.costs) AS totalCost-->
<!--        from repair_apply_details rad-->
<!--        left join ma_type mt on rad.type_id = mt.type_id-->
<!--        left join ma_machine mm on mm.ma_id = rad.ma_id-->
<!--        left join sys_user su on rad.repairer = su.user_id-->
<!--        left join ma_type mt2 on mt.parent_id = mt2.type_id-->
<!--        left join repair_cost rc ON rad.id = rc.repair_id-->
<!--        where rad.task_id = #{taskId}-->
<!--        <if test="companyId != null and companyId != ''">-->
<!--            and rad.company_id = #{companyId}-->
<!--        </if>-->
<!--        <if test="keyword != null and keyword != ''">-->
<!--            AND (locate(#{keyword}, mm.ma_code) > 0-->
<!--            or locate(#{keyword}, su.nick_name) > 0-->
<!--            or locate(#{keyword}, mt2.type_name) > 0-->
<!--            or locate(#{keyword}, mt.type_name) > 0)-->
<!--        </if>-->
<!--        <if test="type != null and type != ''">-->
<!--            AND mt.type_id = #{type}-->
<!--        </if>-->
<!--        <if test="typeName != null and typeName != ''">-->
<!--            AND mt2.type_id = #{typeName}-->
<!--        </if>-->
<!--        GROUP BY rad.id-->
<!--        order by rad.create_time desc-->
<!--    </select>-->

    <select id="getById" resultType="com.bonus.material.repair.domain.RepairTaskDetails">
        select rad.id as id,
               rad.repair_num as repairNum,
               rad.repaired_num as repairedNum,
               rad.scrap_num as scrapNum
        from repair_apply_details rad
        where rad.id = #{id}
    </select>

    <select id="selectUserList" resultType="com.bonus.system.api.domain.SysUser">
        SELECT
            su.user_id AS userId,
            su.nick_name AS nickName
        FROM
            sys_user su
                LEFT JOIN sys_user_role sur on su.user_id = sur.user_id
                LEFT JOIN sys_role sr on sur.role_id = sr.role_id
        WHERE sr.role_key = 'wx'
    </select>

    <select id="getAgreementId" resultType="java.lang.Long">
        select agreement_id
        from tm_task_agreement
        where task_id = #{taskId}
    </select>

    <select id="getUnFinish" resultType="java.lang.Integer">
        select count(*)
        from repair_apply_details
        where task_id = #{taskId} and status = '0'
    </select>

    <select id="selectPartPrice" resultType="java.math.BigDecimal">
        select ifnull(buy_price,0) from ma_part_type where pa_id = #{partId}
    </select>

    <!--获取修试后入库列表-->
    <select id="getRepairedList" resultType="com.bonus.material.repair.domain.vo.RepairInputDetailsVo">
        SELECT b.id,
        b.repairCode,
        b.maTypeName,
        b.projectName,
        b.unitName,
        b.wxName,
        b.wxTime,
        b.taskStatus
        FROM (
        SELECT ANY_VALUE(rid.task_id) AS id,
        ANY_VALUE(tt.`code`) AS repairCode,
        ANY_VALUE(GROUP_CONCAT(DISTINCT a.typeName2)) AS maTypeName,
        ANY_VALUE(GROUP_CONCAT(DISTINCT a.typeId2)) AS typeId2,
        bpl.pro_name projectName,
        bui.unit_name unitName,
        ANY_VALUE(us.user_name) AS wxName,
        ANY_VALUE(tt.create_time) AS wxTime,
        tt.task_status AS taskStatus
        FROM repair_input_details rid
        LEFT JOIN repair_apply_details rad on rid.repair_id=rad.id
        LEFT JOIN sys_user us on us.user_id=rad.repairer
        LEFT JOIN tm_task tt ON rid.task_id = tt.task_id
        LEFT JOIN tm_task_agreement tta ON tt.task_id = tta.task_id
        LEFT JOIN bm_agreement_info bai ON tta.agreement_id = bai.agreement_id
        LEFT JOIN bm_project bpl ON bai.project_id = bpl.pro_id
        LEFT JOIN bm_unit bui ON bai.unit_id = bui.unit_id
        LEFT JOIN tm_task tt2 ON rad.task_id = tt2.task_id
        LEFT JOIN (
        SELECT mt.type_id AS typeId,mt.type_name AS typeName,mt.manage_type,mt2.type_id AS typeId2,mt2.type_name AS typeName2
        FROM ma_type mt
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id AND mt2.`level` = '3'
        LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id AND mt3.`level` = '2'
        WHERE mt.`level` = '4'
        )a ON rid.type_id = a.typeId
        GROUP BY rid.task_id
        ORDER BY tt.create_time DESC
        ) b
        <where>
            <if test="deviceTypeId!=null and deviceTypeId!=''">
                b.typeId2 = #{deviceTypeId}
            </if>
            <if test="wxTime!=null and wxTime!=''">
                AND b.wxTime BETWEEN CONCAT(#{wxTime},' 00:00:00') AND CONCAT(#{wxTime},' 23:59:59')
            </if>
            <if test="keyWord!=null and keyWord!=''">
                AND (
                INSTR(b.repairCode,#{keyWord}) > 0 OR
                INSTR(b.wxName,#{keyWord}) > 0
                )
            </if>
        </where>
    </select>

    <!--获取修试后入库-详情列表-->
    <select id="getRepairedDetailList" resultType="com.bonus.material.repair.domain.vo.RepairInputDetailsVo">
        SELECT rid.id,
        rid.ma_id AS maId,
        rid.type_id AS typeId,
        a.typeName,
        a.typeName2,
        rid.repair_num AS repairNum,
        a.manage_type AS manageType,
        mm.ma_code AS maCode,
        rid.update_by AS updateBy,
        rid.update_time AS updateTime,
        rid.remark,
        CASE rid.`status` WHEN '0' THEN '进行中' WHEN '1' THEN '已入库' WHEN '2' THEN '驳回' ELSE '进行中' END AS `status`
        FROM repair_input_details rid
        LEFT JOIN ma_machine mm ON rid.ma_id = mm.ma_id
        LEFT JOIN (
        SELECT mt.type_id AS typeId,mt2.type_id AS typeId2,mt.type_name AS typeName,mt2.type_name AS typeName2,mt.manage_type
        FROM ma_type mt
        LEFT JOIN ma_type mt2 ON mt2.type_id = mt.parent_id AND mt2.`level` = '3'
        LEFT JOIN ma_type mt3 ON mt2.parent_id = mt3.type_id AND mt3.`level` = '2'
        LEFT JOIN ma_type mt4 ON mt3.parent_id = mt4.type_id AND mt4.`level` = '1'
        WHERE mt.`level` = '4'
        )a ON rid.type_id = a.typeId
        WHERE rid.task_id = #{taskId}
        <if test="keyWord!=null and keyWord!=''">
            AND (
            INSTR(rid.update_by,#{keyWord}) > 0 OR
            INSTR(mm.ma_code,#{keyWord}) > 0
            )
        </if>
        <if test="deviceTypeId!=null and deviceTypeId!=''">
            AND a.typeId2 = #{deviceTypeId}
        </if>
        <if test="typeId!=null and typeId!=''">
            AND a.typeId = #{typeId}
        </if>
        ORDER BY rid.create_time DESC
    </select>
</mapper>
