package com.rongwei.bscommon.sys.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rongwei.bsentity.domain.ApsProcessOperationDo;
import com.rongwei.bsentity.domain.ApsProductionMergeOrderDo;
import com.rongwei.bsentity.vo.ApsProcessOperationAndMaterialVo;
import com.rongwei.bsentity.vo.ProcessIdRelationVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 生产订单_坯料计划_工序作业 Mapper 接口
 * </p>
 *
 * @author fpy
 * @since 2024-03-01
 */
public interface ApsProcessOperationDao extends BaseMapper<ApsProcessOperationDo> {
    void updateBachmaterialprocessByIds(@Param("ids") List<String> ids, @Param("batchNum") String batchNum,
                                        @Param("unqualifiedQuantity")int unqualifiedQuantity);

    void updateAboutCheckBatchNum(@Param("id") String id,
                                  @Param("currentCheckNum") Integer currentCheckNum,
                                  @Param("qualifiedQuantity") Integer qualifiedQuantity,
                                  @Param("unqualifiedQuantity") Integer unqualifiedQuantity);

    @Update("UPDATE aps_process_operation set DELETED='1' where BLANKID=#{blanckid}")
    void updateProcessOperation(@Param("blanckid") String blanckid);

    @Update("UPDATE aps_process_operation_out_mater set DELETED='1' where BLANKID=#{blanckid}")
    void updateProcessOperationOutMeter(@Param("blanckid") String blanckid);

    @Update("UPDATE aps_process_operation_equ set DELETED='1' where BLANKID=#{blanckid}")
    void updateProcessOperationOEqu(@Param("blanckid") String blanckid);

    @Update("UPDATE sys_mind_mapping set DELETED='1' where BSID=#{blanckid}")
    void updateSysMindMapping(@Param("blanckid") String blanckid);

    @Select("select MAINID,GROUP_CONCAT(EQUIPMENTID) as EQUIDS from aps_resources_equipment where DELETED='0' group by MAINID")
    List<Map<String, Object>> getEquGroupIds();

    @Select("select apo.*,apoom.ID AS 'outputMaterialId',apoom.OUTPUTMATERIAL AS 'outputMaterial',apoom.PLANROLLNUM AS 'planRollNum',apoom.PLANSTOCKROLLNUM AS 'planStockRollNum',apoom.PLANPRODROLLNUM AS 'planProdRollNum' from aps_process_operation apo LEFT JOIN aps_process_operation_out_mater apoom ON apoom.MAINID = apo.ID AND apoom.DELETED = 0 where apo.DELETED = 0 AND apo.BLANKID = #{blankId}")
    List<ApsProcessOperationAndMaterialVo> selectOperationAndMaterialByBlankId(@Param("blankId") String blankId);

    List<ProcessIdRelationVo> getAllNextProcessOperationById(@Param("ids") List<String> ids);

    @Update("update aps_process_operation set STARTINGROLL = STARTINGROLL - 1 ,ACTUALSTARTDATE = if((STARTINGROLL - 1) <= 0,null,ACTUALSTARTDATE) where ID = #{processoperationid}")
    void updateByCancelRecord(@Param("processoperationid") String processoperationid);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = concat(BACHMATERIALPROCESS,',',#{batchnumber}) where DELETED = 0 AND ID = #{processoperationid}")
    void updateBatchNumberByProcessId(@Param("batchnumber") String batchnumber, @Param("processoperationid") String processoperationid);

    void updateBatchNumberByProcessList(@Param("batchnumber") String batchnumber, @Param("processIdList") List<String> processIdList);

    @Select("select ID,PLANPROCESSRALL from aps_process_operation where DELETED='0' and BLANKID=#{blankid}")
    List<ApsProcessOperationDo> getProcessOperation(@Param("blankid") String blankid);

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

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

    @Select("SELECT * FROM aps_process_operation WHERE DELETED = 0 AND BLANKID = #{blankid}")
    List<ApsProcessOperationDo> getProcessOperationByBlankId(@Param("blankid") String blankid);

    @Update("update aps_process_operation_process_equ set BACHMATERIALPROCESS = IF(BACHMATERIALPROCESS is null || BACHMATERIALPROCESS = '',#{batchnumber},concat(BACHMATERIALPROCESS,',',#{batchnumber})) where DELETED = 0 AND PREVIOUSPROCESSESIDS = #{previousprocessesids}")
    void updateBatchNumberByPrevious(@Param("batchnumber") String batchnumber, @Param("previousprocessesids") String previousprocessesids);
}
