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

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

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

/**
* @Author
* @Description：
* @Date:
* @Modified By:
*/
public interface TurnstoreInItemDao extends BaseDao<TurnstoreInItem, Integer> {
    @Modifying
    @Query(value = "DELETE FROM zhxx_turnstore_in_item WHERE in_id=?1",nativeQuery = true)
    void deleteByInId(Integer id);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE in_id=?1",nativeQuery = true)
    List<TurnstoreInItem> getItemsByInId(Integer inId);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE order_no=?1",nativeQuery = true)
    List<TurnstoreInItem> getItemsByOrderNo(String orderNo);

    @Query(value = "SELECT 0 AS id,DATE_FORMAT(DATE_SUB(time,INTERVAL 510 minute),'%Y-%m-%d') AS time,type,\n" +
            "SUM(instore_length) AS instore_length,SUM(actual_length) AS actual_length,\n" +
            "SUM(actual_weight) AS  actual_weight,SUM(instore_weight) AS instore_weight \n" +
            "FROM zhxx_turnstore_in_item \n" +
            "WHERE time >=?1 AND time<=?2 AND type=?3 \n" +
            "GROUP BY DATE_FORMAT(DATE_SUB(time,INTERVAL 510 minute),'%Y-%m-%d'),type",nativeQuery = true)
    List<Map<String,Object>> getStaticIn(String sDate, String eDate, String type);


    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no in (?1) AND state='待入库'",nativeQuery = true)
    List<TurnstoreInItem> getItemsBySectionNos(List<String> list);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no in (?1)",nativeQuery = true)
    List<TurnstoreInItem> getItemsBySectionNosNoState(List<String> list);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no in (?1) AND type=?2",nativeQuery = true)
    List<TurnstoreInItem> getItemsBySectionNosNoStateAndType(List<String> list, String type);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no in (?1) AND state='待入库' AND in_id <>?2",nativeQuery = true)
    List<TurnstoreInItem> getItemsBySectionNosUpdate(List<String> list, Integer turnstoreId);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no in (?1) AND in_id <>?2",nativeQuery = true)
    List<TurnstoreInItem> getItemsBySectionNosUpdateNoState(List<String> list, Integer turnstoreId);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no = ?1 AND type=?2 AND state='已入库'",nativeQuery = true)
    List<TurnstoreInItem> getItemsBySectionNoAndType(String sectionNo, String type);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no IN (?1) AND type IN ('方棒退料','综计退料') AND state='已入库' GROUP BY section_no",nativeQuery = true)
    List<TurnstoreInItem> getWmsBackBySectionNoAndType(List<String> list);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no=?1 AND type=?2 order by time desc limit 1",nativeQuery = true)
    TurnstoreInItem getItems2SectionNoAndType(String sectionNo, String type);


    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE section_no =?1 AND type='圆棒入库' ORDER BY id DESC LIMIT 2",nativeQuery = true)
    List<TurnstoreInItem> getLastYBRK(String sectionNo);

    @Query(value = "SELECT section_no as sectionNo ,count(1) as count FROM zhxx_turnstore_in_item WHERE section_no in (?1) AND type in(?2) and state=?3 " +
            "group  by section_no ",nativeQuery = true)
    List<Map<String,Object>> groupCountBySectionNosAndTypeAndState(List<String> list, List<String> type, String status);


    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE type=?1 and state=?2 and section_no=?3 order by id desc limit 1",nativeQuery = true)
    TurnstoreInItem getLastByTypeAndStateAndSectionNoOrderByIdDesc(String type, String state, String sectionNo);

    @Query(value = "SELECT section_no FROM zhxx_turnstore_in_item WHERE in_id=?1",nativeQuery = true)
    List<String> getSectionNoByInId(Integer id);

    @Query(value = "SELECT " +
            " COUNT(*) " +
            " FROM " +
            " `zhxx_turnstore_in_item` " +
            " WHERE " +
            " state = '退回'  " +
            " AND withdrawing_type = '退库复检'  " +
            " AND  " +
            " section_no = ?1",nativeQuery = true)
    Integer getBackCount(String sectionNo);


    @Modifying
    @Transactional
    @Query(value = "UPDATE zhxx_turnstore_in_item SET state=?1,confirm_user=?3 WHERE in_id=?2",nativeQuery = true)
    Integer updateById(String state,Integer id,String confirmUser);

    @Modifying
    @Transactional
    @Query(value = "UPDATE zhxx_turnstore_in_item SET state=?1,user=?3 WHERE in_id=?2",nativeQuery = true)
    Integer updateStateAndUserById(String state,Integer id,String confirmUser);

    @Query(value = "SELECT * FROM zhxx_turnstore_in_item WHERE purpose = ?2 AND section_no in (?1)",nativeQuery = true)
    List<TurnstoreInItem> getItemsBySectionNoList(List<String> sectionNoList,String purpose);

    TurnstoreInItem findOneBySectionNoOrderByIdDesc(String sectionNo);
}
