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

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

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.InputFurnaceDetail;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

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

	List<InputFurnaceDetail> findByAgainTaskDetailId(Long taskId);

	List<InputFurnaceDetail> findByAgainTaskDetailIdAndBucketNo(Long taskId, String bucketNo);

	List<InputFurnaceDetail> findByInputFurnaceId(Long inputFurnaceId);

	@Query(value = "select * from production_input_furnace_detail where again_task_detail_id in ?1", nativeQuery = true)
	List<InputFurnaceDetail> findByAgainTaskDetailIdList(List<Long> idList);

	@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,1 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_input_furnace_detail pbfd\n" +
			"left join production_again_task_detail plt on plt.id=pbfd.again_task_detail_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.pulled = 0 and pbfd.erp_batch_no is not null and pbfd.erp_batch_no != \"\"", nativeQuery = true)
	List<Map<String, Object>> pullMaterialData();

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