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

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

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

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

    //    @Query(value = "SELECT equipment_code As code ,id AS crystalId, length ,lot_no AS lotNo,weight,specs,dopant ,again_num AS againNum,wind_up AS windUp  FROM `production_long_crystal` WHERE crystal_no = ?1", nativeQuery = true)
//    Map<String, Object> findByCrystalNo(String crystalNo);
//    ==
    @Query(value = "SELECT *   FROM `production_long_crystal` WHERE crystal_no = ?1 AND crystal_status <> 9 order  by id DESC LIMIT 1", nativeQuery = true)
    LongCrystal findByCrystalNo(String crystalNo);

    @Query(value = "SELECT *   FROM `production_long_crystal` WHERE crystal_status <> 5 and output_confirmation_time <= date_sub(now() , interval 10 hour) order by id asc", nativeQuery = true)
    List<LongCrystal> findLongCrystal();

    @Query(value = "SELECT *  FROM `production_long_crystal` WHERE crystal_no = ?1 AND id != ?2 AND crystal_status != 9 ", nativeQuery = true)
    List<LongCrystal> findByCrystalNoAndNotId(String crystalNo, Long id);

    @Query(value = "SELECT *   FROM `production_long_crystal` WHERE crystal_no in ?1 AND crystal_status <> 9", nativeQuery = true)
    List<LongCrystal> findAllByCrystalNo(List<String> crystalNos);

    @Query(value = "SELECT *   FROM `production_long_crystal` WHERE lot_no = ?1  order  by id DESC", nativeQuery = true)
    List<LongCrystal> findByLotNo(String lotNo);

    @Query(value = "SELECT *  FROM `production_long_crystal` WHERE lot_no in ?1  order  by id DESC", nativeQuery = true)
    List<LongCrystal> findByLotNos(List<String> lotNos);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE equipment_code = ?1  order by id DESC limit 1", nativeQuery = true)
    List<LongCrystal> findByEquipmentCode(String lotNo);

    @Query(value = "SELECT lot_no  FROM `production_long_crystal` WHERE lot_no in ?1  and closure_identification = 'W'", nativeQuery = true)
    List<String> findByLotNoAndClosureIdentification(List<String> lotNoList, String closureIdentification);

    @Query(value = "SELECT *   FROM `production_long_crystal` WHERE crystal_no = ?1  order  by id DESC LIMIT 1", nativeQuery = true)
    LongCrystal findByCrystalNo2(String crystalNo);

    @Query(value = "select lot_no\n" +
            "from \n" +
            "production_long_crystal\n" +
            "order by id desc\n" +
            "limit 3\n", nativeQuery = true)
    List<String> findTop3LotNo();


    @Query(value = "select \n" +
            "distinct lot_no\n" +
            "from \n" +
            "production_long_crystal\n" +
            "where equipment_code = ?1\n" +
            "order by id desc\n" +
            "limit 3",nativeQuery = true)
    List<String> findTop3LotNoByEquipmentCode(String equipmentCode);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE equipment_code = ?1 order by id DESC limit 1", nativeQuery = true)
    LongCrystal getLastCrystalNo(String equipmentCode);

    // 样片编号找到对应的单晶信息
    @Query(value = "SELECT * from production_long_crystal WHERE id = (SELECT crystal_id FROM `production_piece_detail` WHERE piece_no = ?1 order by id DESC limit 1)",nativeQuery = true)
    LongCrystal findByPieceNo(String pieceNo);

	List<LongCrystal> findByAgvTaskCode(String taskCode);

	List<LongCrystal> findByEquipmentCodeAndCrystalStatusOrderById(String equipmentCode, Integer crystalStatus);

    List<LongCrystal> findTop3ByEquipmentCodeAndCrystalStatusNotOrderByIdDesc(String equipmentCode, Integer crystalStatus);

    LongCrystal findTopByLotIdOrderByIdDesc(Long id);

    @Query(value = "SELECT *   FROM `production_long_crystal` WHERE id in ?1", nativeQuery = true)
    List<LongCrystal> findByIds(List<Integer> ids);

    List<LongCrystal> findByLotIdAndAgainNumAndCrystalNumAndCrystalStatusNotAndIdNot(Long lotId, Integer againNum, Integer crystalNum, int crystalStatus, Long id);

    LongCrystal findByLotIdAndAgainNumAndCrystalNumAndCrystalStatusNot(Long lotId, Integer againNum, Integer crystalNum, int crystalStatus);

    List<LongCrystal> findByLotIdAndCrystalStatusNot(Long lotId, Integer crystalStatus);

    @Modifying
    @Query(value = "update production_long_crystal set yb_work_order=?2 where crystal_no=?1 ", nativeQuery = true)
    Integer updateYbWorkOrder(String crystalNo, String ybWorkOrder);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE lot_no like  CONCAT('%',?1,'%') AND closure_identification = ?2 order by id DESC limit 1", nativeQuery = true)
    LongCrystal findBLotNoAndClosureIdentification(String crystalNo, String closureIdentification);

    LongCrystal findTopByLotIdAndCrystalStatus(Long lotId, Integer crystalStatus);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE lot_no = ?1 limit 1", nativeQuery = true)
    LongCrystal findDataByLotNo(String lotNo);


    @Modifying
    @Query(value = "update production_long_crystal set last_weight=?4 where equipment_code=?1 and length between ?2 and ?3  ORDER BY id DESC LIMIT 1", nativeQuery = true)
    void updateLastLongCrstalWeight(String equipmentCode, BigDecimal length1, BigDecimal length2, BigDecimal lastWeight);

    @Query(value = "SELECT *   FROM `production_long_crystal` WHERE crystal_no LIKE ?1 order  by id DESC LIMIT 1", nativeQuery = true)
    LongCrystal likeByCrystalNo(String crystalNo);

    @Query(value = "SELECT *   FROM `production_long_crystal` WHERE gmt_create >= ?1 and gmt_create < ?2", nativeQuery = true)
    List<LongCrystal> findByGmtCreate(String startTime, String endTime);

    @Query(value = "SELECT lot_no FROM `production_long_crystal` WHERE gmt_create >= ?1 and gmt_create < ?2 and closure_identification = \"W\" and area = ?3 and series = ?4", nativeQuery = true)
    List<String> findByGmtCreate1(String startTime, String endTime, String area, String s);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE lot_no in ?1 and area = ?2 and series = ?3", nativeQuery = true)
    List<LongCrystal> findByGmtCreate2(List<String> lotNoList, String area, String s);

    @Query(value = "SELECT SUM(weight) FROM `production_long_crystal` WHERE \n" +
            "lot_no in (select lot_no from production_long_crystal where gmt_create >= ?1 and gmt_create < ?2 AND area = ?3 and closure_identification = \"W\")", nativeQuery = true)
    BigDecimal findByGmtCreateAndAreaAndarea(String startTime, String endTime, String area);

    @Query(value = "SELECT SUM(weight) FROM `production_long_crystal` WHERE gmt_create >= ?1 AND gmt_create < ?2 AND area = ?3", nativeQuery = true)
    BigDecimal findByGmtCreateAndAreaAndarea1(String startTime, String endTime, String area);

    @Query(value = "select plc.id as id,plc.weight as weight,ppo.erp_order_no as erpOrderNo,plc.lot_no as lotNo,plc.crystal_no as crystalNo,plc.order_no as orderNo,ppo.auart as auart\n" +
            "from  production_long_crystal plc left join plan_production_order ppo on ppo.aufnr = plc.order_no\n" +
            "where plc.order_no is not null and plc.crystal_status = 5 and plc.rod_pulled = 0  and erp_order_no is not null",nativeQuery = true)
    List<Map<String, Object>> pullOutPutData();

    @Query(value = "select plc.id as id,plc.weight as weight,ppo.fb_order_no as erpOrderNo,plc.lot_no as lotNo,plc.crystal_no as crystalNo,plc.order_no as orderNo,ppo.auart as auart\n" +
            "from interface_eap_cut_craft c left join production_long_crystal plc on  plc.crystal_no = c.round_sn left join plan_production_order ppo on ppo.aufnr = plc.order_no\n" +
            "where plc.order_no is not null and plc.crystal_status = 5 and plc.square_pulled = 0  and fb_order_no is not null",nativeQuery = true)
    List<Map<String, Object>> pullOutPutDataCut();

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

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

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE equipment_code = ?1 and id < ?2 ORDER BY id DESC limit 1",nativeQuery = true)
    LongCrystal getLastId(String equipmentCode, Long id);

    List<LongCrystal> findByLotNoAndAgainNum(String lotNo, Integer againNum);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE equipment_code = ?1 AND  gmt_create < ?2 AND crystal_status <= ?3 ORDER BY id DESC limit 1",nativeQuery = true)
    LongCrystal findDeviceNoAndCreateTime(String equipmentCode, Date createTime, Integer crystalStatus);

    @Query(value = "SELECT ifnull(SUM(weight),0) as weight FROM `production_long_crystal` WHERE order_no = ?1", nativeQuery = true)
    BigDecimal findSumWeightByOrderNo(String orderNo);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE lot_no = ?1 and crystal_status = ?2 ORDER BY id DESC ",nativeQuery = true)
    List<LongCrystal> findByLotNoCodeAndCrystalStatusByIdDesc(String lotNo, Integer crystalStatus);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE lot_no = ?1 AND again_num = ?2 ORDER BY crystal_num DESC LIMIT 1 ", nativeQuery = true)
    LongCrystal findLastByLotNoAndAgainNum(String lotNo, Integer againNum);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE lot_no = ?1 AND again_num = ?2 AND id != ?3 AND crystal_status != 9 ORDER BY crystal_num DESC LIMIT 1 ", nativeQuery = true)
    LongCrystal findLastRightByLotNoAndAgainNum(String lotNo, Integer againNum, Long id);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE lot_no = ?1 ORDER BY id DESC ",nativeQuery = true)
    List<LongCrystal> findByLotNoCodeByIdDesc(String lotNo);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE crystal_status = 9 AND lot_no = ?1 ",nativeQuery = true)
    List<LongCrystal> findUnconfirmedByLotNo(String lotNo);

    @Query(value = "SELECT * FROM `production_long_crystal` WHERE order_no in ?1 ",nativeQuery = true)
    List<LongCrystal> findByOrderNo(List<String> orderList);

    List<LongCrystal> findByTakeCrystalAgvTaskCode(String takeCrystalAgvTaskCode);

}
