package com.sw.mes.fb.maketray.dao;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.fb.maketray.entity.MakeTrayItem;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

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

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
public interface MakeTrayItemDao extends BaseDao<MakeTrayItem, Integer> {
	@Modifying
	@Query(value = "DELETE FROM  zhxx_fb_make_tray_item WHERE tray_id=?1",nativeQuery = true)
	void deleteByTrayId(Integer trayId);

	@Query(value = "SELECT * FROM  zhxx_fb_make_tray_item WHERE tray_id=?1 ORDER BY id ASC",nativeQuery = true)
	List<MakeTrayItem> getItemsByTrayId(Integer trayId);

	// 获取方棒物料卡的信息
	@Query(value = "SELECT\n" +
			" zo.material_number AS materialNumber,\n" +
			" zo.yin_yang_face AS yinYangFace,\n" +
			" zo.water_mark AS waterMark,\n" +
			" zo.flat_handle AS flatHandle,\n" +
			" zo.wheel_mark AS wheelMark,\n" +
			" zo.round_handle AS roundHandle,\n" +
			" zo.hot_spot AS hotSpot,\n" +
			" zo.edge_collapse AS edgeCollapse,\n" +
			" zo.wheel_high_lights AS wheelHighLights,\n" +
			" zo.fb_experiment_level AS fbExperimentLevel,\n" +
			" zi.id,\n" +
			" zi.tray_id AS trayId,\n" +
			" zi.check_out_id AS checkOutId,\n" +
			" zi.tray_no AS trayNo,\n" +
			" zi.user ,\n" +
			" zi.time,\n" +
			" zi.remark,\n" +
			" zi.section_no_fb AS sectionNoFb,\n" +
			" zi.product_level AS productLevel,\n" +
			" zi.diameter ,\n" +
			" zi.conduction_type AS conductionType,\n" +
			" zi.specification,\n" +
			" zi.product_level_name AS productLevelName,\n" +
			" zi.ware_house_length AS wareHouseLength,\n" +
			" zi.length,\n" +
			" zi.weight,\n" +
			" zi.theory_weight AS theoryWeight,\n" +
			" zi.collapse_loss AS collapseLoss,\n" +
			" zi.length_slope AS lengthSlope,\n" +
			" zi.crystal_grayness AS crystalGrayness,\n" +
			" zi.square_rod_grayness AS squareRodGrayness,\n" +
			" zi.line_scratch AS lineScratch,\n" +
			" zi.knife_scratch AS knifeScratch,\n" +
			" zi.scratch,\n" +
			" zi.no_friction AS noFriction,\n" +
			" zi.size_exception AS sizeException,\n" +
			" zi.square_rod_remark AS squareRodRemark,\n" +
			" zi.state,\n" +
			" zi.checkout_name AS checkoutName,\n" +
			" zi.section_type AS sectionType,\n" +
			" zi.product_level_experiment AS productLevelExperiment,\n" +
			" zi.spell_bar_type_value AS spellBarTypeValue,\n" +
			" zi.spell_bar_length AS spellBarLength,\n" +
			" zi.product_level_fb AS productLevelFb,\n" +
			" zi.confirm_time AS confirmTime,\n" +
			" zi.confirmer,\n" +
			" zi.first_checkout_id,\n" +
			" zi.first_section_type,\n" +
			" zi.zfb_confirm_id,\n" +
			" zi.export_time AS exportTime,\n" +
			" zi.factory_name AS factoryName\n" +
			" FROM\n" +
			" zhxx_fb_make_tray_item zi\n" +
			//" LEFT JOIN zhxx_operate_check_out zo ON zi.section_no_fb = zo.section_no_fb\n" +
			" LEFT JOIN zhxx_operate_check_out zo ON zi.check_out_id = zo.id\n" +
			" WHERE zi.tray_id = ?1 GROUP BY zi.id ",nativeQuery = true)
	List<Map<String,Object>> getTrayItemDtoTrayId(Integer trayId);

	@Query(value = "SELECT * FROM  zhxx_fb_make_tray_item WHERE tray_no=?1",nativeQuery = true)
	List<MakeTrayItem> getItemsByTrayNo(String trayNo);

	//新建组拖时验证
	@Query(value = "SELECT * FROM zhxx_fb_make_tray_item WHERE section_no_fb in (?1) \n" +
			"   AND tray_id IS NOT NULL",nativeQuery = true)
	List<MakeTrayItem> checkCreate(List<String> sectionNoItems);

