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

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

import java.math.BigDecimal;

/**
 * @Author: xushu
 * @Date: 2022/08/17/16:25
 * @Description:
 */
public interface RecycledMaterialManageDao extends BaseDao<RecycledMaterialManage, Long> {

    @Modifying
    @Query(value = "UPDATE recycled_material_manage SET del = 1 WHERE id = ?1", nativeQuery = true)
    void softDeleteById(Long id);

    @Query(value = "select count(id) from recycled_material_manage where tray_no LIKE CONCAT(?1,'%')", nativeQuery = true)
    Integer countTimeByParam(String key);

    @Query(value = "select ifnull(SUM(case when rmm.material_name like '%提渣盖%' then rmm.weight end),0)  as 提渣盖 from recycled_material_manage rmm where rmm.lot_no = ?1 group by rmm.lot_no", nativeQuery = true)
    BigDecimal findTiZhaGaiByLotNo(String lotNo);

    @Query(value = "select ifnull(SUM(case when rmm.material_name like '%头尾肩%' then rmm.weight end),0)  as 提肩料 from recycled_material_manage rmm where rmm.lot_no = ?1 group by rmm.lot_no", nativeQuery = true)
    BigDecimal findTiJianLiaoByLotNo(String lotNo);

    @Query(value = "select ifnull(SUM(case when rmm.material_name like '%埚底料%' then rmm.weight end),0)  as 埚底料 from recycled_material_manage rmm where rmm.lot_no = ?1 group by rmm.lot_no", nativeQuery = true)
    BigDecimal findGuoDiLiaoByLotNo(String lotNo);

    @Query(value = "select ifnull(SUM(case when rmm.material_name like '%墩埚料%' then rmm.weight end),0)  as 墩埚料 from recycled_material_manage rmm where rmm.lot_no = ?1 group by rmm.lot_no", nativeQuery = true)
    BigDecimal findDunGuoLiaoByLotNo(String lotNo);


    RecycledMaterialManage findByTrayNo(String trayNo);

    @Modifying
    @Query(value = "UPDATE recycled_material_manage SET weight = ?2 WHERE materialBoxNo = ?1", nativeQuery = true)
    void updateRecycledManageWeight(String materialBoxNo, BigDecimal weight);


    @Query(value = "select * from recycled_material_manage  where box_no = ?1 AND del =0 AND status IN(1,3) ORDER BY create_time DESC LIMIT 1", nativeQuery = true)
    RecycledMaterialManage getByBoxNoAndStatus(String getByBoxNoAndStatus);


    @Query(value = "select * from recycled_material_manage  where box_no = ?1 ORDER BY create_time DESC LIMIT 1", nativeQuery = true)
    RecycledMaterialManage getByBoxNo(String boxNo);

    @Query(value = "select * from recycled_material_manage  where produce_batch_no = ?1 ORDER BY create_time DESC LIMIT 1", nativeQuery = true)
    RecycledMaterialManage getByProduceBatchNo(String produceBatchNo);

    @Query(value = "SELECT ifnull(SUM(weight),0) as weight FROM `recycled_material_manage` WHERE work_order_no = ?1", nativeQuery = true)
    BigDecimal findSumWeightByWorkOrderNo(String orderNo);
    @Query(value = "select * from recycled_material_manage  where tray_no = ?1 ORDER BY create_time DESC LIMIT 1", nativeQuery = true)
    RecycledMaterialManage getRecycleMaterialByTrayNo(String trayNo);

    @Query(value = "select a.* from recycled_material_manage a left join recycled_material_manage_detail b on a.id = b.manage_id where b.recycle_no =?1 limit 1", nativeQuery = true)
    RecycledMaterialManage getRecycleMaterialByRecycleNo(String recycleNo);
}
