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

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

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

/**
 * @Author
 * @Description：
 * @Date:
 * @Modified By:
 */
public interface FbCheckOutDataDao extends BaseDao<FbCheckOutData, Long> {
    /**
     * 根据单晶编号查询
     *
     * @param fbSectionNo 单晶编号
     * @return 检验数据
     */
    @Query(value = "SELECT * FROM zhxx_operate_check_out_data WHERE fb_section_no=?1", nativeQuery = true)
    List<FbCheckOutData> findDataByFbSectionNo(String fbSectionNo);

    @Query(value = "SELECT * FROM `zhxx_operate_check_out_data` WHERE fb_section_no IN ?1 ORDER BY length DESC", nativeQuery = true)
    List<FbCheckOutData> findCheckOutDataByFbSectionNoIns(List<String> sectionNoFbs);

    @Query(value = "SELECT * FROM zhxx_operate_check_out_data WHERE fb_section_no=?1", nativeQuery = true)
    Map<String, Object> findBySectionNoMap(String sectionNo);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_check_out_data SET re_check_type = case when (re_check_type REGEXP '[^0-9.]')=0 then re_check_type + 1 else 1 end," + " order_type= ?2, checkout_name = ?3 WHERE fb_section_no IN (?1) ", nativeQuery = true)
    Integer updateOrderTypeByFbSectionNoList(List<String> fbSectionNoList, String orderType, String checkoutName);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_check_out_data SET sticks_count = ?2,section_compose=?3, spell_bar_length = length WHERE fb_section_no IN (?1) ", nativeQuery = true)
    Integer updateSticksCountFbSectionNoList(List<String> fbSectionNoList, Integer sticksCount, Object o);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_check_out SET sticks_count = ?2, spell_bar_length = length WHERE fb_section_no IN (?1) ", nativeQuery = true)
    Integer updateFbCheckOutRecordSticksCountFbSectionNoList(List<String> fbSectionNoList, Integer sticksCount);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_check_out_data SET sticks_count = ?2,section_compose=?3,spell_bar_length=?4 WHERE fb_section_no IN (?1) ", nativeQuery = true)
    void updateCheckOutFbSectionNoList(List<String> sectionNoFbList, int size, String join, BigDecimal totalLength);

    /**
     * 查询方棒组合编号
     *
     * @param fbSectionNo 方棒编号
     * @return 方棒组合编号
     */
    @Query(value = "SELECT section_compose FROM zhxx_operate_check_out_data WHERE fb_section_no=?1 ORDER BY id DESC LIMIT 1", nativeQuery = true)
    String getSectionComposeBySectionNo(String fbSectionNo);

    @Query(value = "SELECT fb_section_no FROM zhxx_operate_check_out_data WHERE  update_time BETWEEN '2022-03-31 00:00:00' AND '2022-03-31 04:50:00'  OR check_time BETWEEN '2022-03-31 00:00:00' AND '2022-03-31 04:50:00' ", nativeQuery = true)
    List<String> getNos();
    /**
     * 重新匹配订单，修改检验数据中的值
     *
     * @param sectionNo 晶段
     * @param orderNo   新订单号
     */
    @Modifying
    @Query(value = "UPDATE zhxx_operate_check_out_data SET order_no = ?2 WHERE fb_section_no = ?1 ", nativeQuery = true)
    void updateOrderNoBySectionNo(String sectionNo, String orderNo);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_check_out_data SET theory_weight_fb = ?2 WHERE id = ?1 ", nativeQuery = true)
    void updateWeight(Long id, BigDecimal weight);

    @Query(value = "SELECT ifnull(SUM(theory_weight_fb),0) as weight FROM `zhxx_operate_check_out_data` WHERE order_no = ?1 and back_cut = ?2", nativeQuery = true)
    String findSumWeightByOrderNoAndBackCut(String orderNo, int backCut);
    @Query(value = "SELECT * FROM zhxx_operate_check_out_data WHERE fb_section_no like ?1 LIMIT 1", nativeQuery = true)
    FbCheckOutData findDataByLikeFbSectionNo(String fbSectionNo);

    @Modifying
    @Query(value = "UPDATE zhxx_operate_check_out_data SET section_type = ?2WHERE fb_section_no=?1", nativeQuery = true)
    void updateCheckOutFbSectionType(String sectionNoFb, String type);

}
