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

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

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.BlastFurnaceDetail;
import org.springframework.transaction.annotation.Transactional;

/**
* @Author
* @Description：
* @Date:
* @Modified By:
*/
public interface BlastFurnaceDetailDao extends BaseDao<BlastFurnaceDetail, Long>{

	List<BlastFurnaceDetail> findByMaterialCode(String materialCode);

	@Query(value = "select * from production_blast_furnace_detail where blast_furnace_id=?1 order by id desc",nativeQuery = true)
	List<BlastFurnaceDetail> findByTaskId(Long taskId);

	@Query(value = "select pbfd.id as id,pbfd.weight as weight,plt.id as pid,ppo.erp_order_no as erpOrderNo,\n" +
			"pbfd.batch_no as batchNo,pbfd.material_name as materialName,0 as type,\n" +
			"ppo.dept_code as deptCode,ppo.work_shop as workShop,pbfd.material_code as materialCode,\n" +
			"plt.lot_no as lotNo,pbfd.erp_warehouse_code as erpWarehouseCode,pbfd.erp_batch_no as erpBatchNo\n" +
			"from  production_blast_furnace_detail pbfd\n" +
			"left join production_loading_task plt on plt.id=pbfd.blast_furnace_id\n" +
			"left join plan_production_order ppo on ppo.aufnr = plt.sap_no\n" +
			"where ppo.erp_order_no is not null and pbfd.weight is not null\n" +
			"and pbfd.material_name not like \"%坩埚%\" and pbfd.pulled = 0 and pbfd.erp_batch_no is not null\n" +
			"and pbfd.erp_batch_no !=\"\"",nativeQuery = true)
	List<Map<String, Object>> pullMaterialData();

	@Modifying
	@Transactional
	@Query(value = "UPDATE production_blast_furnace_detail SET pulled = 1 WHERE id IN (?1) ", nativeQuery = true)
	void updatePulledState(List<Long> idList);

	@Query(value = "select * from production_blast_furnace_detail where blast_furnace_id in (?1)",nativeQuery = true)
    List<BlastFurnaceDetail> findByLoadingTaskIds(List<Long> loadingTaskIdList);

	@Modifying
	@Query(value = "delete from production_blast_furnace_detail where blast_furnace_id= ?1", nativeQuery = true)
	void deleteByBlastFurnaceId(Long loadingTaskId);

}
