package com.rongwei.bscommon.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rongwei.bsentity.domain.ApsProcessOperationProcessEquDo;
import com.rongwei.bsentity.domain.ApsProductDetailDo;
import com.rongwei.bsentity.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * @Entity com.rongwei.training.domain.ApsProcessOperationProcessEqu
 */
public interface ApsProcessOperationProcessEquDao extends BaseMapper<ApsProcessOperationProcessEquDo> {

    void updateBachmaterialprocessByIds(@Param("ids") List<String> ids, @Param("batchNum") String batchNum,
                                        @Param("nextIds") List<String> nextProcessesIds);

    /**
     * 更新 工序明细的 实际完工时间 完工状态 作业状态  已检验卷数 未完工卷数
     *
     * @param id                  工序明细表ID aps_process_operation_process_equ
     * @param currentCheckNum     检验卷数
     * @param qualifiedQuantity   合格数量
     * @param unqualifiedQuantity 不合格数量
     * @param previousprocessid   当前工序的上一个工序ID
     * @param processOperationId  当前工序的ID
     */
    void updateAboutCheckBatchNum(@Param("id") String id,
                                  @Param("currentCheckNum") Integer currentCheckNum,
                                  @Param("qualifiedQuantity") Integer qualifiedQuantity,
                                  @Param("unqualifiedQuantity") Integer unqualifiedQuantity,
                                  @Param("previousprocessid") String previousprocessid,
                                  @Param("processOperationId") String processOperationId);

    List<GanttVos> getGanttDataList(@Param("startTime") Date startTime,
                                    @Param("endTime") Date endTime,
                                    @Param("factoryId") String factoryId,
                                    @Param("workshopIds") List<String> workshopId,
                                    @Param("orderNos") List<String> orderNo,
                                    @Param("processids") String processids,
                                    @Param("productionLineIds")List<String> productionLineId,
                                    @Param("equIds") List<String> equId,
                                    @Param("ids")List<String> ids,
                                    @Param("productionLineName") String  productionLineName,
                                    @Param("equName") String  equName);

    void updateAboutStateByProcessId(@Param("updateIds") List<ProcessIdRelationVo> updateIds);


    @Select("select PROCESSID AS 'processOperationId',MAX(CLOSESTATUS) AS 'productionStatus' from aps_process_operation_process_equ where DELETED = 0 AND PROCESSID = (select PROCESSID from aps_process_operation_process_equ where ID = #{processOperationEquId}) GROUP BY PROCESSID")
    CommonUpdateProductionStatusReq selectEquMinStatus(@Param("processOperationEquId") String processOperationEquId);

    @Select("select BLANKID AS 'blankId',MIN(PROCESSSTATUS) AS 'productionStatus' from aps_process_operation where DELETED = 0 AND BLANKID = (select BLANKID from aps_process_operation where ID = #{processOperationId}) GROUP BY BLANKID")
    CommonUpdateProductionStatusReq selectProcessMinStatus(@Param("processOperationId") String processOperationId);

    @Select("select PRODUCTIONORDERID AS 'orderId',MIN(PRODUCTSTATUS) AS 'minProductionStatus',MAX(PRODUCTSTATUS) AS 'maxProductionStatus' from aps_blank_order where DELETED = 0 AND PRODUCTIONORDERID = (select PRODUCTIONORDERID from aps_blank_order where ID = #{blankId}) GROUP BY PRODUCTIONORDERID")
    CommonUpdateProductionStatusReq selectBlankMinAndMaxStatus(@Param("blankId") String blankId);

    @Update("update aps_process_operation_process_equ set STARTINGROLL = STARTINGROLL - 1,WAITREPORTID = null,DOINGBATCHNUMBER = '' where ID = #{processequid}")
    void updateByCancelRecord(@Param("processequid") String processequid);

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

    List<CommonUpdateProductionStatusReq> selectNeedUpdate(@Param("productIdList") List<String> productIdList);

    @Select("select apo.PRODUCTIONORDERID from aps_blank_order apo LEFT JOIN aps_process_operation apo2 ON apo2.DELETED = 0 AND apo.ID = apo2.BLANKID where apo.DELETED = 0 AND apo2.ID = #{processOperationId} limit 1")
    String selectOrderIdByProcessOperationId(@Param("processOperationId") String processOperationId);

    @Select("select apo.ID AS 'blankId',MIN(apo2.PROCESSSTATUS) AS 'minProductionStatus',MAX(apo2.PROCESSSTATUS) AS 'maxProductionStatus' from aps_blank_order apo LEFT JOIN aps_process_operation apo2 ON apo2.DELETED = 0 AND apo.ID = apo2.BLANKID where apo.DELETED = 0 AND apo.PRODUCTIONORDERID = #{orderId} GROUP BY apo.ID")
    List<CommonUpdateProductionStatusReq> selectProcessMinAndmaxStatusList(@Param("orderId") String orderId);

    void publish(@Param("productIdList") List<String> productIdList);

    void publishCancel(@Param("productIdList") List<String> productIdList);

    List<OptionalEquipmentVo> getOptionalEquipmentById(@Param("idList")List<String> id);

    List<String> checkConflict(@Param("checkConflictVos") List<CheckConflictVo> checkConflictVos);

    List<ApsProcessOperationProcessEquDo> getConflictId(@Param("equId") String equId,@Param("id") String id,
                               @Param("startTime")Date startTime,@Param("endTime") Date endTime);
}




