/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.youchain.businessdata.repository;

import com.youchain.businessdata.domain.Asn;
import com.youchain.businessdata.domain.PickTicket;
import com.youchain.businessdata.domain.PlanPickDetail;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author JiangKun
 * @website https://eladmin.vip
 * @date 2024-01-11
 **/
public interface PickTicketRepository extends JpaRepository<PickTicket, Long>, JpaSpecificationExecutor<PickTicket> {
    /**
     * 根据 BillType 查询
     *
     * @param bill_type /
     * @return /
     */
    PickTicket findByBillType(Long bill_type);

    @Query(value = "SELECT * FROM `data_pick_ticket` pickticket ORDER BY pickticket.create_time DESC LIMIT 0,1", nativeQuery = true)
    PickTicket queryMaxCode();

    @Query(value = "SELECT * FROM `data_pick_ticket` dpt where  dpt.`code` = :code", nativeQuery = true)
    List<PickTicket> queryCode(@Param("code") String code);

    @Query(value = "SELECT dpt.area,dpt.gc_code FROM `data_pick_ticket` dpt where  dpt.`code` like 'BH%' and DATE_FORMAT(dpt.order_date,'%Y-%m-%d')=:order_date" +
            " group by dpt.area,dpt.gc_code", nativeQuery = true)
    List<Object[]> queryBhCkAreas(@Param("order_date") String order_date);
    /**
     * 根据单号 状态 查询数据
     *
     * @return 数据
     */
    @Query(value = "SELECT * FROM `data_pick_ticket` dpt where dpt.`status` = :status and dpt.`related_bill1` like :code", nativeQuery = true)
    List<PickTicket> queryPickTicketByCodeAll(@Param("status") String status, @Param("code") String code);

    @Transactional     //也可以只标注在上层调用者方法上
    @Modifying
    @Query(value = "UPDATE data_pick_ticket dpt set dpt.order_quantity = :orderQuantity where dpt.id= :id", nativeQuery = true)
    void updataOrderQuantity(@Param("orderQuantity") double orderQuantity, @Param("id") Long id);


    /**
     * 发货确认后修改的值
     */
    @Transactional
    @Modifying
    @Query(value = "UPDATE data_pick_ticket dpt set dpt.`ship_date` = :shipDate, dpt.`related_bill1` =:relatedBill1 ,dpt.`status`=:status,dpt.shipped_quantity = :shippedQuantity where dpt.id = :id", nativeQuery = true)
    void updataPT(@Param("shipDate") String shipDate, @Param("relatedBill1") String relatedBill1, @Param("status")String status,@Param("shippedQuantity")double shippedQuantity, @Param("id") Long id);

}