package com.sw.mes.yb.production.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.RecycledMaterialManageDetail;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: xushu
 * @Date: 2022/08/17/16:27
 * @Description:
 */
public interface RecycledMaterialManageDetailDao extends BaseDao<RecycledMaterialManageDetail, Long> {

    @Query(value = "SELECT * FROM recycled_material_manage_detail WHERE manage_id = ?1", nativeQuery = true)
    List<RecycledMaterialManageDetail> getDetailByManageId(Long manageId);

    @Modifying
    @Query(value = "DELETE FROM recycled_material_manage_detail  WHERE manage_id = ?1", nativeQuery = true)
    void deleteByManageId(Long manageId);

    @Query(value = "SELECT rmmd.recycle_no FROM recycled_material_manage_detail rmmd,recycled_material_manage rmm WHERE rmmd.manage_id = rmm.id and  rmm.status in (1,3,4) " +
            "and IF(?1 <>'' AND ?1 IS NOT NULL, rmmd.manage_id != ?1,1=1)", nativeQuery = true)
    List<String> findUsedRecycleNos(Long manageId);

    @Query(value = "select recycle_no from recycled_material_manage_detail  where recycle_no IN(?1)",nativeQuery = true)
    List<String> getDetailByRecycleNos(List<String> recycleNos);

    @Query(value = "SELECT ifnull(SUM(weight),0) as weight FROM `recycled_material_manage_detail` WHERE work_order_no = ?1", nativeQuery = true)
    BigDecimal findSumWeightByWorkOrderNo(String orderNo);

    @Query(value = "select distinct work_order_no from recycled_material_manage_detail  where recycle_no IN(?1)",nativeQuery = true)
    List<String> getWorkOrderNosByRecycleNos(List<String> recycleNos);
}