	//编辑组拖时验证
	@Query(value = "SELECT * FROM zhxx_fb_make_tray_item WHERE section_no_fb in (?1)  AND tray_id <> ?2",nativeQuery = true)
	List<MakeTrayItem> checkUpdate(List<String> sectionNoItems, Integer trayId);

	@Modifying
	@Query(value = "UPDATE zhxx_fb_make_tray_item SET state='已入库',export_time=now() WHERE tray_id=?1",nativeQuery = true)
	void updateStateByTrayId(Integer trayId);

	@Modifying
	@Query(value = "UPDATE zhxx_fb_make_tray_item SET state='已入库',export_time=?2 WHERE tray_id=?1",nativeQuery = true)
	void updateStateByTrayIdNew(Integer trayId, Date exportTime);

	@Query(value = "SELECT user,SUM(theory_weight) AS theoryWeight FROM zhxx_fb_make_tray_item "
			+ "WHERE user LIKE CONCAT('%',?1,'%') GROUP BY user LIMIT ?2,?3",nativeQuery = true)
	List<Map<String, Object>> getItmsBySum(String name, Integer page, Integer rows);

	@Query(value = "SELECT user,SUM(theory_weight) AS theoryWeight FROM zhxx_fb_make_tray_item "
			+ "WHERE time>=?1 AND time<?2 GROUP BY user LIMIT ?3,?4",nativeQuery = true)
	List<Map<String, Object>> getItmsByTime(String date1, String date2, Integer page, Integer rows);

	@Modifying
	@Query(value="DELETE FROM zhxx_fb_make_tray_item WHERE tray_id=?1 AND id not in(?2)",nativeQuery = true)
	void deleteByTrayIdAndItemIds(Integer trayId, List<Integer> itemIds);

	@Query(value = "SELECT user,SUM(theory_weight) AS theoryWeight FROM zhxx_fb_make_tray_item GROUP BY user LIMIT ?1,?2",
			nativeQuery = true)
	List<Map<String, Object>> getMakeTray(Integer page, Integer rows);

	@Query(value = "SELECT user,SUM(theory_weight) AS theoryWeight FROM zhxx_fb_make_tray_item "
			+ "WHERE user LIKE CONCAT('%',?1,'%') AND time>=?2 AND time<?3 GROUP BY user LIMIT ?4,?5",nativeQuery = true)
	List<Map<String, Object>> getCondition(String name, String date1, String date2, Integer page, Integer rows);

	@Query(value = "SELECT user,SUM(theory_weight) AS theoryWeight FROM zhxx_fb_make_tray_item GROUP BY user ",
			nativeQuery = true)
	List<Map<String, Object>> getByCount();

	@Query(value = "SELECT user,SUM(theory_weight) AS theoryWeight FROM zhxx_fb_make_tray_item"
			+ " WHERE user LIKE CONCAT('%',?1,'%') GROUP BY user ",
			nativeQuery = true)
	List<Map<String, Object>> getByName(String name);

	@Query(value = "SELECT user,SUM(theory_weight) AS theoryWeight FROM zhxx_fb_make_tray_item "
			+ "WHERE user LIKE CONCAT('%',?1,'%') AND time>=?2 AND time<?3 GROUP BY user ",nativeQuery = true)
	List<Map<String, Object>> getByAll(String name, String date1, String date2);

	@Query(value = "SELECT user,SUM(theory_weight) AS theoryWeight FROM zhxx_fb_make_tray_item "
			+ "WHERE time>=?1 AND time<?2 GROUP BY user",nativeQuery = true)
	List<Map<String, Object>> getByDay(String date1, String date2);

	List<MakeTrayItem> getByTrayNo(String trayNo);

	List<MakeTrayItem> findBySectionNoFb(String sectionFb);

    //    @Query(value = "SELECT COUNT(user) FROM zhxx_fb_make_tray_item "
	//    		+ "WHERE user LIKE CONCAT('%',?1,'%') AND time>=?2 AND time<?3 GROUP BY user",nativeQuery = true)
	//	Long getByCount(String name, String date1, String date2);
	//
	//    @Query(value = "SELECT COUNT(user) FROM zhxx_fb_make_tray_item "
	//    		+ "WHERE time>=?1 AND time<?2 GROUP BY user",nativeQuery = true)
	//	Long getTimeCount(String date1, String date2);

	@Query(value = "SELECT * FROM zhxx_fb_make_tray_item  WHERE section_no_fb=?1 ORDER BY id DESC limit 1",nativeQuery = true)
    MakeTrayItem getBySectionFb(String sectionFb);

}
