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

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

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.production.entity.AgainTaskDetail;

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

	List<AgainTaskDetail> findByLotNoAndPailNum(String lotNo, String pailNum);

	List<AgainTaskDetail> findByLotNoAndPailNumAndAgainNum(String lotNo, String pailNum, Integer againNum);

	@Query(nativeQuery = true, value = "select * from (select * from production_again_task_detail where raw_status<=1 "
			+ "and if(?1 is not null, equipment_code like CONCAT(?1,'%'),1=1) "
			+ "and series in (?2) "
			+ "group by again_task_id order by carrier_sort asc) t order by again_time desc",
			countQuery = "select count(*) from (select * from production_again_task_detail where raw_status<=1 "
					+ "and if(?1 is not null, equipment_code like CONCAT(?1,'%'),1=1) "
					+ "and series in (?2) "
					+ "group by again_task_id order by carrier_sort asc) t order by again_time desc")
	Page<AgainTaskDetail> findByPdaPage(String equipmentCode, List<String> series, Pageable page);

	@Query(nativeQuery = true, value = "select * from (select * from production_again_task_detail where raw_status<=1 "
			+ "and if(?1 is not null, equipment_code like CONCAT(?1,'%'),1=1) "
			+ "group by again_task_id order by carrier_sort asc) t order by again_time desc",
			countQuery = "select count(*) from (select * from production_again_task_detail where raw_status<=1 "
					+ "and if(?1 is not null, equipment_code like CONCAT(?1,'%'),1=1) "
					+ "group by again_task_id order by carrier_sort asc) t order by again_time desc")
	Page<AgainTaskDetail> findByPdaPage(String equipmentCode, Pageable page);


	@Query(nativeQuery = true, value = "select * from production_again_task_detail where equipment_code = ?1 and lot_no=?2 and (raw_status=3 or raw_status=4 or raw_status=7) ORDER BY id desc limit 1")
	AgainTaskDetail getLastSuccessTask(String equipmentCode, String lotNo);

	@Query(nativeQuery = true, value = "select sum(again_real_weight)guanliaoWeight from production_again_task_detail WHERE lot_no=?1 and (raw_status = 3 OR  raw_status = 4) group by lot_no")
	BigDecimal getTotaGuanLiaoWeightByLotNo(String lotNo);

	List<AgainTaskDetail> findByAgainTaskId(Long againTaskId);

	@Query(value = "SELECT \n" +
			"\ttd.id\n" +
			"\t,td.equipment_code equipmentCode  ##设备号 \n" +
			"\t,td.lot_no lotNo ##炉次号\n" +
			"\t,fb.vehicle_code vehicleCode ##载具编号\n" +
			"\t,CONCAT(td.again_num, '次复投') againNum ##任务类型\n" +
			"\t,td.product_code productCode ##产品料号\n" +
			"\t,\n" +
			"\t\tCASE\n" +
			"\t\t\tWHEN td.raw_status = 0 THEN '新建'\n" +
			"\t\t\tWHEN td.raw_status = 1 THEN '生产'\n" +
			"\t\t\tWHEN td.raw_status = 2 THEN '已取消'\n" +
			"\t\t\tWHEN td.raw_status = 3 THEN '自动完工'\n" +
			"\t\t\tWHEN td.raw_status = 4 THEN '人工完工'\n" +
			"\t\t\tWHEN td.raw_status = 5 THEN '退料'\n" +
			"\t\t\tWHEN td.raw_status = 6 THEN '等待'\n" +
			"\t\t\tWHEN td.raw_status = 7 THEN '满料入库送走'\n" +
            "\t\t\tELSE td.raw_status\n" +
            "\t\tEND rawStatus ##状态\n" +
            "\t,fb.pail_num pailNum ##灌料桶数\n" +
            "\t,fb.class_no classNo ##班次\n" +
            "\t,fb.team_group teamGroup ##班组\n" +
            "\t,fb.user_name userName1 ##灌料人\n" +
            "\t,td.start_time startTime ##灌料开始时间\n" +
            "\t,fb.end_time endTime ##灌料结束时间\n" +
            "\t,td.operation_cause operationCause ##操作原因\n" +
            "\t,td.user_name userName2 ##操作人\n" +
            "\t,td.operation_time operationTime ##操作时间\n" +
            "\t,fb.left_code leftCode ##左桶编号\n" +
            "\t,fb.right_code rightCode ##右桶编号\n" +
            "\t,fd.material_code materialCode ##料号\n" +
            "\t,fd.material_name materialName ##物料名称\n" +
            "\t,fd.batch_no batchNo ##批次号\n" +
            "\t,fd.supplier_name supplierName ##厂牌\n" +
            "\t,fd.weight weight ##重量\n" +
            "\t,fd.bucket_forty bucketForty ##箱数(40KG)\n" +
            "\t,fd.bucket_ten bucketTen ##箱数(10KG)\n" +
            "FROM production_again_task_detail td\n" +
            "LEFT JOIN production_input_furnace_burden fb ON fb.again_task_detail_id = td.id\n" +
            "LEFT JOIN production_input_furnace_detail fd ON fd.again_task_detail_id = td.id AND fd.input_furnace_id = fb.id \n" +
            "WHERE td.raw_status not in (0,6,7) AND fb.again_real_weight > 0 \n" +
            "\t\t\tAND IF(?1 is NOT NULL AND LENGTH(TRIM(?1)) > 0, td.start_time >= ?1, 1=1)\n" +
            "\t\t\tAND IF(?2 is NOT NULL AND LENGTH(TRIM(?2)) > 0, td.start_time <= ?2, 1=1)\n" +
            "\t\t\tAND IF(?3 is NOT NULL AND LENGTH(TRIM(?3)) > 0, td.raw_status = ?3, 1=1)\n" +
            "\t\t\tAND IF(?4 is NOT NULL AND LENGTH(TRIM(?4)) > 0, td.user_name LIKE CONCAT('%', ?4, '%'), 1=1)\n" +
            "\t\t\tAND IF(?5 is NOT NULL AND LENGTH(TRIM(?5)) > 0, td.lot_no = ?5, 1=1)\n" +
			"\t\t\tAND IF(?6 is NOT NULL AND LENGTH(TRIM(?6)) > 0, td.equipment_code LIKE CONCAT('%', ?6, '%'), 1=1)\n" +
            "ORDER BY td.start_time DESC", nativeQuery = true)
    List<Map<String, Object>> findInputTask(String search_gte_startTime, String search_lte_startTime, String search_eq_rawStatus, String search_like_userName, String search_eq_lotNo, String search_LIKE_equipmentCode);

	@Query(nativeQuery = true, value = "select id from production_again_task_detail where again_task_id = ?1 and raw_status != 5 and raw_status != 2 ")
	List<Long> findNotReturnByAgainTaskId(Long againTaskId);

	@Query(nativeQuery = true, value = "select * from production_again_task_detail where lot_no = ?1 and again_num= ?2 and raw_status !=2 and raw_status !=5")
	List<AgainTaskDetail> findByLotNoAndAgainNumAndStatus(String lotNo, Integer againNum);

	@Query(nativeQuery = true, value = "select * from production_again_task_detail where lot_no = ?1 and again_num= ?2 AND raw_status != 5 AND raw_status != 2")
	List<AgainTaskDetail> findByLotNoAndAgainNum(String lotNo, Integer againNum);

	@Query(value = "select * from production_again_task_detail where sap_no = ?1 and (raw_status=3 or raw_status=4)", nativeQuery = true)
	List<AgainTaskDetail> findBySapNo(String sapNo);

	@Query(nativeQuery = true, value = "select * from production_again_task_detail WHERE again_task_id in ?1 ")
	List<AgainTaskDetail> findByAgainTaskIdList(List<Long> idList);

	AgainTaskDetail findTopByEquipmentCodeOrderByIdDesc(String equipmentCode);

	@Query(nativeQuery = true, value = "select * from production_again_task_detail where again_task_id = ?1 and raw_status != 2 and raw_status != 5 ")
	List<AgainTaskDetail> findNormalTaskByAgainTaskId(Long id);

	@Query(value = "SELECT * FROM production_again_task_detail WHERE lot_no=?1 AND (raw_status=3 OR raw_status=4) AND confirm = 0 ORDER BY id DESC LIMIT 1 ", nativeQuery = true)
	AgainTaskDetail findUnconfirmedTask(String lotNo);

	@Query(value = "select * from production_again_task_detail where lot_no = ?1 and (raw_status=3 or raw_status=4) order by id desc limit 1",nativeQuery = true)
	AgainTaskDetail findSuccessByLotNo(String lotNo);

	@Query(value = "SELECT \n" +
			"atd.id AS taskId, \n" +
			"CONCAT('1-', fd.id) AS detailId, \n" +
			"'复投灌料' AS rawType, \n" +
			"atd.sap_no AS sapNo, \n" +
			"atd.equipment_code AS equipmentCode, \n" +
			"atd.lot_no AS lotNo, \n" +
			"atd.again_num AS againNum, \n" +
			"atd.again_pail_num AS againPailNum, \n" +
			"atd.pail_num AS pailNum, \n" +
			"fd.bucket_no AS bucketNo, \n" +
			"fd.material_code AS materialCode, \n" +
			"fd.material_name AS materialName, \n" +
			"fd.batch_no AS batchNo, \n" +
			"fd.factory_brand AS factoryBrand, \n" +
			"fd.resistance_gear AS materialGear, \n" +
			"fd.material_level AS materialLevel, \n" +
			"fd.supplies_grain AS suppliesGrain, \n" +
			"fd.weight AS weight, \n" +
			"ifb.user_name AS fillUser, \n" +
			"ifb.start_time AS fillTime, \n" +
			"CASE \n" +
			"\tWHEN atd.confirm = 1 THEN '是' \n" +
			"\tWHEN atd.confirm = 0 THEN '否' \n" +
			"\tELSE atd.confirm \n" +
			"END  confirm, \n" +
			"atd.confirm_time AS confirmTime \n" +
			"FROM production_input_furnace_burden AS ifb \n" +
			"LEFT JOIN production_again_task_detail AS atd ON atd.id = ifb.again_task_detail_id \n" +
			"LEFT JOIN production_input_furnace_detail AS fd ON fd.input_furnace_id = ifb.id AND fd.again_task_detail_id = atd.id \n" +
			"WHERE atd.raw_status = 4 " +
			"AND IF(?1 is NOT NULL AND LENGTH(TRIM(?1)) > 0, ifb.start_time >= ?1, 1=1) \n" +
			"AND IF(?2 is NOT NULL AND LENGTH(TRIM(?2)) > 0, ifb.start_time <= ?2, 1=1) \n" +
			"AND IF(?3 is NOT NULL AND LENGTH(TRIM(?3)) > 0, atd.equipment_code LIKE CONCAT('%', ?3, '%'), 1=1) \n" +
			"AND IF(?4 is NOT NULL AND LENGTH(TRIM(?4)) > 0, atd.lot_no LIKE CONCAT('%', ?4, '%'), 1=1) \n",nativeQuery = true)
	List<Map<String, Object>> findTaskAndDetails(String startTime, String endTime, String equipmentCode, String lotNo);

	@Query(value = "SELECT ifnull(SUM(weight),0) as weight FROM `production_again_task_detail` WHERE sap_no = ?1 and (raw_status=3 OR raw_status=4)", nativeQuery = true)
	BigDecimal findAgainSumWeightBySapNoAndInRawStatus(String orderNo);

	@Query(value = "select\n" +
			"\t  ifnull(SUM(weight),0) as weight \n" +
			"from\n" +
			"\t(\n" +
			"\tselect\n" +
			"\t\ttid,\n" +
			"\t\tCONCAT('0-', did) as did,\n" +
			"\t\tsap_no,\n" +
			"\t\tweight\n" +
			"\tfrom\n" +
			"\t\t(\n" +
			"\t\tselect\n" +
			"\t\t\tlt.id as tid,\n" +
			"\t\t\tfd.id as did,\n" +
			"\t\t\tlt.sap_no,\n" +
			"\t\t\tfd.weight\n" +
			"\t\tfrom\n" +
			"\t\t\tproduction_blast_furnace_detail as fd\n" +
			"\t\tinner join production_loading_task as lt on\n" +
			"\t\t\tfd.blast_furnace_id = lt.id\n" +
			"\t\twhere\n" +
			"\t\t\tlt.raw_status = 4\n" +
			"\t\t\tand lt.confirm = 1\n" +
			"\t\t\tand lt.sap_no =  ?1 \n" +
			"\t\t\tand fd.mark_crucible = 0\n" +
			") as t1\n" +
			"union all\n" +
			"\tselect\n" +
			"\t\ttid,\n" +
			"\t\tCONCAT('1-', did) as did,\n" +
			"\t\tsap_no,\n" +
			"\t\tweight\n" +
			"\tfrom\n" +
			"\t\t(\n" +
			"\t\tselect\n" +
			"\t\t\tatd.id as tid,\n" +
			"\t\t\tfd.id as did,\n" +
			"\t\t\tatd.sap_no,\n" +
			"\t\t\tfd.weight\n" +
			"\t\tfrom\n" +
			"\t\t\tproduction_input_furnace_detail as fd\n" +
			"\t\tinner join production_input_furnace_burden as ifb on\n" +
			"\t\t\tfd.input_furnace_id = ifb.id\n" +
			"\t\tinner join production_again_task_detail as atd on\n" +
			"\t\t\tfd.again_task_detail_id = atd.id\n" +
			"\t\twhere\n" +
			"\t\t\tatd.raw_status = 4\n" +
			"\t\t\tand ifb.confirm = 1\n" +
			"\t\t\tand atd.sap_no =  ?1 \n" +
			") as t2 \n" +
			") as t3\n" +
			"group by\n" +
			"\tsap_no;", nativeQuery = true)
	BigDecimal findTotalSumWeightBySapNoAndInRawStatus(String orderNo);

	@Query(value = "select * from production_again_task_detail where again_task_id = ?1 and is_fill_end = ?2 and again_num = ?3 order by id limit 1",nativeQuery = true)
	AgainTaskDetail findByAgainTaskIdAndFillEndAndAgainNum(Long againTaskId, int fillEnd, Integer againNum);

	@Query(value = "select * from production_again_task_detail where again_task_id = ?1 and pre_detail_id = ?2 ",nativeQuery = true)
	List<AgainTaskDetail> findByAgainTaskAndPreDetailId(Long againTaskId, Long preDetailId);

	@Query(value = "select * from production_again_task_detail where again_task_id = ?1 and again_num = ?2 and carrier_sort = ?3 ",nativeQuery = true)
	List<AgainTaskDetail> findByCarrierSort(Long againTaskId, Integer againNum, Integer carrierSort);

}
