/*
*  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 me.zhengjie.modules.dm.printingApproval.repository;

import me.zhengjie.modules.dm.printingApproval.domain.DmPrintingApproval;
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.stereotype.Repository;

/**
* @website https://el-admin.vip
* @author xwx
* @date 2021-03-17
**/
public interface DmPrintingApprovalRepository extends JpaRepository<DmPrintingApproval, Long>, JpaSpecificationExecutor<DmPrintingApproval> {

    @Modifying
    @Query(value = "delete from dm_printing_approval where if(?1 !='',request_id = ?1,1=2 )",nativeQuery = true)
    void deleteByRequestId(Long requestId);

    /**
     * TODO 查询最大的code
     * @return
     */
    @Query(value = "select max(task_code) from dm_printing_approval", nativeQuery = true)
    String getMaxCode();

    /**
     * todo 修改审批人
     */
    @Modifying
    @Query(value = "update dm_printing_approval set approval_user = ?1 where request_id = ?2",nativeQuery = true)
    void editApprovalUser(String approvalUser,Long requestId);

    @Modifying
    @Query(value = "update dm_printing_approval set is_new = '0' where request_id = ?1",nativeQuery = true)
    void changeOld(Long requestId);

    /**
     * todo 重置
     * @param requestId
     */
    @Modifying
    @Query(value = "update dm_printing_approval set approval_status = '00',approval_remark = ''  where is_new = '1' and  request_id = ?1",nativeQuery = true)
    void reset(Long requestId);

    /**
     * todo 刷新任务信息
     */
    @Modifying
    @Query(value = "update dm_printing_approval set task_id = ?3 , proc_inst_id=?4  where is_new = '1' and  request_id = ?1 and approval_user = ?2 ",nativeQuery = true)
    void resetTask(Long requestId,String approvalUser,String taskId,String procInstId);
}