package com.rongwei.bscommon.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rongwei.bsentity.domain.ApsCheckParameterDo;
import com.rongwei.bsentity.domain.ApsParameterDo;
import com.rongwei.bsentity.domain.ApsReportRecordsDo;
import com.rongwei.bsentity.vo.OrganizationCodeAndDeviceCodeVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @Entity com.rongwei.training.domain.ApsReportRecords
 */
public interface ApsReportRecordsDao extends BaseMapper<ApsReportRecordsDo> {

    @Update("update aps_process_operation set ACTUALSTARTDATE = IFNULL(ACTUALSTARTDATE,NOW()),STARTINGROLL = STARTINGROLL+1 where ID = #{id}")
    void updateProcessActualStartDate(@Param("id") String id);

    @Update("update asp_check_items set WAITREPORTID = #{id} where ID = #{processdeviceid}")
    void updateDeviceWaitReportId(@Param("id") String id, @Param("processdeviceid") String processdeviceid);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = REPLACE(REPLACE(REPLACE(BACHMATERIALPROCESS,'${inputBatchNumber},',''),',${inputBatchNumber}',''),'${inputBatchNumber}','') where PROCESSID = #{id}")
    void updateBatchMaterialProcess(@Param("inputBatchNumber") String inputBatchNumber, @Param("id") String id);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = REPLACE(REPLACE(REPLACE(BACHMATERIALPROCESS,'${inputBatchNumber},',''),',${inputBatchNumber}',''),'${inputBatchNumber}','') where PROCESSID in (${processIds})")
    void updateBatchMaterialProcessBatch(@Param("inputBatchNumber") String inputBatchNumber, @Param("processIds") String processIds);

    @Select("select ap.ID AS 'id',ap.PARAMETER AS 'parameter' from aps_production_processes app LEFT JOIN aps_parameter ap ON app.ID = ap.MAINID AND ap.DELETED = 0 where app.PRODUCTPROCESSNAME = #{process}")
    List<ApsParameterDo> selectApsParameterByProcess(@Param("process") String process);

    @Select("select acp.ID AS 'id',acp.CKECKPARAMETER AS 'checkparameter' from aps_production_processes app LEFT JOIN aps_check_parameter acp ON app.ID = acp.MAINID AND acp.DELETED = 0 where app.PRODUCTPROCESSNAME = #{process}")
    List<ApsCheckParameterDo> selectApsCheckParameterByProcess(@Param("process") String process);

    @Update("update aps_process_operation set REPORTROLL = REPORTROLL+1 where ID = #{id}")
    void updateProcessReportRoll(@Param("id") String id);

    @Update("update asp_check_items set WAITREPORTID = null where ID = #{processdeviceid}")
    void updateDeviceWorkId(@Param("processdeviceid") String processdeviceid);

    @Delete("DELETE from aps_report_output where MAINID = #{id}")
    void deleteOutputmaterialbyMainId(@Param("id") String id);

    @Select("select aci.EQUIPMENTCODE AS 'deviceCode',so.ROPTION AS 'organizationCode',aci.WAITREPORTID AS 'waitReportId',aci.OWNEDFACTORY AS 'ownedFactory' from asp_check_items aci LEFT JOIN sys_organization so ON aci.USEDEPTID = so.ID AND so.DELETED = 0 where aci.ID = #{processdeviceid} limit 1")
    OrganizationCodeAndDeviceCodeVo selectOrganizationCodeAndDeviceCode(@Param("processdeviceid") String processdeviceid);

    @Select("select arr.ID from aps_process_operation apo LEFT JOIN aps_report_records arr ON arr.DELETED = 0 AND arr.PROCESSOPERATIONID = apo.ID LEFT JOIN aps_report_output aro ON aro.DELETED = 0 AND aro.MAINID = arr.ID where apo.DELETED = 0 AND aro.OUTPUTNUMBER = #{inputBatchNumber} ORDER BY arr.CREATEDATE desc limit 1")
    String selectLastReoprtRecordByBatchNumber(@Param("inputBatchNumber") String inputBatchNumber);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = REPLACE(REPLACE(REPLACE(BACHMATERIALPROCESS,'${inputBatchNumber},',''),',${inputBatchNumber}',''),'${inputBatchNumber}','') where DELETED = 0 AND PREVIOUSPROCESSESIDS = #{previousprocessesids}")
    void updateAllProcessEquBatchNumber(@Param("inputBatchNumber") String inputBatchNumber, @Param("previousprocessesids") String previousprocessesids);
}




